Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/KeyWrap/NISTWrapKAT.java
38889 views
/*1* Copyright (c) 2004, 2007, 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 500815626* @run main NISTWrapKAT27* @summary Verify that the "AESWrap" key wrap cipher work as28* expected using NIST test vectors.29* @author Valerie Peng30*/31import java.security.Key;32import java.security.AlgorithmParameters;33import javax.crypto.*;34import javax.crypto.spec.*;35import java.util.Arrays;36import java.math.BigInteger;3738public class NISTWrapKAT {3940private static final String KEK =41"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";42private static final String DATA =43"00112233445566778899aabbccddeeff000102030405060708090a0b0c0d0e0f";4445private static String AES128_128 =46"1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5";47private static String AES192_128 =48"96778b25ae6ca435f92b5b97c050aed2468ab8a17ad84e5d";49private static String AES192_192 =50"031d33264e15d33268f24ec260743edce1c6c7ddee725a936ba814915c6762d2";51private static String AES256_128 =52"64e8c3f9ce0f5ba263e9777905818a2a93c8191e7d6e8ae7";53private static String AES256_192 =54"a8f9bc1612c68b3ff6e6f4fbe30e71e4769c8b80a32cb8958cd5d17d6b254da1";55private static String AES256_256 =56"28c9f404c4b810f4cbccb35cfb87f8263f5786e2d80ed326cbc7f0e71a99f43bfb988b9b7a02dd21";5758public static void testKeyWrap(int keyLen, int dataLen,59String expected) throws Exception {60System.out.println("Testing AESWrap Cipher with " +61dataLen + "-byte data with " + 8*keyLen + "-bit key");62Cipher c = Cipher.getInstance("AESWrap", "SunJCE");63byte[] keyVal = new byte[keyLen];64byte[] dataVal = new byte[dataLen];6566// setup the key encryption key and the to-be-wrapped key67BigInteger temp = new BigInteger(KEK.substring(0, keyLen*2), 16);68byte[] val = temp.toByteArray();69System.arraycopy(val, 0, keyVal, keyVal.length-val.length,70val.length);71temp = new BigInteger(DATA.substring(0, dataLen*2), 16);72val = temp.toByteArray();73System.arraycopy(val, 0, dataVal, dataVal.length-val.length,74val.length);7576SecretKey cipherKey = new SecretKeySpec(keyVal, "AES");77c.init(Cipher.WRAP_MODE, cipherKey);78SecretKey toBeWrappedKey = new SecretKeySpec(dataVal, "AES");7980// first test WRAP with known values81byte[] wrapped = c.wrap(toBeWrappedKey);82byte[] expectedVal = new BigInteger(expected, 16).toByteArray();83// need to add offset since BigInteger may pad "0x00" in the beginning84int offset = expectedVal.length - wrapped.length;85for (int i=0; i<wrapped.length; i++) {86if (wrapped[i] != expectedVal[offset + i]) {87throw new Exception("Wrap failed; got different result");88}89}9091// then test UNWRAP and compare with the initial values92c.init(Cipher.UNWRAP_MODE, cipherKey);93Key unwrapped = c.unwrap(wrapped, "AES", Cipher.SECRET_KEY);94if (!Arrays.equals(unwrapped.getEncoded(), dataVal)) {95throw new Exception("Unwrap failed; got different result");96}97}9899public static void main(String[] argv) throws Exception {100testKeyWrap(16, 16, AES128_128);101// only run the tests on longer key lengths if unlimited version102// of JCE jurisdiction policy files are installed103int allowed = Cipher.getMaxAllowedKeyLength("AES");104if (allowed >= 24*8) {105testKeyWrap(24, 16, AES192_128);106testKeyWrap(24, 24, AES192_192);107}108if (allowed >= 32*8) {109testKeyWrap(32, 16, AES256_128);110testKeyWrap(32, 24, AES256_192);111testKeyWrap(32, 32, AES256_256);112}113System.out.println("All Tests Passed");114}115}116117118