X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FPk%2FCryptRsaExt.c;h=b941d6fac6028270981b1b252754cfd0ae80a907;hp=67d8826e6c4a706d9db930dc2aeb86f4ebbd8b5b;hb=1cae0c83bbd88822076542e2715077ca2a8bcadb;hpb=532616bbd62bea0fe4873f2e7a57de7be9ba3976 diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c index 67d8826e6c..b941d6fac6 100644 --- a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c +++ b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c @@ -7,7 +7,7 @@ 3) RsaCheckKey 4) RsaPkcs1Sign -Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
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 @@ -20,28 +20,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "InternalCryptLib.h" +#include #include #include - -// -// ASN.1 value for Hash Algorithm ID with the Distringuished Encoding Rules (DER) -// Refer to Section 9.2 of PKCS#1 v2.1 -// -CONST UINT8 Asn1IdMd5[] = { - 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, - 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 - }; - -CONST UINT8 Asn1IdSha1[] = { - 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, - 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 - }; - -CONST UINT8 Asn1IdSha256[] = { - 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, - 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, - 0x00, 0x04, 0x20 - }; +#include /** Gets the tag-designated RSA key component from the established RSA context. @@ -231,28 +213,40 @@ RsaGenerateKey ( // // Check input parameters. // - if (RsaContext == NULL) { + if (RsaContext == NULL || ModulusLength > INT_MAX || PublicExponentSize > INT_MAX) { return FALSE; } KeyE = BN_new (); + if (KeyE == NULL) { + return FALSE; + } + + RetVal = FALSE; + if (PublicExponent == NULL) { - BN_set_word (KeyE, 0x10001); + if (BN_set_word (KeyE, 0x10001) == 0) { + goto _Exit; + } } else { - BN_bin2bn (PublicExponent, (UINT32) PublicExponentSize, KeyE); + if (BN_bin2bn (PublicExponent, (UINT32) PublicExponentSize, KeyE) == NULL) { + goto _Exit; + } } - RetVal = FALSE; if (RSA_generate_key_ex ((RSA *) RsaContext, (UINT32) ModulusLength, KeyE, NULL) == 1) { RetVal = TRUE; } +_Exit: BN_free (KeyE); return RetVal; } /** - Validates key components of RSA context. + Validates key components of RSA context. + NOTE: This function performs integrity checks on all the RSA key material, so + the RSA key structure must contain all the private key data. This function validates key compoents of RSA context in following aspects: - Whether p is a prime @@ -296,63 +290,6 @@ RsaCheckKey ( return TRUE; } -/** - Performs the PKCS1-v1_5 encoding methods defined in RSA PKCS #1. - - @param Message Message buffer to be encoded. - @param MessageSize Size of message buffer in bytes. - @param DigestInfo Pointer to buffer of digest info for output. - - @return Size of DigestInfo in bytes. - -**/ -UINTN -DigestInfoEncoding ( - IN CONST UINT8 *Message, - IN UINTN MessageSize, - OUT UINT8 *DigestInfo - ) -{ - CONST UINT8 *HashDer; - UINTN DerSize; - - // - // Check input parameters. - // - if (Message == NULL || DigestInfo == NULL) { - return FALSE; - } - - // - // The original message length is used to determine the hash algorithm since - // message is digest value hashed by the specified algorithm. - // - switch (MessageSize) { - case MD5_DIGEST_SIZE: - HashDer = Asn1IdMd5; - DerSize = sizeof (Asn1IdMd5); - break; - - case SHA1_DIGEST_SIZE: - HashDer = Asn1IdSha1; - DerSize = sizeof (Asn1IdSha1); - break; - - case SHA256_DIGEST_SIZE: - HashDer = Asn1IdSha256; - DerSize = sizeof (Asn1IdSha256); - break; - - default: - return FALSE; - } - - CopyMem (DigestInfo, HashDer, DerSize); - CopyMem (DigestInfo + DerSize, Message, MessageSize); - - return (DerSize + MessageSize); -} - /** Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme. @@ -390,13 +327,12 @@ RsaPkcs1Sign ( { RSA *Rsa; UINTN Size; - INTN ReturnVal; + INT32 DigestType; // // Check input parameters. // - if (RsaContext == NULL || MessageHash == NULL || - (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE)) { + if (RsaContext == NULL || MessageHash == NULL) { return FALSE; } @@ -407,26 +343,38 @@ RsaPkcs1Sign ( *SigSize = Size; return FALSE; } - + if (Signature == NULL) { return FALSE; } + + // + // Determine the message digest algorithm according to digest size. + // Only MD5, SHA-1 or SHA-256 algorithm is supported. + // + 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; - Size = DigestInfoEncoding (MessageHash, HashSize, Signature); - - ReturnVal = RSA_private_encrypt ( - (UINT32) Size, - Signature, - Signature, - Rsa, - RSA_PKCS1_PADDING - ); - - if (ReturnVal < (INTN) Size) { + default: return FALSE; - } - - *SigSize = (UINTN)ReturnVal; - return TRUE; + } + + return (BOOLEAN) RSA_sign ( + DigestType, + MessageHash, + (UINT32) HashSize, + Signature, + (UINT32 *) SigSize, + (RSA *) RsaContext + ); } -