Пример #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 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);
            }
        }
Пример #3
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);
            }
        }
Пример #4
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);
            }
        }
Пример #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 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]));
            }
        }
Пример #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 InitializeWithDisposedKey(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            var k = new Key(a);

            k.Dispose();
            Assert.Throws <ObjectDisposedException>(() => IncrementalMac.Initialize(k, out _));
        }
Пример #10
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]));
            }
        }
Пример #11
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)));
            }
        }
Пример #12
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]));
            }
        }
Пример #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 FinalizeInvalid()
        {
            var state = default(IncrementalMac);

            Assert.Throws <InvalidOperationException>(() => IncrementalMac.Finalize(ref state));
        }
Пример #16
0
        public static void UpdateInvalid()
        {
            var state = default(IncrementalMac);

            Assert.Throws <InvalidOperationException>(() => IncrementalMac.Update(ref state, Utilities.RandomBytes.Slice(0, 100)));
        }
Пример #17
0
        public static void FinalizeAndVerifyInvalid()
        {
            var state = default(IncrementalMac);

            Assert.Throws <InvalidOperationException>(() => IncrementalMac.FinalizeAndVerify(ref state, ReadOnlySpan <byte> .Empty));
        }
Пример #18
0
 public static void InitializeWithNullKey()
 {
     Assert.Throws <ArgumentNullException>("key", () => IncrementalMac.Initialize(null, out _));
 }
Пример #19
0
        public static void FinalizeWithSpanInvalid()
        {
            var state = default(IncrementalMac);

            Assert.Throws <InvalidOperationException>(() => IncrementalMac.Finalize(ref state, Span <byte> .Empty));
        }