001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
004: import org.bouncycastle.jce.provider.BouncyCastleProvider;
005: import org.bouncycastle.util.encoders.Hex;
006:
007: import javax.crypto.Cipher;
008: import javax.crypto.CipherInputStream;
009: import javax.crypto.CipherOutputStream;
010: import javax.crypto.NoSuchPaddingException;
011: import javax.crypto.spec.IvParameterSpec;
012: import javax.crypto.spec.SecretKeySpec;
013: import java.io.ByteArrayInputStream;
014: import java.io.ByteArrayOutputStream;
015: import java.io.DataInputStream;
016: import java.io.IOException;
017: import java.security.Key;
018: import java.security.Security;
019:
020: /**
021: * basic test class for the AES cipher vectors from FIPS-197
022: */
023: public class AESTest extends BaseBlockCipherTest {
024: static String[] cipherTests = {
025: "128",
026: "000102030405060708090a0b0c0d0e0f",
027: "00112233445566778899aabbccddeeff",
028: "69c4e0d86a7b0430d8cdb78070b4c55a",
029: "192",
030: "000102030405060708090a0b0c0d0e0f1011121314151617",
031: "00112233445566778899aabbccddeeff",
032: "dda97ca4864cdfe06eaf70a0ec0d7191",
033: "256",
034: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
035: "00112233445566778899aabbccddeeff",
036: "8ea2b7ca516745bfeafc49904b496089", };
037:
038: public AESTest() {
039: super ("AES");
040: }
041:
042: private void test(int strength, byte[] keyBytes, byte[] input,
043: byte[] output) throws Exception {
044: Key key;
045: Cipher in, out;
046: CipherInputStream cIn;
047: CipherOutputStream cOut;
048: ByteArrayInputStream bIn;
049: ByteArrayOutputStream bOut;
050:
051: key = new SecretKeySpec(keyBytes, "AES");
052:
053: in = Cipher.getInstance("AES/ECB/NoPadding", "BC");
054: out = Cipher.getInstance("AES/ECB/NoPadding", "BC");
055:
056: try {
057: out.init(Cipher.ENCRYPT_MODE, key);
058: } catch (Exception e) {
059: fail("AES failed initialisation - " + e.toString(), e);
060: }
061:
062: try {
063: in.init(Cipher.DECRYPT_MODE, key);
064: } catch (Exception e) {
065: fail("AES failed initialisation - " + e.toString(), e);
066: }
067:
068: //
069: // encryption pass
070: //
071: bOut = new ByteArrayOutputStream();
072:
073: cOut = new CipherOutputStream(bOut, out);
074:
075: try {
076: for (int i = 0; i != input.length / 2; i++) {
077: cOut.write(input[i]);
078: }
079: cOut.write(input, input.length / 2, input.length
080: - input.length / 2);
081: cOut.close();
082: } catch (IOException e) {
083: fail("AES failed encryption - " + e.toString(), e);
084: }
085:
086: byte[] bytes;
087:
088: bytes = bOut.toByteArray();
089:
090: if (!areEqual(bytes, output)) {
091: fail("AES failed encryption - expected "
092: + new String(Hex.encode(output)) + " got "
093: + new String(Hex.encode(bytes)));
094: }
095:
096: //
097: // decryption pass
098: //
099: bIn = new ByteArrayInputStream(bytes);
100:
101: cIn = new CipherInputStream(bIn, in);
102:
103: try {
104: DataInputStream dIn = new DataInputStream(cIn);
105:
106: bytes = new byte[input.length];
107:
108: for (int i = 0; i != input.length / 2; i++) {
109: bytes[i] = (byte) dIn.read();
110: }
111: dIn.readFully(bytes, input.length / 2, bytes.length
112: - input.length / 2);
113: } catch (Exception e) {
114: fail("AES failed encryption - " + e.toString(), e);
115: }
116:
117: if (!areEqual(bytes, input)) {
118: fail("AES failed decryption - expected "
119: + new String(Hex.encode(input)) + " got "
120: + new String(Hex.encode(bytes)));
121: }
122: }
123:
124: private void eaxTest() throws Exception {
125: byte[] K = Hex.decode("233952DEE4D5ED5F9B9C6D6FF80FF478");
126: byte[] N = Hex.decode("62EC67F9C3A4A407FCB2A8C49031A8B3");
127: byte[] P = Hex.decode("68656c6c6f20776f726c642121");
128: byte[] C = Hex
129: .decode("2f9f76cb7659c70e4be11670a3e193ae1bc6b5762a");
130:
131: Key key;
132: Cipher in, out;
133:
134: key = new SecretKeySpec(K, "AES");
135:
136: in = Cipher.getInstance("AES/EAX/NoPadding", "BC");
137: out = Cipher.getInstance("AES/EAX/NoPadding", "BC");
138:
139: in.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(N));
140:
141: byte[] enc = in.doFinal(P);
142: if (!areEqual(enc, C)) {
143: fail("ciphertext doesn't match in EAX");
144: }
145:
146: out.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(N));
147:
148: byte[] dec = out.doFinal(C);
149: if (!areEqual(dec, P)) {
150: fail("plaintext doesn't match in EAX");
151: }
152:
153: try {
154: in = Cipher.getInstance("AES/EAX/PKCS5Padding", "BC");
155:
156: fail("bad padding missed in EAX");
157: } catch (NoSuchPaddingException e) {
158: // expected
159: }
160: }
161:
162: private void ccmTest() throws Exception {
163: byte[] K = Hex.decode("404142434445464748494a4b4c4d4e4f");
164: byte[] N = Hex.decode("10111213141516");
165: byte[] P = Hex.decode("68656c6c6f20776f726c642121");
166: byte[] C = Hex
167: .decode("39264f148b54c456035de0a531c8344f46db12b388");
168:
169: Key key;
170: Cipher in, out;
171:
172: key = new SecretKeySpec(K, "AES");
173:
174: in = Cipher.getInstance("AES/CCM/NoPadding", "BC");
175: out = Cipher.getInstance("AES/CCM/NoPadding", "BC");
176:
177: in.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(N));
178:
179: byte[] enc = in.doFinal(P);
180: if (!areEqual(enc, C)) {
181: fail("ciphertext doesn't match in CCM");
182: }
183:
184: out.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(N));
185:
186: byte[] dec = out.doFinal(C);
187: if (!areEqual(dec, P)) {
188: fail("plaintext doesn't match in CCM");
189: }
190:
191: try {
192: in = Cipher.getInstance("AES/CCM/PKCS5Padding", "BC");
193:
194: fail("bad padding missed in CCM");
195: } catch (NoSuchPaddingException e) {
196: // expected
197: }
198: }
199:
200: public void performTest() throws Exception {
201: for (int i = 0; i != cipherTests.length; i += 4) {
202: test(Integer.parseInt(cipherTests[i]), Hex
203: .decode(cipherTests[i + 1]), Hex
204: .decode(cipherTests[i + 2]), Hex
205: .decode(cipherTests[i + 3]));
206: }
207:
208: byte[] kek1 = Hex.decode("000102030405060708090a0b0c0d0e0f");
209: byte[] in1 = Hex.decode("00112233445566778899aabbccddeeff");
210: byte[] out1 = Hex
211: .decode("1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5");
212:
213: wrapTest(1, "AESWrap", kek1, in1, out1);
214:
215: String[] oids = { NISTObjectIdentifiers.id_aes128_ECB.getId(),
216: NISTObjectIdentifiers.id_aes128_CBC.getId(),
217: NISTObjectIdentifiers.id_aes128_OFB.getId(),
218: NISTObjectIdentifiers.id_aes128_CFB.getId(),
219: NISTObjectIdentifiers.id_aes192_ECB.getId(),
220: NISTObjectIdentifiers.id_aes192_CBC.getId(),
221: NISTObjectIdentifiers.id_aes192_OFB.getId(),
222: NISTObjectIdentifiers.id_aes192_CFB.getId(),
223: NISTObjectIdentifiers.id_aes256_ECB.getId(),
224: NISTObjectIdentifiers.id_aes256_CBC.getId(),
225: NISTObjectIdentifiers.id_aes256_OFB.getId(),
226: NISTObjectIdentifiers.id_aes256_CFB.getId() };
227:
228: String[] names = { "AES/ECB/PKCS7Padding",
229: "AES/CBC/PKCS7Padding", "AES/OFB/PKCS7Padding",
230: "AES/CFB/PKCS7Padding", "AES/ECB/PKCS7Padding",
231: "AES/CBC/PKCS7Padding", "AES/OFB/PKCS7Padding",
232: "AES/CFB/PKCS7Padding", "AES/ECB/PKCS7Padding",
233: "AES/CBC/PKCS7Padding", "AES/OFB/PKCS7Padding",
234: "AES/CFB/PKCS7Padding" };
235:
236: oidTest(oids, names, 4);
237:
238: String[] wrapOids = {
239: NISTObjectIdentifiers.id_aes128_wrap.getId(),
240: NISTObjectIdentifiers.id_aes192_wrap.getId(),
241: NISTObjectIdentifiers.id_aes256_wrap.getId() };
242:
243: wrapOidTest(wrapOids, "AESWrap");
244:
245: eaxTest();
246: ccmTest();
247: }
248:
249: public static void main(String[] args) {
250: Security.addProvider(new BouncyCastleProvider());
251:
252: runTest(new AESTest());
253: }
254: }
|