001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.InvalidCipherTextException;
004: import org.bouncycastle.crypto.engines.AESEngine;
005: import org.bouncycastle.crypto.modes.AEADBlockCipher;
006: import org.bouncycastle.crypto.modes.EAXBlockCipher;
007: import org.bouncycastle.crypto.params.AEADParameters;
008: import org.bouncycastle.crypto.params.CCMParameters;
009: import org.bouncycastle.crypto.params.KeyParameter;
010: import org.bouncycastle.crypto.params.ParametersWithIV;
011: import org.bouncycastle.util.Strings;
012: import org.bouncycastle.util.encoders.Hex;
013: import org.bouncycastle.util.test.SimpleTest;
014:
015: public class EAXTest extends SimpleTest {
016: private byte[] K1 = Hex.decode("233952DEE4D5ED5F9B9C6D6FF80FF478");
017: private byte[] N1 = Hex.decode("62EC67F9C3A4A407FCB2A8C49031A8B3");
018: private byte[] A1 = Hex.decode("6BFB914FD07EAE6B");
019: private byte[] P1 = Hex.decode("");
020: private byte[] C1 = Hex.decode("E037830E8389F27B025A2D6527E79D01");
021: private byte[] T1 = Hex.decode("E037830E8389F27B025A2D6527E79D01");
022:
023: private byte[] K2 = Hex.decode("91945D3F4DCBEE0BF45EF52255F095A4");
024: private byte[] N2 = Hex.decode("BECAF043B0A23D843194BA972C66DEBD");
025: private byte[] A2 = Hex.decode("FA3BFD4806EB53FA");
026: private byte[] P2 = Hex.decode("F7FB");
027: private byte[] C2 = Hex
028: .decode("19DD5C4C9331049D0BDAB0277408F67967E5");
029: private byte[] T2 = Hex.decode("5C4C9331049D0BDAB0277408F67967E5");
030:
031: private byte[] K3 = Hex.decode("01F74AD64077F2E704C0F60ADA3DD523");
032: private byte[] N3 = Hex.decode("70C3DB4F0D26368400A10ED05D2BFF5E");
033: private byte[] A3 = Hex.decode("234A3463C1264AC6");
034: private byte[] P3 = Hex.decode("1A47CB4933");
035: private byte[] C3 = Hex
036: .decode("D851D5BAE03A59F238A23E39199DC9266626C40F80");
037: private byte[] T3 = Hex.decode("3A59F238A23E39199DC9266626C40F80");
038:
039: private byte[] K4 = Hex.decode("D07CF6CBB7F313BDDE66B727AFD3C5E8");
040: private byte[] N4 = Hex.decode("8408DFFF3C1A2B1292DC199E46B7D617");
041: private byte[] A4 = Hex.decode("33CCE2EABFF5A79D");
042: private byte[] P4 = Hex.decode("481C9E39B1");
043: private byte[] C4 = Hex
044: .decode("632A9D131AD4C168A4225D8E1FF755939974A7BEDE");
045: private byte[] T4 = Hex.decode("D4C168A4225D8E1FF755939974A7BEDE");
046:
047: private byte[] K5 = Hex.decode("35B6D0580005BBC12B0587124557D2C2");
048: private byte[] N5 = Hex.decode("FDB6B06676EEDC5C61D74276E1F8E816");
049: private byte[] A5 = Hex.decode("AEB96EAEBE2970E9");
050: private byte[] P5 = Hex.decode("40D0C07DA5E4");
051: private byte[] C5 = Hex
052: .decode("071DFE16C675CB0677E536F73AFE6A14B74EE49844DD");
053: private byte[] T5 = Hex.decode("CB0677E536F73AFE6A14B74EE49844DD");
054:
055: private byte[] K6 = Hex.decode("BD8E6E11475E60B268784C38C62FEB22");
056: private byte[] N6 = Hex.decode("6EAC5C93072D8E8513F750935E46DA1B");
057: private byte[] A6 = Hex.decode("D4482D1CA78DCE0F");
058: private byte[] P6 = Hex.decode("4DE3B35C3FC039245BD1FB7D");
059: private byte[] C6 = Hex
060: .decode("835BB4F15D743E350E728414ABB8644FD6CCB86947C5E10590210A4F");
061: private byte[] T6 = Hex.decode("ABB8644FD6CCB86947C5E10590210A4F");
062:
063: private byte[] K7 = Hex.decode("7C77D6E813BED5AC98BAA417477A2E7D");
064: private byte[] N7 = Hex.decode("1A8C98DCD73D38393B2BF1569DEEFC19");
065: private byte[] A7 = Hex.decode("65D2017990D62528");
066: private byte[] P7 = Hex
067: .decode("8B0A79306C9CE7ED99DAE4F87F8DD61636");
068: private byte[] C7 = Hex
069: .decode("02083E3979DA014812F59F11D52630DA30137327D10649B0AA6E1C181DB617D7F2");
070: private byte[] T7 = Hex.decode("137327D10649B0AA6E1C181DB617D7F2");
071:
072: private byte[] K8 = Hex.decode("5FFF20CAFAB119CA2FC73549E20F5B0D");
073: private byte[] N8 = Hex.decode("DDE59B97D722156D4D9AFF2BC7559826");
074: private byte[] A8 = Hex.decode("54B9F04E6A09189A");
075: private byte[] P8 = Hex
076: .decode("1BDA122BCE8A8DBAF1877D962B8592DD2D56");
077: private byte[] C8 = Hex
078: .decode("2EC47B2C4954A489AFC7BA4897EDCDAE8CC33B60450599BD02C96382902AEF7F832A");
079: private byte[] T8 = Hex.decode("3B60450599BD02C96382902AEF7F832A");
080:
081: private byte[] K9 = Hex.decode("A4A4782BCFFD3EC5E7EF6D8C34A56123");
082: private byte[] N9 = Hex.decode("B781FCF2F75FA5A8DE97A9CA48E522EC");
083: private byte[] A9 = Hex.decode("899A175897561D7E");
084: private byte[] P9 = Hex
085: .decode("6CF36720872B8513F6EAB1A8A44438D5EF11");
086: private byte[] C9 = Hex
087: .decode("0DE18FD0FDD91E7AF19F1D8EE8733938B1E8E7F6D2231618102FDB7FE55FF1991700");
088: private byte[] T9 = Hex.decode("E7F6D2231618102FDB7FE55FF1991700");
089:
090: private byte[] K10 = Hex.decode("8395FCF1E95BEBD697BD010BC766AAC3");
091: private byte[] N10 = Hex.decode("22E7ADD93CFC6393C57EC0B3C17D6B44");
092: private byte[] A10 = Hex.decode("126735FCC320D25A");
093: private byte[] P10 = Hex
094: .decode("CA40D7446E545FFAED3BD12A740A659FFBBB3CEAB7");
095: private byte[] C10 = Hex
096: .decode("CB8920F87A6C75CFF39627B56E3ED197C552D295A7CFC46AFC253B4652B1AF3795B124AB6E");
097: private byte[] T10 = Hex.decode("CFC46AFC253B4652B1AF3795B124AB6E");
098:
099: private byte[] K11 = Hex.decode("8395FCF1E95BEBD697BD010BC766AAC3");
100: private byte[] N11 = Hex.decode("22E7ADD93CFC6393C57EC0B3C17D6B44");
101: private byte[] A11 = Hex.decode("126735FCC320D25A");
102: private byte[] P11 = Hex
103: .decode("CA40D7446E545FFAED3BD12A740A659FFBBB3CEAB7");
104: private byte[] C11 = Hex
105: .decode("CB8920F87A6C75CFF39627B56E3ED197C552D295A7CFC46AFC");
106: private byte[] T11 = Hex.decode("CFC46AFC");
107:
108: public String getName() {
109: return "EAX";
110: }
111:
112: public void performTest() throws Exception {
113: EAXBlockCipher eax = new EAXBlockCipher(new AESEngine());
114:
115: checkVectors(1, eax, K1, 128, N1, A1, P1, T1, C1);
116: checkVectors(2, eax, K2, 128, N2, A2, P2, T2, C2);
117: checkVectors(3, eax, K3, 128, N3, A3, P3, T3, C3);
118: checkVectors(4, eax, K4, 128, N4, A4, P4, T4, C4);
119: checkVectors(5, eax, K5, 128, N5, A5, P5, T5, C5);
120: checkVectors(6, eax, K6, 128, N6, A6, P6, T6, C6);
121: checkVectors(7, eax, K7, 128, N7, A7, P7, T7, C7);
122: checkVectors(8, eax, K8, 128, N8, A8, P8, T8, C8);
123: checkVectors(9, eax, K9, 128, N9, A9, P9, T9, C9);
124: checkVectors(10, eax, K10, 128, N10, A10, P10, T10, C10);
125: checkVectors(11, eax, K11, 32, N11, A11, P11, T11, C11);
126:
127: ivParamTest(1, eax, K1, N1);
128:
129: //
130: // exception tests
131: //
132:
133: try {
134: eax.init(false, new CCMParameters(new KeyParameter(K1), 32,
135: N2, A2));
136:
137: byte[] enc = new byte[C2.length];
138: int len = eax.processBytes(C2, 0, C2.length, enc, 0);
139:
140: len += eax.doFinal(enc, len);
141:
142: fail("invalid cipher text not picked up");
143: } catch (InvalidCipherTextException e) {
144: // expected
145: }
146:
147: try {
148: eax.init(false, new KeyParameter(K1));
149:
150: fail("illegal argument not picked up");
151: } catch (IllegalArgumentException e) {
152: // expected
153: }
154: }
155:
156: private void checkVectors(int count, EAXBlockCipher eax, byte[] k,
157: int macSize, byte[] n, byte[] a, byte[] p, byte[] t,
158: byte[] c) throws InvalidCipherTextException {
159: eax.init(true, new AEADParameters(new KeyParameter(k), macSize,
160: n, a));
161:
162: byte[] enc = new byte[c.length];
163:
164: int len = eax.processBytes(p, 0, p.length, enc, 0);
165:
166: len += eax.doFinal(enc, len);
167:
168: if (!areEqual(c, enc)) {
169: fail("encrypted stream fails to match in test " + count);
170: }
171:
172: eax.init(false, new AEADParameters(new KeyParameter(k),
173: macSize, n, a));
174:
175: byte[] tmp = new byte[enc.length];
176:
177: len = eax.processBytes(enc, 0, enc.length, tmp, 0);
178:
179: len += eax.doFinal(tmp, len);
180:
181: byte[] dec = new byte[len];
182:
183: System.arraycopy(tmp, 0, dec, 0, len);
184:
185: if (!areEqual(p, dec)) {
186: fail("decrypted stream fails to match in test " + count);
187: }
188:
189: if (!areEqual(t, eax.getMac())) {
190: fail("MAC fails to match in test " + count);
191: }
192: }
193:
194: private void ivParamTest(int count, AEADBlockCipher ccm, byte[] k,
195: byte[] n) throws InvalidCipherTextException {
196: byte[] p = Strings.toByteArray("hello world!!");
197:
198: ccm.init(true, new ParametersWithIV(new KeyParameter(k), n));
199:
200: byte[] enc = new byte[p.length + 8];
201:
202: int len = ccm.processBytes(p, 0, p.length, enc, 0);
203:
204: len += ccm.doFinal(enc, len);
205:
206: ccm.init(false, new ParametersWithIV(new KeyParameter(k), n));
207:
208: byte[] tmp = new byte[enc.length];
209:
210: len = ccm.processBytes(enc, 0, enc.length, tmp, 0);
211:
212: len += ccm.doFinal(tmp, len);
213:
214: byte[] dec = new byte[len];
215:
216: System.arraycopy(tmp, 0, dec, 0, len);
217:
218: if (!areEqual(p, dec)) {
219: fail("decrypted stream fails to match in test " + count);
220: }
221: }
222:
223: public static void main(String[] args) {
224: runTest(new EAXTest());
225: }
226: }
|