Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 0dee7da

Browse files
author
Yuri Nesterenko
committed
8281628: KeyAgreement : generateSecret intermittently not resetting
Reviewed-by: bae Backport-of: 1485883c9e6e24315bb21f20604b1c326e862a5b
1 parent 3d8ec30 commit 0dee7da

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-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, 2017, 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
@@ -334,6 +334,8 @@ protected int engineGenerateSecret(byte[] sharedSecret, int offset)
334334
} else {
335335
// Array too short, pad it w/ leading 0s
336336
if (secret.length < expectedLen) {
337+
Arrays.fill(sharedSecret, offset,
338+
offset + (expectedLen - secret.length), (byte)0);
337339
System.arraycopy(secret, 0, sharedSecret,
338340
offset + (expectedLen - secret.length),
339341
secret.length);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
37+
public class DHKeyAgreementPadding {
38+
39+
public static void main(String[] args) throws Exception {
40+
41+
byte[] aliceSecret = new byte[80];
42+
byte[] bobSecret = new byte[80];
43+
44+
KeyAgreement alice = KeyAgreement.getInstance("DiffieHellman");
45+
KeyAgreement bob = KeyAgreement.getInstance("DiffieHellman");
46+
47+
// The probability of an error is 0.2% or 1/500. Try more times.
48+
for (int i = 0; i < 5000; i++) {
49+
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DiffieHellman");
50+
keyPairGen.initialize(512);
51+
KeyPair aliceKeyPair = keyPairGen.generateKeyPair();
52+
KeyPair bobKeyPair = keyPairGen.generateKeyPair();
53+
54+
// Different stale data
55+
Arrays.fill(aliceSecret, (byte)'a');
56+
Arrays.fill(bobSecret, (byte)'b');
57+
58+
alice.init(aliceKeyPair.getPrivate());
59+
alice.doPhase(bobKeyPair.getPublic(), true);
60+
int aliceLen = alice.generateSecret(aliceSecret, 0);
61+
62+
bob.init(bobKeyPair.getPrivate());
63+
bob.doPhase(aliceKeyPair.getPublic(), true);
64+
int bobLen = bob.generateSecret(bobSecret, 0);
65+
66+
if (!Arrays.equals(aliceSecret, 0, aliceLen, bobSecret, 0, bobLen)) {
67+
System.out.println(bytesToHex(aliceSecret));
68+
System.out.println(bytesToHex(bobSecret));
69+
throw new RuntimeException("Different secrets observed at runs #" + i);
70+
}
71+
}
72+
}
73+
public static String bytesToHex(byte[] in) {
74+
final StringBuilder builder = new StringBuilder();
75+
for(byte b : in) {
76+
builder.append(String.format("%02x", b)+":");
77+
}
78+
return builder.toString();
79+
}
80+
}

0 commit comments

Comments
 (0)