2 RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
4 This file implements following APIs which provide more capabilities for RSA:
10 Copyright (c) 2009 - 2013, 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>
25 #include <openssl/objects.h>
28 Gets the tag-designated RSA key component from the established RSA context.
30 This function retrieves the tag-designated RSA key component from the
31 established RSA context as a non-negative integer (octet string format
32 represented in RSA PKCS#1).
33 If specified key component has not been set or has been cleared, then returned
35 If the BigNumber buffer is too small to hold the contents of the key, FALSE
36 is returned and BnSize is set to the required buffer size to obtain the key.
38 If RsaContext is NULL, then return FALSE.
39 If BnSize is NULL, then return FALSE.
40 If BnSize is large enough but BigNumber is NULL, then return FALSE.
42 @param[in, out] RsaContext Pointer to RSA context being set.
43 @param[in] KeyTag Tag of RSA key component being set.
44 @param[out] BigNumber Pointer to octet integer buffer.
45 @param[in, out] BnSize On input, the size of big number buffer in bytes.
46 On output, the size of data returned in big number buffer in bytes.
48 @retval TRUE RSA key component was retrieved successfully.
49 @retval FALSE Invalid RSA key component tag.
50 @retval FALSE BnSize is too small.
56 IN OUT VOID
*RsaContext
,
57 IN RSA_KEY_TAG KeyTag
,
67 // Check input parameters.
69 if (RsaContext
== NULL
|| BnSize
== NULL
) {
73 RsaKey
= (RSA
*) RsaContext
;
80 // RSA Public Modulus (N)
83 if (RsaKey
->n
== NULL
) {
90 // RSA Public Exponent (e)
93 if (RsaKey
->e
== NULL
) {
100 // RSA Private Exponent (d)
103 if (RsaKey
->d
== NULL
) {
110 // RSA Secret Prime Factor of Modulus (p)
113 if (RsaKey
->p
== NULL
) {
120 // RSA Secret Prime Factor of Modules (q)
123 if (RsaKey
->q
== NULL
) {
130 // p's CRT Exponent (== d mod (p - 1))
133 if (RsaKey
->dmp1
== NULL
) {
136 BnKey
= RsaKey
->dmp1
;
140 // q's CRT Exponent (== d mod (q - 1))
143 if (RsaKey
->dmq1
== NULL
) {
146 BnKey
= RsaKey
->dmq1
;
150 // The CRT Coefficient (== 1/q mod p)
153 if (RsaKey
->iqmp
== NULL
) {
156 BnKey
= RsaKey
->iqmp
;
164 Size
= BN_num_bytes (BnKey
);
166 if (*BnSize
< Size
) {
171 if (BigNumber
== NULL
) {
174 *BnSize
= BN_bn2bin (BnKey
, BigNumber
) ;
180 Generates RSA key components.
182 This function generates RSA key components. It takes RSA public exponent E and
183 length in bits of RSA modulus N as input, and generates all key components.
184 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
186 Before this function can be invoked, pseudorandom number generator must be correctly
187 initialized by RandomSeed().
189 If RsaContext is NULL, then return FALSE.
191 @param[in, out] RsaContext Pointer to RSA context being set.
192 @param[in] ModulusLength Length of RSA modulus N in bits.
193 @param[in] PublicExponent Pointer to RSA public exponent.
194 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
196 @retval TRUE RSA key component was generated successfully.
197 @retval FALSE Invalid RSA key component tag.
203 IN OUT VOID
*RsaContext
,
204 IN UINTN ModulusLength
,
205 IN CONST UINT8
*PublicExponent
,
206 IN UINTN PublicExponentSize
213 // Check input parameters.
215 if (RsaContext
== NULL
|| ModulusLength
> INT_MAX
|| PublicExponentSize
> INT_MAX
) {
226 if (PublicExponent
== NULL
) {
227 if (BN_set_word (KeyE
, 0x10001) == 0) {
231 if (BN_bin2bn (PublicExponent
, (UINT32
) PublicExponentSize
, KeyE
) == NULL
) {
236 if (RSA_generate_key_ex ((RSA
*) RsaContext
, (UINT32
) ModulusLength
, KeyE
, NULL
) == 1) {
246 Validates key components of RSA context.
248 This function validates key compoents of RSA context in following aspects:
249 - Whether p is a prime
250 - Whether q is a prime
252 - Whether d*e = 1 mod lcm(p-1,q-1)
254 If RsaContext is NULL, then return FALSE.
256 @param[in] RsaContext Pointer to RSA context to check.
258 @retval TRUE RSA key components are valid.
259 @retval FALSE RSA key components are not valid.
271 // Check input parameters.
273 if (RsaContext
== NULL
) {
277 if (RSA_check_key ((RSA
*) RsaContext
) != 1) {
278 Reason
= ERR_GET_REASON (ERR_peek_last_error ());
279 if (Reason
== RSA_R_P_NOT_PRIME
||
280 Reason
== RSA_R_Q_NOT_PRIME
||
281 Reason
== RSA_R_N_DOES_NOT_EQUAL_P_Q
||
282 Reason
== RSA_R_D_E_NOT_CONGRUENT_TO_1
) {
291 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
293 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
295 If the Signature buffer is too small to hold the contents of signature, FALSE
296 is returned and SigSize is set to the required buffer size to obtain the signature.
298 If RsaContext is NULL, then return FALSE.
299 If MessageHash is NULL, then return FALSE.
300 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
301 If SigSize is large enough but Signature is NULL, then return FALSE.
303 @param[in] RsaContext Pointer to RSA context for signature generation.
304 @param[in] MessageHash Pointer to octet message hash to be signed.
305 @param[in] HashSize Size of the message hash in bytes.
306 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
307 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
308 On output, the size of data returned in Signature buffer in bytes.
310 @retval TRUE Signature successfully generated in PKCS1-v1_5.
311 @retval FALSE Signature generation failed.
312 @retval FALSE SigSize is too small.
319 IN CONST UINT8
*MessageHash
,
321 OUT UINT8
*Signature
,
322 IN OUT UINTN
*SigSize
330 // Check input parameters.
332 if (RsaContext
== NULL
|| MessageHash
== NULL
) {
336 Rsa
= (RSA
*) RsaContext
;
337 Size
= BN_num_bytes (Rsa
->n
);
339 if (*SigSize
< Size
) {
344 if (Signature
== NULL
) {
349 // Determine the message digest algorithm according to digest size.
350 // Only MD5, SHA-1 or SHA-256 algorithm is supported.
353 case MD5_DIGEST_SIZE
:
354 DigestType
= NID_md5
;
357 case SHA1_DIGEST_SIZE
:
358 DigestType
= NID_sha1
;
361 case SHA256_DIGEST_SIZE
:
362 DigestType
= NID_sha256
;
369 return (BOOLEAN
) RSA_sign (