]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c
SecurityPkg: Add UEFI-2.5 PKCS7 Verification Protocol Support
[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
246 Validates key components of RSA context.\r
247\r
248 This function validates key compoents of RSA context in following aspects:\r
249 - Whether p is a prime\r
250 - Whether q is a prime\r
251 - Whether n = p * q\r
252 - Whether d*e = 1 mod lcm(p-1,q-1)\r
253\r
254 If RsaContext is NULL, then return FALSE.\r
255\r
256 @param[in] RsaContext Pointer to RSA context to check.\r
257\r
258 @retval TRUE RSA key components are valid.\r
259 @retval FALSE RSA key components are not valid.\r
260\r
261**/\r
262BOOLEAN\r
263EFIAPI\r
264RsaCheckKey (\r
265 IN VOID *RsaContext\r
266 )\r
267{\r
268 UINTN Reason;\r
269\r
270 //\r
271 // Check input parameters.\r
272 //\r
273 if (RsaContext == NULL) {\r
274 return FALSE;\r
275 }\r
276 \r
277 if (RSA_check_key ((RSA *) RsaContext) != 1) {\r
278 Reason = ERR_GET_REASON (ERR_peek_last_error ());\r
279 if (Reason == RSA_R_P_NOT_PRIME ||\r
280 Reason == RSA_R_Q_NOT_PRIME ||\r
281 Reason == RSA_R_N_DOES_NOT_EQUAL_P_Q ||\r
282 Reason == RSA_R_D_E_NOT_CONGRUENT_TO_1) {\r
283 return FALSE;\r
284 }\r
285 }\r
286\r
287 return TRUE;\r
288}\r
289\r
532616bb 290/**\r
291 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.\r
292\r
293 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in\r
294 RSA PKCS#1.\r
295 If the Signature buffer is too small to hold the contents of signature, FALSE\r
296 is returned and SigSize is set to the required buffer size to obtain the signature.\r
297\r
298 If RsaContext is NULL, then return FALSE.\r
299 If MessageHash is NULL, then return FALSE.\r
300 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.\r
301 If SigSize is large enough but Signature is NULL, then return FALSE.\r
302\r
303 @param[in] RsaContext Pointer to RSA context for signature generation.\r
304 @param[in] MessageHash Pointer to octet message hash to be signed.\r
305 @param[in] HashSize Size of the message hash in bytes.\r
306 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.\r
307 @param[in, out] SigSize On input, the size of Signature buffer in bytes.\r
308 On output, the size of data returned in Signature buffer in bytes.\r
309\r
310 @retval TRUE Signature successfully generated in PKCS1-v1_5.\r
311 @retval FALSE Signature generation failed.\r
312 @retval FALSE SigSize is too small.\r
313\r
314**/\r
315BOOLEAN\r
316EFIAPI\r
317RsaPkcs1Sign (\r
318 IN VOID *RsaContext,\r
319 IN CONST UINT8 *MessageHash,\r
320 IN UINTN HashSize,\r
321 OUT UINT8 *Signature,\r
322 IN OUT UINTN *SigSize\r
323 )\r
324{\r
325 RSA *Rsa;\r
326 UINTN Size;\r
86b5c3ee 327 INT32 DigestType;\r
532616bb 328\r
329 //\r
330 // Check input parameters.\r
331 //\r
86b5c3ee 332 if (RsaContext == NULL || MessageHash == NULL) {\r
532616bb 333 return FALSE;\r
334 }\r
335\r
336 Rsa = (RSA *) RsaContext;\r
337 Size = BN_num_bytes (Rsa->n);\r
338\r
339 if (*SigSize < Size) {\r
340 *SigSize = Size;\r
341 return FALSE;\r
342 }\r
86b5c3ee 343 \r
532616bb 344 if (Signature == NULL) {\r
345 return FALSE;\r
346 }\r
86b5c3ee 347 \r
348 //\r
349 // Determine the message digest algorithm according to digest size.\r
350 // Only MD5, SHA-1 or SHA-256 algorithm is supported. \r
351 //\r
352 switch (HashSize) {\r
353 case MD5_DIGEST_SIZE:\r
354 DigestType = NID_md5;\r
355 break;\r
356 \r
357 case SHA1_DIGEST_SIZE:\r
358 DigestType = NID_sha1;\r
359 break;\r
360 \r
361 case SHA256_DIGEST_SIZE:\r
362 DigestType = NID_sha256;\r
363 break;\r
532616bb 364\r
86b5c3ee 365 default:\r
532616bb 366 return FALSE;\r
86b5c3ee 367 } \r
368\r
369 return (BOOLEAN) RSA_sign (\r
370 DigestType,\r
371 MessageHash,\r
372 (UINT32) HashSize,\r
373 Signature,\r
374 (UINT32 *) SigSize,\r
375 (RSA *) RsaContext\r
376 );\r
532616bb 377}\r