jdk/test/sun/security/pkcs11/KeyGenerator/TestKeyGenerator.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7286 4e48fc5922c6
child 35379 1e8e336ef66b
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7286
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
    26
 * @bug 4917233 6461727 6490213 6720456
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test the KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
enum TestResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    PASS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    FAIL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    TBD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class TestKeyGenerator extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        main(new TestKeyGenerator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private TestResult test(String algorithm, int keyLen, Provider p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                      TestResult expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        TestResult actual = TestResult.TBD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.out.println("Testing " + algorithm + ", " + keyLen + " bits...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        KeyGenerator kg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            kg = KeyGenerator.getInstance(algorithm, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            System.out.println("Not supported, skipping: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return TestResult.PASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            kg.init(keyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            actual = TestResult.PASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } catch (InvalidParameterException ipe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            actual = TestResult.FAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (actual == TestResult.PASS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                SecretKey key = kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (expected == TestResult.FAIL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    throw new Exception("Generated " + key +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        " using invalid key length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } catch (ProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw (Exception) (new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    ("key generation failed using valid length").initCause(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (expected != TestResult.TBD && expected != actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new Exception("Expected to " + expected + ", but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return actual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        test("DES", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        test("DES", 56, p, TestResult.PASS); // ensure JCE-Compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        test("DES", 64, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        test("DES", 128, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        test("DESede", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // Special handling since not all PKCS11 providers support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // 2-key DESede, e.g. SunPKCS11-Solaris.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        TestResult temp = test("DESede", 112, p, TestResult.TBD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        test("DESede", 128, p, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        test("DESede", 168, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        test("DESede", 192, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        test("DESede", 64, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        test("DESede", 256, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // Different PKCS11 impls have different ranges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // of supported key sizes for variable-key-length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // algorithms.
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   107
        // Solaris> Blowfish: 32-128 or even 448 bits, RC4: 8-128 bits or as much as 2048 bits
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // NSS>     Blowfish: n/a,         RC4: 8-2048 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // However, we explicitly disallowed key sizes less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // than 40-bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        test("Blowfish", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        test("Blowfish", 24, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        test("Blowfish", 32, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        test("Blowfish", 40, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        test("Blowfish", 128, p, TestResult.PASS);
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   117
        test("Blowfish", 136, p, TestResult.TBD);
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   118
        test("Blowfish", 448, p, TestResult.TBD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        test("Blowfish", 456, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        test("ARCFOUR", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        test("ARCFOUR", 32, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        test("ARCFOUR", 40, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        test("ARCFOUR", 128, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (p.getName().equals("SunPKCS11-Solaris")) {
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   127
            test("ARCFOUR", 1024, p, TestResult.TBD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else if (p.getName().equals("SunPKCS11-NSS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            test("ARCFOUR", 1024, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            test("ARCFOUR", 2048, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            test("ARCFOUR", 2056, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}