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