Beispiel #1
0
        internal override bool FinalizeAndVerifyCore(
            ref IncrementalHashState state,
            ReadOnlySpan <byte> hash)
        {
            Debug.Assert(hash.Length <= crypto_hash_sha512_BYTES);

            Span <byte> temp = stackalloc byte[crypto_hash_sha512_BYTES];

            int error = crypto_hash_sha512_final(
                ref state.sha512,
                ref temp.GetPinnableReference());

            Debug.Assert(error == 0);

            return(CryptographicOperations.FixedTimeEquals(temp.Slice(0, hash.Length), hash));
        }
Beispiel #2
0
        internal override bool FinalizeAndVerifyCore(
            ref IncrementalMacState state,
            ReadOnlySpan <byte> mac)
        {
            Debug.Assert(mac.Length == crypto_auth_hmacsha256_BYTES);

            Span <byte> temp = stackalloc byte[crypto_auth_hmacsha256_BYTES];

            int error = crypto_auth_hmacsha256_final(
                ref state.hmacsha256,
                ref temp.GetPinnableReference());

            Debug.Assert(error == 0);

            return(CryptographicOperations.FixedTimeEquals(temp, mac));
        }
Beispiel #3
0
        private protected override bool VerifyCore(
            ReadOnlySpan <byte> key,
            ReadOnlySpan <byte> data,
            ReadOnlySpan <byte> mac)
        {
            Debug.Assert(key.Length == crypto_hash_sha256_BYTES);
            Debug.Assert(mac.Length == crypto_auth_hmacsha256_BYTES);

            Span <byte> temp = stackalloc byte[crypto_auth_hmacsha256_BYTES];

            crypto_auth_hmacsha256_init(out crypto_auth_hmacsha256_state state, in key.GetPinnableReference(), (UIntPtr)key.Length);
            crypto_auth_hmacsha256_update(ref state, in data.GetPinnableReference(), (ulong)data.Length);
            crypto_auth_hmacsha256_final(ref state, ref temp.GetPinnableReference());

            return(CryptographicOperations.FixedTimeEquals(temp, mac));
        }
Beispiel #4
0
        private protected override bool VerifyCore(
            ReadOnlySpan <byte> data,
            ReadOnlySpan <byte> hash)
        {
            Debug.Assert(hash.Length <= crypto_hash_sha512_BYTES);

            Span <byte> temp = stackalloc byte[crypto_hash_sha512_BYTES];

            int error = crypto_hash_sha512(
                ref temp.GetPinnableReference(),
                in data.GetPinnableReference(),
                (ulong)data.Length);

            Debug.Assert(error == 0);

            return(CryptographicOperations.FixedTimeEquals(temp.Slice(0, hash.Length), hash));
        }
Beispiel #5
0
        private static void ExpandCore(
            ReadOnlySpan <byte> pseudorandomKey,
            ReadOnlySpan <byte> info,
            Span <byte> bytes)
        {
            Debug.Assert(pseudorandomKey.Length >= crypto_auth_hmacsha512_BYTES);
            Debug.Assert(bytes.Length <= byte.MaxValue * crypto_auth_hmacsha512_BYTES);

            Span <byte> temp = stackalloc byte[crypto_auth_hmacsha512_BYTES];

            try
            {
                int  tempLength = 0;
                int  offset     = 0;
                byte counter    = 0;
                int  chunkSize;

                while ((chunkSize = bytes.Length - offset) > 0)
                {
                    counter++;

                    crypto_auth_hmacsha512_init(out crypto_auth_hmacsha512_state state, in pseudorandomKey.GetPinnableReference(), (UIntPtr)pseudorandomKey.Length);
                    crypto_auth_hmacsha512_update(ref state, in temp.GetPinnableReference(), (ulong)tempLength);
                    crypto_auth_hmacsha512_update(ref state, in info.GetPinnableReference(), (ulong)info.Length);
                    crypto_auth_hmacsha512_update(ref state, in counter, sizeof(byte));
                    crypto_auth_hmacsha512_final(ref state, ref temp.GetPinnableReference());

                    tempLength = crypto_auth_hmacsha512_BYTES;

                    if (chunkSize > crypto_auth_hmacsha512_BYTES)
                    {
                        chunkSize = crypto_auth_hmacsha512_BYTES;
                    }

                    temp.Slice(0, chunkSize).CopyTo(bytes.Slice(offset));
                    offset += chunkSize;
                }
            }
            finally
            {
                CryptographicOperations.ZeroMemory(temp);
            }
        }
