Exemplo n.º 1
0
        public static void Initialize(
            Key key,
            out IncrementalMac state)
        {
            if (key == null)
            {
                throw Error.ArgumentNull_Key(nameof(key));
            }
            if (!(key.Algorithm is MacAlgorithm algorithm))
            {
                throw Error.Argument_MacKey(nameof(key), key.Algorithm.GetType().FullName, typeof(MacAlgorithm).FullName);
            }

            bool success = false;

            try
            {
                state = default;
                algorithm.InitializeCore(key.Span, algorithm.MacSize, out Unsafe.AsRef(in state._state));
                Unsafe.AsRef(in state._algorithm) = algorithm;
                success = true;
            }
            finally
            {
                if (!success)
                {
                    state = default;
                }
            }
        }
Exemplo n.º 2
0
        public static void Update(
            ref IncrementalMac state,
            ReadOnlySpan <byte> data)
        {
            if (state._algorithm == null)
            {
                throw Error.InvalidOperation_UninitializedState();
            }

            state._algorithm.UpdateCore(ref Unsafe.AsRef(in state._state), data);
        }
Exemplo n.º 3
0
        public static bool FinalizeAndVerify(
            ref IncrementalMac state,
            ReadOnlySpan <byte> mac)
        {
            if (state._algorithm == null)
            {
                throw Error.InvalidOperation_UninitializedState();
            }

            try
            {
                return(mac.Length == state._algorithm.MacSize && state._algorithm.FinalizeAndVerifyCore(ref Unsafe.AsRef(in state._state), mac));
            }
            finally
            {
                Unsafe.AsRef(in state._algorithm) = null;
            }
        }
Exemplo n.º 4
0
        public static byte[] Finalize(
            ref IncrementalMac state)
        {
            if (state._algorithm == null)
            {
                throw Error.InvalidOperation_UninitializedState();
            }

            try
            {
                byte[] mac = new byte[state._algorithm.MacSize];
                state._algorithm.FinalizeCore(ref Unsafe.AsRef(in state._state), mac);
                return(mac);
            }
            finally
            {
                Unsafe.AsRef(in state._algorithm) = null;
            }
        }
Exemplo n.º 5
0
        public static void Finalize(
            ref IncrementalMac state,
            Span <byte> mac)
        {
            if (state._algorithm == null)
            {
                throw Error.InvalidOperation_UninitializedState();
            }
            if (mac.Length != state._algorithm.MacSize)
            {
                throw Error.Argument_MacLength(nameof(mac), state._algorithm.MacSize);
            }

            try
            {
                state._algorithm.FinalizeCore(ref Unsafe.AsRef(in state._state), mac);
            }
            finally
            {
                Unsafe.AsRef(in state._algorithm) = null;
            }
        }