]>
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 - 2020, Intel Corporation. All rights reserved.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include "InternalCryptLib.h"
17 #include <openssl/bn.h>
18 #include <openssl/rsa.h>
19 #include <openssl/objects.h>
22 Allocates and initializes one RSA context for subsequent use.
24 @return Pointer to the RSA context that has been initialized.
25 If the allocations fails, RsaNew() returns NULL.
35 // Allocates & Initializes RSA Context by OpenSSL RSA_new()
37 return (VOID
*)RSA_new ();
41 Release the specified RSA context.
43 @param[in] RsaContext Pointer to the RSA context to be released.
53 // Free OpenSSL RSA Context
55 RSA_free ((RSA
*)RsaContext
);
59 Sets the tag-designated key component into the established RSA context.
61 This function sets the tag-designated RSA key component into the established
62 RSA context from the user-specified non-negative integer (octet string format
63 represented in RSA PKCS#1).
64 If BigNumber is NULL, then the specified key component in RSA context is cleared.
66 If RsaContext is NULL, then return FALSE.
68 @param[in, out] RsaContext Pointer to RSA context being set.
69 @param[in] KeyTag Tag of RSA key component being set.
70 @param[in] BigNumber Pointer to octet integer buffer.
71 If NULL, then the specified key component in RSA
73 @param[in] BnSize Size of big number buffer in bytes.
74 If BigNumber is NULL, then it is ignored.
76 @retval TRUE RSA key component was set successfully.
77 @retval FALSE Invalid RSA key component tag.
83 IN OUT VOID
*RsaContext
,
84 IN RSA_KEY_TAG KeyTag
,
85 IN CONST UINT8
*BigNumber
,
100 // Check input parameters.
102 if ((RsaContext
== NULL
) || (BnSize
> INT_MAX
)) {
116 // Retrieve the components from RSA object.
118 RsaKey
= (RSA
*)RsaContext
;
119 RSA_get0_key (RsaKey
, (const BIGNUM
**)&BnN
, (const BIGNUM
**)&BnE
, (const BIGNUM
**)&BnD
);
120 RSA_get0_factors (RsaKey
, (const BIGNUM
**)&BnP
, (const BIGNUM
**)&BnQ
);
121 RSA_get0_crt_params (RsaKey
, (const BIGNUM
**)&BnDp
, (const BIGNUM
**)&BnDq
, (const BIGNUM
**)&BnQInv
);
124 // Set RSA Key Components by converting octet string to OpenSSL BN representation.
125 // NOTE: For RSA public key (used in signature verification), only public components
126 // (N, e) are needed.
130 // RSA Public Modulus (N), Public Exponent (e) and Private Exponent (d)
147 if ((BnN
== NULL
) || (BnE
== NULL
) || (BnD
== NULL
)) {
153 BnN
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnN
);
156 BnE
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnE
);
159 BnD
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnD
);
165 if (RSA_set0_key (RsaKey
, BN_dup (BnN
), BN_dup (BnE
), BN_dup (BnD
)) == 0) {
172 // RSA Secret Prime Factor of Modulus (p and q)
184 if ((BnP
== NULL
) || (BnQ
== NULL
)) {
190 BnP
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnP
);
193 BnQ
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnQ
);
199 if (RSA_set0_factors (RsaKey
, BN_dup (BnP
), BN_dup (BnQ
)) == 0) {
206 // p's CRT Exponent (== d mod (p - 1)), q's CRT Exponent (== d mod (q - 1)),
207 // and CRT Coefficient (== 1/q mod p)
220 if (BnQInv
== NULL
) {
224 if ((BnDp
== NULL
) || (BnDq
== NULL
) || (BnQInv
== NULL
)) {
230 BnDp
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnDp
);
233 BnDq
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnDq
);
236 BnQInv
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnQInv
);
242 if (RSA_set0_crt_params (RsaKey
, BN_dup (BnDp
), BN_dup (BnDq
), BN_dup (BnQInv
)) == 0) {
256 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
259 If RsaContext is NULL, then return FALSE.
260 If MessageHash is NULL, then return FALSE.
261 If Signature is NULL, then return FALSE.
262 If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-384 or SHA-512 digest, then return FALSE.
264 @param[in] RsaContext Pointer to RSA context for signature verification.
265 @param[in] MessageHash Pointer to octet message hash to be checked.
266 @param[in] HashSize Size of the message hash in bytes.
267 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
268 @param[in] SigSize Size of signature in bytes.
270 @retval TRUE Valid signature encoded in PKCS1-v1_5.
271 @retval FALSE Invalid signature or invalid RSA context.
278 IN CONST UINT8
*MessageHash
,
280 IN CONST UINT8
*Signature
,
288 // Check input parameters.
290 if ((RsaContext
== NULL
) || (MessageHash
== NULL
) || (Signature
== NULL
)) {
294 if ((SigSize
> INT_MAX
) || (SigSize
== 0)) {
299 // Determine the message digest algorithm according to digest size.
300 // Only MD5, SHA-1, SHA-256, SHA-384 or SHA-512 algorithm is supported.
303 case MD5_DIGEST_SIZE
:
304 DigestType
= NID_md5
;
307 case SHA1_DIGEST_SIZE
:
308 DigestType
= NID_sha1
;
311 case SHA256_DIGEST_SIZE
:
312 DigestType
= NID_sha256
;
315 case SHA384_DIGEST_SIZE
:
316 DigestType
= NID_sha384
;
319 case SHA512_DIGEST_SIZE
:
320 DigestType
= NID_sha512
;
327 SigBuf
= (UINT8
*)Signature
;
328 return (BOOLEAN
)RSA_verify (