0N/A/*
2362N/A * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5008159 5008156
0N/A * @run main XMLEncKAT
0N/A * @summary Verify that the two key wrap ciphers, i.e. "DESedeWrap"
0N/A * and "AESWrap", work as expected.
0N/A * @author Valerie Peng
0N/A */
0N/Aimport java.security.Key;
0N/Aimport java.security.AlgorithmParameters;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/Aimport sun.misc.BASE64Decoder;
0N/Aimport sun.misc.BASE64Encoder;
0N/Aimport java.io.UnsupportedEncodingException;
0N/Aimport java.io.IOException;
0N/A
0N/Apublic class XMLEncKAT {
0N/A
0N/A private static byte[] desEdeKey_1;
0N/A private static byte[] aes128Key_1;
0N/A private static byte[] aes192Key_1;
0N/A private static byte[] aes256Key_1;
0N/A private static byte[] desEdeKey_2;
0N/A private static byte[] aes128Key_2;
0N/A private static byte[] aes192Key_2;
0N/A private static byte[] aes256Key_2;
0N/A
0N/A private static BASE64Decoder base64D = new BASE64Decoder();
0N/A private static BASE64Encoder base64E = new BASE64Encoder();
0N/A
0N/A static {
0N/A try {
0N/A desEdeKey_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");
0N/A aes128Key_1 = "abcdefghijklmnop".getBytes("ASCII");
0N/A aes192Key_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");
0N/A aes256Key_1 = "abcdefghijklmnopqrstuvwxyz012345".getBytes("ASCII");
0N/A } catch (UnsupportedEncodingException uee) {
0N/A // should never happen
0N/A }
0N/A try {
0N/A desEdeKey_2 = base64D.decodeBuffer
0N/A ("yI+J1f3puYAERjIcT6vfg6RitmKX8nD0");
0N/A aes128Key_2 = base64D.decodeBuffer
0N/A ("01+yuQ2huPS1+Qv0LH+zaQ==");
0N/A aes192Key_2 = base64D.decodeBuffer
0N/A ("IlfuS40LvStVU0Mj8ePrrGHVhAb48y++");
0N/A aes256Key_2 = base64D.decodeBuffer
0N/A ("ZhZ4v3RlwTlCEOpIrHfLKVyJOBDtEJOOQDat/4xR1bA=");
0N/A } catch (IOException ioe) {
0N/A // should never happen
0N/A }
0N/A }
0N/A private static String[] desEdeWrappedKey_1 = {
0N/A "ZyJbVsjRM4MEsswwwHz57aUz1eMqZHuEIoEPGS47CcmLvhuCtlzWZ9S/WcVJZIpz",
0N/A "gHMpx5iF7+KXtNHLasZrkcLHn8Ti4rxUjCIRK+IcgbQir6FUsQ/uxQ3o8enEMWq1"
0N/A };
0N/A private static String[] desEdeWrappedKey_2 = {
0N/A "/PZvvn42E9dmMUZ8KCY6B5XtLaaIaG4X5YNDwgV5Vlo=",
0N/A "HgVuHoXxBQWD9fvi0gt9TanywZ5lJokM/12fcMG6gRoMjsCPulH+4A=="
0N/A };
0N/A private static String[] aes128WrappedKey_1 = {
0N/A "dV45TUpJbidb9iKa34xj1WVtTZ036cnqvym2TBJWR5c=",
0N/A "rPnY/XoSGCbuwy7vpslf29rs9dbvSCmGFOjEs3LT6g/qyZjfDA+2fQ=="
0N/A };
0N/A private static String[] aes128WrappedKey_2 = {
0N/A "GPl6bneL1jKl0/lGnf9gejlYHRI6XxFz"
0N/A };
0N/A private static String[] aes192WrappedKey_1 = {
0N/A "IbjZH7Mq564oMybpvCHWYM/5ER3eFsAV",
0N/A "19D633XVohP6UJvaVRAhJek+ahtM3gOiVs6nZyAasDEb+WCUQOcWZw=="
0N/A };
0N/A private static String[] aes192WrappedKey_2 = {
0N/A "5+GpVUQNTAT3uY8pPedEg/PpftiX+fJsTCun+fgmIz0=",
0N/A "iuZvvGBWScikHld9TtNIOz0Sm7Srg5AcxOBMA8qIvQY=",
0N/A "PeDwjnCsg6xWzs3SmzUtc2nyUz28nGu7"
0N/A };
0N/A private static String[] aes256WrappedKey_1 = {
0N/A "4AAgyi3M7xNdBimbQZKdGJLn3/cS4Yv8QKuA01+gUnY=",
0N/A "tPCC89jQShB+WDINCdRfKgf8wTlAx8xRXD73RmEHPBfix8zS1N82KQ==",
0N/A "bsL63D0hPN6EOyzdgfEmKsAAvoJiGM+Wp9a9KZM92IKdl7s3YSntRg=="
0N/A };
0N/A private static String[] aes256WrappedKey_2 = {
0N/A "IbnoS1cvuIFIGB46jj1V1FGftc92irrCwcC7BoBvxwQ=",
0N/A "ic+Om6/3ZKcThVN3iv9lUEankNkDv3Et",
0N/A "jOvQe4SxDqEMvAHcmb3Z+/Uedj23pvL6BRQsl2sjJlQ=",
0N/A "IMwdsyg89IZ4Txf1SYYZNKUOKuYdDoIi/zEKXCjj4j9PM6BdkZligA=="
0N/A };
0N/A
0N/A public static void testKeyWrap(String cAlg, byte[] cKeyVal,
0N/A String cKeyAlg, String[] base64Wrapped) throws Exception {
0N/A System.out.println("Testing " + cAlg + " Cipher with " +
0N/A 8*cKeyVal.length + "-bit key");
0N/A Cipher c = Cipher.getInstance(cAlg, "SunJCE");
0N/A SecretKey cKey = new SecretKeySpec(cKeyVal, cKeyAlg);
0N/A c.init(Cipher.UNWRAP_MODE, cKey);
0N/A Key[] key = new SecretKey[base64Wrapped.length];
0N/A IvParameterSpec[] params =
0N/A new IvParameterSpec[base64Wrapped.length];
0N/A // first test UNWRAP with known values
0N/A for (int i = 0; i < base64Wrapped.length; i++) {
0N/A byte[] wrappedKey = base64D.decodeBuffer(base64Wrapped[i]);
0N/A key[i] = c.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
0N/A if (c.getIV() != null) {
0N/A params[i] = new IvParameterSpec(c.getIV());
0N/A }
0N/A }
0N/A // then test WRAP and compare with the known values
0N/A for (int i = 0; i < key.length; i++) {
0N/A c.init(Cipher.WRAP_MODE, cKey, params[i]);
0N/A byte[] wrapped2 = c.wrap(key[i]);
0N/A String out = base64E.encode(wrapped2);
0N/A if (!out.equalsIgnoreCase(base64Wrapped[i])) {
0N/A throw new Exception("Wrap failed; got " + out + ", expect " +
0N/A base64Wrapped[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] argv) throws Exception {
0N/A String wrapAlg = "DESedeWrap";
0N/A String keyAlg = "DESede";
0N/A testKeyWrap(wrapAlg, desEdeKey_1, keyAlg, desEdeWrappedKey_1);
0N/A testKeyWrap(wrapAlg, desEdeKey_2, keyAlg, desEdeWrappedKey_2);
0N/A
0N/A wrapAlg = "AESWrap";
0N/A keyAlg = "AES";
0N/A testKeyWrap(wrapAlg, aes128Key_1, keyAlg, aes128WrappedKey_1);
0N/A testKeyWrap(wrapAlg, aes128Key_2, keyAlg, aes128WrappedKey_2);
0N/A // only run the tests on longer key lengths if unlimited version
0N/A // of JCE jurisdiction policy files are installed
0N/A if (Cipher.getMaxAllowedKeyLength(keyAlg) == Integer.MAX_VALUE) {
0N/A testKeyWrap(wrapAlg, aes192Key_1, keyAlg, aes192WrappedKey_1);
0N/A testKeyWrap(wrapAlg, aes192Key_2, keyAlg, aes192WrappedKey_2);
0N/A testKeyWrap(wrapAlg, aes256Key_1, keyAlg, aes256WrappedKey_1);
0N/A testKeyWrap(wrapAlg, aes256Key_2, keyAlg, aes256WrappedKey_2);
0N/A }
0N/A System.out.println("All Tests Passed");
0N/A }
0N/A}