]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
d24e1fdf68019d0ab2ab57474164d08d4844903d
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.
131 // RSA Public Modulus (N), Public Exponent (e) and Private Exponent (d)
146 if ((BnN
== NULL
) || (BnE
== NULL
) || (BnD
== NULL
)) {
152 BnN
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnN
);
155 BnE
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnE
);
158 BnD
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnD
);
163 if (RSA_set0_key (RsaKey
, BN_dup(BnN
), BN_dup(BnE
), BN_dup(BnD
)) == 0) {
170 // RSA Secret Prime Factor of Modulus (p and q)
180 if ((BnP
== NULL
) || (BnQ
== NULL
)) {
186 BnP
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnP
);
189 BnQ
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnQ
);
194 if (RSA_set0_factors (RsaKey
, BN_dup(BnP
), BN_dup(BnQ
)) == 0) {
201 // p's CRT Exponent (== d mod (p - 1)), q's CRT Exponent (== d mod (q - 1)),
202 // and CRT Coefficient (== 1/q mod p)
213 if (BnQInv
== NULL
) {
216 if ((BnDp
== NULL
) || (BnDq
== NULL
) || (BnQInv
== NULL
)) {
222 BnDp
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnDp
);
225 BnDq
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnDq
);
228 BnQInv
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnQInv
);
233 if (RSA_set0_crt_params (RsaKey
, BN_dup(BnDp
), BN_dup(BnDq
), BN_dup(BnQInv
)) == 0) {
247 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
250 If RsaContext is NULL, then return FALSE.
251 If MessageHash is NULL, then return FALSE.
252 If Signature is NULL, then return FALSE.
253 If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-384 or SHA-512 digest, then return FALSE.
255 @param[in] RsaContext Pointer to RSA context for signature verification.
256 @param[in] MessageHash Pointer to octet message hash to be checked.
257 @param[in] HashSize Size of the message hash in bytes.
258 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
259 @param[in] SigSize Size of signature in bytes.
261 @retval TRUE Valid signature encoded in PKCS1-v1_5.
262 @retval FALSE Invalid signature or invalid RSA context.
269 IN CONST UINT8
*MessageHash
,
271 IN CONST UINT8
*Signature
,
279 // Check input parameters.
281 if (RsaContext
== NULL
|| MessageHash
== NULL
|| Signature
== NULL
) {
285 if (SigSize
> INT_MAX
|| SigSize
== 0) {
290 // Determine the message digest algorithm according to digest size.
291 // Only MD5, SHA-1, SHA-256, SHA-384 or SHA-512 algorithm is supported.
294 case MD5_DIGEST_SIZE
:
295 DigestType
= NID_md5
;
298 case SHA1_DIGEST_SIZE
:
299 DigestType
= NID_sha1
;
302 case SHA256_DIGEST_SIZE
:
303 DigestType
= NID_sha256
;
306 case SHA384_DIGEST_SIZE
:
307 DigestType
= NID_sha384
;
310 case SHA512_DIGEST_SIZE
:
311 DigestType
= NID_sha512
;
318 SigBuf
= (UINT8
*) Signature
;
319 return (BOOLEAN
) RSA_verify (