Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/KeyWrap/XMLEncKAT.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 5008159 500815626* @run main XMLEncKAT27* @summary Verify that the two key wrap ciphers, i.e. "DESedeWrap"28* and "AESWrap", work as expected.29* @author Valerie Peng30*/31import java.security.Key;32import java.security.AlgorithmParameters;33import javax.crypto.*;34import javax.crypto.spec.*;35import sun.misc.BASE64Decoder;36import sun.misc.BASE64Encoder;37import java.io.UnsupportedEncodingException;38import java.io.IOException;3940public class XMLEncKAT {4142private static byte[] desEdeKey_1;43private static byte[] aes128Key_1;44private static byte[] aes192Key_1;45private static byte[] aes256Key_1;46private static byte[] desEdeKey_2;47private static byte[] aes128Key_2;48private static byte[] aes192Key_2;49private static byte[] aes256Key_2;5051private static BASE64Decoder base64D = new BASE64Decoder();52private static BASE64Encoder base64E = new BASE64Encoder();5354static {55try {56desEdeKey_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");57aes128Key_1 = "abcdefghijklmnop".getBytes("ASCII");58aes192Key_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");59aes256Key_1 = "abcdefghijklmnopqrstuvwxyz012345".getBytes("ASCII");60} catch (UnsupportedEncodingException uee) {61// should never happen62}63try {64desEdeKey_2 = base64D.decodeBuffer65("yI+J1f3puYAERjIcT6vfg6RitmKX8nD0");66aes128Key_2 = base64D.decodeBuffer67("01+yuQ2huPS1+Qv0LH+zaQ==");68aes192Key_2 = base64D.decodeBuffer69("IlfuS40LvStVU0Mj8ePrrGHVhAb48y++");70aes256Key_2 = base64D.decodeBuffer71("ZhZ4v3RlwTlCEOpIrHfLKVyJOBDtEJOOQDat/4xR1bA=");72} catch (IOException ioe) {73// should never happen74}75}76private static String[] desEdeWrappedKey_1 = {77"ZyJbVsjRM4MEsswwwHz57aUz1eMqZHuEIoEPGS47CcmLvhuCtlzWZ9S/WcVJZIpz",78"gHMpx5iF7+KXtNHLasZrkcLHn8Ti4rxUjCIRK+IcgbQir6FUsQ/uxQ3o8enEMWq1"79};80private static String[] desEdeWrappedKey_2 = {81"/PZvvn42E9dmMUZ8KCY6B5XtLaaIaG4X5YNDwgV5Vlo=",82"HgVuHoXxBQWD9fvi0gt9TanywZ5lJokM/12fcMG6gRoMjsCPulH+4A=="83};84private static String[] aes128WrappedKey_1 = {85"dV45TUpJbidb9iKa34xj1WVtTZ036cnqvym2TBJWR5c=",86"rPnY/XoSGCbuwy7vpslf29rs9dbvSCmGFOjEs3LT6g/qyZjfDA+2fQ=="87};88private static String[] aes128WrappedKey_2 = {89"GPl6bneL1jKl0/lGnf9gejlYHRI6XxFz"90};91private static String[] aes192WrappedKey_1 = {92"IbjZH7Mq564oMybpvCHWYM/5ER3eFsAV",93"19D633XVohP6UJvaVRAhJek+ahtM3gOiVs6nZyAasDEb+WCUQOcWZw=="94};95private static String[] aes192WrappedKey_2 = {96"5+GpVUQNTAT3uY8pPedEg/PpftiX+fJsTCun+fgmIz0=",97"iuZvvGBWScikHld9TtNIOz0Sm7Srg5AcxOBMA8qIvQY=",98"PeDwjnCsg6xWzs3SmzUtc2nyUz28nGu7"99};100private static String[] aes256WrappedKey_1 = {101"4AAgyi3M7xNdBimbQZKdGJLn3/cS4Yv8QKuA01+gUnY=",102"tPCC89jQShB+WDINCdRfKgf8wTlAx8xRXD73RmEHPBfix8zS1N82KQ==",103"bsL63D0hPN6EOyzdgfEmKsAAvoJiGM+Wp9a9KZM92IKdl7s3YSntRg=="104};105private static String[] aes256WrappedKey_2 = {106"IbnoS1cvuIFIGB46jj1V1FGftc92irrCwcC7BoBvxwQ=",107"ic+Om6/3ZKcThVN3iv9lUEankNkDv3Et",108"jOvQe4SxDqEMvAHcmb3Z+/Uedj23pvL6BRQsl2sjJlQ=",109"IMwdsyg89IZ4Txf1SYYZNKUOKuYdDoIi/zEKXCjj4j9PM6BdkZligA=="110};111112public static void testKeyWrap(String cAlg, byte[] cKeyVal,113String cKeyAlg, String[] base64Wrapped) throws Exception {114System.out.println("Testing " + cAlg + " Cipher with " +1158*cKeyVal.length + "-bit key");116Cipher c = Cipher.getInstance(cAlg, "SunJCE");117SecretKey cKey = new SecretKeySpec(cKeyVal, cKeyAlg);118c.init(Cipher.UNWRAP_MODE, cKey);119Key[] key = new SecretKey[base64Wrapped.length];120IvParameterSpec[] params =121new IvParameterSpec[base64Wrapped.length];122// first test UNWRAP with known values123for (int i = 0; i < base64Wrapped.length; i++) {124byte[] wrappedKey = base64D.decodeBuffer(base64Wrapped[i]);125key[i] = c.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);126if (c.getIV() != null) {127params[i] = new IvParameterSpec(c.getIV());128}129}130// then test WRAP and compare with the known values131for (int i = 0; i < key.length; i++) {132c.init(Cipher.WRAP_MODE, cKey, params[i]);133byte[] wrapped2 = c.wrap(key[i]);134String out = base64E.encode(wrapped2);135if (!out.equalsIgnoreCase(base64Wrapped[i])) {136throw new Exception("Wrap failed; got " + out + ", expect " +137base64Wrapped[i]);138}139}140}141142public static void main(String[] argv) throws Exception {143String wrapAlg = "DESedeWrap";144String keyAlg = "DESede";145testKeyWrap(wrapAlg, desEdeKey_1, keyAlg, desEdeWrappedKey_1);146testKeyWrap(wrapAlg, desEdeKey_2, keyAlg, desEdeWrappedKey_2);147148wrapAlg = "AESWrap";149keyAlg = "AES";150testKeyWrap(wrapAlg, aes128Key_1, keyAlg, aes128WrappedKey_1);151testKeyWrap(wrapAlg, aes128Key_2, keyAlg, aes128WrappedKey_2);152// only run the tests on longer key lengths if unlimited version153// of JCE jurisdiction policy files are installed154if (Cipher.getMaxAllowedKeyLength(keyAlg) == Integer.MAX_VALUE) {155testKeyWrap(wrapAlg, aes192Key_1, keyAlg, aes192WrappedKey_1);156testKeyWrap(wrapAlg, aes192Key_2, keyAlg, aes192WrappedKey_2);157testKeyWrap(wrapAlg, aes256Key_1, keyAlg, aes256WrappedKey_1);158testKeyWrap(wrapAlg, aes256Key_2, keyAlg, aes256WrappedKey_2);159}160System.out.println("All Tests Passed");161}162}163164165