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