]>
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 - 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/objects.h>
27 Allocates and initializes one RSA context for subsequent use.
29 @return Pointer to the RSA context that has been initialized.
30 If the allocations fails, RsaNew() returns NULL.
40 // Allocates & Initializes RSA Context by OpenSSL RSA_new()
42 return (VOID
*) RSA_new ();
46 Release the specified RSA context.
48 @param[in] RsaContext Pointer to the RSA context to be released.
58 // Free OpenSSL RSA Context
60 RSA_free ((RSA
*) RsaContext
);
64 Sets the tag-designated key component into the established RSA context.
66 This function sets the tag-designated RSA key component into the established
67 RSA context from the user-specified non-negative integer (octet string format
68 represented in RSA PKCS#1).
69 If BigNumber is NULL, then the specified key componenet in RSA context is cleared.
71 If RsaContext is NULL, then return FALSE.
73 @param[in, out] RsaContext Pointer to RSA context being set.
74 @param[in] KeyTag Tag of RSA key component being set.
75 @param[in] BigNumber Pointer to octet integer buffer.
76 If NULL, then the specified key componenet in RSA
78 @param[in] BnSize Size of big number buffer in bytes.
79 If BigNumber is NULL, then it is ignored.
81 @retval TRUE RSA key component was set successfully.
82 @retval FALSE Invalid RSA key component tag.
88 IN OUT VOID
*RsaContext
,
89 IN RSA_KEY_TAG KeyTag
,
90 IN CONST UINT8
*BigNumber
,
97 // Check input parameters.
99 if (RsaContext
== NULL
|| BnSize
> INT_MAX
) {
103 RsaKey
= (RSA
*) RsaContext
;
105 // Set RSA Key Components by converting octet string to OpenSSL BN representation.
106 // NOTE: For RSA public key (used in signature verification), only public components
107 // (N, e) are needed.
112 // RSA Public Modulus (N)
115 if (RsaKey
->n
!= NULL
) {
119 if (BigNumber
== NULL
) {
122 RsaKey
->n
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->n
);
123 if (RsaKey
->n
== NULL
) {
130 // RSA Public Exponent (e)
133 if (RsaKey
->e
!= NULL
) {
137 if (BigNumber
== NULL
) {
140 RsaKey
->e
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->e
);
141 if (RsaKey
->e
== NULL
) {
148 // RSA Private Exponent (d)
151 if (RsaKey
->d
!= NULL
) {
155 if (BigNumber
== NULL
) {
158 RsaKey
->d
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->d
);
159 if (RsaKey
->d
== NULL
) {
166 // RSA Secret Prime Factor of Modulus (p)
169 if (RsaKey
->p
!= NULL
) {
173 if (BigNumber
== NULL
) {
176 RsaKey
->p
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->p
);
177 if (RsaKey
->p
== NULL
) {
184 // RSA Secret Prime Factor of Modules (q)
187 if (RsaKey
->q
!= NULL
) {
191 if (BigNumber
== NULL
) {
194 RsaKey
->q
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->q
);
195 if (RsaKey
->q
== NULL
) {
202 // p's CRT Exponent (== d mod (p - 1))
205 if (RsaKey
->dmp1
!= NULL
) {
206 BN_free (RsaKey
->dmp1
);
209 if (BigNumber
== NULL
) {
212 RsaKey
->dmp1
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->dmp1
);
213 if (RsaKey
->dmp1
== NULL
) {
220 // q's CRT Exponent (== d mod (q - 1))
223 if (RsaKey
->dmq1
!= NULL
) {
224 BN_free (RsaKey
->dmq1
);
227 if (BigNumber
== NULL
) {
230 RsaKey
->dmq1
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->dmq1
);
231 if (RsaKey
->dmq1
== NULL
) {
238 // The CRT Coefficient (== 1/q mod p)
241 if (RsaKey
->iqmp
!= NULL
) {
242 BN_free (RsaKey
->iqmp
);
245 if (BigNumber
== NULL
) {
248 RsaKey
->iqmp
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->iqmp
);
249 if (RsaKey
->iqmp
== NULL
) {
263 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
266 If RsaContext is NULL, then return FALSE.
267 If MessageHash is NULL, then return FALSE.
268 If Signature is NULL, then return FALSE.
269 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
271 @param[in] RsaContext Pointer to RSA context for signature verification.
272 @param[in] MessageHash Pointer to octet message hash to be checked.
273 @param[in] HashSize Size of the message hash in bytes.
274 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
275 @param[in] SigSize Size of signature in bytes.
277 @retval TRUE Valid signature encoded in PKCS1-v1_5.
278 @retval FALSE Invalid signature or invalid RSA context.
285 IN CONST UINT8
*MessageHash
,
287 IN CONST UINT8
*Signature
,
295 // Check input parameters.
297 if (RsaContext
== NULL
|| MessageHash
== NULL
|| Signature
== NULL
) {
301 if (SigSize
> INT_MAX
|| SigSize
== 0) {
306 // Determine the message digest algorithm according to digest size.
307 // Only MD5, SHA-1 or SHA-256 algorithm is supported.
310 case MD5_DIGEST_SIZE
:
311 DigestType
= NID_md5
;
314 case SHA1_DIGEST_SIZE
:
315 DigestType
= NID_sha1
;
318 case SHA256_DIGEST_SIZE
:
319 DigestType
= NID_sha256
;
326 SigBuf
= (UINT8
*) Signature
;
327 return (BOOLEAN
) RSA_verify (