]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
Close the corresponding GUIDed section extraction protocol notify event in CloseSecti...
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptRsaBasic.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 basic capabilities for RSA:\r
5 1) RsaNew\r
6 2) RsaFree\r
7 3) RsaSetKey\r
8 4) RsaPkcs1Verify\r
9\r
10Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
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
25\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
70 If BigNumber is NULL, then the specified key componenet in RSA context is cleared.\r
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
77 If NULL, then the specified key componenet in RSA\r
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
95 RSA *RsaKey;\r
96\r
97 //\r
98 // Check input parameters.\r
99 //\r
100 if (RsaContext == NULL) {\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
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
124 break;\r
125\r
126 //\r
127 // RSA Public Exponent (e)\r
128 //\r
129 case RsaKeyE:\r
130 if (RsaKey->e != NULL) {\r
131 BN_free (RsaKey->e);\r
132 }\r
133 RsaKey->e = NULL;\r
134 if (BigNumber == NULL) {\r
135 break;\r
136 }\r
137 RsaKey->e = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->e);\r
138 break;\r
139\r
140 //\r
141 // RSA Private Exponent (d)\r
142 //\r
143 case RsaKeyD:\r
144 if (RsaKey->d != NULL) {\r
145 BN_free (RsaKey->d);\r
146 }\r
147 RsaKey->d = NULL;\r
148 if (BigNumber == NULL) {\r
149 break;\r
150 }\r
151 RsaKey->d = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->d);\r
152 break;\r
153\r
154 //\r
155 // RSA Secret Prime Factor of Modulus (p)\r
156 //\r
157 case RsaKeyP:\r
158 if (RsaKey->p != NULL) {\r
159 BN_free (RsaKey->p);\r
160 }\r
161 RsaKey->p = NULL;\r
162 if (BigNumber == NULL) {\r
163 break;\r
164 }\r
165 RsaKey->p = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->p);\r
166 break;\r
167\r
168 //\r
169 // RSA Secret Prime Factor of Modules (q)\r
170 //\r
171 case RsaKeyQ:\r
172 if (RsaKey->q != NULL) {\r
173 BN_free (RsaKey->q);\r
174 }\r
175 RsaKey->q = NULL;\r
176 if (BigNumber == NULL) {\r
177 break;\r
178 }\r
179 RsaKey->q = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->q);\r
180 break;\r
181\r
182 //\r
183 // p's CRT Exponent (== d mod (p - 1))\r
184 //\r
185 case RsaKeyDp:\r
186 if (RsaKey->dmp1 != NULL) {\r
187 BN_free (RsaKey->dmp1);\r
188 }\r
189 RsaKey->dmp1 = NULL;\r
190 if (BigNumber == NULL) {\r
191 break;\r
192 }\r
193 RsaKey->dmp1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmp1);\r
194 break;\r
195\r
196 //\r
197 // q's CRT Exponent (== d mod (q - 1))\r
198 //\r
199 case RsaKeyDq:\r
200 if (RsaKey->dmq1 != NULL) {\r
201 BN_free (RsaKey->dmq1);\r
202 }\r
203 RsaKey->dmq1 = NULL;\r
204 if (BigNumber == NULL) {\r
205 break;\r
206 }\r
207 RsaKey->dmq1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmq1);\r
208 break;\r
209\r
210 //\r
211 // The CRT Coefficient (== 1/q mod p)\r
212 //\r
213 case RsaKeyQInv:\r
214 if (RsaKey->iqmp != NULL) {\r
215 BN_free (RsaKey->iqmp);\r
216 }\r
217 RsaKey->iqmp = NULL;\r
218 if (BigNumber == NULL) {\r
219 break;\r
220 }\r
221 RsaKey->iqmp = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->iqmp);\r
222 break;\r
223\r
224 default:\r
225 return FALSE;\r
226 }\r
227\r
228 return TRUE;\r
229}\r
230\r
231/**\r
232 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
233 RSA PKCS#1.\r
234\r
235 If RsaContext is NULL, then return FALSE.\r
236 If MessageHash is NULL, then return FALSE.\r
237 If Signature is NULL, then return FALSE.\r
238 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.\r
239\r
240 @param[in] RsaContext Pointer to RSA context for signature verification.\r
241 @param[in] MessageHash Pointer to octet message hash to be checked.\r
242 @param[in] HashSize Size of the message hash in bytes.\r
243 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.\r
244 @param[in] SigSize Size of signature in bytes.\r
245\r
246 @retval TRUE Valid signature encoded in PKCS1-v1_5.\r
247 @retval FALSE Invalid signature or invalid RSA context.\r
248\r
249**/\r
250BOOLEAN\r
251EFIAPI\r
252RsaPkcs1Verify (\r
253 IN VOID *RsaContext,\r
254 IN CONST UINT8 *MessageHash,\r
255 IN UINTN HashSize,\r
256 IN UINT8 *Signature,\r
257 IN UINTN SigSize\r
258 )\r
259{\r
260 INTN Length;\r
261\r
262 //\r
263 // Check input parameters.\r
264 //\r
265 if (RsaContext == NULL || MessageHash == NULL || Signature == NULL) {\r
266 return FALSE;\r
267 }\r
268\r
269 \r
270 //\r
271 // Check for unsupported hash size:\r
272 // Only MD5, SHA-1 or SHA-256 digest size is supported\r
273 //\r
274 if (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE) {\r
275 return FALSE;\r
276 }\r
277 \r
278 //\r
279 // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key\r
280 //\r
281 Length = RSA_public_decrypt (\r
282 (UINT32) SigSize,\r
283 Signature,\r
284 Signature,\r
285 RsaContext,\r
286 RSA_PKCS1_PADDING\r
287 );\r
288\r
289 //\r
290 // Invalid RSA Key or PKCS#1 Padding Checking Failed (if Length < 0)\r
291 // NOTE: Length should be the addition of HashSize and some DER value.\r
292 // Ignore more strict length checking here.\r
293 //\r
294 if (Length < (INTN) HashSize) {\r
295 return FALSE;\r
296 }\r
297\r
298 //\r
299 // Validate the MessageHash and Decoded Signature\r
300 // NOTE: The decoded Signature should be the DER encoding of the DigestInfo value\r
301 // DigestInfo ::= SEQUENCE {\r
302 // digestAlgorithm AlgorithmIdentifier\r
303 // digest OCTET STRING\r
304 // }\r
305 // Then Memory Comparing should skip the DER value of the underlying SEQUENCE\r
306 // type and AlgorithmIdentifier.\r
307 //\r
308 if (CompareMem (MessageHash, Signature + Length - HashSize, HashSize) == 0) {\r
309 //\r
310 // Valid RSA PKCS#1 Signature\r
311 //\r
312 return TRUE;\r
313 } else {\r
314 //\r
315 // Failed to verification\r
316 //\r
317 return FALSE;\r
318 }\r
319}\r