jdk/test/com/sun/crypto/provider/KeyFactory/PBKDF2HmacSHA1FactoryTest.java
author duke
Wed, 05 Jul 2017 16:42:03 +0200
changeset 1266 c2036bf76829
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5083253
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify that PBKDF2WithHmacSHA1 SecretKeyFactory works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class PBKDF2HmacSHA1FactoryTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final String ALGO = "PBKDF2WithHmacSHA1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static final int[] KEY_SIZES = { 128, 256 }; // in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Use test vectors found in the appendix B of RFC 3962
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * "Advanced Encryption Standard (AES) Encryption for Kerberos 5"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final TestVector[] TEST_VECTORS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        new TestVector(1, "password", "ATHENA.MIT.EDUraeburn",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        "cdedb5281bb2f801565a1122b25635150ad1f7a04bb9f3a333ecc0e2e1f70837"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        new TestVector(2, "password", "ATHENA.MIT.EDUraeburn",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        "01dbee7f4a9e243e988b62c73cda935da05378b93244ec8f48a99e61ad799d86"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        new TestVector(1200, "password", "ATHENA.MIT.EDUraeburn",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        new TestVector(5, "password", fromHexString("1234567878563412"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "d1daa78615f287e6a1c8b120d7062a493f98d203e6be49a6adf4fa574b6e64ee"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        new TestVector(1200,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "pass phrase equals block size",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "139c30c0966bc32ba55fdbf212530ac9c5ec59f1a452f5cc9ad940fea0598ed1"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        new TestVector(1200,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        "pass phrase exceeds block size",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "9ccad6d468770cd51b10e6a68721be611a8b4d282601db3b36be9246915ec82a"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        new TestVector(50, fromHexString("f09d849e"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "EXAMPLE.COMpianist",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "6b9cf26d45455a43a5b8bb276a403b39e7fe37a0c41e02c281ff3069e1e94f52"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGO, "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (int i = 0; i < TEST_VECTORS.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.out.println("=>Testing vector#" + (i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            TestVector tv = TEST_VECTORS[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            for (int j = 0; j < KEY_SIZES.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                PBEKeySpec keySpec = tv.keySpecs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                PBEKey key = (PBEKey) skf.generateSecret(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                byte[] derivedKey = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (!(key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    throw new Exception("Wrong format for derived key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (derivedKey.length != KEY_SIZES[j]/8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    throw new Exception("Wrong length for derived key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // Test generateSecret(...) using test vectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (!tv.expectedVals[j].equals(toHexString(derivedKey))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    System.out.println("got:      " + toHexString(derivedKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    System.out.println("expected: " + tv.expectedVals[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    throw new Exception("Wrong value for derived key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                // Test getKeySpec(...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                PBEKeySpec keySpec2 = (PBEKeySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    skf.getKeySpec(key, PBEKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (!isEqual(keySpec, keySpec2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    throw new Exception("Wrong derived keySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static boolean isEqual(PBEKeySpec spec1, PBEKeySpec spec2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ((spec1 == null) || (spec2 == null)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (Arrays.equals(spec1.getPassword(), spec2.getPassword()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Arrays.equals(spec1.getSalt(), spec2.getSalt()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            spec1.getIterationCount() == spec2.getIterationCount() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            spec1.getKeyLength() == spec2.getKeyLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
   private static String toHexString(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String mapping = "0123456789abcdef";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        StringBuilder sb = new StringBuilder(bytes.length*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (int i = 0; i < bytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int low = bytes[i] & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int high = ((bytes[i] >> 4) & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            char[] res = new char[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            res[0] = mapping.charAt(high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            res[1] = mapping.charAt(low);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            sb.append(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static byte[] fromHexString(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        byte[] bytes = new byte[value.length()/2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String mapping = "0123456789abcdef";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        StringBuilder sb = new StringBuilder(bytes.length*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i = 0; i < bytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            String high = value.substring(2*i, 2*i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            String low = value.substring(2*i+1, 2*i+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            bytes[i] = (byte) ((mapping.indexOf(high) << 4) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                               mapping.indexOf(low));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        System.out.println("Test Passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
class TestVector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    PBEKeySpec[] keySpecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    String[] expectedVals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    TestVector(int iterCount, String password, String saltString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
               String expectedVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            init(iterCount, password, saltString.getBytes("UTF-8"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                 expectedVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            keySpecs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            expectedVals = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    TestVector(int iterCount, byte[] passwordUTF8, String saltString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
               String expectedVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            init(iterCount, new String(passwordUTF8, "UTF-8"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                 saltString.getBytes("UTF-8"), expectedVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            keySpecs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            expectedVals = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    TestVector(int iterCount, String password, byte[] salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
               String expectedVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        init(iterCount, password, salt, expectedVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private void init(int iterCount, String password, byte[] salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                      String expectedVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int numOfKeySizes = PBKDF2HmacSHA1FactoryTest.KEY_SIZES.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            keySpecs = new PBEKeySpec[numOfKeySizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            expectedVals = new String[numOfKeySizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            for (int i = 0; i < numOfKeySizes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                int keySize = PBKDF2HmacSHA1FactoryTest.KEY_SIZES[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                keySpecs[i] = new PBEKeySpec(password.toCharArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                             salt, iterCount, keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                expectedVals[i] = expectedVal.substring(0, keySize/4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            keySpecs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            expectedVals = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}