J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-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 4635086 |
| 27 | * @library ../UTIL |
| 28 | * @build TestUtil |
| 29 | * @run main TestKATForECB_IV |
| 30 | * @summary Known Answer Test for AES cipher with ECB mode |
| 31 | * @author Valerie Peng |
| 32 | */ |
| 33 | import java.security.*; |
| 34 | import javax.crypto.*; |
| 35 | import javax.crypto.spec.*; |
| 36 | import java.math.*; |
| 37 | import com.sun.crypto.provider.*; |
| 38 | |
| 39 | import java.util.*; |
| 40 | |
| 41 | public class TestKATForECB_IV |
| 42 | { |
| 43 | private static final String ALGO = "AES"; |
| 44 | private static final String MODE = "ECB"; |
| 45 | private static final String PADDING = "NoPadding"; |
| 46 | |
| 47 | //ecb-iv.txt |
| 48 | private static String[] PTS = { |
| 49 | "000102030405060708090A0B0C0D0E0F", |
| 50 | "000102030405060708090A0B0C0D0E0F", |
| 51 | "000102030405060708090A0B0C0D0E0F" |
| 52 | }; |
| 53 | private static String[] CTS = { |
| 54 | "0A940BB5416EF045F1C39458C653EA5A", |
| 55 | "0060BFFE46834BB8DA5CF9A61FF220AE", |
| 56 | "5A6E045708FB7196F02E553D02C3A692" |
| 57 | }; |
| 58 | private static String[] KEYS = { |
| 59 | "000102030405060708090A0B0C0D0E0F", |
| 60 | "000102030405060708090A0B0C0D0E0F1011121314151617", |
| 61 | "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F" |
| 62 | }; |
| 63 | |
| 64 | private static SecretKey constructAESKey(String s) throws Exception { |
| 65 | int len = s.length()/2; |
| 66 | if ((len != 16) && (len != 24) && (len != 32)) { |
| 67 | throw new IllegalArgumentException("Wrong Key Length: " + len); |
| 68 | } |
| 69 | byte[] rawKeyValue = constructByteArray(s); |
| 70 | SecretKeySpec key = new SecretKeySpec(rawKeyValue, "AES"); |
| 71 | return key; |
| 72 | } |
| 73 | |
| 74 | private static byte[] constructByteArray(String s) { |
| 75 | int len = s.length()/2; |
| 76 | byte[] tempValue = new byte[len]; |
| 77 | for (int i = 0; i < len; i++) { |
| 78 | tempValue[i] = Integer.valueOf(s.substring(2*i, 2*i+2), |
| 79 | 16).byteValue(); |
| 80 | } |
| 81 | return tempValue; |
| 82 | |
| 83 | } |
| 84 | |
| 85 | public boolean execute() throws Exception { |
| 86 | String transformation = ALGO+"/"+MODE+"/"+PADDING; |
| 87 | Cipher c = Cipher.getInstance(transformation, "SunJCE"); |
| 88 | for (int i=0; i<KEYS.length; i++) { |
| 89 | SecretKey aesKey = constructAESKey(KEYS[i]); |
| 90 | if (aesKey.getEncoded().length*8 > |
| 91 | Cipher.getMaxAllowedKeyLength(transformation)) { |
| 92 | // skip if this key length is larger than what's |
| 93 | // configured in the jce jurisdiction policy files |
| 94 | continue; |
| 95 | } |
| 96 | try { |
| 97 | c.init(Cipher.ENCRYPT_MODE, aesKey); |
| 98 | byte[] plainText = constructByteArray(PTS[i]); |
| 99 | byte[] cipherText = c.doFinal(plainText); |
| 100 | byte[] answer = constructByteArray(CTS[i]); |
| 101 | if (!Arrays.equals(cipherText, answer)) { |
| 102 | throw new Exception((i+1) + "th known answer test failed for encryption"); |
| 103 | } |
| 104 | |
| 105 | c.init(Cipher.DECRYPT_MODE, aesKey); |
| 106 | byte[] recoveredText = c.doFinal(answer); |
| 107 | if (!Arrays.equals(recoveredText, plainText)) { |
| 108 | throw new Exception("known answer test failed for decryption"); |
| 109 | } |
| 110 | System.out.println("Finished KAT for " + aesKey.getEncoded().length + "-byte key"); |
| 111 | } catch (SecurityException se) { |
| 112 | TestUtil.handleSE(se); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // passed all tests...hooray! |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | public static void main (String[] args) throws Exception { |
| 121 | Security.addProvider(new com.sun.crypto.provider.SunJCE()); |
| 122 | |
| 123 | TestKATForECB_IV test = new TestKATForECB_IV(); |
| 124 | String testName = test.getClass().getName() + "[" + ALGO + |
| 125 | "/" + MODE + "/" + PADDING + "]"; |
| 126 | if (test.execute()) { |
| 127 | System.out.println(testName + ": Passed!"); |
| 128 | } |
| 129 | } |
| 130 | } |