示例#1
0
        public static void InitializeWithDisposedKey(MacAlgorithm a)
        {
            var k = new Key(a);

            k.Dispose();
            Assert.Throws <ObjectDisposedException>(() => IncrementalMac.Initialize(k, out _));
        }
示例#2
0
        public static void FinalizeSuccess(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            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);
            }
        }
示例#3
0
        public static void FinalizeWithSpanSuccessNoUpdate(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            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);
            }
        }
示例#4
0
        public static void FinalizeWithSpanSuccess(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 = new byte[a.MacSize];

                IncrementalMac.Finalize(ref state, actual);

                Assert.Null(state.Algorithm);

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

                Assert.Equal(expected, actual);
            }
        }
示例#5
0
        public static void InitializeWithWrongKey()
        {
            var a = AeadAlgorithm.ChaCha20Poly1305;

            using var k = new Key(a);

            Assert.Throws <ArgumentException>("key", () => IncrementalMac.Initialize(k, out _));
        }
示例#6
0
        public static void FinalizeWithSpanTooLarge(MacAlgorithm a)
        {
            using var k = new Key(a);

            IncrementalMac.Initialize(k, out var state);

            Assert.Throws <ArgumentException>("mac", () => IncrementalMac.Finalize(ref state, new byte[a.MacSize + 1]));
        }
示例#7
0
        public static void InitializeWithDisposedKey(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            var k = new Key(a);

            k.Dispose();
            Assert.Throws <ObjectDisposedException>(() => IncrementalMac.Initialize(k, out _));
        }
示例#8
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)));
            }
        }
示例#9
0
        public static void FinalizeAndVerifyFail(MacAlgorithm a)
        {
            using (var k = new Key(a))
            {
                IncrementalMac.Initialize(k, out var state);

                Assert.False(IncrementalMac.FinalizeAndVerify(ref state, new byte[a.MacSize]));
            }
        }
示例#10
0
        public static void FinalizeAndVerifySuccess(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            using (var k = new Key(a))
            {
                IncrementalMac.Initialize(k, out var state);

                Assert.True(IncrementalMac.FinalizeAndVerify(ref state, a.Mac(k, ReadOnlySpan <byte> .Empty)));
            }
        }
示例#11
0
        public static void FinalizeAndVerifyFail(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            using (var k = new Key(a))
            {
                IncrementalMac.Initialize(k, out var state);

                Assert.False(IncrementalMac.FinalizeAndVerify(ref state, new byte[a.MacSize]));
            }
        }
示例#12
0
        public static void FinalizeWithSpanTooLarge(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            using (var k = new Key(a))
            {
                IncrementalMac.Initialize(k, out var state);

                Assert.Throws <ArgumentException>("mac", () => IncrementalMac.Finalize(ref state, new byte[a.MacSize + 1]));
            }
        }
示例#13
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);
        }
示例#14
0
        public static void Mac()
        {
            #region Incremental MAC

            // select the BLAKE2b-256 algorithm
            var algorithm = MacAlgorithm.Blake2b_256;

            // create a new key
            using (var key = Key.Create(algorithm))
            {
                // initialize the state with the key
                IncrementalMac.Initialize(key, out var state);

                // incrementally update the state with some data
                var lines = new[]
                {
                    "It is a dark time for the\n",
                    "Rebellion. Although the Death\n",
                    "Star has been destroyed,\n",
                    "Imperial troops have driven the\n",
                    "Rebel forces from their hidden\n",
                    "base and pursued them across\n",
                    "the galaxy.\n"
                };
                foreach (var line in lines)
                {
                    IncrementalMac.Update(ref state, Encoding.UTF8.GetBytes(line));
                }

                // finalize the computation and get the result
                var mac = IncrementalMac.Finalize(ref state);

                Assert.Equal(algorithm.Mac(key, Encoding.UTF8.GetBytes(string.Concat(lines))), mac);
            }

            #endregion
        }
示例#15
0
 public static void InitializeWithNullKey()
 {
     Assert.Throws <ArgumentNullException>("key", () => IncrementalMac.Initialize(null, out _));
 }