jdk/test/com/sun/crypto/provider/Cipher/PBE/PBKDF2Translate.java
author rhalade
Thu, 10 Jul 2014 03:07:48 +0000
changeset 25404 c922a1b67208
child 30046 cf2c86e1819e
permissions -rw-r--r--
8041781: Need new regression tests for PBE keys Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25404
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     1
/*
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     4
 *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     8
 *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    13
 * accompanied this code).
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    14
 *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    18
 *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    21
 * questions.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    22
 */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    23
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    24
import java.security.InvalidKeyException;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    25
import java.security.NoSuchAlgorithmException;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    26
import java.security.spec.InvalidKeySpecException;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    27
import java.util.Arrays;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    28
import java.util.Random;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    29
import javax.crypto.SecretKey;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    30
import javax.crypto.SecretKeyFactory;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    31
import javax.crypto.interfaces.PBEKey;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    32
import javax.crypto.spec.PBEKeySpec;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    33
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    34
/**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    35
 * @test
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    36
 * @bug 8041781
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    37
 * @summary Verify if the SecretKeyFactory.translateKey() method works
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    38
 * @author Alexander Fomin
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    39
 * @run main PBKDF2Translate
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    40
 */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    41
public class PBKDF2Translate {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    42
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    43
    private static final String[] ALGO_TO_TEST = {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    44
        "PBKDF2WithHmacSHA1",
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    45
        "PBKDF2WithHmacSHA224",
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    46
        "PBKDF2WithHmacSHA256",
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    47
        "PBKDF2WithHmacSHA384",
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    48
        "PBKDF2WithHmacSHA512"
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    49
    };
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    50
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    51
    private static final String PASS_PHRASE = "some hidden string";
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    52
    private static final int ITERATION_COUNT = 1000;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    53
    private static final int KEY_SIZE = 128;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    54
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    55
    private final String algoToTest;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    56
    private final byte[] salt = new byte[8];
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    57
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    59
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    60
        boolean failed = false;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    61
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    62
        for (String algo : ALGO_TO_TEST) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    63
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    64
            System.out.println("Testing " + algo + ":");
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    65
            PBKDF2Translate theTest = new PBKDF2Translate(algo);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    66
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    67
            try {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    68
                if (!theTest.testMyOwnSecretKey()
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    69
                        || !theTest.generateAndTranslateKey()
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    70
                        || !theTest.translateSpoiledKey()) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    71
                    // we don't want to set failed to false
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    72
                    failed = true;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    73
                }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    74
            } catch (InvalidKeyException | NoSuchAlgorithmException |
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    75
                    InvalidKeySpecException e) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    76
                e.printStackTrace(System.err);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    77
                failed = true;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    78
            }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    79
        }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    80
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    81
        if (failed) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    82
            throw new RuntimeException("One or more tests failed....");
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    83
        }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    84
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    85
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    86
    public PBKDF2Translate(String algoToTest) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    87
        this.algoToTest = algoToTest;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    88
        new Random().nextBytes(this.salt);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    89
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    90
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    91
    /**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    92
     * The test case scenario implemented in the method: - derive PBKDF2 key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    93
     * using the given algorithm; - translate the key - check if the translated
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    94
     * and original keys have the same key value.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    95
     *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    96
     * @return true if the test case passed; false - otherwise.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    97
     * @throws NoSuchAlgorithmException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    98
     * @throws InvalidKeySpecException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
    99
     * @throws InvalidKeyException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   100
     */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   101
    public boolean generateAndTranslateKey() throws NoSuchAlgorithmException,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   102
            InvalidKeySpecException, InvalidKeyException {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   103
        // derive PBKDF2 key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   104
        SecretKey key1 = getSecretKeyForPBKDF2(algoToTest);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   105
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   106
        // translate key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   107
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   108
        SecretKey key2 = skf.translateKey(key1);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   109
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   110
        // check if it still the same after translation
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   111
        if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   112
            System.err.println("generateAndTranslateKey test case failed: the "
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   113
                    + "key1 and key2 values in its primary encoding format are "
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   114
                    + "not the same for " + algoToTest + "algorithm.");
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   115
            return false;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   116
        }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   117
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   118
        return true;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   119
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   120
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   121
    /**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   122
     * The test case scenario implemented in the method: - derive Key1 for the
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   123
     * given PBKDF2 algorithm - create my own secret Key2 as an instance of a
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   124
     * class implements PBEKey - translate Key2 - check if the key value of the
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   125
     * translated key and Key1 are the same.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   126
     *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   127
     * @return true if the test case passed; false - otherwise.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   128
     * @throws NoSuchAlgorithmException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   129
     * @throws InvalidKeySpecException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   130
     * @throws InvalidKeyException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   131
     */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   132
    public boolean testMyOwnSecretKey()
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   133
            throws NoSuchAlgorithmException, InvalidKeySpecException,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   134
            InvalidKeyException {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   135
        SecretKey key1 = getSecretKeyForPBKDF2(algoToTest);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   136
        SecretKey key2 = getMyOwnSecretKey();
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   137
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   138
        // Is it actually the same?
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   139
        if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   140
            System.err.println("We shouldn't be here. The key1 and key2 values "
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   141
                    + "in its primary encoding format have to be the same!");
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   142
            return false;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   143
        }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   144
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   145
        // Translate key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   146
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   147
        SecretKey key3 = skf.translateKey(key2);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   148
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   149
        // Check if it still the same after translation
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   150
        if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   151
            System.err.println("testMyOwnSecretKey test case failed: the key1 "
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   152
                    + "and key3 values in its primary encoding format are not "
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   153
                    + "the same for " + algoToTest + "algorithm.");
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   154
            return false;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   155
        }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   156
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   157
        return true;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   158
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   159
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   160
    /**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   161
     * The test case scenario implemented in the method: - create my own secret
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   162
     * Key2 as an instance of a class implements PBEKey - spoil the key (set
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   163
     * iteration count to 0, for example) - try to translate key -
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   164
     * InvalidKeyException is expected.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   165
     *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   166
     * @return true if InvalidKeyException occurred; false - otherwise.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   167
     * @throws NoSuchAlgorithmException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   168
     * @throws InvalidKeySpecException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   169
     */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   170
    public boolean translateSpoiledKey() throws NoSuchAlgorithmException,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   171
            InvalidKeySpecException {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   172
        // derive the key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   173
        SecretKey key1 = getMyOwnSecretKey();
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   174
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   175
        // spoil the key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   176
        ((MyPBKDF2SecretKey) key1).spoil();
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   177
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   178
        // translate key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   179
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   180
        try {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   181
            SecretKey key2 = skf.translateKey(key1);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   182
        } catch (InvalidKeyException ike) {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   183
            // this is expected
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   184
            return true;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   185
        }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   186
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   187
        return false;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   188
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   189
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   190
    /**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   191
     * Generate a PBKDF2 secret key using given algorithm.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   192
     *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   193
     * @param algoToDeriveKey PBKDF2 algorithm
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   194
     * @return PBKDF2 secret key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   195
     * @throws NoSuchAlgorithmException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   196
     * @throws InvalidKeySpecException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   197
     */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   198
    private SecretKey getSecretKeyForPBKDF2(String algoToDeriveKey)
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   199
            throws NoSuchAlgorithmException, InvalidKeySpecException {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   200
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToDeriveKey);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   201
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   202
        PBEKeySpec spec = new PBEKeySpec(PASS_PHRASE.toCharArray(),
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   203
                this.salt, ITERATION_COUNT, KEY_SIZE);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   204
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   205
        return skf.generateSecret(spec);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   206
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   207
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   208
    /**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   209
     * Generate a secrete key as an instance of a class implements PBEKey.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   210
     *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   211
     * @return secrete key
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   212
     * @throws InvalidKeySpecException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   213
     * @throws NoSuchAlgorithmException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   214
     */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   215
    private SecretKey getMyOwnSecretKey() throws InvalidKeySpecException,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   216
            NoSuchAlgorithmException {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   217
        return new MyPBKDF2SecretKey(PASS_PHRASE, this.algoToTest, this.salt,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   218
                ITERATION_COUNT, KEY_SIZE);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   219
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   220
}
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   221
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   222
/**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   223
 * An utility class to check the SecretKeyFactory.translateKey() method.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   224
 */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   225
class MyPBKDF2SecretKey implements PBEKey {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   226
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   227
    private final byte[] key;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   228
    private final byte[] salt;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   229
    private final String algorithm;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   230
    private final int keySize, keyLength;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   231
    private int itereationCount;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   232
    private final String pass;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   233
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   234
    @Override
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   235
    public String getAlgorithm() {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   236
        return algorithm;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   237
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   238
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   239
    @Override
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   240
    public String getFormat() {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   241
        return "RAW";
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   242
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   243
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   244
    @Override
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   245
    public byte[] getEncoded() {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   246
        byte[] copy = new byte[keyLength];
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   247
        System.arraycopy(this.key, 0, copy, 0, keyLength);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   248
        return copy;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   249
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   250
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   251
    /**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   252
     * The key is generating by SecretKeyFactory and its value just copying in
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   253
     * the key field of MySecretKey class. So, this is real key derived using
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   254
     * the given algorithm.
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   255
     *
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   256
     * @param passPhrase some string intended to be a password
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   257
     * @param algo PBKDF2 algorithm
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   258
     * @param salt slat for PBKDF2
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   259
     * @param iterationCount iteration count
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   260
     * @param keySize key size in bits
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   261
     * @throws InvalidKeySpecException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   262
     * @throws NoSuchAlgorithmException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   263
     */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   264
    public MyPBKDF2SecretKey(String passPhrase, String algo, byte[] salt,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   265
            int iterationCount, int keySize)
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   266
            throws InvalidKeySpecException, NoSuchAlgorithmException {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   267
        this.algorithm = algo;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   268
        this.salt = salt;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   269
        this.itereationCount = iterationCount;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   270
        this.keySize = keySize;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   271
        this.pass = passPhrase;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   272
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   273
        PBEKeySpec spec = new PBEKeySpec(passPhrase.toCharArray(),
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   274
                this.salt, iterationCount, this.keySize);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   275
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   276
        SecretKeyFactory keyFactory
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   277
                = SecretKeyFactory.getInstance(algo);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   278
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   279
        SecretKey realKey = keyFactory.generateSecret(spec);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   280
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   281
        this.keyLength = realKey.getEncoded().length;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   282
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   283
        this.key = new byte[this.keyLength];
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   284
        System.arraycopy(realKey.getEncoded(), 0, this.key, 0,
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   285
                this.keyLength);
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   286
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   287
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   288
    @Override
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   289
    public int getIterationCount() {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   290
        return itereationCount;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   291
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   292
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   293
    @Override
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   294
    public byte[] getSalt() {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   295
        return salt;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   296
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   297
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   298
    @Override
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   299
    public char[] getPassword() {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   300
        return this.pass.toCharArray();
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   301
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   302
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   303
    /**
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   304
     * Spoil the generated key (before translation) to cause an
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   305
     * InvalidKeyException
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   306
     */
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   307
    public void spoil() {
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   308
        this.itereationCount = -1;
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   309
    }
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   310
c922a1b67208 8041781: Need new regression tests for PBE keys
rhalade
parents:
diff changeset
   311
}