Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8258915: Temporary buffer cleanup #2070

Closed
wants to merge 12 commits into from
15 changes: 9 additions & 6 deletions src/java.base/share/classes/sun/security/provider/CtrDrbg.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -495,13 +495,16 @@ public synchronized void generateAlgorithm(
// Step 4.1. Increment
addOne(v, ctrLen);
try {
// Step 4.2. Encrypt
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg));
byte[] out = cipher.doFinal(v);

// Step 4.2. Encrypt
// Step 4.3 and 5. Cat bytes and leftmost
System.arraycopy(out, 0, result, pos,
(len > blockLen) ? blockLen : len);
if (len > blockLen) {
cipher.doFinal(v, 0, blockLen, result, pos);
} else {
byte[] out = cipher.doFinal(v);
System.arraycopy(out, 0, result, pos, len);
Arrays.fill(out, (byte)0);
}
wangweij marked this conversation as resolved.
Show resolved Hide resolved
} catch (GeneralSecurityException e) {
throw new InternalError(e);
}
Expand Down
12 changes: 6 additions & 6 deletions src/java.base/share/classes/sun/security/provider/HashDrbg.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -254,15 +254,15 @@ private void hashGen(byte[] output, byte[] v) {
int len = output.length;

while (len > 0) {
// Step 4.1 w = Hash (data).
digest.update(data);
if (len < outLen) {
// Step 4.1 w = Hash (data).
// Step 4.2 W = W || w.
System.arraycopy(digest.digest(data), 0, output, pos,
len);
byte[] out = digest.digest();
System.arraycopy(out, 0, output, pos, len);
Arrays.fill(out, (byte)0);
} else {
try {
// Step 4.1 w = Hash (data).
digest.update(data);
// Step 4.2 digest into right position, no need to cat
digest.digest(output, pos, outLen);
} catch (DigestException e) {
Expand Down