]>
Commit | Line | Data |
---|---|---|
532616bb | 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 | |
1cae0c83 | 10 | Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r |
532616bb | 11 | This program and the accompanying materials\r |
12 | are licensed and made available under the terms and conditions of the BSD License\r | |
13 | which accompanies this distribution. The full text of the license may be found at\r | |
14 | http://opensource.org/licenses/bsd-license.php\r | |
15 | \r | |
16 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
17 | WITHOUT 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 |
86b5c3ee | 25 | #include <openssl/objects.h>\r |
532616bb | 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 | |
34 | VOID *\r | |
35 | EFIAPI\r | |
36 | RsaNew (\r | |
37 | VOID\r | |
38 | )\r | |
39 | {\r | |
40 | //\r | |
41 | // Allocates & Initializes RSA Context by OpenSSL RSA_new()\r | |
42 | //\r | |
6b8ebcb8 | 43 | return (VOID *) RSA_new ();\r |
532616bb | 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 | |
52 | VOID\r | |
53 | EFIAPI\r | |
54 | RsaFree (\r | |
55 | IN VOID *RsaContext\r | |
56 | )\r | |
57 | {\r | |
58 | //\r | |
59 | // Free OpenSSL RSA Context\r | |
60 | //\r | |
6b8ebcb8 | 61 | RSA_free ((RSA *) RsaContext);\r |
532616bb | 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 | |
2998af86 | 70 | If BigNumber is NULL, then the specified key component in RSA context is cleared.\r |
532616bb | 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 | |
2998af86 | 77 | If NULL, then the specified key component in RSA\r |
532616bb | 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 | |
86 | BOOLEAN\r | |
87 | EFIAPI\r | |
88 | RsaSetKey (\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 | |
95 | RSA *RsaKey;\r | |
96 | \r | |
97 | //\r | |
98 | // Check input parameters.\r | |
99 | //\r | |
dda39f3a | 100 | if (RsaContext == NULL || BnSize > INT_MAX) {\r |
532616bb | 101 | return FALSE;\r |
102 | }\r | |
103 | \r | |
6b8ebcb8 | 104 | RsaKey = (RSA *) RsaContext;\r |
532616bb | 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 | |
113 | // RSA Public Modulus (N)\r | |
114 | //\r | |
115 | case RsaKeyN:\r | |
116 | if (RsaKey->n != NULL) {\r | |
117 | BN_free (RsaKey->n);\r | |
118 | }\r | |
119 | RsaKey->n = NULL;\r | |
120 | if (BigNumber == NULL) {\r | |
121 | break;\r | |
122 | }\r | |
123 | RsaKey->n = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->n);\r | |
dda39f3a | 124 | if (RsaKey->n == NULL) {\r |
125 | return FALSE;\r | |
126 | }\r | |
127 | \r | |
532616bb | 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 | |
dda39f3a | 142 | if (RsaKey->e == NULL) {\r |
143 | return FALSE;\r | |
144 | }\r | |
145 | \r | |
532616bb | 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 | |
157 | break;\r | |
158 | }\r | |
159 | RsaKey->d = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->d);\r | |
dda39f3a | 160 | if (RsaKey->d == NULL) {\r |
161 | return FALSE;\r | |
162 | }\r | |
163 | \r | |
532616bb | 164 | break;\r |
165 | \r | |
166 | //\r | |
167 | // RSA Secret Prime Factor of Modulus (p)\r | |
168 | //\r | |
169 | case RsaKeyP:\r | |
170 | if (RsaKey->p != NULL) {\r | |
171 | BN_free (RsaKey->p);\r | |
172 | }\r | |
173 | RsaKey->p = NULL;\r | |
174 | if (BigNumber == NULL) {\r | |
175 | break;\r | |
176 | }\r | |
177 | RsaKey->p = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->p);\r | |
dda39f3a | 178 | if (RsaKey->p == NULL) {\r |
179 | return FALSE;\r | |
180 | }\r | |
181 | \r | |
532616bb | 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 | |
193 | break;\r | |
194 | }\r | |
195 | RsaKey->q = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->q);\r | |
dda39f3a | 196 | if (RsaKey->q == NULL) {\r |
197 | return FALSE;\r | |
198 | }\r | |
199 | \r | |
532616bb | 200 | break;\r |
201 | \r | |
202 | //\r | |
203 | // p's CRT Exponent (== d mod (p - 1))\r | |
204 | //\r | |
205 | case RsaKeyDp:\r | |
206 | if (RsaKey->dmp1 != NULL) {\r | |
207 | BN_free (RsaKey->dmp1);\r | |
208 | }\r | |
209 | RsaKey->dmp1 = NULL;\r | |
210 | if (BigNumber == NULL) {\r | |
211 | break;\r | |
212 | }\r | |
213 | RsaKey->dmp1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmp1);\r | |
dda39f3a | 214 | if (RsaKey->dmp1 == NULL) {\r |
215 | return FALSE;\r | |
216 | }\r | |
217 | \r | |
532616bb | 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 | |
229 | break;\r | |
230 | }\r | |
231 | RsaKey->dmq1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmq1);\r | |
dda39f3a | 232 | if (RsaKey->dmq1 == NULL) {\r |
233 | return FALSE;\r | |
234 | }\r | |
235 | \r | |
532616bb | 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 | |
dda39f3a | 250 | if (RsaKey->iqmp == NULL) {\r |
251 | return FALSE;\r | |
252 | }\r | |
253 | \r | |
532616bb | 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 | |
282 | BOOLEAN\r | |
283 | EFIAPI\r | |
284 | RsaPkcs1Verify (\r | |
285 | IN VOID *RsaContext,\r | |
286 | IN CONST UINT8 *MessageHash,\r | |
287 | IN UINTN HashSize,\r | |
8c5720b4 | 288 | IN CONST UINT8 *Signature,\r |
532616bb | 289 | IN UINTN SigSize\r |
290 | )\r | |
291 | {\r | |
86b5c3ee | 292 | INT32 DigestType;\r |
293 | UINT8 *SigBuf;\r | |
532616bb | 294 | \r |
295 | //\r | |
296 | // Check input parameters.\r | |
297 | //\r | |
8c5720b4 | 298 | if (RsaContext == NULL || MessageHash == NULL || Signature == NULL) {\r |
532616bb | 299 | return FALSE;\r |
300 | }\r | |
301 | \r | |
8c5720b4 | 302 | if (SigSize > INT_MAX || SigSize == 0) {\r |
303 | return FALSE;\r | |
304 | }\r | |
8c5720b4 | 305 | \r |
532616bb | 306 | //\r |
86b5c3ee | 307 | // Determine the message digest algorithm according to digest size.\r |
308 | // Only MD5, SHA-1 or SHA-256 algorithm is supported. \r | |
532616bb | 309 | //\r |
86b5c3ee | 310 | switch (HashSize) {\r |
311 | case MD5_DIGEST_SIZE:\r | |
312 | DigestType = NID_md5;\r | |
313 | break;\r | |
314 | \r | |
315 | case SHA1_DIGEST_SIZE:\r | |
316 | DigestType = NID_sha1;\r | |
317 | break;\r | |
318 | \r | |
319 | case SHA256_DIGEST_SIZE:\r | |
320 | DigestType = NID_sha256;\r | |
321 | break;\r | |
532616bb | 322 | \r |
86b5c3ee | 323 | default:\r |
532616bb | 324 | return FALSE;\r |
325 | }\r | |
326 | \r | |
86b5c3ee | 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 | |
532616bb | 336 | }\r |