Beispiel #1
0
 public override void Dispose(bool disposing)
 {
     if (disposing && _hmacCtx != null)
     {
         _hmacCtx.Dispose();
         _hmacCtx = null !;
     }
 }
 public sealed override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_hmacCtx != null)
         {
             _hmacCtx.Dispose();
             _hmacCtx = null;
         }
     }
 }
            public HmacHashProvider(IntPtr algorithmEvp, ReadOnlySpan <byte> key)
            {
                Debug.Assert(algorithmEvp != IntPtr.Zero);

                _hashSize = Interop.Crypto.EvpMdSize(algorithmEvp);
                if (_hashSize <= 0 || _hashSize > Interop.Crypto.EVP_MAX_MD_SIZE)
                {
                    throw new CryptographicException();
                }

                _hmacCtx = Interop.Crypto.HmacCreate(ref MemoryMarshal.GetReference(key), key.Length, algorithmEvp);
                Interop.Crypto.CheckValidOpenSslHandle(_hmacCtx);
            }
            public HmacHashProvider(IntPtr algorithmEvp, byte[] key)
            {
                Debug.Assert(algorithmEvp != IntPtr.Zero);
                Debug.Assert(key != null);

                _hashSize = Interop.Crypto.EvpMdSize(algorithmEvp);
                if (_hashSize <= 0 || _hashSize > Interop.Crypto.EVP_MAX_MD_SIZE)
                {
                    throw new CryptographicException();
                }

                _hmacCtx = Interop.Crypto.HmacCreate(ref new Span <byte>(key).DangerousGetPinnableReference(), key.Length, algorithmEvp);
                Interop.Crypto.CheckValidOpenSslHandle(_hmacCtx);
            }
Beispiel #5
0
        internal LiteHmac(IntPtr algorithm, ReadOnlySpan <byte> key)
        {
            Debug.Assert(algorithm != IntPtr.Zero);
            _hashSizeInBytes = Interop.Crypto.EvpMdSize(algorithm);

            if (_hashSizeInBytes <= 0 || _hashSizeInBytes > Interop.Crypto.EVP_MAX_MD_SIZE)
            {
                Debug.Fail($"Unexpected hash '{_hashSizeInBytes}' size from {nameof(Interop.Crypto.EvpMdSize)}.");
                throw new CryptographicException();
            }

            _ctx = Interop.Crypto.HmacCreate(ref MemoryMarshal.GetReference(key), key.Length, algorithm);
            Interop.Crypto.CheckValidOpenSslHandle(_ctx);
        }
            public unsafe HmacHashProvider(IntPtr algorithmEvp, byte[] key)
            {
                Debug.Assert(algorithmEvp != IntPtr.Zero);
                Debug.Assert(key != null);

                _hashSize = Interop.Crypto.EvpMdSize(algorithmEvp);
                if (_hashSize <= 0 || _hashSize > Interop.Crypto.EVP_MAX_MD_SIZE)
                {
                    throw new CryptographicException();
                }

                fixed(byte *keyPtr = key)
                {
                    _hmacCtx = Interop.Crypto.HmacCreate(keyPtr, key.Length, algorithmEvp);
                    Interop.libcrypto.CheckValidOpenSslHandle(_hmacCtx);
                }
            }
Beispiel #7
0
            public override bool TryGetCurrentHash(Span <byte> destination, out int bytesWritten)
            {
                if (destination.Length < _hashSize)
                {
                    bytesWritten = 0;
                    return(false);
                }

                SafeHmacCtxHandle copy = Interop.Crypto.HmacCopy(_hmacCtx);

                Interop.Crypto.CheckValidOpenSslHandle(copy);

                int length = destination.Length;

                Check(Interop.Crypto.HmacFinal(copy, ref MemoryMarshal.GetReference(destination), ref length));
                Debug.Assert(length == _hashSize);
                bytesWritten = (int)length;

                // Destroy the copy
                copy.Close();

                return(true);
            }
Beispiel #8
0
 private static partial int HmacUpdate(SafeHmacCtxHandle ctx, ref byte data, int len);
Beispiel #9
0
 internal static partial int HmacUpdate(SafeHmacCtxHandle ctx, ReadOnlySpan <byte> data, int len);
Beispiel #10
0
 internal extern static unsafe int HmacFinal(SafeHmacCtxHandle ctx, byte *data, ref int len);
Beispiel #11
0
 internal extern static unsafe int HmacUpdate(SafeHmacCtxHandle ctx, byte *data, int len);
Beispiel #12
0
 internal extern static int HmacReset(SafeHmacCtxHandle ctx);
Beispiel #13
0
 internal extern static int HmacFinal(SafeHmacCtxHandle ctx, ref byte data, ref int len);
Beispiel #14
0
 private static extern int HmacUpdate(SafeHmacCtxHandle ctx, ref byte data, int len);
Beispiel #15
0
 internal static extern int HmacReset(SafeHmacCtxHandle ctx);
Beispiel #16
0
 internal static partial int HmacReset(SafeHmacCtxHandle ctx);
Beispiel #17
0
 internal static extern SafeHmacCtxHandle HmacCopy(SafeHmacCtxHandle ctx);
Beispiel #18
0
 internal static int HmacUpdate(SafeHmacCtxHandle ctx, ReadOnlySpan <byte> data, int len) =>
 HmacUpdate(ctx, ref MemoryMarshal.GetReference(data), len);
Beispiel #19
0
 internal static partial int HmacFinal(SafeHmacCtxHandle ctx, ref byte data, ref int len);
Beispiel #20
0
 internal static extern int HmacCurrent(SafeHmacCtxHandle ctx, ref byte data, ref int len);
Beispiel #21
0
 internal static int HmacUpdate(SafeHmacCtxHandle ctx, ReadOnlySpan <byte> data, int len) =>
 HmacUpdate(ctx, ref data.DangerousGetPinnableReference(), len);