]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaBasic.c
CorebootPayloadPkg: Replace BSD License with BSD+Patent License
[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
630f67dd 10Copyright (c) 2009 - 2018, 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
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
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
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
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
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
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
f56b11d2
QL
95 RSA *RsaKey;\r
96 BIGNUM *BnN;\r
97 BIGNUM *BnE;\r
98 BIGNUM *BnD;\r
99 BIGNUM *BnP;\r
100 BIGNUM *BnQ;\r
101 BIGNUM *BnDp;\r
102 BIGNUM *BnDq;\r
103 BIGNUM *BnQInv;\r
532616bb 104\r
105 //\r
106 // Check input parameters.\r
107 //\r
dda39f3a 108 if (RsaContext == NULL || BnSize > INT_MAX) {\r
532616bb 109 return FALSE;\r
110 }\r
111\r
f56b11d2
QL
112 BnN = NULL;\r
113 BnE = NULL;\r
114 BnD = NULL;\r
115 BnP = NULL;\r
116 BnQ = NULL;\r
117 BnDp = NULL;\r
118 BnDq = NULL;\r
119 BnQInv = NULL;\r
120\r
121 //\r
122 // Retrieve the components from RSA object.\r
123 //\r
6b8ebcb8 124 RsaKey = (RSA *) RsaContext;\r
f56b11d2
QL
125 RSA_get0_key (RsaKey, (const BIGNUM **)&BnN, (const BIGNUM **)&BnE, (const BIGNUM **)&BnD);\r
126 RSA_get0_factors (RsaKey, (const BIGNUM **)&BnP, (const BIGNUM **)&BnQ);\r
127 RSA_get0_crt_params (RsaKey, (const BIGNUM **)&BnDp, (const BIGNUM **)&BnDq, (const BIGNUM **)&BnQInv);\r
128\r
532616bb 129 //\r
130 // Set RSA Key Components by converting octet string to OpenSSL BN representation.\r
131 // NOTE: For RSA public key (used in signature verification), only public components\r
132 // (N, e) are needed.\r
133 //\r
134 switch (KeyTag) {\r
135\r
136 //\r
f56b11d2 137 // RSA Public Modulus (N), Public Exponent (e) and Private Exponent (d)\r
532616bb 138 //\r
139 case RsaKeyN:\r
532616bb 140 case RsaKeyE:\r
f56b11d2
QL
141 case RsaKeyD:\r
142 if (BnN == NULL) {\r
143 BnN = BN_new ();\r
532616bb 144 }\r
f56b11d2
QL
145 if (BnE == NULL) {\r
146 BnE = BN_new ();\r
532616bb 147 }\r
f56b11d2
QL
148 if (BnD == NULL) {\r
149 BnD = BN_new ();\r
dda39f3a 150 }\r
151\r
f56b11d2 152 if ((BnN == NULL) || (BnE == NULL) || (BnD == NULL)) {\r
dda39f3a 153 return FALSE;\r
154 }\r
155\r
f56b11d2
QL
156 switch (KeyTag) {\r
157 case RsaKeyN:\r
158 BnN = BN_bin2bn (BigNumber, (UINT32)BnSize, BnN);\r
159 break;\r
160 case RsaKeyE:\r
161 BnE = BN_bin2bn (BigNumber, (UINT32)BnSize, BnE);\r
532616bb 162 break;\r
f56b11d2
QL
163 case RsaKeyD:\r
164 BnD = BN_bin2bn (BigNumber, (UINT32)BnSize, BnD);\r
165 break;\r
166 default:\r
167 return FALSE;\r
532616bb 168 }\r
f56b11d2 169 if (RSA_set0_key (RsaKey, BN_dup(BnN), BN_dup(BnE), BN_dup(BnD)) == 0) {\r
dda39f3a 170 return FALSE;\r
171 }\r
172\r
532616bb 173 break;\r
174\r
175 //\r
f56b11d2 176 // RSA Secret Prime Factor of Modulus (p and q)\r
532616bb 177 //\r
f56b11d2 178 case RsaKeyP:\r
532616bb 179 case RsaKeyQ:\r
f56b11d2
QL
180 if (BnP == NULL) {\r
181 BnP = BN_new ();\r
532616bb 182 }\r
f56b11d2
QL
183 if (BnQ == NULL) {\r
184 BnQ = BN_new ();\r
532616bb 185 }\r
f56b11d2 186 if ((BnP == NULL) || (BnQ == NULL)) {\r
dda39f3a 187 return FALSE;\r
188 }\r
189\r
f56b11d2
QL
190 switch (KeyTag) {\r
191 case RsaKeyP:\r
192 BnP = BN_bin2bn (BigNumber, (UINT32)BnSize, BnP);\r
532616bb 193 break;\r
f56b11d2
QL
194 case RsaKeyQ:\r
195 BnQ = BN_bin2bn (BigNumber, (UINT32)BnSize, BnQ);\r
196 break;\r
197 default:\r
198 return FALSE;\r
532616bb 199 }\r
f56b11d2 200 if (RSA_set0_factors (RsaKey, BN_dup(BnP), BN_dup(BnQ)) == 0) {\r
dda39f3a 201 return FALSE;\r
202 }\r
203\r
532616bb 204 break;\r
205\r
206 //\r
f56b11d2
QL
207 // p's CRT Exponent (== d mod (p - 1)), q's CRT Exponent (== d mod (q - 1)),\r
208 // and CRT Coefficient (== 1/q mod p)\r
532616bb 209 //\r
f56b11d2 210 case RsaKeyDp:\r
532616bb 211 case RsaKeyDq:\r
f56b11d2
QL
212 case RsaKeyQInv:\r
213 if (BnDp == NULL) {\r
214 BnDp = BN_new ();\r
532616bb 215 }\r
f56b11d2
QL
216 if (BnDq == NULL) {\r
217 BnDq = BN_new ();\r
532616bb 218 }\r
f56b11d2
QL
219 if (BnQInv == NULL) {\r
220 BnQInv = BN_new ();\r
221 }\r
222 if ((BnDp == NULL) || (BnDq == NULL) || (BnQInv == NULL)) {\r
dda39f3a 223 return FALSE;\r
224 }\r
225\r
f56b11d2
QL
226 switch (KeyTag) {\r
227 case RsaKeyDp:\r
228 BnDp = BN_bin2bn (BigNumber, (UINT32)BnSize, BnDp);\r
229 break;\r
230 case RsaKeyDq:\r
231 BnDq = BN_bin2bn (BigNumber, (UINT32)BnSize, BnDq);\r
232 break;\r
233 case RsaKeyQInv:\r
234 BnQInv = BN_bin2bn (BigNumber, (UINT32)BnSize, BnQInv);\r
532616bb 235 break;\r
f56b11d2
QL
236 default:\r
237 return FALSE;\r
532616bb 238 }\r
f56b11d2 239 if (RSA_set0_crt_params (RsaKey, BN_dup(BnDp), BN_dup(BnDq), BN_dup(BnQInv)) == 0) {\r
dda39f3a 240 return FALSE;\r
241 }\r
242\r
532616bb 243 break;\r
244\r
245 default:\r
246 return FALSE;\r
247 }\r
248\r
249 return TRUE;\r
250}\r
251\r
252/**\r
253 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
254 RSA PKCS#1.\r
255\r
256 If RsaContext is NULL, then return FALSE.\r
257 If MessageHash is NULL, then return FALSE.\r
258 If Signature is NULL, then return FALSE.\r
259 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.\r
260\r
261 @param[in] RsaContext Pointer to RSA context for signature verification.\r
262 @param[in] MessageHash Pointer to octet message hash to be checked.\r
263 @param[in] HashSize Size of the message hash in bytes.\r
264 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.\r
265 @param[in] SigSize Size of signature in bytes.\r
266\r
267 @retval TRUE Valid signature encoded in PKCS1-v1_5.\r
268 @retval FALSE Invalid signature or invalid RSA context.\r
269\r
270**/\r
271BOOLEAN\r
272EFIAPI\r
273RsaPkcs1Verify (\r
274 IN VOID *RsaContext,\r
275 IN CONST UINT8 *MessageHash,\r
276 IN UINTN HashSize,\r
8c5720b4 277 IN CONST UINT8 *Signature,\r
532616bb 278 IN UINTN SigSize\r
279 )\r
280{\r
86b5c3ee 281 INT32 DigestType;\r
282 UINT8 *SigBuf;\r
532616bb 283\r
284 //\r
285 // Check input parameters.\r
286 //\r
8c5720b4 287 if (RsaContext == NULL || MessageHash == NULL || Signature == NULL) {\r
532616bb 288 return FALSE;\r
289 }\r
290\r
8c5720b4 291 if (SigSize > INT_MAX || SigSize == 0) {\r
292 return FALSE;\r
293 }\r
8c5720b4 294\r
532616bb 295 //\r
86b5c3ee 296 // Determine the message digest algorithm according to digest size.\r
630f67dd 297 // Only MD5, SHA-1 or SHA-256 algorithm is supported.\r
532616bb 298 //\r
86b5c3ee 299 switch (HashSize) {\r
300 case MD5_DIGEST_SIZE:\r
301 DigestType = NID_md5;\r
302 break;\r
f56b11d2 303\r
86b5c3ee 304 case SHA1_DIGEST_SIZE:\r
305 DigestType = NID_sha1;\r
306 break;\r
f56b11d2 307\r
86b5c3ee 308 case SHA256_DIGEST_SIZE:\r
309 DigestType = NID_sha256;\r
310 break;\r
532616bb 311\r
86b5c3ee 312 default:\r
532616bb 313 return FALSE;\r
314 }\r
315\r
86b5c3ee 316 SigBuf = (UINT8 *) Signature;\r
317 return (BOOLEAN) RSA_verify (\r
318 DigestType,\r
319 MessageHash,\r
320 (UINT32) HashSize,\r
321 SigBuf,\r
322 (UINT32) SigSize,\r
323 (RSA *) RsaContext\r
324 );\r
532616bb 325}\r