]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c
b941d6fac6028270981b1b252754cfd0ae80a907
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptRsaExt.c
1 /** @file
2 RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
3
4 This file implements following APIs which provide more capabilities for RSA:
5 1) RsaGetKey
6 2) RsaGenerateKey
7 3) RsaCheckKey
8 4) RsaPkcs1Sign
9
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
15
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.
18
19 **/
20
21 #include "InternalCryptLib.h"
22
23 #include <openssl/bn.h>
24 #include <openssl/rsa.h>
25 #include <openssl/err.h>
26 #include <openssl/objects.h>
27
28 /**
29 Gets the tag-designated RSA key component from the established RSA context.
30
31 This function retrieves the tag-designated RSA key component from the
32 established RSA context as a non-negative integer (octet string format
33 represented in RSA PKCS#1).
34 If specified key component has not been set or has been cleared, then returned
35 BnSize is set to 0.
36 If the BigNumber buffer is too small to hold the contents of the key, FALSE
37 is returned and BnSize is set to the required buffer size to obtain the key.
38
39 If RsaContext is NULL, then return FALSE.
40 If BnSize is NULL, then return FALSE.
41 If BnSize is large enough but BigNumber is NULL, then return FALSE.
42
43 @param[in, out] RsaContext Pointer to RSA context being set.
44 @param[in] KeyTag Tag of RSA key component being set.
45 @param[out] BigNumber Pointer to octet integer buffer.
46 @param[in, out] BnSize On input, the size of big number buffer in bytes.
47 On output, the size of data returned in big number buffer in bytes.
48
49 @retval TRUE RSA key component was retrieved successfully.
50 @retval FALSE Invalid RSA key component tag.
51 @retval FALSE BnSize is too small.
52
53 **/
54 BOOLEAN
55 EFIAPI
56 RsaGetKey (
57 IN OUT VOID *RsaContext,
58 IN RSA_KEY_TAG KeyTag,
59 OUT UINT8 *BigNumber,
60 IN OUT UINTN *BnSize
61 )
62 {
63 RSA *RsaKey;
64 BIGNUM *BnKey;
65 UINTN Size;
66
67 //
68 // Check input parameters.
69 //
70 if (RsaContext == NULL || BnSize == NULL) {
71 return FALSE;
72 }
73
74 RsaKey = (RSA *) RsaContext;
75 Size = *BnSize;
76 *BnSize = 0;
77
78 switch (KeyTag) {
79
80 //
81 // RSA Public Modulus (N)
82 //
83 case RsaKeyN:
84 if (RsaKey->n == NULL) {
85 return TRUE;
86 }
87 BnKey = RsaKey->n;
88 break;
89
90 //
91 // RSA Public Exponent (e)
92 //
93 case RsaKeyE:
94 if (RsaKey->e == NULL) {
95 return TRUE;
96 }
97 BnKey = RsaKey->e;
98 break;
99
100 //
101 // RSA Private Exponent (d)
102 //
103 case RsaKeyD:
104 if (RsaKey->d == NULL) {
105 return TRUE;
106 }
107 BnKey = RsaKey->d;
108 break;
109
110 //
111 // RSA Secret Prime Factor of Modulus (p)
112 //
113 case RsaKeyP:
114 if (RsaKey->p == NULL) {
115 return TRUE;
116 }
117 BnKey = RsaKey->p;
118 break;
119
120 //
121 // RSA Secret Prime Factor of Modules (q)
122 //
123 case RsaKeyQ:
124 if (RsaKey->q == NULL) {
125 return TRUE;
126 }
127 BnKey = RsaKey->q;
128 break;
129
130 //
131 // p's CRT Exponent (== d mod (p - 1))
132 //
133 case RsaKeyDp:
134 if (RsaKey->dmp1 == NULL) {
135 return TRUE;
136 }
137 BnKey = RsaKey->dmp1;
138 break;
139
140 //
141 // q's CRT Exponent (== d mod (q - 1))
142 //
143 case RsaKeyDq:
144 if (RsaKey->dmq1 == NULL) {
145 return TRUE;
146 }
147 BnKey = RsaKey->dmq1;
148 break;
149
150 //
151 // The CRT Coefficient (== 1/q mod p)
152 //
153 case RsaKeyQInv:
154 if (RsaKey->iqmp == NULL) {
155 return TRUE;
156 }
157 BnKey = RsaKey->iqmp;
158 break;
159
160 default:
161 return FALSE;
162 }
163
164 *BnSize = Size;
165 Size = BN_num_bytes (BnKey);
166
167 if (*BnSize < Size) {
168 *BnSize = Size;
169 return FALSE;
170 }
171
172 if (BigNumber == NULL) {
173 return FALSE;
174 }
175 *BnSize = BN_bn2bin (BnKey, BigNumber) ;
176
177 return TRUE;
178 }
179
180 /**
181 Generates RSA key components.
182
183 This function generates RSA key components. It takes RSA public exponent E and
184 length in bits of RSA modulus N as input, and generates all key components.
185 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
186
187 Before this function can be invoked, pseudorandom number generator must be correctly
188 initialized by RandomSeed().
189
190 If RsaContext is NULL, then return FALSE.
191
192 @param[in, out] RsaContext Pointer to RSA context being set.
193 @param[in] ModulusLength Length of RSA modulus N in bits.
194 @param[in] PublicExponent Pointer to RSA public exponent.
195 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
196
197 @retval TRUE RSA key component was generated successfully.
198 @retval FALSE Invalid RSA key component tag.
199
200 **/
201 BOOLEAN
202 EFIAPI
203 RsaGenerateKey (
204 IN OUT VOID *RsaContext,
205 IN UINTN ModulusLength,
206 IN CONST UINT8 *PublicExponent,
207 IN UINTN PublicExponentSize
208 )
209 {
210 BIGNUM *KeyE;
211 BOOLEAN RetVal;
212
213 //
214 // Check input parameters.
215 //
216 if (RsaContext == NULL || ModulusLength > INT_MAX || PublicExponentSize > INT_MAX) {
217 return FALSE;
218 }
219
220 KeyE = BN_new ();
221 if (KeyE == NULL) {
222 return FALSE;
223 }
224
225 RetVal = FALSE;
226
227 if (PublicExponent == NULL) {
228 if (BN_set_word (KeyE, 0x10001) == 0) {
229 goto _Exit;
230 }
231 } else {
232 if (BN_bin2bn (PublicExponent, (UINT32) PublicExponentSize, KeyE) == NULL) {
233 goto _Exit;
234 }
235 }
236
237 if (RSA_generate_key_ex ((RSA *) RsaContext, (UINT32) ModulusLength, KeyE, NULL) == 1) {
238 RetVal = TRUE;
239 }
240
241 _Exit:
242 BN_free (KeyE);
243 return RetVal;
244 }
245
246 /**
247 Validates key components of RSA context.
248 NOTE: This function performs integrity checks on all the RSA key material, so
249 the RSA key structure must contain all the private key data.
250
251 This function validates key compoents of RSA context in following aspects:
252 - Whether p is a prime
253 - Whether q is a prime
254 - Whether n = p * q
255 - Whether d*e = 1 mod lcm(p-1,q-1)
256
257 If RsaContext is NULL, then return FALSE.
258
259 @param[in] RsaContext Pointer to RSA context to check.
260
261 @retval TRUE RSA key components are valid.
262 @retval FALSE RSA key components are not valid.
263
264 **/
265 BOOLEAN
266 EFIAPI
267 RsaCheckKey (
268 IN VOID *RsaContext
269 )
270 {
271 UINTN Reason;
272
273 //
274 // Check input parameters.
275 //
276 if (RsaContext == NULL) {
277 return FALSE;
278 }
279
280 if (RSA_check_key ((RSA *) RsaContext) != 1) {
281 Reason = ERR_GET_REASON (ERR_peek_last_error ());
282 if (Reason == RSA_R_P_NOT_PRIME ||
283 Reason == RSA_R_Q_NOT_PRIME ||
284 Reason == RSA_R_N_DOES_NOT_EQUAL_P_Q ||
285 Reason == RSA_R_D_E_NOT_CONGRUENT_TO_1) {
286 return FALSE;
287 }
288 }
289
290 return TRUE;
291 }
292
293 /**
294 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
295
296 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
297 RSA PKCS#1.
298 If the Signature buffer is too small to hold the contents of signature, FALSE
299 is returned and SigSize is set to the required buffer size to obtain the signature.
300
301 If RsaContext is NULL, then return FALSE.
302 If MessageHash is NULL, then return FALSE.
303 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
304 If SigSize is large enough but Signature is NULL, then return FALSE.
305
306 @param[in] RsaContext Pointer to RSA context for signature generation.
307 @param[in] MessageHash Pointer to octet message hash to be signed.
308 @param[in] HashSize Size of the message hash in bytes.
309 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
310 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
311 On output, the size of data returned in Signature buffer in bytes.
312
313 @retval TRUE Signature successfully generated in PKCS1-v1_5.
314 @retval FALSE Signature generation failed.
315 @retval FALSE SigSize is too small.
316
317 **/
318 BOOLEAN
319 EFIAPI
320 RsaPkcs1Sign (
321 IN VOID *RsaContext,
322 IN CONST UINT8 *MessageHash,
323 IN UINTN HashSize,
324 OUT UINT8 *Signature,
325 IN OUT UINTN *SigSize
326 )
327 {
328 RSA *Rsa;
329 UINTN Size;
330 INT32 DigestType;
331
332 //
333 // Check input parameters.
334 //
335 if (RsaContext == NULL || MessageHash == NULL) {
336 return FALSE;
337 }
338
339 Rsa = (RSA *) RsaContext;
340 Size = BN_num_bytes (Rsa->n);
341
342 if (*SigSize < Size) {
343 *SigSize = Size;
344 return FALSE;
345 }
346
347 if (Signature == NULL) {
348 return FALSE;
349 }
350
351 //
352 // Determine the message digest algorithm according to digest size.
353 // Only MD5, SHA-1 or SHA-256 algorithm is supported.
354 //
355 switch (HashSize) {
356 case MD5_DIGEST_SIZE:
357 DigestType = NID_md5;
358 break;
359
360 case SHA1_DIGEST_SIZE:
361 DigestType = NID_sha1;
362 break;
363
364 case SHA256_DIGEST_SIZE:
365 DigestType = NID_sha256;
366 break;
367
368 default:
369 return FALSE;
370 }
371
372 return (BOOLEAN) RSA_sign (
373 DigestType,
374 MessageHash,
375 (UINT32) HashSize,
376 Signature,
377 (UINT32 *) SigSize,
378 (RSA *) RsaContext
379 );
380 }