Пример #1
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);
            }
        }
Пример #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 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]));
        }
Пример #5
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]));
            }
        }
Пример #6
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);
        }
Пример #7
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
        }
Пример #8
0
        public static void FinalizeInvalid()
        {
            var state = default(IncrementalMac);

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

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