Exemplo n.º 1
0
 public byte[] Mac(
     string password,
     ReadOnlySpan <byte> salt,
     ReadOnlySpan <byte> data)
 {
     using Key key = _keyDerivationAlgorithm.DeriveKey(password, salt, _macAlgorithm);
     return(_macAlgorithm.Mac(key, data));
 }
Exemplo n.º 2
0
        public static void MacSuccess(MacAlgorithm a)
        {
            using var k = new Key(a);
            var data = Utilities.RandomBytes.Slice(0, 100);

            var expected = a.Mac(k, data);
            var actual   = a.Mac(k, data);

            Assert.NotNull(actual);
            Assert.Equal(a.MacSize, actual.Length);
            Assert.Equal(expected, actual);
        }
Exemplo n.º 3
0
        public static void MacWithSpanSuccess(MacAlgorithm a)
        {
            using var k = new Key(a);
            var data = Utilities.RandomBytes.Slice(0, 100);

            var expected = new byte[a.MacSize];
            var actual   = new byte[a.MacSize];

            a.Mac(k, data, expected);
            a.Mac(k, data, actual);
            Assert.Equal(expected, actual);
        }
Exemplo n.º 4
0
        public static void MacWithSpanOverlapping(MacAlgorithm a)
        {
            using var k = new Key(a);
            var data = Utilities.RandomBytes.Slice(0, 100).ToArray();

            var expected = new byte[a.MacSize];
            var actual   = data.AsSpan(0, a.MacSize);

            a.Mac(k, data, expected);
            a.Mac(k, data, actual);

            Assert.Equal(expected, actual.ToArray());
        }
Exemplo n.º 5
0
 public static void MacWithWrongKey(MacAlgorithm a)
 {
     using (var k = new Key(SignatureAlgorithm.Ed25519))
     {
         Assert.Throws <ArgumentException>("key", () => a.Mac(k, ReadOnlySpan <byte> .Empty));
     }
 }
Exemplo n.º 6
0
        public static void MacWithDisposedKey(MacAlgorithm a)
        {
            var k = new Key(a);

            k.Dispose();
            Assert.Throws <ObjectDisposedException>(() => a.Mac(k, ReadOnlySpan <byte> .Empty));
        }
Exemplo n.º 7
0
 public static void MacWithSpanTooLarge(MacAlgorithm a)
 {
     using (var k = new Key(a))
     {
         Assert.Throws <ArgumentException>("mac", () => a.Mac(k, ReadOnlySpan <byte> .Empty, new byte[a.MacSize + 1]));
     }
 }
Exemplo n.º 8
0
        public static void FinalizeSuccess(MacAlgorithm a)
        {
            using (var k = new Key(a))
            {
                var state = default(IncrementalMac);

                Assert.Null(state.Algorithm);

                IncrementalMac.Initialize(k, out state);

                Assert.Same(a, state.Algorithm);

                IncrementalMac.Update(ref state, Utilities.RandomBytes.Slice(0, 100));
                IncrementalMac.Update(ref state, Utilities.RandomBytes.Slice(100, 100));
                IncrementalMac.Update(ref state, Utilities.RandomBytes.Slice(200, 100));

                var actual = IncrementalMac.Finalize(ref state);

                Assert.Null(state.Algorithm);

                var expected = a.Mac(k, Utilities.RandomBytes.Slice(0, 300));

                Assert.Equal(expected, actual);
            }
        }
Exemplo n.º 9
0
        public static void VerifyWithSpanSuccess(MacAlgorithm a)
        {
            using var k = new Key(a);
            var d = ReadOnlySpan <byte> .Empty;

            var mac = a.Mac(k, d);

            Assert.True(a.Verify(k, d, mac));
        }
Exemplo n.º 10
0
        public static void FinalizeAndVerifySuccess(MacAlgorithm a)
        {
            using (var k = new Key(a))
            {
                IncrementalMac.Initialize(k, out var state);

                Assert.True(IncrementalMac.FinalizeAndVerify(ref state, a.Mac(k, ReadOnlySpan <byte> .Empty)));
            }
        }
Exemplo n.º 11
0
        public static void FinalizeWithSpanSuccessNoUpdate(MacAlgorithm a)
        {
            using var k = new Key(a);
            var state = default(IncrementalMac);

            Assert.Null(state.Algorithm);

            IncrementalMac.Initialize(k, out state);

            Assert.Same(a, state.Algorithm);

            var actual = new byte[a.MacSize];

            IncrementalMac.Finalize(ref state, actual);

            Assert.Null(state.Algorithm);

            var expected = a.Mac(k, ReadOnlySpan <byte> .Empty);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 12
0
 public static void MacWithNullKey(MacAlgorithm a)
 {
     Assert.Throws <ArgumentNullException>("key", () => a.Mac(null, ReadOnlySpan <byte> .Empty));
 }