1 /*
   2  * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5008159 5008156
  27  * @summary Verify that the two key wrap ciphers, i.e. "DESedeWrap"
  28  * and "AESWrap", work as expected.
  29  * @modules java.base/sun.misc
  30  * @run main XMLEncKAT
  31  * @author Valerie Peng
  32  */
  33 import java.security.Key;
  34 import java.security.AlgorithmParameters;
  35 import javax.crypto.*;
  36 import javax.crypto.spec.*;
  37 import sun.misc.BASE64Decoder;
  38 import sun.misc.BASE64Encoder;
  39 import java.io.UnsupportedEncodingException;
  40 import java.io.IOException;
  41 
  42 public class XMLEncKAT {
  43 
  44     private static byte[] desEdeKey_1;
  45     private static byte[] aes128Key_1;
  46     private static byte[] aes192Key_1;
  47     private static byte[] aes256Key_1;
  48     private static byte[] desEdeKey_2;
  49     private static byte[] aes128Key_2;
  50     private static byte[] aes192Key_2;
  51     private static byte[] aes256Key_2;
  52 
  53     private static BASE64Decoder base64D = new BASE64Decoder();
  54     private static BASE64Encoder base64E = new BASE64Encoder();
  55 
  56     static {
  57         try {
  58             desEdeKey_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");
  59             aes128Key_1 = "abcdefghijklmnop".getBytes("ASCII");
  60             aes192Key_1 = "abcdefghijklmnopqrstuvwx".getBytes("ASCII");
  61             aes256Key_1 = "abcdefghijklmnopqrstuvwxyz012345".getBytes("ASCII");
  62         } catch (UnsupportedEncodingException uee) {
  63             // should never happen
  64         }
  65         try {
  66             desEdeKey_2 = base64D.decodeBuffer
  67                 ("yI+J1f3puYAERjIcT6vfg6RitmKX8nD0");
  68             aes128Key_2 = base64D.decodeBuffer
  69                 ("01+yuQ2huPS1+Qv0LH+zaQ==");
  70             aes192Key_2 = base64D.decodeBuffer
  71                 ("IlfuS40LvStVU0Mj8ePrrGHVhAb48y++");
  72             aes256Key_2 = base64D.decodeBuffer
  73                 ("ZhZ4v3RlwTlCEOpIrHfLKVyJOBDtEJOOQDat/4xR1bA=");
  74         } catch (IOException ioe) {
  75             // should never happen
  76         }
  77     }
  78     private static String[] desEdeWrappedKey_1 = {
  79         "ZyJbVsjRM4MEsswwwHz57aUz1eMqZHuEIoEPGS47CcmLvhuCtlzWZ9S/WcVJZIpz",
  80         "gHMpx5iF7+KXtNHLasZrkcLHn8Ti4rxUjCIRK+IcgbQir6FUsQ/uxQ3o8enEMWq1"
  81     };
  82     private static String[] desEdeWrappedKey_2 = {
  83         "/PZvvn42E9dmMUZ8KCY6B5XtLaaIaG4X5YNDwgV5Vlo=",
  84         "HgVuHoXxBQWD9fvi0gt9TanywZ5lJokM/12fcMG6gRoMjsCPulH+4A=="
  85     };
  86     private static String[] aes128WrappedKey_1 = {
  87         "dV45TUpJbidb9iKa34xj1WVtTZ036cnqvym2TBJWR5c=",
  88         "rPnY/XoSGCbuwy7vpslf29rs9dbvSCmGFOjEs3LT6g/qyZjfDA+2fQ=="
  89     };
  90     private static String[] aes128WrappedKey_2 = {
  91         "GPl6bneL1jKl0/lGnf9gejlYHRI6XxFz"
  92     };
  93     private static String[] aes192WrappedKey_1 = {
  94         "IbjZH7Mq564oMybpvCHWYM/5ER3eFsAV",
  95         "19D633XVohP6UJvaVRAhJek+ahtM3gOiVs6nZyAasDEb+WCUQOcWZw=="
  96     };
  97     private static String[] aes192WrappedKey_2 = {
  98         "5+GpVUQNTAT3uY8pPedEg/PpftiX+fJsTCun+fgmIz0=",
  99         "iuZvvGBWScikHld9TtNIOz0Sm7Srg5AcxOBMA8qIvQY=",
 100         "PeDwjnCsg6xWzs3SmzUtc2nyUz28nGu7"
 101     };
 102     private static String[] aes256WrappedKey_1 = {
 103         "4AAgyi3M7xNdBimbQZKdGJLn3/cS4Yv8QKuA01+gUnY=",
 104         "tPCC89jQShB+WDINCdRfKgf8wTlAx8xRXD73RmEHPBfix8zS1N82KQ==",
 105         "bsL63D0hPN6EOyzdgfEmKsAAvoJiGM+Wp9a9KZM92IKdl7s3YSntRg=="
 106     };
 107     private static String[] aes256WrappedKey_2 = {
 108         "IbnoS1cvuIFIGB46jj1V1FGftc92irrCwcC7BoBvxwQ=",
 109         "ic+Om6/3ZKcThVN3iv9lUEankNkDv3Et",
 110         "jOvQe4SxDqEMvAHcmb3Z+/Uedj23pvL6BRQsl2sjJlQ=",
 111         "IMwdsyg89IZ4Txf1SYYZNKUOKuYdDoIi/zEKXCjj4j9PM6BdkZligA=="
 112     };
 113 
 114     public static void testKeyWrap(String cAlg, byte[] cKeyVal,
 115         String cKeyAlg, String[] base64Wrapped) throws Exception {
 116         System.out.println("Testing " + cAlg + " Cipher with " +
 117             8*cKeyVal.length + "-bit key");
 118         Cipher c = Cipher.getInstance(cAlg, "SunJCE");
 119         SecretKey cKey = new SecretKeySpec(cKeyVal, cKeyAlg);
 120         c.init(Cipher.UNWRAP_MODE, cKey);
 121         Key[] key = new SecretKey[base64Wrapped.length];
 122         IvParameterSpec[] params =
 123             new IvParameterSpec[base64Wrapped.length];
 124         // first test UNWRAP with known values
 125         for (int i = 0; i < base64Wrapped.length; i++) {
 126             byte[] wrappedKey = base64D.decodeBuffer(base64Wrapped[i]);
 127             key[i] = c.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
 128             if (c.getIV() != null) {
 129                 params[i] = new IvParameterSpec(c.getIV());
 130             }
 131         }
 132         // then test WRAP and compare with the known values
 133         for (int i = 0; i < key.length; i++) {
 134             c.init(Cipher.WRAP_MODE, cKey, params[i]);
 135             byte[] wrapped2 = c.wrap(key[i]);
 136             String out = base64E.encode(wrapped2);
 137             if (!out.equalsIgnoreCase(base64Wrapped[i])) {
 138                 throw new Exception("Wrap failed; got " + out + ", expect " +
 139                                    base64Wrapped[i]);
 140             }
 141         }
 142     }
 143 
 144     public static void main(String[] argv) throws Exception {
 145         String wrapAlg = "DESedeWrap";
 146         String keyAlg = "DESede";
 147         testKeyWrap(wrapAlg, desEdeKey_1, keyAlg, desEdeWrappedKey_1);
 148         testKeyWrap(wrapAlg, desEdeKey_2, keyAlg, desEdeWrappedKey_2);
 149 
 150         wrapAlg = "AESWrap";
 151         keyAlg = "AES";
 152         testKeyWrap(wrapAlg, aes128Key_1, keyAlg, aes128WrappedKey_1);
 153         testKeyWrap(wrapAlg, aes128Key_2, keyAlg, aes128WrappedKey_2);
 154         // only run the tests on longer key lengths if unlimited version
 155         // of JCE jurisdiction policy files are installed
 156         if (Cipher.getMaxAllowedKeyLength(keyAlg) == Integer.MAX_VALUE) {
 157             testKeyWrap(wrapAlg, aes192Key_1, keyAlg, aes192WrappedKey_1);
 158             testKeyWrap(wrapAlg, aes192Key_2, keyAlg, aes192WrappedKey_2);
 159             testKeyWrap(wrapAlg, aes256Key_1, keyAlg, aes256WrappedKey_1);
 160             testKeyWrap(wrapAlg, aes256Key_2, keyAlg, aes256WrappedKey_2);
 161         }
 162         System.out.println("All Tests Passed");
 163     }
 164 }