001: package org.bouncycastle.jce.provider.test;
002:
003: import java.security.Key;
004: import java.security.Security;
005:
006: import javax.crypto.Cipher;
007: import javax.crypto.spec.IvParameterSpec;
008: import javax.crypto.spec.SecretKeySpec;
009:
010: import org.bouncycastle.jce.provider.BouncyCastleProvider;
011: import org.bouncycastle.util.encoders.Hex;
012: import org.bouncycastle.util.test.SimpleTest;
013:
014: /**
015: * test vectors based on NIST Special Publication 800-38A,
016: * "Recommendation for Block Cipher Modes of Operation"
017: */
018: public class AESSICTest extends SimpleTest {
019: private byte[][] keys = {
020: Hex.decode("2b7e151628aed2a6abf7158809cf4f3c"),
021: Hex
022: .decode("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"),
023: Hex
024: .decode("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4") };
025:
026: private byte[][] plain = {
027: Hex.decode("6bc1bee22e409f96e93d7e117393172a"),
028: Hex.decode("ae2d8a571e03ac9c9eb76fac45af8e51"),
029: Hex.decode("30c81c46a35ce411e5fbc1191a0a52ef"),
030: Hex.decode("f69f2445df4f9b17ad2b417be66c3710") };
031:
032: private byte[][][] cipher = {
033: { Hex.decode("874d6191b620e3261bef6864990db6ce"),
034: Hex.decode("9806f66b7970fdff8617187bb9fffdff"),
035: Hex.decode("5ae4df3edbd5d35e5b4f09020db03eab"),
036: Hex.decode("1e031dda2fbe03d1792170a0f3009cee") },
037: { Hex.decode("1abc932417521ca24f2b0459fe7e6e0b"),
038: Hex.decode("090339ec0aa6faefd5ccc2c6f4ce8e94"),
039: Hex.decode("1e36b26bd1ebc670d1bd1d665620abf7"),
040: Hex.decode("4f78a7f6d29809585a97daec58c6b050") },
041: { Hex.decode("601ec313775789a5b7a7f504bbf3d228"),
042: Hex.decode("f443e3ca4d62b59aca84e990cacaf5c5"),
043: Hex.decode("2b0930daa23de94ce87017ba2d84988d"),
044: Hex.decode("dfc9c58db67aada613c2dd08457941a6") } };
045:
046: public String getName() {
047: return "AESSIC";
048: }
049:
050: public void performTest() throws Exception {
051: Cipher c = Cipher.getInstance("AES/SIC/NoPadding", "BC");
052:
053: //
054: // NIST vectors
055: //
056: for (int i = 0; i != keys.length; i++) {
057: Key sk = new SecretKeySpec(keys[i], "AES");
058: c.init(Cipher.ENCRYPT_MODE, sk, new IvParameterSpec(Hex
059: .decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF")));
060:
061: for (int j = 0; j != plain.length; j++) {
062: byte[] crypt = c.update(plain[j]);
063: if (!areEqual(crypt, cipher[i][j])) {
064: fail("AESSIC encrypt failed: key " + i + " block "
065: + j);
066: }
067: }
068:
069: c.init(Cipher.DECRYPT_MODE, sk, new IvParameterSpec(Hex
070: .decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF")));
071:
072: for (int j = 0; j != plain.length; j++) {
073: byte[] crypt = c.update(cipher[i][j]);
074: if (!areEqual(crypt, plain[j])) {
075: fail("AESSIC decrypt failed: key " + i + " block "
076: + j);
077: }
078: }
079: }
080:
081: //
082: // check CTR also recognised.
083: //
084: c = Cipher.getInstance("AES/CTR/NoPadding", "BC");
085:
086: Key sk = new SecretKeySpec(Hex
087: .decode("2B7E151628AED2A6ABF7158809CF4F3C"), "AES");
088:
089: c.init(Cipher.ENCRYPT_MODE, sk, new IvParameterSpec(Hex
090: .decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFD0001")));
091:
092: byte[] crypt = c.doFinal(Hex
093: .decode("00000000000000000000000000000000"));
094:
095: if (!areEqual(crypt, Hex
096: .decode("D23513162B02D0F72A43A2FE4A5F97AB"))) {
097: fail("AESSIC failed test 2");
098: }
099:
100: //
101: // check partial block processing
102: //
103: c = Cipher.getInstance("AES/CTR/NoPadding", "BC");
104:
105: sk = new SecretKeySpec(Hex
106: .decode("2B7E151628AED2A6ABF7158809CF4F3C"), "AES");
107:
108: c.init(Cipher.ENCRYPT_MODE, sk, new IvParameterSpec(Hex
109: .decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFD0001")));
110:
111: crypt = c.doFinal(Hex.decode("12345678"));
112:
113: c.init(Cipher.DECRYPT_MODE, sk, new IvParameterSpec(Hex
114: .decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFD0001")));
115:
116: crypt = c.doFinal(crypt);
117:
118: if (!areEqual(crypt, Hex.decode("12345678"))) {
119: fail("AESSIC failed partial test");
120: }
121: }
122:
123: public static void main(String[] args) {
124: Security.addProvider(new BouncyCastleProvider());
125:
126: runTest(new AESSICTest());
127: }
128: }
|