提交 99eee371 编写于 作者: I igerasim

8226543: Reduce GC pressure during message digest calculations in password-based encryption

Reviewed-by: mullan
上级 c062ff78
/* /*
* Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -268,17 +268,20 @@ final class PBES1Core { ...@@ -268,17 +268,20 @@ final class PBES1Core {
if (algo.equals("DES")) { if (algo.equals("DES")) {
// P || S (password concatenated with salt) // P || S (password concatenated with salt)
byte[] concat = new byte[Math.addExact(passwdBytes.length, salt.length)]; md.update(passwdBytes);
System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length); md.update(salt);
System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length); // digest P || S with iCount iterations
// first iteration
// digest P || S with c iterations byte[] toBeHashed = md.digest(); // this resets the digest
byte[] toBeHashed = concat; // remaining (iCount - 1) iterations
for (int i = 0; i < iCount; i++) { for (int i = 1; i < iCount; ++i) {
md.update(toBeHashed); md.update(toBeHashed);
toBeHashed = md.digest(); // this resets the digest try {
md.digest(toBeHashed, 0, toBeHashed.length);
} catch (DigestException e) {
throw new ProviderException("Internal error", e);
}
} }
Arrays.fill(concat, (byte)0x00);
result = toBeHashed; result = toBeHashed;
} else if (algo.equals("DESede")) { } else if (algo.equals("DESede")) {
// if the 2 salt halves are the same, invert one of them // if the 2 salt halves are the same, invert one of them
...@@ -305,13 +308,19 @@ final class PBES1Core { ...@@ -305,13 +308,19 @@ final class PBES1Core {
result = new byte[DESedeKeySpec.DES_EDE_KEY_LEN + result = new byte[DESedeKeySpec.DES_EDE_KEY_LEN +
DESConstants.DES_BLOCK_SIZE]; DESConstants.DES_BLOCK_SIZE];
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
toBeHashed = new byte[salt.length/2]; // first iteration
System.arraycopy(salt, i*(salt.length/2), toBeHashed, 0, md.update(salt, i * (salt.length / 2), salt.length / 2);
toBeHashed.length);
for (int j=0; j < iCount; j++) {
md.update(toBeHashed);
md.update(passwdBytes); md.update(passwdBytes);
toBeHashed = md.digest(); toBeHashed = md.digest();
// remaining (iCount - 1) iterations
for (int j = 1; j < iCount; ++j) {
md.update(toBeHashed);
md.update(passwdBytes);
try {
md.digest(toBeHashed, 0, toBeHashed.length);
} catch (DigestException e) {
throw new ProviderException("Internal error", e);
}
} }
System.arraycopy(toBeHashed, 0, result, i*16, System.arraycopy(toBeHashed, 0, result, i*16,
toBeHashed.length); toBeHashed.length);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册