|
| 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