]>
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 - 2018, 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/bn.h>
24 #include <openssl/rsa.h>
25 #include <openssl/objects.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 component 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 component 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
,
106 // Check input parameters.
108 if (RsaContext
== NULL
|| BnSize
> INT_MAX
) {
122 // Retrieve the components from RSA object.
124 RsaKey
= (RSA
*) RsaContext
;
125 RSA_get0_key (RsaKey
, (const BIGNUM
**)&BnN
, (const BIGNUM
**)&BnE
, (const BIGNUM
**)&BnD
);
126 RSA_get0_factors (RsaKey
, (const BIGNUM
**)&BnP
, (const BIGNUM
**)&BnQ
);
127 RSA_get0_crt_params (RsaKey
, (const BIGNUM
**)&BnDp
, (const BIGNUM
**)&BnDq
, (const BIGNUM
**)&BnQInv
);
130 // Set RSA Key Components by converting octet string to OpenSSL BN representation.
131 // NOTE: For RSA public key (used in signature verification), only public components
132 // (N, e) are needed.
137 // RSA Public Modulus (N), Public Exponent (e) and Private Exponent (d)
152 if ((BnN
== NULL
) || (BnE
== NULL
) || (BnD
== NULL
)) {
158 BnN
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnN
);
161 BnE
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnE
);
164 BnD
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnD
);
169 if (RSA_set0_key (RsaKey
, BN_dup(BnN
), BN_dup(BnE
), BN_dup(BnD
)) == 0) {
176 // RSA Secret Prime Factor of Modulus (p and q)
186 if ((BnP
== NULL
) || (BnQ
== NULL
)) {
192 BnP
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnP
);
195 BnQ
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnQ
);
200 if (RSA_set0_factors (RsaKey
, BN_dup(BnP
), BN_dup(BnQ
)) == 0) {
207 // p's CRT Exponent (== d mod (p - 1)), q's CRT Exponent (== d mod (q - 1)),
208 // and CRT Coefficient (== 1/q mod p)
219 if (BnQInv
== NULL
) {
222 if ((BnDp
== NULL
) || (BnDq
== NULL
) || (BnQInv
== NULL
)) {
228 BnDp
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnDp
);
231 BnDq
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnDq
);
234 BnQInv
= BN_bin2bn (BigNumber
, (UINT32
)BnSize
, BnQInv
);
239 if (RSA_set0_crt_params (RsaKey
, BN_dup(BnDp
), BN_dup(BnDq
), BN_dup(BnQInv
)) == 0) {
253 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
256 If RsaContext is NULL, then return FALSE.
257 If MessageHash is NULL, then return FALSE.
258 If Signature is NULL, then return FALSE.
259 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
261 @param[in] RsaContext Pointer to RSA context for signature verification.
262 @param[in] MessageHash Pointer to octet message hash to be checked.
263 @param[in] HashSize Size of the message hash in bytes.
264 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
265 @param[in] SigSize Size of signature in bytes.
267 @retval TRUE Valid signature encoded in PKCS1-v1_5.
268 @retval FALSE Invalid signature or invalid RSA context.
275 IN CONST UINT8
*MessageHash
,
277 IN CONST UINT8
*Signature
,
285 // Check input parameters.
287 if (RsaContext
== NULL
|| MessageHash
== NULL
|| Signature
== NULL
) {
291 if (SigSize
> INT_MAX
|| SigSize
== 0) {
296 // Determine the message digest algorithm according to digest size.
297 // Only MD5, SHA-1 or SHA-256 algorithm is supported.
300 case MD5_DIGEST_SIZE
:
301 DigestType
= NID_md5
;
304 case SHA1_DIGEST_SIZE
:
305 DigestType
= NID_sha1
;
308 case SHA256_DIGEST_SIZE
:
309 DigestType
= NID_sha256
;
316 SigBuf
= (UINT8
*) Signature
;
317 return (BOOLEAN
) RSA_verify (