TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptRsaBasic.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/objects.h>
20
28VOID *
29EFIAPI
31 VOID
32 )
33{
34 //
35 // Allocates & Initializes RSA Context by OpenSSL RSA_new()
36 //
37 return (VOID *)RSA_new ();
38}
39
46VOID
47EFIAPI
49 IN VOID *RsaContext
50 )
51{
52 //
53 // Free OpenSSL RSA Context
54 //
55 RSA_free ((RSA *)RsaContext);
56}
57
80BOOLEAN
81EFIAPI
83 IN OUT VOID *RsaContext,
84 IN RSA_KEY_TAG KeyTag,
85 IN CONST UINT8 *BigNumber,
86 IN UINTN BnSize
87 )
88{
89 RSA *RsaKey;
90 BIGNUM *BnN;
91 BIGNUM *BnE;
92 BIGNUM *BnD;
93 BIGNUM *BnP;
94 BIGNUM *BnQ;
95 BIGNUM *BnDp;
96 BIGNUM *BnDq;
97 BIGNUM *BnQInv;
98
99 //
100 // Check input parameters.
101 //
102 if ((RsaContext == NULL) || (BnSize > INT_MAX)) {
103 return FALSE;
104 }
105
106 BnN = NULL;
107 BnE = NULL;
108 BnD = NULL;
109 BnP = NULL;
110 BnQ = NULL;
111 BnDp = NULL;
112 BnDq = NULL;
113 BnQInv = NULL;
114
115 //
116 // Retrieve the components from RSA object.
117 //
118 RsaKey = (RSA *)RsaContext;
119 RSA_get0_key (RsaKey, (const BIGNUM **)&BnN, (const BIGNUM **)&BnE, (const BIGNUM **)&BnD);
120 RSA_get0_factors (RsaKey, (const BIGNUM **)&BnP, (const BIGNUM **)&BnQ);
121 RSA_get0_crt_params (RsaKey, (const BIGNUM **)&BnDp, (const BIGNUM **)&BnDq, (const BIGNUM **)&BnQInv);
122
123 //
124 // Set RSA Key Components by converting octet string to OpenSSL BN representation.
125 // NOTE: For RSA public key (used in signature verification), only public components
126 // (N, e) are needed.
127 //
128 switch (KeyTag) {
129 //
130 // RSA Public Modulus (N), Public Exponent (e) and Private Exponent (d)
131 //
132 case RsaKeyN:
133 case RsaKeyE:
134 case RsaKeyD:
135 if (BnN == NULL) {
136 BnN = BN_new ();
137 }
138
139 if (BnE == NULL) {
140 BnE = BN_new ();
141 }
142
143 if (BnD == NULL) {
144 BnD = BN_new ();
145 }
146
147 if ((BnN == NULL) || (BnE == NULL) || (BnD == NULL)) {
148 return FALSE;
149 }
150
151 switch (KeyTag) {
152 case RsaKeyN:
153 BnN = BN_bin2bn (BigNumber, (UINT32)BnSize, BnN);
154 break;
155 case RsaKeyE:
156 BnE = BN_bin2bn (BigNumber, (UINT32)BnSize, BnE);
157 break;
158 case RsaKeyD:
159 BnD = BN_bin2bn (BigNumber, (UINT32)BnSize, BnD);
160 break;
161 default:
162 return FALSE;
163 }
164
165 if (RSA_set0_key (RsaKey, BN_dup (BnN), BN_dup (BnE), BN_dup (BnD)) == 0) {
166 return FALSE;
167 }
168
169 break;
170
171 //
172 // RSA Secret Prime Factor of Modulus (p and q)
173 //
174 case RsaKeyP:
175 case RsaKeyQ:
176 if (BnP == NULL) {
177 BnP = BN_new ();
178 }
179
180 if (BnQ == NULL) {
181 BnQ = BN_new ();
182 }
183
184 if ((BnP == NULL) || (BnQ == NULL)) {
185 return FALSE;
186 }
187
188 switch (KeyTag) {
189 case RsaKeyP:
190 BnP = BN_bin2bn (BigNumber, (UINT32)BnSize, BnP);
191 break;
192 case RsaKeyQ:
193 BnQ = BN_bin2bn (BigNumber, (UINT32)BnSize, BnQ);
194 break;
195 default:
196 return FALSE;
197 }
198
199 if (RSA_set0_factors (RsaKey, BN_dup (BnP), BN_dup (BnQ)) == 0) {
200 return FALSE;
201 }
202
203 break;
204
205 //
206 // p's CRT Exponent (== d mod (p - 1)), q's CRT Exponent (== d mod (q - 1)),
207 // and CRT Coefficient (== 1/q mod p)
208 //
209 case RsaKeyDp:
210 case RsaKeyDq:
211 case RsaKeyQInv:
212 if (BnDp == NULL) {
213 BnDp = BN_new ();
214 }
215
216 if (BnDq == NULL) {
217 BnDq = BN_new ();
218 }
219
220 if (BnQInv == NULL) {
221 BnQInv = BN_new ();
222 }
223
224 if ((BnDp == NULL) || (BnDq == NULL) || (BnQInv == NULL)) {
225 return FALSE;
226 }
227
228 switch (KeyTag) {
229 case RsaKeyDp:
230 BnDp = BN_bin2bn (BigNumber, (UINT32)BnSize, BnDp);
231 break;
232 case RsaKeyDq:
233 BnDq = BN_bin2bn (BigNumber, (UINT32)BnSize, BnDq);
234 break;
235 case RsaKeyQInv:
236 BnQInv = BN_bin2bn (BigNumber, (UINT32)BnSize, BnQInv);
237 break;
238 default:
239 return FALSE;
240 }
241
242 if (RSA_set0_crt_params (RsaKey, BN_dup (BnDp), BN_dup (BnDq), BN_dup (BnQInv)) == 0) {
243 return FALSE;
244 }
245
246 break;
247
248 default:
249 return FALSE;
250 }
251
252 return TRUE;
253}
254
274BOOLEAN
275EFIAPI
277 IN VOID *RsaContext,
278 IN CONST UINT8 *MessageHash,
279 IN UINTN HashSize,
280 IN CONST UINT8 *Signature,
281 IN UINTN SigSize
282 )
283{
284 INT32 DigestType;
285 UINT8 *SigBuf;
286
287 //
288 // Check input parameters.
289 //
290 if ((RsaContext == NULL) || (MessageHash == NULL) || (Signature == NULL)) {
291 return FALSE;
292 }
293
294 if ((SigSize > INT_MAX) || (SigSize == 0)) {
295 return FALSE;
296 }
297
298 //
299 // Determine the message digest algorithm according to digest size.
300 // Only MD5, SHA-1, SHA-256, SHA-384 or SHA-512 algorithm is supported.
301 //
302 switch (HashSize) {
303 case MD5_DIGEST_SIZE:
304 DigestType = NID_md5;
305 break;
306
307 case SHA1_DIGEST_SIZE:
308 DigestType = NID_sha1;
309 break;
310
312 DigestType = NID_sha256;
313 break;
314
316 DigestType = NID_sha384;
317 break;
318
320 DigestType = NID_sha512;
321 break;
322
323 default:
324 return FALSE;
325 }
326
327 SigBuf = (UINT8 *)Signature;
328 return (BOOLEAN)RSA_verify (
329 DigestType,
330 MessageHash,
331 (UINT32)HashSize,
332 SigBuf,
333 (UINT32)SigSize,
334 (RSA *)RsaContext
335 );
336}
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
VOID *EFIAPI RsaNew(VOID)
Definition: CryptRsaBasic.c:30
BOOLEAN EFIAPI RsaSetKey(IN OUT VOID *RsaContext, IN RSA_KEY_TAG KeyTag, IN CONST UINT8 *BigNumber, IN UINTN BnSize)
Definition: CryptRsaBasic.c:82
VOID EFIAPI RsaFree(IN VOID *RsaContext)
Definition: CryptRsaBasic.c:48
BOOLEAN EFIAPI RsaPkcs1Verify(IN VOID *RsaContext, IN CONST UINT8 *MessageHash, IN UINTN HashSize, IN CONST UINT8 *Signature, IN UINTN SigSize)