jdk/test/com/sun/crypto/provider/KeyGenerator/Test4628062.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2007, 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
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    26
 * @bug 4628062 4963723
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify that AES KeyGenerator supports default initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      when init is not called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Test4628062 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    37
    private static final int[] AES_SIZES = { 16, 24, 32 }; // in bytes
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    38
    private static final int[] HMACSHA224_SIZES = { 28 };
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    39
    private static final int[] HMACSHA256_SIZES = { 32 };
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    40
    private static final int[] HMACSHA384_SIZES = { 48 };
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    41
    private static final int[] HMACSHA512_SIZES = { 64 };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    43
    public boolean execute(String algo, int[] keySizes) throws Exception {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    44
        KeyGenerator kg = KeyGenerator.getInstance(algo, "SunJCE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // TEST FIX 4628062
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Key keyWithDefaultSize = kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        byte[] encoding = keyWithDefaultSize.getEncoded();
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    49
        int defKeyLen = encoding.length ;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    50
        if (defKeyLen == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            throw new Exception("default key length is 0!");
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    52
        } else if (defKeyLen != keySizes[0]) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    53
            throw new Exception("default key length mismatch!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // BONUS TESTS
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    57
        if (keySizes.length > 1) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    58
            // 1. call init(int keysize) with various valid key sizes
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    59
            // and see if the generated key is the right size.
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    60
            for (int i=0; i<keySizes.length; i++) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    61
                kg.init(keySizes[i]*8); // in bits
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    62
                Key key = kg.generateKey();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    63
                if (key.getEncoded().length != keySizes[i]) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    64
                    throw new Exception("key is generated with the wrong length!");
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    65
                }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    66
            }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    67
            // 2. call init(int keysize) with invalid key size and see
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    68
            // if the expected InvalidParameterException is thrown.
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    69
            try {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    70
                kg.init(keySizes[0]*8+1);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    71
            } catch (InvalidParameterException ex) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    72
            } catch (Exception ex) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    73
                throw new Exception("wrong exception is thrown for invalid key size!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // passed all tests...hooray!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Test4628062 test = new Test4628062();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        String testName = test.getClass().getName();
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    85
        if (test.execute("AES", AES_SIZES)) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    86
            System.out.println(testName + ": AES Passed!");
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    87
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    88
        if (test.execute("HmacSHA224", HMACSHA224_SIZES)) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    89
            System.out.println(testName + ": HmacSHA224 Passed!");
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    90
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    91
        if (test.execute("HmacSHA256", HMACSHA256_SIZES)) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    92
            System.out.println(testName + ": HmacSHA256 Passed!");
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    93
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    94
        if (test.execute("HmacSHA384", HMACSHA384_SIZES)) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    95
            System.out.println(testName + ": HmacSHA384 Passed!");
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    96
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    97
        if (test.execute("HmacSHA512", HMACSHA512_SIZES)) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    98
            System.out.println(testName + ": HmacSHA512 Passed!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}