J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 0000000 |
| 27 | * @summary DesAPITest |
| 28 | * @author Jan Luehe |
| 29 | */ |
| 30 | import java.io.*; |
| 31 | import java.security.*; |
| 32 | import java.security.spec.*; |
| 33 | import javax.crypto.*; |
| 34 | import javax.crypto.spec.*; |
| 35 | import com.sun.crypto.provider.*; |
| 36 | |
| 37 | public class DesAPITest { |
| 38 | |
| 39 | Cipher cipher; |
| 40 | IvParameterSpec params = null; |
| 41 | SecretKey cipherKey = null; |
| 42 | |
| 43 | public static byte[] key = { |
| 44 | (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67, |
| 45 | (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef |
| 46 | }; |
| 47 | |
| 48 | public static byte[] key3 = { |
| 49 | (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67, |
| 50 | (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef, |
| 51 | (byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3, |
| 52 | (byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87, |
| 53 | (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98, |
| 54 | (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10}; |
| 55 | |
| 56 | public static byte[] iv = { |
| 57 | (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98, |
| 58 | (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10}; |
| 59 | |
| 60 | static String[] crypts = {"DES", "DESede"}; |
| 61 | //static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"}; |
| 62 | static String[] modes = {"CFB24"}; |
| 63 | //static String[] paddings = {"PKCS5Padding", "NoPadding"}; |
| 64 | static String[] paddings = {"PKCS5Padding"}; |
| 65 | |
| 66 | public static void main(String[] args) throws Exception { |
| 67 | DesAPITest test = new DesAPITest(); |
| 68 | test.run(); |
| 69 | } |
| 70 | |
| 71 | public void run() throws Exception { |
| 72 | |
| 73 | for (int i=0; i<crypts.length; i++) { |
| 74 | for (int j=0; j<modes.length; j++) { |
| 75 | for (int k=0; k<paddings.length; k++) { |
| 76 | System.out.println |
| 77 | ("==============================="); |
| 78 | System.out.println |
| 79 | (crypts[i]+" "+modes[j]+" " + paddings[k]); |
| 80 | init(crypts[i], modes[j], paddings[k]); |
| 81 | runTest(); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public void init(String crypt, String mode, String padding) |
| 88 | throws Exception { |
| 89 | |
| 90 | SunJCE jce = new SunJCE(); |
| 91 | Security.addProvider(jce); |
| 92 | |
| 93 | KeySpec desKeySpec = null; |
| 94 | SecretKeyFactory factory = null; |
| 95 | |
| 96 | StringBuffer cipherName = new StringBuffer(crypt); |
| 97 | if (mode.length() != 0) |
| 98 | cipherName.append("/" + mode); |
| 99 | if (padding.length() != 0) |
| 100 | cipherName.append("/" + padding); |
| 101 | |
| 102 | cipher = Cipher.getInstance(cipherName.toString()); |
| 103 | if (crypt.endsWith("ede")) { |
| 104 | desKeySpec = new DESedeKeySpec(key3); |
| 105 | factory = SecretKeyFactory.getInstance("DESede", "SunJCE"); |
| 106 | } |
| 107 | else { |
| 108 | desKeySpec = new DESKeySpec(key); |
| 109 | factory = SecretKeyFactory.getInstance("DES", "SunJCE"); |
| 110 | } |
| 111 | |
| 112 | // retrieve the cipher key |
| 113 | cipherKey = factory.generateSecret(desKeySpec); |
| 114 | |
| 115 | // retrieve iv |
| 116 | if ( !mode.equals("ECB")) |
| 117 | params = new IvParameterSpec(iv); |
| 118 | else |
| 119 | params = null; |
| 120 | } |
| 121 | |
| 122 | public void runTest() throws Exception { |
| 123 | |
| 124 | int bufferLen = 512; |
| 125 | byte[] input = new byte[bufferLen]; |
| 126 | int len; |
| 127 | |
| 128 | // encrypt test |
| 129 | cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params); |
| 130 | |
| 131 | // getIV |
| 132 | System.out.println("getIV, " + cipher.getIV()); |
| 133 | byte[] output = null; |
| 134 | boolean thrown = false; |
| 135 | try { |
| 136 | input = null; |
| 137 | output = cipher.update(input, 0, -1); |
| 138 | } catch (IllegalArgumentException ex) { |
| 139 | thrown = true; |
| 140 | } |
| 141 | if (!thrown) { |
| 142 | throw new Exception("Expected IAE not thrown!"); |
| 143 | } |
| 144 | byte[] inbuf = "itaoti7890123456".getBytes(); |
| 145 | System.out.println("inputLength: " + inbuf.length); |
| 146 | output = cipher.update(inbuf); |
| 147 | |
| 148 | len = cipher.getOutputSize(16); |
| 149 | byte[] out = new byte[len]; |
| 150 | output = cipher.doFinal(); |
| 151 | System.out.println(len + " " + TestUtility.hexDump(output)); |
| 152 | } |
| 153 | } |