Beispiel #6
0
        private protected override void DeriveBytesCore(
            ReadOnlySpan <byte> inputKeyingMaterial,
            ReadOnlySpan <byte> salt,
            ReadOnlySpan <byte> info,
            Span <byte> bytes)
        {
            Debug.Assert(bytes.Length <= byte.MaxValue * crypto_auth_hmacsha512_BYTES);

            Span <byte> pseudorandomKey = stackalloc byte[crypto_auth_hmacsha512_BYTES];

            try
            {
                ExtractCore(inputKeyingMaterial, salt, pseudorandomKey);

                ExpandCore(pseudorandomKey, info, bytes);
            }
            finally
            {
                CryptographicOperations.ZeroMemory(pseudorandomKey);
            }
        }
Beispiel #7
0
        internal unsafe override bool FinalizeAndVerifyCore(
            ref IncrementalMacState state,
            ReadOnlySpan <byte> mac)
        {
            Debug.Assert(mac.Length <= crypto_auth_hmacsha256_BYTES);

            byte *temp = stackalloc byte[crypto_auth_hmacsha256_BYTES];

            fixed(crypto_auth_hmacsha256_state *state_ = &state.hmacsha256)
            {
                int error = crypto_auth_hmacsha256_final(
                    state_,
                    temp);

                Debug.Assert(error == 0);
            }

            fixed(byte * @out = mac)
            {
                return(CryptographicOperations.FixedTimeEquals(temp, @out, mac.Length));
            }
        }
Beispiel #8
0
        internal unsafe override bool FinalizeAndVerifyCore(
            ref IncrementalHashState state,
            ReadOnlySpan <byte> hash)
        {
            Debug.Assert(hash.Length <= crypto_hash_sha512_BYTES);

            byte *temp = stackalloc byte[crypto_hash_sha512_BYTES];

            fixed(crypto_hash_sha512_state *state_ = &state.sha512)
            {
                int error = crypto_hash_sha512_final(
                    state_,
                    temp);

                Debug.Assert(error == 0);
            }

            fixed(byte * @out = hash)
            {
                return(CryptographicOperations.FixedTimeEquals(temp, @out, hash.Length));
            }
        }
Beispiel #9
0
        private protected unsafe override bool VerifyCore(
            ReadOnlySpan <byte> data,
            ReadOnlySpan <byte> hash)
        {
            Debug.Assert(hash.Length == crypto_hash_sha256_BYTES);

            byte *temp = stackalloc byte[crypto_hash_sha256_BYTES];

            fixed(byte * @in = data)
            {
                int error = crypto_hash_sha256(
                    temp,
                    @in,
                    (ulong)data.Length);

                Debug.Assert(error == 0);
            }

            fixed(byte * @out = hash)
            {
                return(CryptographicOperations.FixedTimeEquals(temp, @out, hash.Length));
            }
        }
Beispiel #10
0
        internal unsafe override bool FinalizeAndVerifyCore(
            ref IncrementalHashState state,
            ReadOnlySpan <byte> hash)
        {
            Debug.Assert(hash.Length >= crypto_generichash_blake2b_BYTES_MIN);
            Debug.Assert(hash.Length <= crypto_generichash_blake2b_BYTES_MAX);

            byte *temp = stackalloc byte[hash.Length];

            fixed(crypto_generichash_blake2b_state *state_ = &state.blake2b)
            {
                int error = crypto_generichash_blake2b_final(
                    state_,
                    temp,
                    (UIntPtr)hash.Length);

                Debug.Assert(error == 0);
            }

            fixed(byte * @out = hash)
            {
                return(CryptographicOperations.FixedTimeEquals(temp, @out, hash.Length));
            }
        }