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