test/jdk/sun/security/ssl/internal/java.base/sun/security/ssl/TestHkdf.java
changeset 50768 68fa3d4026ea
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
       
     1 /*
       
     2  * Copyright (c) 2018, 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  * Actual test code for the package private HKDF implementation
       
    26  */
       
    27 
       
    28 package sun.security.ssl;
       
    29 
       
    30 import java.util.Arrays;
       
    31 import java.util.List;
       
    32 import java.util.LinkedList;
       
    33 import java.util.Objects;
       
    34 import java.security.NoSuchAlgorithmException;
       
    35 import java.security.InvalidKeyException;
       
    36 import javax.crypto.SecretKey;
       
    37 import javax.crypto.spec.SecretKeySpec;
       
    38 
       
    39 public class TestHkdf {
       
    40     public static class TestData {
       
    41         public TestData(String name, String algStr, String ikmStr,
       
    42                 String saltStr, String infoStr, int oLen, String expPrkStr,
       
    43                 String expOkmStr) {
       
    44             testName = Objects.requireNonNull(name);
       
    45             algName = Objects.requireNonNull(algStr);
       
    46             IKM = hex2bin(Objects.requireNonNull(ikmStr));
       
    47             if ((outLen = oLen) <= 0) {
       
    48                 throw new IllegalArgumentException(
       
    49                         "Output length must be greater than 0");
       
    50             }
       
    51             expectedPRK = hex2bin(Objects.requireNonNull(expPrkStr));
       
    52             expectedOKM = hex2bin(Objects.requireNonNull(expOkmStr));
       
    53 
       
    54             // Non-mandatory fields - may be null
       
    55             salt = (saltStr != null) ? hex2bin(saltStr) : null;
       
    56             info = (infoStr != null) ? hex2bin(infoStr) : null;
       
    57         }
       
    58 
       
    59         public final String testName;
       
    60         public final String algName;
       
    61         public final byte[] IKM;
       
    62         public final byte[] salt;
       
    63         public final byte[] info;
       
    64         public final int outLen;
       
    65         public final byte[] expectedPRK;
       
    66         public final byte[] expectedOKM;
       
    67     }
       
    68 
       
    69     public static final List<TestData> testList = new LinkedList<TestData>() {{
       
    70         add(new TestData("RFC 5689 Test Case 1", "SHA-256",
       
    71             "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
       
    72             "000102030405060708090a0b0c",
       
    73             "f0f1f2f3f4f5f6f7f8f9",
       
    74             42,
       
    75             "077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5",
       
    76             "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf" +
       
    77             "34007208d5b887185865"));
       
    78         add(new TestData("RFC 5689 Test Case 2", "SHA-256",
       
    79             "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
       
    80             "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f" +
       
    81             "404142434445464748494a4b4c4d4e4f",
       
    82             "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f" +
       
    83             "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f" +
       
    84             "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
       
    85             "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecf" +
       
    86             "d0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeef" +
       
    87             "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
       
    88             82,
       
    89             "06a6b88c5853361a06104c9ceb35b45cef760014904671014a193f40c15fc244",
       
    90             "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c" +
       
    91             "59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71" +
       
    92             "cc30c58179ec3e87c14c01d5c1f3434f1d87"));
       
    93         add(new TestData("RFC 5689 Test Case 3", "SHA-256",
       
    94             "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
       
    95             null, null, 42,
       
    96             "19ef24a32c717b167f33a91d6f648bdf96596776afdb6377ac434c1c293ccb04",
       
    97             "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d" +
       
    98             "9d201395faa4b61a96c8"));
       
    99         add(new TestData("RFC 5689 Test Case 4", "SHA-1",
       
   100             "0b0b0b0b0b0b0b0b0b0b0b",
       
   101             "000102030405060708090a0b0c",
       
   102             "f0f1f2f3f4f5f6f7f8f9",
       
   103             42,
       
   104             "9b6c18c432a7bf8f0e71c8eb88f4b30baa2ba243",
       
   105             "085a01ea1b10f36933068b56efa5ad81a4f14b822f5b091568a9cdd4f155fda2" +
       
   106             "c22e422478d305f3f896"));
       
   107         add(new TestData("RFC 5689 Test Case 5", "SHA-1",
       
   108             "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
       
   109             "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f" +
       
   110             "404142434445464748494a4b4c4d4e4f",
       
   111             "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f" +
       
   112             "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f" +
       
   113             "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
       
   114             "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecf" +
       
   115             "d0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeef" +
       
   116             "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
       
   117             82,
       
   118             "8adae09a2a307059478d309b26c4115a224cfaf6",
       
   119             "0bd770a74d1160f7c9f12cd5912a06ebff6adcae899d92191fe4305673ba2ffe" +
       
   120             "8fa3f1a4e5ad79f3f334b3b202b2173c486ea37ce3d397ed034c7f9dfeb15c5e" +
       
   121             "927336d0441f4c4300e2cff0d0900b52d3b4"));
       
   122         add(new TestData("RFC 5689 Test Case 6", "SHA-1",
       
   123             "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
       
   124             null, null, 42,
       
   125             "da8c8a73c7fa77288ec6f5e7c297786aa0d32d01",
       
   126             "0ac1af7002b3d761d1e55298da9d0506b9ae52057220a306e07b6b87e8df21d0" +
       
   127             "ea00033de03984d34918"));
       
   128         add(new TestData("RFC 5689 Test Case 7", "SHA-1",
       
   129             "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
       
   130             null, null, 42,
       
   131             "2adccada18779e7c2077ad2eb19d3f3e731385dd",
       
   132             "2c91117204d745f3500d636a62f64f0ab3bae548aa53d423b0d1f27ebba6f5e5" +
       
   133             "673a081d70cce7acfc48"));
       
   134     }};
       
   135 
       
   136     public static void main(String args[]) throws Exception {
       
   137         int testsPassed = 0;
       
   138 
       
   139         int testNo = 0;
       
   140         for (TestData test : testList) {
       
   141             System.out.println("*** Test " + ++testNo + ": " +
       
   142                     test.testName);
       
   143             if (runVector(test)) {
       
   144                 testsPassed++;
       
   145             }
       
   146         }
       
   147 
       
   148         System.out.println("Total tests: " + testList.size() +
       
   149                 ", Passed: " + testsPassed + ", Failed: " +
       
   150                 (testList.size() - testsPassed));
       
   151         if (testsPassed != testList.size()) {
       
   152             throw new RuntimeException("One or more tests failed.  " +
       
   153                     "Check output for details");
       
   154         }
       
   155     }
       
   156 
       
   157     private static boolean runVector(TestData testData)
       
   158             throws NoSuchAlgorithmException, InvalidKeyException {
       
   159         String kdfName, prfName;
       
   160         HKDF kdfHkdf;
       
   161         boolean result = true;
       
   162         SecretKey actualPRK;
       
   163         SecretKey actualOKM;
       
   164         byte[] deriveData;
       
   165 
       
   166         // Get an instance of the HKDF derivation engine
       
   167         kdfHkdf = new HKDF(testData.algName);
       
   168 
       
   169         // Set up the input keying material and the salt as a secret
       
   170         SecretKey ikmKey = new SecretKeySpec(testData.IKM, "HKDF-IKM");
       
   171         SecretKey saltKey = (testData.salt != null) ?
       
   172                 new SecretKeySpec(testData.salt, "HKDF-Salt") : null;
       
   173 
       
   174         // *** HKDF-Extract-only testing
       
   175         System.out.println("* HKDF-Extract-Only:");
       
   176         actualPRK = kdfHkdf.extract(saltKey, ikmKey, "HKDF-PRK");
       
   177         result &= compareKeyAndData(actualPRK, testData.expectedPRK);
       
   178 
       
   179         // *** HKDF Expand-Only testing
       
   180         // For these tests, we'll use the actualPRK as the input key
       
   181         System.out.println("* HKDF-Expand-Only:");
       
   182         actualOKM = kdfHkdf.expand(actualPRK, testData.info, testData.outLen,
       
   183                 "HKDF-OKM");
       
   184         result &= compareKeyAndData(actualOKM, testData.expectedOKM);
       
   185 
       
   186         // *** HKDF Extract-then-Expand testing
       
   187         // System.out.println("* HKDF-Extract-then-Expand:");
       
   188         // actualOKM = kdfHkdf.extractExpand(ikmKey, saltKey, testData.info,
       
   189         //         testData.outLen, "HKDF-OKM2");
       
   190         // result &= compareKeyAndData(actualOKM, testData.expectedOKM);
       
   191 
       
   192         return result;
       
   193     }
       
   194 
       
   195     /**
       
   196      * Compare actual key output from HKDF against an expected output value.
       
   197      *
       
   198      * @param outKey the KDF output in key form
       
   199      * @param expectedOut the expected value
       
   200      *
       
   201      * @return true if the underlying data for outKey, outData and
       
   202      * expectedOut are the same.
       
   203      */
       
   204     private static boolean compareKeyAndData(SecretKey outKey,
       
   205             byte[] expectedOut) {
       
   206         boolean result = false;
       
   207 
       
   208         if (Arrays.equals(outKey.getEncoded(), expectedOut)) {
       
   209             System.out.println("\t* Key output: Pass");
       
   210             result = true;
       
   211         } else {
       
   212             System.out.println("\t* Key output: FAIL");
       
   213             System.out.println("Expected:\n" +
       
   214                     dumpHexBytes(expectedOut, 16, "\n", " "));
       
   215             System.out.println("Actual:\n" +
       
   216                     dumpHexBytes(outKey.getEncoded(), 16, "\n", " "));
       
   217             System.out.println();
       
   218         }
       
   219 
       
   220         return result;
       
   221     }
       
   222 
       
   223     /**
       
   224      * Dump the hex bytes of a buffer into string form.
       
   225      *
       
   226      * @param data The array of bytes to dump to stdout.
       
   227      * @param itemsPerLine The number of bytes to display per line
       
   228      *      if the {@code lineDelim} character is blank then all bytes
       
   229      *      will be printed on a single line.
       
   230      * @param lineDelim The delimiter between lines
       
   231      * @param itemDelim The delimiter between bytes
       
   232      *
       
   233      * @return The hexdump of the byte array
       
   234      */
       
   235     private static String dumpHexBytes(byte[] data, int itemsPerLine,
       
   236             String lineDelim, String itemDelim) {
       
   237         StringBuilder sb = new StringBuilder();
       
   238         if (data != null) {
       
   239             for (int i = 0; i < data.length; i++) {
       
   240                 if (i % itemsPerLine == 0 && i != 0) {
       
   241                     sb.append(lineDelim);
       
   242                 }
       
   243                 sb.append(String.format("%02X", data[i])).append(itemDelim);
       
   244             }
       
   245         }
       
   246 
       
   247         return sb.toString();
       
   248     }
       
   249 
       
   250     private static byte[] hex2bin(String hex) {
       
   251         int i;
       
   252         int len = hex.length();
       
   253         byte[] data = new byte [len / 2];
       
   254         for (i = 0; i < len; i += 2) {
       
   255             data[i / 2] = (byte)((Character.digit(hex.charAt(i), 16) << 4) +
       
   256                     Character.digit(hex.charAt(i + 1), 16));
       
   257         }
       
   258         return data;
       
   259     }
       
   260 }