Skip to content

Commit a2ecf87

Browse files
committed
8281628: KeyAgreement : generateSecret intermittently not resetting
Reviewed-by: mdoerr Backport-of: 1485883c9e6e24315bb21f20604b1c326e862a5b
1 parent cb4e3fe commit a2ecf87

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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(toHexString(aliceSecret, aliceLen));
68+
System.out.println(toHexString(bobSecret, bobLen));
69+
throw new RuntimeException("Different secrets observed at runs #" + i);
70+
}
71+
}
72+
}
73+
74+
/*
75+
* Converts a byte to hex digit and writes to the supplied buffer
76+
*/
77+
private static void byte2hex(byte b, StringBuffer buf) {
78+
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
79+
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
80+
int high = ((b & 0xf0) >> 4);
81+
int low = (b & 0x0f);
82+
buf.append(hexChars[high]);
83+
buf.append(hexChars[low]);
84+
}
85+
86+
/*
87+
* Converts a byte array to hex string
88+
*/
89+
private static String toHexString(byte[] block, int len) {
90+
StringBuffer buf = new StringBuffer();
91+
92+
for (int i = 0; i < len; i++) {
93+
byte2hex(block[i], buf);
94+
if (i < len-1) {
95+
buf.append(":");
96+
}
97+
}
98+
return buf.toString();
99+
}
100+
}

0 commit comments

Comments
 (0)