Generates keys for the Poly1305 MAC.
Poly1305 keys are 256 bit keys consisting of a 128 bit secret key used for the underlying block cipher followed by a 128 bit {@code r} value used for the polynomial portion of the Mac.
The {@code r} value has a specific format with some bits required to be cleared, resulting in an effective 106 bit key.
A separately generated 256 bit key can be modified to fit the Poly1305 key format by using the {@link #clamp(byte[])} method to clear the required bits.
Inheritance: Org.BouncyCastle.Crypto.CipherKeyGenerator
Esempio n. 1
0
 public static void CheckKey(byte[] key)
 {
     if (key.Length != 32)
     {
         throw new ArgumentException("Poly1305 key must be 256 bits.");
     }
     Poly1305KeyGenerator.checkMask(key[19], 15);
     Poly1305KeyGenerator.checkMask(key[23], 15);
     Poly1305KeyGenerator.checkMask(key[27], 15);
     Poly1305KeyGenerator.checkMask(key[31], 15);
     Poly1305KeyGenerator.checkMask(key[20], 252);
     Poly1305KeyGenerator.checkMask(key[24], 252);
     Poly1305KeyGenerator.checkMask(key[28], 252);
 }
Esempio n. 2
0
		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
			}
		}
Esempio n. 3
0
		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
			}

		}
Esempio n. 4
0
		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");
			}
		}
Esempio n. 5
0
 protected override byte[] engineGenerateKey()
 {
     byte[] array = base.engineGenerateKey();
     Poly1305KeyGenerator.Clamp(array);
     return(array);
 }