]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
2 RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
4 This file implements following APIs which provide basic capabilities for RSA:
10 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include "InternalCryptLib.h"
23 #include <openssl/rsa.h>
24 #include <openssl/err.h>
28 Allocates and initializes one RSA context for subsequent use.
30 @return Pointer to the RSA context that has been initialized.
31 If the allocations fails, RsaNew() returns NULL.
41 // Allocates & Initializes RSA Context by OpenSSL RSA_new()
43 return (VOID
*)RSA_new ();
47 Release the specified RSA context.
49 @param[in] RsaContext Pointer to the RSA context to be released.
59 // Free OpenSSL RSA Context
61 RSA_free ((RSA
*)RsaContext
);
65 Sets the tag-designated key component into the established RSA context.
67 This function sets the tag-designated RSA key component into the established
68 RSA context from the user-specified non-negative integer (octet string format
69 represented in RSA PKCS#1).
70 If BigNumber is NULL, then the specified key componenet in RSA context is cleared.
72 If RsaContext is NULL, then return FALSE.
74 @param[in, out] RsaContext Pointer to RSA context being set.
75 @param[in] KeyTag Tag of RSA key component being set.
76 @param[in] BigNumber Pointer to octet integer buffer.
77 If NULL, then the specified key componenet in RSA
79 @param[in] BnSize Size of big number buffer in bytes.
80 If BigNumber is NULL, then it is ignored.
82 @retval TRUE RSA key component was set successfully.
83 @retval FALSE Invalid RSA key component tag.
89 IN OUT VOID
*RsaContext
,
90 IN RSA_KEY_TAG KeyTag
,
91 IN CONST UINT8
*BigNumber
,
98 // Check input parameters.
100 if (RsaContext
== NULL
) {
104 RsaKey
= (RSA
*)RsaContext
;
106 // Set RSA Key Components by converting octet string to OpenSSL BN representation.
107 // NOTE: For RSA public key (used in signature verification), only public components
108 // (N, e) are needed.
113 // RSA Public Modulus (N)
116 if (RsaKey
->n
!= NULL
) {
120 if (BigNumber
== NULL
) {
123 RsaKey
->n
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->n
);
127 // RSA Public Exponent (e)
130 if (RsaKey
->e
!= NULL
) {
134 if (BigNumber
== NULL
) {
137 RsaKey
->e
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->e
);
141 // RSA Private Exponent (d)
144 if (RsaKey
->d
!= NULL
) {
148 if (BigNumber
== NULL
) {
151 RsaKey
->d
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->d
);
155 // RSA Secret Prime Factor of Modulus (p)
158 if (RsaKey
->p
!= NULL
) {
162 if (BigNumber
== NULL
) {
165 RsaKey
->p
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->p
);
169 // RSA Secret Prime Factor of Modules (q)
172 if (RsaKey
->q
!= NULL
) {
176 if (BigNumber
== NULL
) {
179 RsaKey
->q
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->q
);
183 // p's CRT Exponent (== d mod (p - 1))
186 if (RsaKey
->dmp1
!= NULL
) {
187 BN_free (RsaKey
->dmp1
);
190 if (BigNumber
== NULL
) {
193 RsaKey
->dmp1
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->dmp1
);
197 // q's CRT Exponent (== d mod (q - 1))
200 if (RsaKey
->dmq1
!= NULL
) {
201 BN_free (RsaKey
->dmq1
);
204 if (BigNumber
== NULL
) {
207 RsaKey
->dmq1
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->dmq1
);
211 // The CRT Coefficient (== 1/q mod p)
214 if (RsaKey
->iqmp
!= NULL
) {
215 BN_free (RsaKey
->iqmp
);
218 if (BigNumber
== NULL
) {
221 RsaKey
->iqmp
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->iqmp
);
232 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
235 If RsaContext is NULL, then return FALSE.
236 If MessageHash is NULL, then return FALSE.
237 If Signature is NULL, then return FALSE.
238 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
240 @param[in] RsaContext Pointer to RSA context for signature verification.
241 @param[in] MessageHash Pointer to octet message hash to be checked.
242 @param[in] HashSize Size of the message hash in bytes.
243 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
244 @param[in] SigSize Size of signature in bytes.
246 @retval TRUE Valid signature encoded in PKCS1-v1_5.
247 @retval FALSE Invalid signature or invalid RSA context.
254 IN CONST UINT8
*MessageHash
,
263 // Check input parameters.
265 if (RsaContext
== NULL
|| MessageHash
== NULL
|| Signature
== NULL
) {
271 // Check for unsupported hash size:
272 // Only MD5, SHA-1 or SHA-256 digest size is supported
274 if (HashSize
!= MD5_DIGEST_SIZE
&& HashSize
!= SHA1_DIGEST_SIZE
&& HashSize
!= SHA256_DIGEST_SIZE
) {
279 // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key
281 Length
= RSA_public_decrypt (
290 // Invalid RSA Key or PKCS#1 Padding Checking Failed (if Length < 0)
291 // NOTE: Length should be the addition of HashSize and some DER value.
292 // Ignore more strict length checking here.
294 if (Length
< (INTN
) HashSize
) {
299 // Validate the MessageHash and Decoded Signature
300 // NOTE: The decoded Signature should be the DER encoding of the DigestInfo value
301 // DigestInfo ::= SEQUENCE {
302 // digestAlgorithm AlgorithmIdentifier
303 // digest OCTET STRING
305 // Then Memory Comparing should skip the DER value of the underlying SEQUENCE
306 // type and AlgorithmIdentifier.
308 if (CompareMem (MessageHash
, Signature
+ Length
- HashSize
, HashSize
) == 0) {
310 // Valid RSA PKCS#1 Signature
315 // Failed to verification