public void PolyReferenceTests(byte[] key, byte[] iv, byte[] data, byte[] expected) { Poly pmac = new Poly(Common.AesFactory); pmac.Init(key, iv, 16); pmac.Process(new ArraySegment <byte>(data)); byte[] mac = pmac.Compute(); var bcpoly = new Poly1305(new AesEngine()); bcpoly.Init(new ParametersWithIV(new KeyParameter(key), iv)); bcpoly.BlockUpdate(data, 0, data.Length); byte[] bcmac = new byte[bcpoly.GetMacSize()]; bcpoly.DoFinal(bcmac, 0); var _key = string.Join(", ", key.Select(b => $"0x{b:x2}")); var _iv = string.Join(", ", iv.Select(b => $"0x{b:x2}")); var _data = string.Join(", ", data.Select(b => $"0x{b:x2}")); var _expected = string.Join(", ", bcmac.Select(b => $"0x{b:x2}")); Log.Verbose($"[InlineData(new byte[] {{{_key}}}, new byte[] {{{_iv}}}, new byte[] {{{_data}}}, new byte[] {{{_expected}}})]"); Assert.Equal(expected, mac); Assert.Equal(expected, bcmac); }
private byte[] EncryptXSalsa20Poly1305(byte[] bytes, byte[] key, byte[] nonce) { var salsa = new XSalsa20Engine(); var poly = new Poly1305(); salsa.Init(true, new ParametersWithIV(new KeyParameter(key), nonce)); byte[] subKey = new byte[key.Length]; salsa.ProcessBytes(subKey, 0, key.Length, subKey, 0); byte[] output = new byte[bytes.Length + poly.GetMacSize()]; salsa.ProcessBytes(bytes, 0, bytes.Length, output, poly.GetMacSize()); poly.Init(new KeyParameter(subKey)); poly.BlockUpdate(output, poly.GetMacSize(), bytes.Length); poly.DoFinal(output, 0); return(output); }
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)); } }
public void BasicPolyTests(int ivBytes, int dataBytes, int macSize) { RandomNumberGenerator rng = new RandomNumberGenerator(); byte[] iv = rng.Generate(ivBytes); byte[] key = rng.Generate(32); byte[] data = rng.Generate(dataBytes); Poly pmac = new Poly(Common.AesFactory); var bcpoly = new Poly1305(new AesEngine()); pmac.Init(key, iv, macSize); bcpoly.Init(new ParametersWithIV(new KeyParameter(key), iv)); pmac.Process(new ArraySegment <byte>(data)); byte[] mac = pmac.Compute(); bcpoly.BlockUpdate(data, 0, data.Length); byte[] bcmac = new byte[bcpoly.GetMacSize()]; bcpoly.DoFinal(bcmac, 0); bcmac = bcmac.Take(macSize).ToArray(); Assert.Equal(bcmac, mac); }
public void PolyReuseKeyTest(int ivBytes, int dataBytes, int macSize) { RandomNumberGenerator rng = new RandomNumberGenerator(); byte[] iv = rng.Generate(ivBytes); byte[] key = rng.Generate(32); byte[] data1 = rng.Generate(dataBytes); byte[] data2 = rng.Generate(dataBytes); var pmac = new Poly(Common.AesFactory); pmac.Init(key, iv, macSize); pmac.Process(new ArraySegment <byte>(data1)); byte[] mac1 = pmac.Compute(); pmac.Init(key, iv, macSize); pmac.Process(new ArraySegment <byte>(data2)); byte[] mac2 = pmac.Compute(); var bcpoly1 = new Poly1305(new AesEngine()); bcpoly1.Init(new ParametersWithIV(new KeyParameter(key), iv)); bcpoly1.BlockUpdate(data1, 0, data1.Length); byte[] bcmac1 = new byte[bcpoly1.GetMacSize()]; bcpoly1.DoFinal(bcmac1, 0); bcmac1 = bcmac1.Take(macSize).ToArray(); var bcpoly2 = new Poly1305(new AesEngine()); bcpoly2.Init(new ParametersWithIV(new KeyParameter(key), iv)); bcpoly2.BlockUpdate(data2, 0, data2.Length); byte[] bcmac2 = new byte[bcpoly2.GetMacSize()]; bcpoly2.DoFinal(bcmac2, 0); bcmac2 = bcmac2.Take(macSize).ToArray(); Assert.Equal(bcmac1, mac1); Assert.Equal(bcmac2, mac2); }