Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/KeyWrap/XMLEncKAT.java
41161 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 5008159 500815626* @summary Verify that the two key wrap ciphers, i.e. "DESedeWrap"27* and "AESWrap", work as expected.28* @run main XMLEncKAT29* @author Valerie Peng30*/31import java.util.Base64;32import java.security.Key;33import java.security.AlgorithmParameters;34import javax.crypto.*;35import javax.crypto.spec.*;36import java.io.UnsupportedEncodingException;37import java.io.IOException;3839public class XMLEncKAT {4041private static byte[] desEdeKey_1;42private static byte[] aes128Key_1;43private static byte[] aes192Key_1;44private static byte[] aes256Key_1;45private static byte[] desEdeKey_2;46private static byte[] aes128Key_2;47private static byte[] aes192Key_2;48private static byte[] aes256Key_2;4950private static Base64.Decoder base64D = Base64.getDecoder();51private static Base64.Encoder base64E = Base64.getEncoder();5253static {54try {55desEdeKey_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");56aes128Key_1 = "abcdefghijklmnop".getBytes("ASCII");57aes192Key_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");58aes256Key_1 = "abcdefghijklmnopqrstuvwxyz012345".getBytes("ASCII");59} catch (UnsupportedEncodingException uee) {60// should never happen61}6263desEdeKey_2 = base64D.decode64("yI+J1f3puYAERjIcT6vfg6RitmKX8nD0");65aes128Key_2 = base64D.decode66("01+yuQ2huPS1+Qv0LH+zaQ==");67aes192Key_2 = base64D.decode68("IlfuS40LvStVU0Mj8ePrrGHVhAb48y++");69aes256Key_2 = base64D.decode70("ZhZ4v3RlwTlCEOpIrHfLKVyJOBDtEJOOQDat/4xR1bA=");7172}73private static String[] desEdeWrappedKey_1 = {74"ZyJbVsjRM4MEsswwwHz57aUz1eMqZHuEIoEPGS47CcmLvhuCtlzWZ9S/WcVJZIpz",75"gHMpx5iF7+KXtNHLasZrkcLHn8Ti4rxUjCIRK+IcgbQir6FUsQ/uxQ3o8enEMWq1"76};77private static String[] desEdeWrappedKey_2 = {78"/PZvvn42E9dmMUZ8KCY6B5XtLaaIaG4X5YNDwgV5Vlo=",79"HgVuHoXxBQWD9fvi0gt9TanywZ5lJokM/12fcMG6gRoMjsCPulH+4A=="80};81private static String[] aes128WrappedKey_1 = {82"dV45TUpJbidb9iKa34xj1WVtTZ036cnqvym2TBJWR5c=",83"rPnY/XoSGCbuwy7vpslf29rs9dbvSCmGFOjEs3LT6g/qyZjfDA+2fQ=="84};85private static String[] aes128WrappedKey_2 = {86"GPl6bneL1jKl0/lGnf9gejlYHRI6XxFz"87};88private static String[] aes192WrappedKey_1 = {89"IbjZH7Mq564oMybpvCHWYM/5ER3eFsAV",90"19D633XVohP6UJvaVRAhJek+ahtM3gOiVs6nZyAasDEb+WCUQOcWZw=="91};92private static String[] aes192WrappedKey_2 = {93"5+GpVUQNTAT3uY8pPedEg/PpftiX+fJsTCun+fgmIz0=",94"iuZvvGBWScikHld9TtNIOz0Sm7Srg5AcxOBMA8qIvQY=",95"PeDwjnCsg6xWzs3SmzUtc2nyUz28nGu7"96};97private static String[] aes256WrappedKey_1 = {98"4AAgyi3M7xNdBimbQZKdGJLn3/cS4Yv8QKuA01+gUnY=",99"tPCC89jQShB+WDINCdRfKgf8wTlAx8xRXD73RmEHPBfix8zS1N82KQ==",100"bsL63D0hPN6EOyzdgfEmKsAAvoJiGM+Wp9a9KZM92IKdl7s3YSntRg=="101};102private static String[] aes256WrappedKey_2 = {103"IbnoS1cvuIFIGB46jj1V1FGftc92irrCwcC7BoBvxwQ=",104"ic+Om6/3ZKcThVN3iv9lUEankNkDv3Et",105"jOvQe4SxDqEMvAHcmb3Z+/Uedj23pvL6BRQsl2sjJlQ=",106"IMwdsyg89IZ4Txf1SYYZNKUOKuYdDoIi/zEKXCjj4j9PM6BdkZligA=="107};108109public static void testKeyWrap(String cAlg, byte[] cKeyVal,110String cKeyAlg, String[] base64Wrapped) throws Exception {111System.out.println("Testing " + cAlg + " Cipher with " +1128*cKeyVal.length + "-bit key");113Cipher c = Cipher.getInstance(cAlg, "SunJCE");114SecretKey cKey = new SecretKeySpec(cKeyVal, cKeyAlg);115c.init(Cipher.UNWRAP_MODE, cKey);116Key[] key = new SecretKey[base64Wrapped.length];117IvParameterSpec[] params =118new IvParameterSpec[base64Wrapped.length];119// first test UNWRAP with known values120for (int i = 0; i < base64Wrapped.length; i++) {121byte[] wrappedKey = base64D.decode(base64Wrapped[i]);122key[i] = c.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);123if (c.getIV() != null) {124params[i] = new IvParameterSpec(c.getIV());125}126}127// then test WRAP and compare with the known values128for (int i = 0; i < key.length; i++) {129c.init(Cipher.WRAP_MODE, cKey, params[i]);130byte[] wrapped2 = c.wrap(key[i]);131String out = base64E.encodeToString(wrapped2);132if (!out.equalsIgnoreCase(base64Wrapped[i])) {133throw new Exception("Wrap failed; got " + out + ", expect " +134base64Wrapped[i]);135}136}137}138139public static void main(String[] argv) throws Exception {140String wrapAlg = "DESedeWrap";141String keyAlg = "DESede";142testKeyWrap(wrapAlg, desEdeKey_1, keyAlg, desEdeWrappedKey_1);143testKeyWrap(wrapAlg, desEdeKey_2, keyAlg, desEdeWrappedKey_2);144145wrapAlg = "AESWrap";146keyAlg = "AES";147testKeyWrap(wrapAlg, aes128Key_1, keyAlg, aes128WrappedKey_1);148testKeyWrap(wrapAlg, aes128Key_2, keyAlg, aes128WrappedKey_2);149// only run the tests on longer key lengths if unlimited version150// of JCE jurisdiction policy files are installed151if (Cipher.getMaxAllowedKeyLength(keyAlg) == Integer.MAX_VALUE) {152testKeyWrap(wrapAlg, aes192Key_1, keyAlg, aes192WrappedKey_1);153testKeyWrap(wrapAlg, aes192Key_2, keyAlg, aes192WrappedKey_2);154testKeyWrap(wrapAlg, aes256Key_1, keyAlg, aes256WrappedKey_1);155testKeyWrap(wrapAlg, aes256Key_2, keyAlg, aes256WrappedKey_2);156}157System.out.println("All Tests Passed");158}159}160161162