/// <summary> /// Generate a key. /// </summary> /// <returns>A Poly1305 key.</returns> public Key GenerateKey() { Poly1305KeyGenerator cipherKeyGenerator = new Poly1305KeyGenerator(); cipherKeyGenerator.Init(new Internal.KeyGenerationParameters(random, keySizeInBits)); return(new Key(cipherKeyGenerator.GenerateKey())); }
private void testReset() { CipherKeyGenerator gen = new Poly1305KeyGenerator(); gen.Init(new KeyGenerationParameters(new SecureRandom(), 256)); byte[] k = gen.GenerateKey(); byte[] m = new byte[10000]; byte[] check = new byte[16]; byte[] output = new byte[16]; // Generate baseline IMac poly = new Poly1305(new AesFastEngine()); poly.Init(new ParametersWithIV(new KeyParameter(k), new byte[16])); poly.BlockUpdate(m, 0, m.Length); poly.DoFinal(check, 0); // Check reset after doFinal poly.BlockUpdate(m, 0, m.Length); poly.DoFinal(output, 0); if (!Arrays.AreEqual(check, output)) { Fail("Mac not reset after doFinal"); } // Check reset poly.Update((byte)1); poly.Update((byte)2); poly.Reset(); poly.BlockUpdate(m, 0, m.Length); poly.DoFinal(output, 0); if (!Arrays.AreEqual(check, output)) { Fail("Mac not reset after doFinal"); } // Check init resets poly.Update((byte)1); poly.Update((byte)2); poly.Init(new ParametersWithIV(new KeyParameter(k), new byte[16])); poly.BlockUpdate(m, 0, m.Length); poly.DoFinal(output, 0); if (!Arrays.AreEqual(check, output)) { Fail("Mac not reset after doFinal"); } }
private void testInit() { CipherKeyGenerator gen = new Poly1305KeyGenerator(); gen.Init(new KeyGenerationParameters(new SecureRandom(), 256)); byte[] k = gen.GenerateKey(); IMac poly = new Poly1305(new AesFastEngine()); poly.Init(new ParametersWithIV(new KeyParameter(k), new byte[16])); try { poly.Init(new ParametersWithIV(new KeyParameter(k), new byte[15])); Fail("16 byte nonce required"); } catch (ArgumentException) { // Expected } try { byte[] k2 = new byte[k.Length - 1]; Array.Copy(k, 0, k2, 0, k2.Length); poly.Init(new ParametersWithIV(new KeyParameter(k2), new byte[16])); Fail("32 byte key required"); } catch (ArgumentException) { // Expected } /* * try * { * k[19] = (byte)0xFF; * poly.Init(new ParametersWithIV(new KeyParameter(k), new byte[16])); * Fail("UnClamped key should not be accepted."); * } * catch (ArgumentException) * { * // Expected * } */ }
private void testKeyGenerator() { CipherKeyGenerator gen = new Poly1305KeyGenerator(); gen.Init(new KeyGenerationParameters(new SecureRandom(), 256)); byte[] k = gen.GenerateKey(); if (k.Length != 32) { Fail("Poly1305 key should be 256 bits."); } try { Poly1305KeyGenerator.CheckKey(k); } catch (ArgumentException) { Fail("Poly1305 key should be Clamped on generation."); } byte[] k2 = new byte[k.Length]; Array.Copy(k, 0, k2, 0, k2.Length); Poly1305KeyGenerator.Clamp(k); if (!Arrays.AreEqual(k, k2)) { Fail("Poly1305 key should be Clamped on generation."); } /* * try * { * k2[19] = (byte)0xff; * Poly1305KeyGenerator.CheckKey(k2); * Fail("UnClamped key should fail check."); * } * catch (ArgumentException) * { * // Expected * } */ }