]>
git.proxmox.com Git - efi-boot-shim.git/blob - Cryptlib/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 - 2015, 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 componenet 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 componenet 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
,
98 // Check input parameters.
100 if (RsaContext
== NULL
|| BnSize
> INT_MAX
) {
104 RsaKey
= (RSA
*) RsaContext
;
106 // Set RSA Key Components by converting octet string to OpenSSL BN representation.
107 // NOTE: For RSA public key (used in signature verification), only public components
108 // (N, e) are needed.
113 // RSA Public Modulus (N)
116 if (RsaKey
->n
!= NULL
) {
120 if (BigNumber
== NULL
) {
123 RsaKey
->n
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->n
);
124 if (RsaKey
->n
== NULL
) {
131 // RSA Public Exponent (e)
134 if (RsaKey
->e
!= NULL
) {
138 if (BigNumber
== NULL
) {
141 RsaKey
->e
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->e
);
142 if (RsaKey
->e
== NULL
) {
149 // RSA Private Exponent (d)
152 if (RsaKey
->d
!= NULL
) {
156 if (BigNumber
== NULL
) {
159 RsaKey
->d
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->d
);
160 if (RsaKey
->d
== NULL
) {
167 // RSA Secret Prime Factor of Modulus (p)
170 if (RsaKey
->p
!= NULL
) {
174 if (BigNumber
== NULL
) {
177 RsaKey
->p
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->p
);
178 if (RsaKey
->p
== NULL
) {
185 // RSA Secret Prime Factor of Modules (q)
188 if (RsaKey
->q
!= NULL
) {
192 if (BigNumber
== NULL
) {
195 RsaKey
->q
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->q
);
196 if (RsaKey
->q
== NULL
) {
203 // p's CRT Exponent (== d mod (p - 1))
206 if (RsaKey
->dmp1
!= NULL
) {
207 BN_free (RsaKey
->dmp1
);
210 if (BigNumber
== NULL
) {
213 RsaKey
->dmp1
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->dmp1
);
214 if (RsaKey
->dmp1
== NULL
) {
221 // q's CRT Exponent (== d mod (q - 1))
224 if (RsaKey
->dmq1
!= NULL
) {
225 BN_free (RsaKey
->dmq1
);
228 if (BigNumber
== NULL
) {
231 RsaKey
->dmq1
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->dmq1
);
232 if (RsaKey
->dmq1
== NULL
) {
239 // The CRT Coefficient (== 1/q mod p)
242 if (RsaKey
->iqmp
!= NULL
) {
243 BN_free (RsaKey
->iqmp
);
246 if (BigNumber
== NULL
) {
249 RsaKey
->iqmp
= BN_bin2bn (BigNumber
, (UINT32
) BnSize
, RsaKey
->iqmp
);
250 if (RsaKey
->iqmp
== NULL
) {
264 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
267 If RsaContext is NULL, then return FALSE.
268 If MessageHash is NULL, then return FALSE.
269 If Signature is NULL, then return FALSE.
270 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
272 @param[in] RsaContext Pointer to RSA context for signature verification.
273 @param[in] MessageHash Pointer to octet message hash to be checked.
274 @param[in] HashSize Size of the message hash in bytes.
275 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
276 @param[in] SigSize Size of signature in bytes.
278 @retval TRUE Valid signature encoded in PKCS1-v1_5.
279 @retval FALSE Invalid signature or invalid RSA context.
286 IN CONST UINT8
*MessageHash
,
288 IN CONST UINT8
*Signature
,
296 // Check input parameters.
298 if (RsaContext
== NULL
|| MessageHash
== NULL
|| Signature
== NULL
) {
302 if (SigSize
> INT_MAX
|| SigSize
== 0) {
307 // Determine the message digest algorithm according to digest size.
308 // Only MD5, SHA-1 or SHA-256 algorithm is supported.
311 case MD5_DIGEST_SIZE
:
312 DigestType
= NID_md5
;
315 case SHA1_DIGEST_SIZE
:
316 DigestType
= NID_sha1
;
319 case SHA256_DIGEST_SIZE
:
320 DigestType
= NID_sha256
;
327 SigBuf
= (UINT8
*) Signature
;
328 return (BOOLEAN
) RSA_verify (