TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptRsaExt.c
Go to the documentation of this file.
1
15#include "InternalCryptLib.h"
16
17#include <openssl/bn.h>
18#include <openssl/rsa.h>
19#include <openssl/err.h>
20#include <openssl/objects.h>
21
48BOOLEAN
49EFIAPI
51 IN OUT VOID *RsaContext,
52 IN RSA_KEY_TAG KeyTag,
53 OUT UINT8 *BigNumber,
54 IN OUT UINTN *BnSize
55 )
56{
57 RSA *RsaKey;
58 BIGNUM *BnKey;
59 UINTN Size;
60
61 //
62 // Check input parameters.
63 //
64 if ((RsaContext == NULL) || (BnSize == NULL)) {
65 return FALSE;
66 }
67
68 RsaKey = (RSA *)RsaContext;
69 Size = *BnSize;
70 *BnSize = 0;
71 BnKey = NULL;
72
73 switch (KeyTag) {
74 //
75 // RSA Public Modulus (N)
76 //
77 case RsaKeyN:
78 RSA_get0_key (RsaKey, (const BIGNUM **)&BnKey, NULL, NULL);
79 break;
80
81 //
82 // RSA Public Exponent (e)
83 //
84 case RsaKeyE:
85 RSA_get0_key (RsaKey, NULL, (const BIGNUM **)&BnKey, NULL);
86 break;
87
88 //
89 // RSA Private Exponent (d)
90 //
91 case RsaKeyD:
92 RSA_get0_key (RsaKey, NULL, NULL, (const BIGNUM **)&BnKey);
93 break;
94
95 //
96 // RSA Secret Prime Factor of Modulus (p)
97 //
98 case RsaKeyP:
99 RSA_get0_factors (RsaKey, (const BIGNUM **)&BnKey, NULL);
100 break;
101
102 //
103 // RSA Secret Prime Factor of Modules (q)
104 //
105 case RsaKeyQ:
106 RSA_get0_factors (RsaKey, NULL, (const BIGNUM **)&BnKey);
107 break;
108
109 //
110 // p's CRT Exponent (== d mod (p - 1))
111 //
112 case RsaKeyDp:
113 RSA_get0_crt_params (RsaKey, (const BIGNUM **)&BnKey, NULL, NULL);
114 break;
115
116 //
117 // q's CRT Exponent (== d mod (q - 1))
118 //
119 case RsaKeyDq:
120 RSA_get0_crt_params (RsaKey, NULL, (const BIGNUM **)&BnKey, NULL);
121 break;
122
123 //
124 // The CRT Coefficient (== 1/q mod p)
125 //
126 case RsaKeyQInv:
127 RSA_get0_crt_params (RsaKey, NULL, NULL, (const BIGNUM **)&BnKey);
128 break;
129
130 default:
131 return FALSE;
132 }
133
134 if (BnKey == NULL) {
135 return FALSE;
136 }
137
138 *BnSize = Size;
139 Size = BN_num_bytes (BnKey);
140
141 if (*BnSize < Size) {
142 *BnSize = Size;
143 return FALSE;
144 }
145
146 if (BigNumber == NULL) {
147 *BnSize = Size;
148 return TRUE;
149 }
150
151 *BnSize = BN_bn2bin (BnKey, BigNumber);
152
153 return TRUE;
154}
155
177BOOLEAN
178EFIAPI
180 IN OUT VOID *RsaContext,
181 IN UINTN ModulusLength,
182 IN CONST UINT8 *PublicExponent,
183 IN UINTN PublicExponentSize
184 )
185{
186 BIGNUM *KeyE;
187 BOOLEAN RetVal;
188
189 //
190 // Check input parameters.
191 //
192 if ((RsaContext == NULL) || (ModulusLength > INT_MAX) || (PublicExponentSize > INT_MAX)) {
193 return FALSE;
194 }
195
196 KeyE = BN_new ();
197 if (KeyE == NULL) {
198 return FALSE;
199 }
200
201 RetVal = FALSE;
202
203 if (PublicExponent == NULL) {
204 if (BN_set_word (KeyE, 0x10001) == 0) {
205 goto _Exit;
206 }
207 } else {
208 if (BN_bin2bn (PublicExponent, (UINT32)PublicExponentSize, KeyE) == NULL) {
209 goto _Exit;
210 }
211 }
212
213 if (RSA_generate_key_ex ((RSA *)RsaContext, (UINT32)ModulusLength, KeyE, NULL) == 1) {
214 RetVal = TRUE;
215 }
216
217_Exit:
218 BN_free (KeyE);
219 return RetVal;
220}
221
241BOOLEAN
242EFIAPI
244 IN VOID *RsaContext
245 )
246{
247 UINTN Reason;
248
249 //
250 // Check input parameters.
251 //
252 if (RsaContext == NULL) {
253 return FALSE;
254 }
255
256 if (RSA_check_key ((RSA *)RsaContext) != 1) {
257 Reason = ERR_GET_REASON (ERR_peek_last_error ());
258 if ((Reason == RSA_R_P_NOT_PRIME) ||
259 (Reason == RSA_R_Q_NOT_PRIME) ||
260 (Reason == RSA_R_N_DOES_NOT_EQUAL_P_Q) ||
261 (Reason == RSA_R_D_E_NOT_CONGRUENT_TO_1))
262 {
263 return FALSE;
264 }
265 }
266
267 return TRUE;
268}
269
295BOOLEAN
296EFIAPI
298 IN VOID *RsaContext,
299 IN CONST UINT8 *MessageHash,
300 IN UINTN HashSize,
301 OUT UINT8 *Signature,
302 IN OUT UINTN *SigSize
303 )
304{
305 RSA *Rsa;
306 UINTN Size;
307 INT32 DigestType;
308
309 //
310 // Check input parameters.
311 //
312 if ((RsaContext == NULL) || (MessageHash == NULL)) {
313 return FALSE;
314 }
315
316 Rsa = (RSA *)RsaContext;
317 Size = RSA_size (Rsa);
318
319 if (*SigSize < Size) {
320 *SigSize = Size;
321 return FALSE;
322 }
323
324 if (Signature == NULL) {
325 return FALSE;
326 }
327
328 //
329 // Determine the message digest algorithm according to digest size.
330 // Only MD5, SHA-1, SHA-256, SHA-384 or SHA-512 algorithm is supported.
331 //
332 switch (HashSize) {
333 case MD5_DIGEST_SIZE:
334 DigestType = NID_md5;
335 break;
336
337 case SHA1_DIGEST_SIZE:
338 DigestType = NID_sha1;
339 break;
340
342 DigestType = NID_sha256;
343 break;
344
346 DigestType = NID_sha384;
347 break;
348
350 DigestType = NID_sha512;
351 break;
352
353 default:
354 return FALSE;
355 }
356
357 return (BOOLEAN)RSA_sign (
358 DigestType,
359 MessageHash,
360 (UINT32)HashSize,
361 Signature,
362 (UINT32 *)SigSize,
363 (RSA *)RsaContext
364 );
365}
UINT64 UINTN
RSA_KEY_TAG
Definition: BaseCryptLib.h:74
@ RsaKeyDq
q's CRT exponent (== d mod (q - 1))
Definition: BaseCryptLib.h:81
@ RsaKeyD
RSA Private exponent (d)
Definition: BaseCryptLib.h:77
@ RsaKeyDp
p's CRT exponent (== d mod (p - 1))
Definition: BaseCryptLib.h:80
@ RsaKeyP
RSA secret prime factor of Modulus (p)
Definition: BaseCryptLib.h:78
@ RsaKeyN
RSA public Modulus (N)
Definition: BaseCryptLib.h:75
@ RsaKeyQ
RSA secret prime factor of Modules (q)
Definition: BaseCryptLib.h:79
@ RsaKeyQInv
The CRT coefficient (== 1/q mod p)
Definition: BaseCryptLib.h:82
@ RsaKeyE
RSA Public exponent (e)
Definition: BaseCryptLib.h:76
#define SHA1_DIGEST_SIZE
Definition: BaseCryptLib.h:39
#define SHA512_DIGEST_SIZE
Definition: BaseCryptLib.h:54
#define SHA256_DIGEST_SIZE
Definition: BaseCryptLib.h:44
#define MD5_DIGEST_SIZE
Definition: BaseCryptLib.h:34
#define SHA384_DIGEST_SIZE
Definition: BaseCryptLib.h:49
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
BOOLEAN EFIAPI RsaCheckKey(IN VOID *RsaContext)
Definition: CryptRsaExt.c:243
BOOLEAN EFIAPI RsaGetKey(IN OUT VOID *RsaContext, IN RSA_KEY_TAG KeyTag, OUT UINT8 *BigNumber, IN OUT UINTN *BnSize)
Definition: CryptRsaExt.c:50
BOOLEAN EFIAPI RsaGenerateKey(IN OUT VOID *RsaContext, IN UINTN ModulusLength, IN CONST UINT8 *PublicExponent, IN UINTN PublicExponentSize)
Definition: CryptRsaExt.c:179
BOOLEAN EFIAPI RsaPkcs1Sign(IN VOID *RsaContext, IN CONST UINT8 *MessageHash, IN UINTN HashSize, OUT UINT8 *Signature, IN OUT UINTN *SigSize)
Definition: CryptRsaExt.c:297