예제 #1
0
        protected override bool VerifySignatureCore(
            ReadOnlySpan <byte> hash,
            ReadOnlySpan <byte> signature,
            DSASignatureFormat signatureFormat)
        {
            Span <byte>         stackBuf = stackalloc byte[WindowsMaxQSize];
            ReadOnlySpan <byte> source   = AdjustHashSizeIfNecessary(hash, stackBuf);

            if (signatureFormat == DSASignatureFormat.Rfc3279DerSequence)
            {
                // source.Length is the field size, in bytes, so just convert to bits.
                int fieldSizeBits = source.Length * 8;
                signature = this.ConvertSignatureToIeeeP1363(signatureFormat, signature, fieldSizeBits);
            }
            else if (signatureFormat != DSASignatureFormat.IeeeP1363FixedFieldConcatenation)
            {
                Debug.Fail($"Missing internal implementation handler for signature format {signatureFormat}");
                throw new CryptographicException(
                          SR.Cryptography_UnknownSignatureFormat,
                          signatureFormat.ToString());
            }

            using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
            {
                unsafe
                {
                    return(CngCommon.VerifyHash(keyHandle, source, signature, AsymmetricPaddingMode.None, null));
                }
            }
        }
예제 #2
0
        public override byte[] CreateSignature(byte[] rgbHash)
        {
            if (rgbHash == null)
            {
                throw new ArgumentNullException(nameof(rgbHash));
            }

            ReadOnlySpan <byte> source = rgbHash;

            byte[] arrayToReturnToArrayPool = AdjustHashSizeIfNecessaryWithArrayPool(ref source);
            try
            {
                using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
                {
                    unsafe
                    {
                        return(CngCommon.SignHash(keyHandle, source, AsymmetricPaddingMode.None, null, source.Length * 2));
                    }
                }
            }
            finally
            {
                if (arrayToReturnToArrayPool != null)
                {
                    Array.Clear(arrayToReturnToArrayPool, 0, source.Length);
                    ArrayPool <byte> .Shared.Return(arrayToReturnToArrayPool);
                }
            }
        }
예제 #3
0
        public override unsafe bool TryCreateSignature(ReadOnlySpan <byte> hash, Span <byte> destination, out int bytesWritten)
        {
            Span <byte>         stackBuf = stackalloc byte[WindowsMaxQSize];
            ReadOnlySpan <byte> source   = AdjustHashSizeIfNecessary(hash, stackBuf);

            using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
            {
                return(CngCommon.TrySignHash(keyHandle, source, destination, AsymmetricPaddingMode.None, null, out bytesWritten));
            }
        }
예제 #4
0
        public override bool VerifySignature(ReadOnlySpan <byte> hash, ReadOnlySpan <byte> signature)
        {
            Span <byte>         stackBuf = stackalloc byte[WindowsMaxQSize];
            ReadOnlySpan <byte> source   = AdjustHashSizeIfNecessary(hash, stackBuf);
#endif
            using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
            {
                unsafe
                {
                    return(CngCommon.VerifyHash(keyHandle, source, signature, AsymmetricPaddingMode.None, null));
                }
            }
        }
예제 #5
0
        public override byte[] CreateSignature(byte[] rgbHash)
        {
            ArgumentNullException.ThrowIfNull(rgbHash);

            Span <byte>         stackBuf = stackalloc byte[WindowsMaxQSize];
            ReadOnlySpan <byte> source   = AdjustHashSizeIfNecessary(rgbHash, stackBuf);

            using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
            {
                unsafe
                {
                    return(CngCommon.SignHash(keyHandle, source, AsymmetricPaddingMode.None, null, source.Length * 2));
                }
            }
        }
예제 #6
0
        public override byte[] CreateSignature(byte[] hash)
        {
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            hash = AdjustHashSizeIfNecessary(hash);
            using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
            {
                unsafe
                {
                    byte[] signature = CngCommon.SignHash(keyHandle, hash, AsymmetricPaddingMode.None, null, hash.Length * 2);
                    return(signature);
                }
            }
        }
예제 #7
0
 public override unsafe bool TryCreateSignature(ReadOnlySpan <byte> hash, Span <byte> destination, out int bytesWritten)
 {
     byte[] arrayToReturnToArrayPool = AdjustHashSizeIfNecessaryWithArrayPool(ref hash);
     try
     {
         using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
         {
             return(CngCommon.TrySignHash(keyHandle, hash, destination, AsymmetricPaddingMode.None, null, out bytesWritten));
         }
     }
     finally
     {
         if (arrayToReturnToArrayPool != null)
         {
             Array.Clear(arrayToReturnToArrayPool, 0, hash.Length);
             ArrayPool <byte> .Shared.Return(arrayToReturnToArrayPool);
         }
     }
 }
예제 #8
0
 public override bool VerifySignature(ReadOnlySpan <byte> hash, ReadOnlySpan <byte> signature)
 {
     byte[] arrayToReturnToArrayPool = AdjustHashSizeIfNecessaryWithArrayPool(ref hash);
     try
     {
         using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
         {
             unsafe
             {
                 return(CngCommon.VerifyHash(keyHandle, hash, signature, AsymmetricPaddingMode.None, null));
             }
         }
     }
     finally
     {
         if (arrayToReturnToArrayPool != null)
         {
             Array.Clear(arrayToReturnToArrayPool, 0, hash.Length);
             ArrayPool <byte> .Shared.Return(arrayToReturnToArrayPool);
         }
     }
 }
예제 #9
0
        public override bool VerifySignature(byte[] hash, byte[] signature)
        {
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }

            hash = AdjustHashSizeIfNecessary(hash);

            using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
            {
                unsafe
                {
                    bool verified = CngCommon.VerifyHash(keyHandle, hash, signature, AsymmetricPaddingMode.None, null);
                    return(verified);
                }
            }
        }
예제 #10
0
        public override unsafe bool TryCreateSignature(ReadOnlySpan <byte> hash, Span <byte> destination, out int bytesWritten)
#endif
        {
            Span <byte>         stackBuf = stackalloc byte[WindowsMaxQSize];
            ReadOnlySpan <byte> source   = AdjustHashSizeIfNecessary(hash, stackBuf);

            using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
            {
                if (!CngCommon.TrySignHash(keyHandle, source, destination, AsymmetricPaddingMode.None, null, out bytesWritten))
                {
                    bytesWritten = 0;
                    return(false);
                }
            }

#if INTERNAL_ASYMMETRIC_IMPLEMENTATIONS
            if (signatureFormat == DSASignatureFormat.IeeeP1363FixedFieldConcatenation)
            {
                return(true);
            }

            if (signatureFormat != DSASignatureFormat.Rfc3279DerSequence)
            {
                Debug.Fail($"Missing internal implementation handler for signature format {signatureFormat}");
                throw new CryptographicException(
                          SR.Cryptography_UnknownSignatureFormat,
                          signatureFormat.ToString());
            }

            return(AsymmetricAlgorithmHelpers.TryConvertIeee1363ToDer(
                       destination.Slice(0, bytesWritten),
                       destination,
                       out bytesWritten));
#else
            return(true);
#endif
        }
예제 #11
0
 protected override bool TryHashData(ReadOnlySpan <byte> source, Span <byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
 CngCommon.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
예제 #12
0
 protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
 CngCommon.HashData(data, hashAlgorithm);
예제 #13
0
 protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
 CngCommon.HashData(data, offset, count, hashAlgorithm);