private static bool VerifySignature <T>(SafeNCryptKeyHandle key, byte[] hash, byte[] signature, ref T paddingInfo, AsymmetricPaddingMode paddingMode, NCryptSignatureVerifier <T> verifier) where T : struct { Debug.Assert(key != null, "key != null"); Debug.Assert(!key.IsClosed && !key.IsInvalid, "!key.IsClosed && !key.IsInvalid"); Debug.Assert(hash != null, "hash != null"); Debug.Assert(signature != null, "signature != null"); Debug.Assert(verifier != null, "verifier != null"); ErrorCode error = verifier(key, ref paddingInfo, hash, hash.Length, signature, signature.Length, paddingMode); if (error != ErrorCode.Success && error != ErrorCode.BadSignature) { throw new CryptographicException((int)error); } return(error == ErrorCode.Success); }
internal static extern int NCryptVerifySignature(SafeNCryptKeyHandle hKey, [In] ref NCRYPT_PKCS1_PADDING_INFO pPaddingInfo, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbHashValue, int cbHashValue, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
internal static extern ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, [In] ref BCryptNative.BCRYPT_PSS_PADDING_INFO pPaddingInfo, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbHashValue, int cbHashValue, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature, int cbSignature, [Out] out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static extern ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput, int cbInput, [In] ref BCryptNative.BCRYPT_PKCS1_PADDING_INFO pvPadding, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbOutput, int cbOutput, [Out] out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static extern int NCryptSignHash( SafeNCryptKeyHandle hKey, [In] ref NCRYPT_PKCS1_PADDING_INFO pPaddingInfo, [In, MarshalAs( UnmanagedType.LPArray )] byte[] pbHashValue, int cbHashValue, [In, MarshalAs( UnmanagedType.LPArray )] byte[] pbSignature, int cbSignature, [Out] out int pcbResult, AsymmetricPaddingMode dwFlags );
private static byte[] SignHash <T>(SafeNCryptKeyHandle key, byte[] hash, ref T paddingInfo, AsymmetricPaddingMode paddingMode, NCryptHashSigner <T> signer) where T : struct { Debug.Assert(key != null, "key != null"); Debug.Assert(!key.IsInvalid && !key.IsClosed, "!key.IsInvalid && !key.IsClosed"); Debug.Assert(hash != null, "hash != null"); Debug.Assert(signer != null, "signer != null"); // Figure out how big the signature is int signatureSize = 0; ErrorCode error = signer(key, ref paddingInfo, hash, hash.Length, null, 0, out signatureSize, paddingMode); if (error != ErrorCode.Success && error != ErrorCode.BufferTooSmall) { throw new CryptographicException((int)error); } // Sign the hash byte[] signature = new byte[signatureSize]; error = signer(key, ref paddingInfo, hash, hash.Length, signature, signature.Length, out signatureSize, paddingMode); if (error != ErrorCode.Success) { throw new CryptographicException((int)error); } return(signature); }
private static byte[] EncryptData <T>(SafeNCryptKeyHandle key, byte[] data, ref T paddingInfo, AsymmetricPaddingMode paddingMode, NCryptEncryptor <T> encryptor) where T : struct { Debug.Assert(key != null, "key != null"); Debug.Assert(!key.IsClosed && !key.IsInvalid, "!key.IsClosed && !key.IsInvalid"); Debug.Assert(data != null, "data != null"); Debug.Assert(encryptor != null, "encryptor != null"); // Figure out how big of a buffer is to encrypt the data int encryptedSize = 0; ErrorCode error = encryptor(key, data, data.Length, ref paddingInfo, null, 0, out encryptedSize, paddingMode); if (error != ErrorCode.Success && error != ErrorCode.BufferTooSmall) { throw new CryptographicException((int)error); } // Do the encryption byte[] encrypted = new byte[encryptedSize]; error = encryptor(key, data, data.Length, ref paddingInfo, encrypted, encrypted.Length, out encryptedSize, paddingMode); if (error != ErrorCode.Success) { throw new CryptographicException((int)error); } return(encrypted); }
/// <summary> /// Generate cipher. The cipher can be reused. /// </summary> /// <param name="padding">Asymmetric algorithm padding mode.</param> /// <param name="asymmetricKey">Asymmetric public key or private key.</param> /// <returns></returns> /// <exception cref="Exception"/> public IAsymmetricBlockCipher GenerateCipher(AsymmetricPaddingMode padding, AsymmetricKeyParameter asymmetricKey) { IAsymmetricBlockCipher cipher = new RsaBlindedEngine(); switch (padding) { case AsymmetricPaddingMode.NoPadding: break; case AsymmetricPaddingMode.PKCS1: cipher = new Pkcs1Encoding(cipher); break; case AsymmetricPaddingMode.OAEP: cipher = new OaepEncoding(cipher); break; case AsymmetricPaddingMode.ISO9796_1: cipher = new ISO9796d1Encoding(cipher); break; default: throw new System.Security.Cryptography.CryptographicException("Unsupported padding mode."); } cipher.Init(!asymmetricKey.IsPrivate, asymmetricKey); return(cipher); }
private static void Test1() { Array paddings = Enum.GetValues(typeof(AsymmetricPaddingMode)); // List <IAsymmetricEncryptionAlgorithm> algorithms = new List <IAsymmetricEncryptionAlgorithm>(); AsymmetricAlgorithmHelper.TryGetAlgorithm("ElGamal", out IAsymmetricEncryptionAlgorithm encryption); algorithms.Add(encryption); AsymmetricAlgorithmHelper.TryGetAlgorithm("RSA", out encryption); algorithms.Add(encryption); // byte[] test = Utilities.ScoopBytes(4); foreach (IAsymmetricEncryptionAlgorithm algorithm in algorithms) { foreach (int paddingValue in paddings) { _total++; AsymmetricPaddingMode padding = (AsymmetricPaddingMode)paddingValue; string mechanism = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", algorithm.Mechanism, padding.ToString()); try { AsymmetricCipherKeyPair keyPair = algorithm.GenerateKeyPair(); IAsymmetricBlockCipher encryptor = algorithm.GenerateCipher(padding, keyPair.Public); IAsymmetricBlockCipher decryptor = algorithm.GenerateCipher(padding, keyPair.Private); XTest(mechanism, encryptor, decryptor, test); _execute++; } catch (Exception) { Console.WriteLine("{0}-------------------------------- Ignored.", mechanism.PadRight(32)); } } } { AsymmetricCipherKeyPair keyPair = ((RSA)AsymmetricAlgorithmHelper.RSA).GenerateKeyPair(true); IAsymmetricBlockCipher encryptor = AsymmetricAlgorithmHelper.RSA.GenerateCipher(AsymmetricPaddingMode.NoPadding, keyPair.Public); IAsymmetricBlockCipher decryptor = AsymmetricAlgorithmHelper.RSA.GenerateCipher(AsymmetricPaddingMode.NoPadding, keyPair.Private); XTest(".NET RSA KEY 2048", encryptor, decryptor, test); } }
/// <summary> /// Generate cipher. The cipher can be reused. /// </summary> /// <param name="padding">Asymmetric algorithm padding mode.</param> /// <param name="asymmetricKey">Asymmetric public key or private key.</param> /// <returns></returns> /// <exception cref="Exception"/> public IAsymmetricBlockCipher GenerateCipher(AsymmetricPaddingMode padding, AsymmetricKeyParameter asymmetricKey) { if (padding == AsymmetricPaddingMode.ISO9796_1) { throw new CryptographicException("ISO9796_1 padding mode does not support ElGamal."); } IAsymmetricBlockCipher cipher = new ElGamalEngine(); switch (padding) { case AsymmetricPaddingMode.NoPadding: break; case AsymmetricPaddingMode.PKCS1: cipher = new Pkcs1Encoding(cipher); break; case AsymmetricPaddingMode.OAEP: cipher = new OaepEncoding(cipher); break; case AsymmetricPaddingMode.ISO9796_1: break; default: throw new CryptographicException("Unsupported padding mode."); } cipher.Init(!asymmetricKey.IsPrivate, asymmetricKey); return(cipher); }
internal static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan <byte> pbHashValue, int cbHashValue, ReadOnlySpan <byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags) { fixed(byte *pbHashValuePtr = &pbHashValue.DangerousGetPinnableReference()) fixed(byte *pbSignaturePtr = &pbSignature.DangerousGetPinnableReference()) { return(NCryptVerifySignature(hKey, pPaddingInfo, pbHashValuePtr, cbHashValue, pbSignaturePtr, cbSignature, dwFlags)); } }
internal static unsafe partial ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan <byte> pbInput, int cbInput, void *pPaddingInfo, Span <byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan <byte> pbInput, int cbInput, void *pPaddingInfo, Span <byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags) => NCryptDecrypt(hKey, ref pbInput.DangerousGetPinnableReference(), cbInput, pPaddingInfo, ref pbOutput.DangerousGetPinnableReference(), cbOutput, out pcbResult, dwFlags);
internal static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan <byte> pbInput, int cbInput, void *pPaddingInfo, Span <byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags) => NCryptDecrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags);
internal static extern unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void *pPaddingInfo, [In] byte[] pbHashValue, int cbHashValue, [Out] byte[] pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags);
private static extern unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static unsafe extern ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, byte *pbInput, int cbInput, void *pPaddingInfo, byte *pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static extern unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, [In] byte[] pbInput, int cbInput, void *pPaddingInfo, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan <byte> pbHashValue, int cbHashValue, ReadOnlySpan <byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags) => NCryptVerifySignature(hKey, pPaddingInfo, ref pbHashValue.DangerousGetPinnableReference(), cbHashValue, ref pbSignature.DangerousGetPinnableReference(), cbSignature, dwFlags);
internal static unsafe partial ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan <byte> pbHashValue, int cbHashValue, Span <byte> pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static unsafe partial ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan <byte> pbHashValue, int cbHashValue, ReadOnlySpan <byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
internal static unsafe extern ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, [In] byte[] pbInput, int cbInput, void* pPaddingInfo, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan <byte> pbInput, int cbInput, void *pPaddingInfo, Span <byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags) { fixed(byte *pbInputPtr = &pbInput.DangerousGetPinnableReference()) fixed(byte *pbOutputPtr = &pbOutput.DangerousGetPinnableReference()) { return(NCryptDecrypt(hKey, pbInputPtr, cbInput, pPaddingInfo, pbOutputPtr, cbOutput, out pcbResult, dwFlags)); } }
internal static unsafe extern ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, [In] byte[] pbHashValue, int cbHashValue, [Out] byte[] pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan <byte> pbHashValue, int cbHashValue, ReadOnlySpan <byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags) => NCryptVerifySignature(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, dwFlags);
internal static unsafe extern ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, [In] byte[] pbHashValue, int cbHashValue, [In] byte[] pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
internal static extern unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, [In] byte[] pbHashValue, int cbHashValue, [In] byte[] pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
private static unsafe partial ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
private static extern unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void *pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
internal static extern unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, byte* pbInput, int cbInput, void* pPaddingInfo, byte* pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);