BlockUpdate() public method

public BlockUpdate ( byte input, int inOff, int len ) : void
input byte
inOff int
len int
return void
Esempio n. 1
0
        static void PolyUpdateMacText(Macs.Poly1305 poly, byte[] buf)
        {
            poly.BlockUpdate(buf, 0, buf.Length);
            int partial = buf.Length % 16;

            if (partial != 0)
            {
                var zeroPadding = new byte[15];
                poly.BlockUpdate(zeroPadding, 0, 16 - partial);
            }
        }
Esempio n. 2
0
 static void PolyUpdateMacLength(Macs.Poly1305 poly, int len)
 {
     byte[] longLen = BitConverter.GetBytes((ulong)len);
     poly.BlockUpdate(longLen, 0, longLen.Length);
 }
Esempio n. 3
0
		private void testSequential()
		{
			// Sequential test, adapted from test-poly1305aes
			int len;
			byte[] kr = new byte[32];
			byte[] m = new byte[MAXLEN];
			byte[] n = new byte[16];
			byte[] output = new byte[16];

			int c = 0;
			IMac mac = new Poly1305(new AesFastEngine());
			for (int loop = 0; loop < 13; loop++)
			{
				len = 0;
				for (;;)
				{
					c++;
					mac.Init(new ParametersWithIV(new KeyParameter(kr), n));
					mac.BlockUpdate(m, 0, len);
					mac.DoFinal(output, 0);

					// if (c == 678)
					// {
					// TestCase tc = CASES[0];
					//
					// if (!Arrays.AreEqual(tc.key, kr))
					// {
					// System.err.println("Key bad");
					// System.err.println(Hex.ToHexString(tc.key)));
					// System.err.println(Hex.ToHexString(kr)));
					// System.exit(1);
					// }
					// if (!Arrays.AreEqual(tc.nonce, n))
					// {
					// System.err.println("Nonce bad");
					// System.exit(1);
					// }
					// System.out.printf("[%d] m: %s\n", c, Hex.ToHexString(m, 0, len)));
					// System.out.printf("[%d] K: %s\n", c, new string(Hex.encodje(kr)));
					// System.out.printf("[%d] N: %s\n", c, Hex.ToHexString(n)));
					// System.out.printf("[%d] M: ", c);
					// }
					// System.out.printf("%d/%s\n", c, Hex.ToHexString(out)));

					if (len >= MAXLEN)
						break;
					n[0] = (byte)(n[0] ^ loop);
					for (int i = 0; i < 16; ++i)
						n[i] ^= output[i];
					if (len % 2 != 0)
						for (int i = 0; i < 16; ++i)
							kr[i] ^= output[i];
					if (len % 3 != 0)
						for (int i = 0; i < 16; ++i)
							kr[i + 16] ^= output[i];
					Poly1305KeyGenerator.Clamp(kr);
					m[len++] ^= output[0];
				}
			}
			// Output after 13 loops as generated by poly1305 ref
			if (c != 13013 || !Arrays.AreEqual(output, Hex.Decode("c96f60a23701a5b0fd2016f58cbe4f7e")))
			{
				Fail("Sequential Poly1305 " + c, "c96f60a23701a5b0fd2016f58cbe4f7e", Hex.ToHexString(output));
			}
		}
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
        private void CheckVector(byte[] keyMaterial, byte[] input, byte[] tag)
        {
            Poly1305 poly1305 = new Poly1305();

            poly1305.Init(new KeyParameter(keyMaterial));

            poly1305.BlockUpdate(input, 0, input.Length);

            byte[] mac = new byte[poly1305.GetMacSize()];

            poly1305.DoFinal(mac, 0);

            if (!Arrays.AreEqual(tag, mac))
            {
                Fail("rfc7539", Hex.ToHexString(tag), Hex.ToHexString(mac));
            }
       }