summaryrefslogtreecommitdiff
path: root/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
diff options
context:
space:
mode:
Diffstat (limited to 'CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c')
-rw-r--r--CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c87
1 files changed, 28 insertions, 59 deletions
diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
index cbe3c50fe..3e4309899 100644
--- a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
+++ b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
@@ -7,7 +7,7 @@
3) RsaSetKey
4) RsaPkcs1Verify
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@@ -21,8 +21,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "InternalCryptLib.h"
#include <openssl/rsa.h>
-#include <openssl/err.h>
-
+#include <openssl/objects.h>
/**
Allocates and initializes one RSA context for subsequent use.
@@ -289,8 +288,8 @@ RsaPkcs1Verify (
IN UINTN SigSize
)
{
- INTN Length;
- UINT8 *DecryptedSigature;
+ INT32 DigestType;
+ UINT8 *SigBuf;
//
// Check input parameters.
@@ -302,65 +301,35 @@ RsaPkcs1Verify (
if (SigSize > INT_MAX || SigSize == 0) {
return FALSE;
}
-
- //
- // Check for unsupported hash size:
- // Only MD5, SHA-1 or SHA-256 digest size is supported
- //
- if (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE) {
- return FALSE;
- }
-
- //
- // Prepare buffer to store decrypted signature.
- //
- DecryptedSigature = (UINT8 *) malloc (SigSize);
- if (DecryptedSigature == NULL) {
- return FALSE;
- }
//
- // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key
+ // Determine the message digest algorithm according to digest size.
+ // Only MD5, SHA-1 or SHA-256 algorithm is supported.
//
- Length = RSA_public_decrypt (
- (UINT32) SigSize,
- Signature,
- DecryptedSigature,
- RsaContext,
- RSA_PKCS1_PADDING
- );
+ switch (HashSize) {
+ case MD5_DIGEST_SIZE:
+ DigestType = NID_md5;
+ break;
+
+ case SHA1_DIGEST_SIZE:
+ DigestType = NID_sha1;
+ break;
+
+ case SHA256_DIGEST_SIZE:
+ DigestType = NID_sha256;
+ break;
- //
- // Invalid RSA Key or PKCS#1 Padding Checking Failed (if Length < 0)
- // NOTE: Length should be the addition of HashSize and some DER value.
- // Ignore more strict length checking here.
- //
- if (Length < (INTN) HashSize) {
- free (DecryptedSigature);
+ default:
return FALSE;
}
- //
- // Validate the MessageHash and Decoded Signature
- // NOTE: The decoded Signature should be the DER encoding of the DigestInfo value
- // DigestInfo ::= SEQUENCE {
- // digestAlgorithm AlgorithmIdentifier
- // digest OCTET STRING
- // }
- // Then Memory Comparing should skip the DER value of the underlying SEQUENCE
- // type and AlgorithmIdentifier.
- //
- if (CompareMem (MessageHash, DecryptedSigature + Length - HashSize, HashSize) == 0) {
- //
- // Valid RSA PKCS#1 Signature
- //
- free (DecryptedSigature);
- return TRUE;
- } else {
- //
- // Failed to verification
- //
- free (DecryptedSigature);
- return FALSE;
- }
+ SigBuf = (UINT8 *) Signature;
+ return (BOOLEAN) RSA_verify (
+ DigestType,
+ MessageHash,
+ (UINT32) HashSize,
+ SigBuf,
+ (UINT32) SigSize,
+ (RSA *) RsaContext
+ );
}