]> git.proxmox.com Git - efi-boot-shim.git/blame - Cryptlib/Pk/CryptRsaBasic.c
New upstream version 15.3
[efi-boot-shim.git] / Cryptlib / Pk / CryptRsaBasic.c
CommitLineData
72bb39c0
SL
1/** @file\r
2 RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
3\r
4 This file implements following APIs which provide basic capabilities for RSA:\r
5 1) RsaNew\r
6 2) RsaFree\r
7 3) RsaSetKey\r
8 4) RsaPkcs1Verify\r
9\r
031e5cce 10Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
72bb39c0
SL
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
62f0afa2 23#include <openssl/bn.h>\r
72bb39c0
SL
24#include <openssl/rsa.h>\r
25#include <openssl/objects.h>\r
26\r
27/**\r
28 Allocates and initializes one RSA context for subsequent use.\r
29\r
30 @return Pointer to the RSA context that has been initialized.\r
31 If the allocations fails, RsaNew() returns NULL.\r
32\r
33**/\r
34VOID *\r
35EFIAPI\r
36RsaNew (\r
37 VOID\r
38 )\r
39{\r
40 //\r
41 // Allocates & Initializes RSA Context by OpenSSL RSA_new()\r
42 //\r
43 return (VOID *) RSA_new ();\r
44}\r
45\r
46/**\r
47 Release the specified RSA context.\r
48\r
49 @param[in] RsaContext Pointer to the RSA context to be released.\r
50\r
51**/\r
52VOID\r
53EFIAPI\r
54RsaFree (\r
55 IN VOID *RsaContext\r
56 )\r
57{\r
58 //\r
59 // Free OpenSSL RSA Context\r
60 //\r
61 RSA_free ((RSA *) RsaContext);\r
62}\r
63\r
64/**\r
65 Sets the tag-designated key component into the established RSA context.\r
66\r
67 This function sets the tag-designated RSA key component into the established\r
68 RSA context from the user-specified non-negative integer (octet string format\r
69 represented in RSA PKCS#1).\r
f4173af1 70 If BigNumber is NULL, then the specified key component in RSA context is cleared.\r
72bb39c0
SL
71\r
72 If RsaContext is NULL, then return FALSE.\r
73\r
74 @param[in, out] RsaContext Pointer to RSA context being set.\r
75 @param[in] KeyTag Tag of RSA key component being set.\r
76 @param[in] BigNumber Pointer to octet integer buffer.\r
f4173af1 77 If NULL, then the specified key component in RSA\r
72bb39c0
SL
78 context is cleared.\r
79 @param[in] BnSize Size of big number buffer in bytes.\r
80 If BigNumber is NULL, then it is ignored.\r
81\r
82 @retval TRUE RSA key component was set successfully.\r
83 @retval FALSE Invalid RSA key component tag.\r
84\r
85**/\r
86BOOLEAN\r
87EFIAPI\r
88RsaSetKey (\r
89 IN OUT VOID *RsaContext,\r
90 IN RSA_KEY_TAG KeyTag,\r
91 IN CONST UINT8 *BigNumber,\r
92 IN UINTN BnSize\r
93 )\r
94{\r
031e5cce 95 RSA *RsaKey;\r
72bb39c0
SL
96\r
97 //\r
98 // Check input parameters.\r
99 //\r
100 if (RsaContext == NULL || BnSize > INT_MAX) {\r
101 return FALSE;\r
102 }\r
103\r
104 RsaKey = (RSA *) RsaContext;\r
105 //\r
106 // Set RSA Key Components by converting octet string to OpenSSL BN representation.\r
107 // NOTE: For RSA public key (used in signature verification), only public components\r
108 // (N, e) are needed.\r
109 //\r
110 switch (KeyTag) {\r
111\r
112 //\r
031e5cce 113 // RSA Public Modulus (N)\r
72bb39c0
SL
114 //\r
115 case RsaKeyN:\r
031e5cce
SM
116 if (RsaKey->n != NULL) {\r
117 BN_free (RsaKey->n);\r
b6f94dbe 118 }\r
031e5cce
SM
119 RsaKey->n = NULL;\r
120 if (BigNumber == NULL) {\r
121 break;\r
b6f94dbe 122 }\r
031e5cce
SM
123 RsaKey->n = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->n);\r
124 if (RsaKey->n == NULL) {\r
125 return FALSE;\r
72bb39c0
SL
126 }\r
127\r
031e5cce
SM
128 break;\r
129\r
130 //\r
131 // RSA Public Exponent (e)\r
132 //\r
133 case RsaKeyE:\r
134 if (RsaKey->e != NULL) {\r
135 BN_free (RsaKey->e);\r
136 }\r
137 RsaKey->e = NULL;\r
138 if (BigNumber == NULL) {\r
139 break;\r
140 }\r
141 RsaKey->e = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->e);\r
142 if (RsaKey->e == NULL) {\r
72bb39c0
SL
143 return FALSE;\r
144 }\r
145\r
031e5cce
SM
146 break;\r
147\r
148 //\r
149 // RSA Private Exponent (d)\r
150 //\r
151 case RsaKeyD:\r
152 if (RsaKey->d != NULL) {\r
153 BN_free (RsaKey->d);\r
154 }\r
155 RsaKey->d = NULL;\r
156 if (BigNumber == NULL) {\r
7bf7a6d0 157 break;\r
72bb39c0 158 }\r
031e5cce
SM
159 RsaKey->d = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->d);\r
160 if (RsaKey->d == NULL) {\r
72bb39c0
SL
161 return FALSE;\r
162 }\r
163\r
b6f94dbe
MTL
164 break;\r
165\r
166 //\r
031e5cce 167 // RSA Secret Prime Factor of Modulus (p)\r
b6f94dbe 168 //\r
7bf7a6d0 169 case RsaKeyP:\r
031e5cce
SM
170 if (RsaKey->p != NULL) {\r
171 BN_free (RsaKey->p);\r
b6f94dbe 172 }\r
031e5cce
SM
173 RsaKey->p = NULL;\r
174 if (BigNumber == NULL) {\r
175 break;\r
72bb39c0 176 }\r
031e5cce
SM
177 RsaKey->p = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->p);\r
178 if (RsaKey->p == NULL) {\r
72bb39c0
SL
179 return FALSE;\r
180 }\r
181\r
031e5cce
SM
182 break;\r
183\r
184 //\r
185 // RSA Secret Prime Factor of Modules (q)\r
186 //\r
187 case RsaKeyQ:\r
188 if (RsaKey->q != NULL) {\r
189 BN_free (RsaKey->q);\r
190 }\r
191 RsaKey->q = NULL;\r
192 if (BigNumber == NULL) {\r
7bf7a6d0 193 break;\r
f4173af1 194 }\r
031e5cce
SM
195 RsaKey->q = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->q);\r
196 if (RsaKey->q == NULL) {\r
72bb39c0
SL
197 return FALSE;\r
198 }\r
199\r
b6f94dbe
MTL
200 break;\r
201\r
202 //\r
031e5cce 203 // p's CRT Exponent (== d mod (p - 1))\r
b6f94dbe 204 //\r
7bf7a6d0 205 case RsaKeyDp:\r
031e5cce
SM
206 if (RsaKey->dmp1 != NULL) {\r
207 BN_free (RsaKey->dmp1);\r
b6f94dbe 208 }\r
031e5cce
SM
209 RsaKey->dmp1 = NULL;\r
210 if (BigNumber == NULL) {\r
211 break;\r
7bf7a6d0 212 }\r
031e5cce
SM
213 RsaKey->dmp1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmp1);\r
214 if (RsaKey->dmp1 == NULL) {\r
f4173af1 215 return FALSE;\r
72bb39c0 216 }\r
b6f94dbe 217\r
031e5cce
SM
218 break;\r
219\r
220 //\r
221 // q's CRT Exponent (== d mod (q - 1))\r
222 //\r
223 case RsaKeyDq:\r
224 if (RsaKey->dmq1 != NULL) {\r
225 BN_free (RsaKey->dmq1);\r
226 }\r
227 RsaKey->dmq1 = NULL;\r
228 if (BigNumber == NULL) {\r
b6f94dbe 229 break;\r
031e5cce
SM
230 }\r
231 RsaKey->dmq1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmq1);\r
232 if (RsaKey->dmq1 == NULL) {\r
7bf7a6d0 233 return FALSE;\r
b6f94dbe 234 }\r
031e5cce
SM
235\r
236 break;\r
237\r
238 //\r
239 // The CRT Coefficient (== 1/q mod p)\r
240 //\r
241 case RsaKeyQInv:\r
242 if (RsaKey->iqmp != NULL) {\r
243 BN_free (RsaKey->iqmp);\r
244 }\r
245 RsaKey->iqmp = NULL;\r
246 if (BigNumber == NULL) {\r
247 break;\r
248 }\r
249 RsaKey->iqmp = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->iqmp);\r
250 if (RsaKey->iqmp == NULL) {\r
72bb39c0
SL
251 return FALSE;\r
252 }\r
253\r
254 break;\r
255\r
256 default:\r
257 return FALSE;\r
258 }\r
259\r
260 return TRUE;\r
261}\r
262\r
263/**\r
264 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
265 RSA PKCS#1.\r
266\r
267 If RsaContext is NULL, then return FALSE.\r
268 If MessageHash is NULL, then return FALSE.\r
269 If Signature is NULL, then return FALSE.\r
270 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.\r
271\r
272 @param[in] RsaContext Pointer to RSA context for signature verification.\r
273 @param[in] MessageHash Pointer to octet message hash to be checked.\r
274 @param[in] HashSize Size of the message hash in bytes.\r
275 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.\r
276 @param[in] SigSize Size of signature in bytes.\r
277\r
278 @retval TRUE Valid signature encoded in PKCS1-v1_5.\r
279 @retval FALSE Invalid signature or invalid RSA context.\r
280\r
281**/\r
282BOOLEAN\r
283EFIAPI\r
284RsaPkcs1Verify (\r
285 IN VOID *RsaContext,\r
286 IN CONST UINT8 *MessageHash,\r
287 IN UINTN HashSize,\r
288 IN CONST UINT8 *Signature,\r
289 IN UINTN SigSize\r
290 )\r
291{\r
292 INT32 DigestType;\r
293 UINT8 *SigBuf;\r
294\r
295 //\r
296 // Check input parameters.\r
297 //\r
298 if (RsaContext == NULL || MessageHash == NULL || Signature == NULL) {\r
299 return FALSE;\r
300 }\r
301\r
302 if (SigSize > INT_MAX || SigSize == 0) {\r
303 return FALSE;\r
304 }\r
305\r
306 //\r
307 // Determine the message digest algorithm according to digest size.\r
308 // Only MD5, SHA-1 or SHA-256 algorithm is supported. \r
309 //\r
310 switch (HashSize) {\r
311 case MD5_DIGEST_SIZE:\r
312 DigestType = NID_md5;\r
313 break;\r
031e5cce 314 \r
72bb39c0
SL
315 case SHA1_DIGEST_SIZE:\r
316 DigestType = NID_sha1;\r
317 break;\r
031e5cce 318 \r
72bb39c0
SL
319 case SHA256_DIGEST_SIZE:\r
320 DigestType = NID_sha256;\r
321 break;\r
322\r
323 default:\r
324 return FALSE;\r
325 }\r
326\r
327 SigBuf = (UINT8 *) Signature;\r
328 return (BOOLEAN) RSA_verify (\r
329 DigestType,\r
330 MessageHash,\r
331 (UINT32) HashSize,\r
332 SigBuf,\r
333 (UINT32) SigSize,\r
334 (RSA *) RsaContext\r
335 );\r
336}\r