2 RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
4 This file implements following APIs which provide more capabilities for RSA:
10 Copyright (c) 2009 - 2017, 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/err.h>
26 #include <openssl/objects.h>
29 Gets the tag-designated RSA key component from the established RSA context.
31 This function retrieves the tag-designated RSA key component from the
32 established RSA context as a non-negative integer (octet string format
33 represented in RSA PKCS#1).
34 If specified key component has not been set or has been cleared, then returned
36 If the BigNumber buffer is too small to hold the contents of the key, FALSE
37 is returned and BnSize is set to the required buffer size to obtain the key.
39 If RsaContext is NULL, then return FALSE.
40 If BnSize is NULL, then return FALSE.
41 If BnSize is large enough but BigNumber is NULL, then return FALSE.
43 @param[in, out] RsaContext Pointer to RSA context being set.
44 @param[in] KeyTag Tag of RSA key component being set.
45 @param[out] BigNumber Pointer to octet integer buffer.
46 @param[in, out] BnSize On input, the size of big number buffer in bytes.
47 On output, the size of data returned in big number buffer in bytes.
49 @retval TRUE RSA key component was retrieved successfully.
50 @retval FALSE Invalid RSA key component tag.
51 @retval FALSE BnSize is too small.
57 IN OUT VOID
*RsaContext
,
58 IN RSA_KEY_TAG KeyTag
,
68 // Check input parameters.
70 if (RsaContext
== NULL
|| BnSize
== NULL
) {
74 RsaKey
= (RSA
*) RsaContext
;
82 // RSA Public Modulus (N)
85 RSA_get0_key (RsaKey
, (const BIGNUM
**)&BnKey
, NULL
, NULL
);
89 // RSA Public Exponent (e)
92 RSA_get0_key (RsaKey
, NULL
, (const BIGNUM
**)&BnKey
, NULL
);
96 // RSA Private Exponent (d)
99 RSA_get0_key (RsaKey
, NULL
, NULL
, (const BIGNUM
**)&BnKey
);
103 // RSA Secret Prime Factor of Modulus (p)
106 RSA_get0_factors (RsaKey
, (const BIGNUM
**)&BnKey
, NULL
);
110 // RSA Secret Prime Factor of Modules (q)
113 RSA_get0_factors (RsaKey
, NULL
, (const BIGNUM
**)&BnKey
);
117 // p's CRT Exponent (== d mod (p - 1))
120 RSA_get0_crt_params (RsaKey
, (const BIGNUM
**)&BnKey
, NULL
, NULL
);
124 // q's CRT Exponent (== d mod (q - 1))
127 RSA_get0_crt_params (RsaKey
, NULL
, (const BIGNUM
**)&BnKey
, NULL
);
131 // The CRT Coefficient (== 1/q mod p)
134 RSA_get0_crt_params (RsaKey
, NULL
, NULL
, (const BIGNUM
**)&BnKey
);
146 Size
= BN_num_bytes (BnKey
);
148 if (*BnSize
< Size
) {
153 if (BigNumber
== NULL
) {
157 *BnSize
= BN_bn2bin (BnKey
, BigNumber
) ;
163 Generates RSA key components.
165 This function generates RSA key components. It takes RSA public exponent E and
166 length in bits of RSA modulus N as input, and generates all key components.
167 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
169 Before this function can be invoked, pseudorandom number generator must be correctly
170 initialized by RandomSeed().
172 If RsaContext is NULL, then return FALSE.
174 @param[in, out] RsaContext Pointer to RSA context being set.
175 @param[in] ModulusLength Length of RSA modulus N in bits.
176 @param[in] PublicExponent Pointer to RSA public exponent.
177 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
179 @retval TRUE RSA key component was generated successfully.
180 @retval FALSE Invalid RSA key component tag.
186 IN OUT VOID
*RsaContext
,
187 IN UINTN ModulusLength
,
188 IN CONST UINT8
*PublicExponent
,
189 IN UINTN PublicExponentSize
196 // Check input parameters.
198 if (RsaContext
== NULL
|| ModulusLength
> INT_MAX
|| PublicExponentSize
> INT_MAX
) {
209 if (PublicExponent
== NULL
) {
210 if (BN_set_word (KeyE
, 0x10001) == 0) {
214 if (BN_bin2bn (PublicExponent
, (UINT32
) PublicExponentSize
, KeyE
) == NULL
) {
219 if (RSA_generate_key_ex ((RSA
*) RsaContext
, (UINT32
) ModulusLength
, KeyE
, NULL
) == 1) {
229 Validates key components of RSA context.
230 NOTE: This function performs integrity checks on all the RSA key material, so
231 the RSA key structure must contain all the private key data.
233 This function validates key components of RSA context in following aspects:
234 - Whether p is a prime
235 - Whether q is a prime
237 - Whether d*e = 1 mod lcm(p-1,q-1)
239 If RsaContext is NULL, then return FALSE.
241 @param[in] RsaContext Pointer to RSA context to check.
243 @retval TRUE RSA key components are valid.
244 @retval FALSE RSA key components are not valid.
256 // Check input parameters.
258 if (RsaContext
== NULL
) {
262 if (RSA_check_key ((RSA
*) RsaContext
) != 1) {
263 Reason
= ERR_GET_REASON (ERR_peek_last_error ());
264 if (Reason
== RSA_R_P_NOT_PRIME
||
265 Reason
== RSA_R_Q_NOT_PRIME
||
266 Reason
== RSA_R_N_DOES_NOT_EQUAL_P_Q
||
267 Reason
== RSA_R_D_E_NOT_CONGRUENT_TO_1
) {
276 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
278 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
280 If the Signature buffer is too small to hold the contents of signature, FALSE
281 is returned and SigSize is set to the required buffer size to obtain the signature.
283 If RsaContext is NULL, then return FALSE.
284 If MessageHash is NULL, then return FALSE.
285 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
286 If SigSize is large enough but Signature is NULL, then return FALSE.
288 @param[in] RsaContext Pointer to RSA context for signature generation.
289 @param[in] MessageHash Pointer to octet message hash to be signed.
290 @param[in] HashSize Size of the message hash in bytes.
291 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
292 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
293 On output, the size of data returned in Signature buffer in bytes.
295 @retval TRUE Signature successfully generated in PKCS1-v1_5.
296 @retval FALSE Signature generation failed.
297 @retval FALSE SigSize is too small.
304 IN CONST UINT8
*MessageHash
,
306 OUT UINT8
*Signature
,
307 IN OUT UINTN
*SigSize
315 // Check input parameters.
317 if (RsaContext
== NULL
|| MessageHash
== NULL
) {
321 Rsa
= (RSA
*) RsaContext
;
322 Size
= RSA_size (Rsa
);
324 if (*SigSize
< Size
) {
329 if (Signature
== NULL
) {
334 // Determine the message digest algorithm according to digest size.
335 // Only MD5, SHA-1 or SHA-256 algorithm is supported.
338 case MD5_DIGEST_SIZE
:
339 DigestType
= NID_md5
;
342 case SHA1_DIGEST_SIZE
:
343 DigestType
= NID_sha1
;
346 case SHA256_DIGEST_SIZE
:
347 DigestType
= NID_sha256
;
354 return (BOOLEAN
) RSA_sign (