Skip to content

Commit c2d2dc7

Browse files
committed
8281628: KeyAgreement : generateSecret intermittently not resetting
Backport-of: 1485883c9e6e24315bb21f20604b1c326e862a5b
1 parent 2903d59 commit c2d2dc7

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/java.base/share/classes/com/sun/crypto/provider/DHKeyAgreement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -344,6 +344,8 @@ protected int engineGenerateSecret(byte[] sharedSecret, int offset)
344344
} else {
345345
// Array too short, pad it w/ leading 0s
346346
if (secret.length < expectedLen) {
347+
Arrays.fill(sharedSecret, offset,
348+
offset + (expectedLen - secret.length), (byte)0);
347349
System.arraycopy(secret, 0, sharedSecret,
348350
offset + (expectedLen - secret.length),
349351
secret.length);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8281628
27+
* @library /test/lib
28+
* @summary ensure padding bytes are always added when generated secret
29+
* is smaller than buffer size.
30+
*/
31+
32+
import javax.crypto.KeyAgreement;
33+
import java.security.KeyPair;
34+
import java.security.KeyPairGenerator;
35+
import java.util.Arrays;
36+
import java.util.HexFormat;
37+
38+
public class DHKeyAgreementPadding {
39+
40+
public static void main(String[] args) throws Exception {
41+
42+
byte[] aliceSecret = new byte[80];
43+
byte[] bobSecret = new byte[80];
44+
45+
KeyAgreement alice = KeyAgreement.getInstance("DiffieHellman");
46+
KeyAgreement bob = KeyAgreement.getInstance("DiffieHellman");
47+
48+
// The probability of an error is 0.2% or 1/500. Try more times.
49+
for (int i = 0; i < 5000; i++) {
50+
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DiffieHellman");
51+
keyPairGen.initialize(512);
52+
KeyPair aliceKeyPair = keyPairGen.generateKeyPair();
53+
KeyPair bobKeyPair = keyPairGen.generateKeyPair();
54+
55+
// Different stale data
56+
Arrays.fill(aliceSecret, (byte)'a');
57+
Arrays.fill(bobSecret, (byte)'b');
58+
59+
alice.init(aliceKeyPair.getPrivate());
60+
alice.doPhase(bobKeyPair.getPublic(), true);
61+
int aliceLen = alice.generateSecret(aliceSecret, 0);
62+
63+
bob.init(bobKeyPair.getPrivate());
64+
bob.doPhase(aliceKeyPair.getPublic(), true);
65+
int bobLen = bob.generateSecret(bobSecret, 0);
66+
67+
if (!Arrays.equals(aliceSecret, 0, aliceLen, bobSecret, 0, bobLen)) {
68+
System.out.println(HexFormat.ofDelimiter(":").formatHex(aliceSecret, 0, aliceLen));
69+
System.out.println(HexFormat.ofDelimiter(":").formatHex(bobSecret, 0, bobLen));
70+
throw new RuntimeException("Different secrets observed at runs #" + i);
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)