Path: blob/master/test/jdk/com/sun/crypto/provider/KeyGenerator/Test4628062.java
41159 views
/*1* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4628062 496372326* @summary Verify that AES KeyGenerator supports default initialization27* when init is not called28* @author Valerie Peng29*/30import java.security.*;31import javax.crypto.*;32import java.util.*;3334public class Test4628062 {3536private static final int[] AES_SIZES = { 16, 24, 32 }; // in bytes37private static final int[] HMACSHA224_SIZES = { 28 };38private static final int[] HMACSHA256_SIZES = { 32 };39private static final int[] HMACSHA384_SIZES = { 48 };40private static final int[] HMACSHA512_SIZES = { 64 };4142public boolean execute(String algo, int[] keySizes) throws Exception {43KeyGenerator kg = KeyGenerator.getInstance(algo, "SunJCE");4445// TEST FIX 462806246Key keyWithDefaultSize = kg.generateKey();47byte[] encoding = keyWithDefaultSize.getEncoded();48int defKeyLen = encoding.length ;49if (defKeyLen == 0) {50throw new Exception("default key length is 0!");51} else if (defKeyLen != keySizes[0]) {52throw new Exception("default key length mismatch!");53}5455// BONUS TESTS56if (keySizes.length > 1) {57// 1. call init(int keysize) with various valid key sizes58// and see if the generated key is the right size.59for (int i=0; i<keySizes.length; i++) {60kg.init(keySizes[i]*8); // in bits61Key key = kg.generateKey();62if (key.getEncoded().length != keySizes[i]) {63throw new Exception("key is generated with the wrong length!");64}65}66// 2. call init(int keysize) with invalid key size and see67// if the expected InvalidParameterException is thrown.68try {69kg.init(keySizes[0]*8+1);70} catch (InvalidParameterException ex) {71} catch (Exception ex) {72throw new Exception("wrong exception is thrown for invalid key size!");73}74}75// passed all tests...hooray!76return true;77}7879public static void main (String[] args) throws Exception {80Test4628062 test = new Test4628062();81String testName = test.getClass().getName();82if (test.execute("AES", AES_SIZES)) {83System.out.println(testName + ": AES Passed!");84}85if (test.execute("HmacSHA224", HMACSHA224_SIZES)) {86System.out.println(testName + ": HmacSHA224 Passed!");87}88if (test.execute("HmacSHA256", HMACSHA256_SIZES)) {89System.out.println(testName + ": HmacSHA256 Passed!");90}91if (test.execute("HmacSHA384", HMACSHA384_SIZES)) {92System.out.println(testName + ": HmacSHA384 Passed!");93}94if (test.execute("HmacSHA512", HMACSHA512_SIZES)) {95System.out.println(testName + ": HmacSHA512 Passed!");96}97}98}99100101