2 RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
4 This file implements following APIs which provide more capabilities for RSA:
10 Copyright (c) 2009 - 2012, 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>
27 // ASN.1 value for Hash Algorithm ID with the Distringuished Encoding Rules (DER)
28 // Refer to Section 9.2 of PKCS#1 v2.1
30 CONST UINT8 Asn1IdMd5
[] = {
31 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,
32 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
35 CONST UINT8 Asn1IdSha1
[] = {
36 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
37 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
40 CONST UINT8 Asn1IdSha256
[] = {
41 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
42 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
47 Gets the tag-designated RSA key component from the established RSA context.
49 This function retrieves the tag-designated RSA key component from the
50 established RSA context as a non-negative integer (octet string format
51 represented in RSA PKCS#1).
52 If specified key component has not been set or has been cleared, then returned
54 If the BigNumber buffer is too small to hold the contents of the key, FALSE
55 is returned and BnSize is set to the required buffer size to obtain the key.
57 If RsaContext is NULL, then return FALSE.
58 If BnSize is NULL, then return FALSE.
59 If BnSize is large enough but BigNumber is NULL, then return FALSE.
61 @param[in, out] RsaContext Pointer to RSA context being set.
62 @param[in] KeyTag Tag of RSA key component being set.
63 @param[out] BigNumber Pointer to octet integer buffer.
64 @param[in, out] BnSize On input, the size of big number buffer in bytes.
65 On output, the size of data returned in big number buffer in bytes.
67 @retval TRUE RSA key component was retrieved successfully.
68 @retval FALSE Invalid RSA key component tag.
69 @retval FALSE BnSize is too small.
75 IN OUT VOID
*RsaContext
,
76 IN RSA_KEY_TAG KeyTag
,
86 // Check input parameters.
88 if (RsaContext
== NULL
|| BnSize
== NULL
) {
92 RsaKey
= (RSA
*) RsaContext
;
99 // RSA Public Modulus (N)
102 if (RsaKey
->n
== NULL
) {
109 // RSA Public Exponent (e)
112 if (RsaKey
->e
== NULL
) {
119 // RSA Private Exponent (d)
122 if (RsaKey
->d
== NULL
) {
129 // RSA Secret Prime Factor of Modulus (p)
132 if (RsaKey
->p
== NULL
) {
139 // RSA Secret Prime Factor of Modules (q)
142 if (RsaKey
->q
== NULL
) {
149 // p's CRT Exponent (== d mod (p - 1))
152 if (RsaKey
->dmp1
== NULL
) {
155 BnKey
= RsaKey
->dmp1
;
159 // q's CRT Exponent (== d mod (q - 1))
162 if (RsaKey
->dmq1
== NULL
) {
165 BnKey
= RsaKey
->dmq1
;
169 // The CRT Coefficient (== 1/q mod p)
172 if (RsaKey
->iqmp
== NULL
) {
175 BnKey
= RsaKey
->iqmp
;
183 Size
= BN_num_bytes (BnKey
);
185 if (*BnSize
< Size
) {
190 if (BigNumber
== NULL
) {
193 *BnSize
= BN_bn2bin (BnKey
, BigNumber
) ;
199 Generates RSA key components.
201 This function generates RSA key components. It takes RSA public exponent E and
202 length in bits of RSA modulus N as input, and generates all key components.
203 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
205 Before this function can be invoked, pseudorandom number generator must be correctly
206 initialized by RandomSeed().
208 If RsaContext is NULL, then return FALSE.
210 @param[in, out] RsaContext Pointer to RSA context being set.
211 @param[in] ModulusLength Length of RSA modulus N in bits.
212 @param[in] PublicExponent Pointer to RSA public exponent.
213 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
215 @retval TRUE RSA key component was generated successfully.
216 @retval FALSE Invalid RSA key component tag.
222 IN OUT VOID
*RsaContext
,
223 IN UINTN ModulusLength
,
224 IN CONST UINT8
*PublicExponent
,
225 IN UINTN PublicExponentSize
232 // Check input parameters.
234 if (RsaContext
== NULL
) {
239 if (PublicExponent
== NULL
) {
240 BN_set_word (KeyE
, 0x10001);
242 BN_bin2bn (PublicExponent
, (UINT32
) PublicExponentSize
, KeyE
);
246 if (RSA_generate_key_ex ((RSA
*) RsaContext
, (UINT32
) ModulusLength
, KeyE
, NULL
) == 1) {
255 Validates key components of RSA context.
257 This function validates key compoents of RSA context in following aspects:
258 - Whether p is a prime
259 - Whether q is a prime
261 - Whether d*e = 1 mod lcm(p-1,q-1)
263 If RsaContext is NULL, then return FALSE.
265 @param[in] RsaContext Pointer to RSA context to check.
267 @retval TRUE RSA key components are valid.
268 @retval FALSE RSA key components are not valid.
280 // Check input parameters.
282 if (RsaContext
== NULL
) {
286 if (RSA_check_key ((RSA
*) RsaContext
) != 1) {
287 Reason
= ERR_GET_REASON (ERR_peek_last_error ());
288 if (Reason
== RSA_R_P_NOT_PRIME
||
289 Reason
== RSA_R_Q_NOT_PRIME
||
290 Reason
== RSA_R_N_DOES_NOT_EQUAL_P_Q
||
291 Reason
== RSA_R_D_E_NOT_CONGRUENT_TO_1
) {
300 Performs the PKCS1-v1_5 encoding methods defined in RSA PKCS #1.
302 @param Message Message buffer to be encoded.
303 @param MessageSize Size of message buffer in bytes.
304 @param DigestInfo Pointer to buffer of digest info for output.
306 @return Size of DigestInfo in bytes.
311 IN CONST UINT8
*Message
,
312 IN UINTN MessageSize
,
313 OUT UINT8
*DigestInfo
316 CONST UINT8
*HashDer
;
320 // Check input parameters.
322 if (Message
== NULL
|| DigestInfo
== NULL
) {
327 // The original message length is used to determine the hash algorithm since
328 // message is digest value hashed by the specified algorithm.
330 switch (MessageSize
) {
331 case MD5_DIGEST_SIZE
:
333 DerSize
= sizeof (Asn1IdMd5
);
336 case SHA1_DIGEST_SIZE
:
337 HashDer
= Asn1IdSha1
;
338 DerSize
= sizeof (Asn1IdSha1
);
341 case SHA256_DIGEST_SIZE
:
342 HashDer
= Asn1IdSha256
;
343 DerSize
= sizeof (Asn1IdSha256
);
350 CopyMem (DigestInfo
, HashDer
, DerSize
);
351 CopyMem (DigestInfo
+ DerSize
, Message
, MessageSize
);
353 return (DerSize
+ MessageSize
);
357 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
359 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
361 If the Signature buffer is too small to hold the contents of signature, FALSE
362 is returned and SigSize is set to the required buffer size to obtain the signature.
364 If RsaContext is NULL, then return FALSE.
365 If MessageHash is NULL, then return FALSE.
366 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
367 If SigSize is large enough but Signature is NULL, then return FALSE.
369 @param[in] RsaContext Pointer to RSA context for signature generation.
370 @param[in] MessageHash Pointer to octet message hash to be signed.
371 @param[in] HashSize Size of the message hash in bytes.
372 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
373 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
374 On output, the size of data returned in Signature buffer in bytes.
376 @retval TRUE Signature successfully generated in PKCS1-v1_5.
377 @retval FALSE Signature generation failed.
378 @retval FALSE SigSize is too small.
385 IN CONST UINT8
*MessageHash
,
387 OUT UINT8
*Signature
,
388 IN OUT UINTN
*SigSize
396 // Check input parameters.
398 if (RsaContext
== NULL
|| MessageHash
== NULL
||
399 (HashSize
!= MD5_DIGEST_SIZE
&& HashSize
!= SHA1_DIGEST_SIZE
&& HashSize
!= SHA256_DIGEST_SIZE
)) {
403 Rsa
= (RSA
*) RsaContext
;
404 Size
= BN_num_bytes (Rsa
->n
);
406 if (*SigSize
< Size
) {
411 if (Signature
== NULL
) {
415 Size
= DigestInfoEncoding (MessageHash
, HashSize
, Signature
);
417 ReturnVal
= RSA_private_encrypt (
425 if (ReturnVal
< (INTN
) Size
) {
429 *SigSize
= (UINTN
)ReturnVal
;