private void randomTest( SecureRandom srng) { int DAT_LEN = srng.Next(1024); byte[] nonce = new byte[NONCE_LEN]; byte[] authen = new byte[AUTHEN_LEN]; byte[] datIn = new byte[DAT_LEN]; byte[] key = new byte[16]; srng.NextBytes(nonce); srng.NextBytes(authen); srng.NextBytes(datIn); srng.NextBytes(key); AesFastEngine engine = new AesFastEngine(); KeyParameter sessKey = new KeyParameter(key); EaxBlockCipher eaxCipher = new EaxBlockCipher(engine); AeadParameters parameters = new AeadParameters(sessKey, MAC_LEN * 8, nonce, authen); eaxCipher.Init(true, parameters); byte[] intrDat = new byte[eaxCipher.GetOutputSize(datIn.Length)]; int outOff = eaxCipher.ProcessBytes(datIn, 0, DAT_LEN, intrDat, 0); outOff += eaxCipher.DoFinal(intrDat, outOff); eaxCipher.Init(false, parameters); byte[] datOut = new byte[eaxCipher.GetOutputSize(outOff)]; int resultLen = eaxCipher.ProcessBytes(intrDat, 0, outOff, datOut, 0); eaxCipher.DoFinal(datOut, resultLen); if (!AreEqual(datIn, datOut)) { Fail("EAX roundtrip failed to match"); } }
private void runCheckVectors( int count, EaxBlockCipher encEax, EaxBlockCipher decEax, string additionalDataType, byte[] sa, byte[] p, byte[] t, byte[] c) { byte[] enc = new byte[c.Length]; if (sa != null) { encEax.ProcessAadBytes(sa, 0, sa.Length); } int len = encEax.ProcessBytes(p, 0, p.Length, enc, 0); len += encEax.DoFinal(enc, len); if (!AreEqual(c, enc)) { Fail("encrypted stream fails to match in test " + count + " with " + additionalDataType); } byte[] tmp = new byte[enc.Length]; if (sa != null) { decEax.ProcessAadBytes(sa, 0, sa.Length); } len = decEax.ProcessBytes(enc, 0, enc.Length, tmp, 0); len += decEax.DoFinal(tmp, len); byte[] dec = new byte[len]; Array.Copy(tmp, 0, dec, 0, len); if (!AreEqual(p, dec)) { Fail("decrypted stream fails to match in test " + count + " with " + additionalDataType); } if (!AreEqual(t, decEax.GetMac())) { Fail("MAC fails to match in test " + count + " with " + additionalDataType); } }
private void runCheckVectors( int count, EaxBlockCipher encEax, EaxBlockCipher decEax, byte[] p, byte[] t, byte[] c) { byte[] enc = new byte[c.Length]; int len = encEax.ProcessBytes(p, 0, p.Length, enc, 0); len += encEax.DoFinal(enc, len); if (!AreEqual(c, enc)) { Fail("encrypted stream fails to match in test " + count); } byte[] tmp = new byte[enc.Length]; len = decEax.ProcessBytes(enc, 0, enc.Length, tmp, 0); len += decEax.DoFinal(tmp, len); byte[] dec = new byte[len]; Array.Copy(tmp, 0, dec, 0, len); if (!AreEqual(p, dec)) { Fail("decrypted stream fails to match in test " + count); } if (!AreEqual(t, decEax.GetMac())) { Fail("MAC fails to match in test " + count); } }
public override void PerformTest() { checkVectors(1, K1, 128, N1, A1, P1, T1, C1); checkVectors(2, K2, 128, N2, A2, P2, T2, C2); checkVectors(3, K3, 128, N3, A3, P3, T3, C3); checkVectors(4, K4, 128, N4, A4, P4, T4, C4); checkVectors(5, K5, 128, N5, A5, P5, T5, C5); checkVectors(6, K6, 128, N6, A6, P6, T6, C6); checkVectors(7, K7, 128, N7, A7, P7, T7, C7); checkVectors(8, K8, 128, N8, A8, P8, T8, C8); checkVectors(9, K9, 128, N9, A9, P9, T9, C9); checkVectors(10, K10, 128, N10, A10, P10, T10, C10); checkVectors(11, K11, 32, N11, A11, P11, T11, C11); EaxBlockCipher eax = new EaxBlockCipher(new AesEngine()); ivParamTest(1, eax, K1, N1); // // exception tests // try { eax.Init(false, new AeadParameters(new KeyParameter(K1), 32, N2, A2)); byte[] enc = new byte[C2.Length]; int len = eax.ProcessBytes(C2, 0, C2.Length, enc, 0); len += eax.DoFinal(enc, len); Fail("invalid cipher text not picked up"); } catch (InvalidCipherTextException) { // expected } try { eax.Init(false, new KeyParameter(K1)); Fail("illegal argument not picked up"); } catch (ArgumentException) { // expected } randomTests(); }
private void DoEax(byte[] key, byte[] iv, byte[] pt, byte[] aad, int tagLength, byte[] expected) { EaxBlockCipher c = new EaxBlockCipher(new SerpentEngine()); c.Init(true, new AeadParameters(new KeyParameter(key), tagLength, iv, aad)); byte[] output = new byte[expected.Length]; int len = c.ProcessBytes(pt, 0, pt.Length, output, 0); c.DoFinal(output, len); if (!Arrays.AreEqual(expected, output)) { Fail("EAX test failed"); } }