X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FPk%2FCryptRsaExt.c;h=b941d6fac6028270981b1b252754cfd0ae80a907;hp=b4faafa0c350e7db61442efc85d6e58c9d7d616b;hb=1cae0c83bbd88822076542e2715077ca2a8bcadb;hpb=dda39f3a5850458391aaab330971d46bc9c2b690 diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c index b4faafa0c3..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. @@ -262,7 +244,9 @@ _Exit: } /** - 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 @@ -306,75 +290,6 @@ RsaCheckKey ( return TRUE; } -/** - Performs the PKCS1-v1_5 encoding methods defined in RSA PKCS #1. - - @param[in] Message Message buffer to be encoded. - @param[in] MessageSize Size of message buffer in bytes. - @param[out] DigestInfo Pointer to buffer of digest info for output. - @param[in,out] DigestInfoSize On input, the size of DigestInfo buffer in bytes. - On output, the size of data returned in DigestInfo - buffer in bytes. - - @retval TRUE PKCS1-v1_5 encoding finished successfully. - @retval FALSE Any input parameter is invalid. - @retval FALSE DigestInfo buffer is not large enough. - -**/ -BOOLEAN -DigestInfoEncoding ( - IN CONST UINT8 *Message, - IN UINTN MessageSize, - OUT UINT8 *DigestInfo, - IN OUT UINTN *DigestInfoSize - ) -{ - CONST UINT8 *HashDer; - UINTN DerSize; - - // - // Check input parameters. - // - if (Message == NULL || DigestInfo == NULL || DigestInfoSize == 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; - } - - if (*DigestInfoSize < DerSize + MessageSize) { - *DigestInfoSize = DerSize + MessageSize; - return FALSE; - } - - CopyMem (DigestInfo, HashDer, DerSize); - CopyMem (DigestInfo + DerSize, Message, MessageSize); - - *DigestInfoSize = DerSize + MessageSize; - return TRUE; -} - /** Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme. @@ -412,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; } @@ -429,28 +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; - if (!DigestInfoEncoding (MessageHash, HashSize, Signature, SigSize)) { - return FALSE; - } - - ReturnVal = RSA_private_encrypt ( - (UINT32) *SigSize, - Signature, - Signature, - Rsa, - RSA_PKCS1_PADDING - ); - - if (ReturnVal < (INTN) *SigSize) { + default: return FALSE; - } - - *SigSize = (UINTN) ReturnVal; - return TRUE; + } + + return (BOOLEAN) RSA_sign ( + DigestType, + MessageHash, + (UINT32) HashSize, + Signature, + (UINT32 *) SigSize, + (RSA *) RsaContext + ); } -