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