]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsa.c
Add CryptoPkg (from UDK2010.UP3)
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptRsa.c
1 /** @file
2 RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <Library/BaseLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18
19 #include <Library/BaseCryptLib.h>
20 #include <openssl/rsa.h>
21
22
23 /**
24 Allocates and Initializes one RSA Context for subsequent use.
25
26 @return Pointer to the RSA Context that has been initialized.
27 If the allocations fails, RsaNew() returns NULL.
28
29 **/
30 VOID *
31 EFIAPI
32 RsaNew (
33 VOID
34 )
35 {
36 //
37 // Allocates & Initializes RSA Context by OpenSSL RSA_new()
38 //
39 return (VOID *)RSA_new ();
40 }
41
42
43 /**
44 Release the specified RSA Context.
45
46 @param[in] RsaContext Pointer to the RSA context to be released.
47
48 **/
49 VOID
50 EFIAPI
51 RsaFree (
52 IN VOID *RsaContext
53 )
54 {
55 //
56 // Free OpenSSL RSA Context
57 //
58 RSA_free ((RSA *)RsaContext);
59 }
60
61
62 /**
63 Sets the tag-designated RSA key component into the established RSA context from
64 the user-specified nonnegative integer (octet string format represented in RSA
65 PKCS#1).
66
67 If RsaContext is NULL, then ASSERT().
68
69 @param[in, out] RsaContext Pointer to RSA context being set.
70 @param[in] KeyTag Tag of RSA key component being set.
71 @param[in] BigNumber Pointer to octet integer buffer.
72 @param[in] BnLength Length of big number buffer in bytes.
73
74 @return TRUE RSA key component was set successfully.
75 @return FALSE Invalid RSA key component tag.
76
77 **/
78 BOOLEAN
79 EFIAPI
80 RsaSetKey (
81 IN OUT VOID *RsaContext,
82 IN RSA_KEY_TAG KeyTag,
83 IN CONST UINT8 *BigNumber,
84 IN UINTN BnLength
85 )
86 {
87 RSA *RsaKey;
88
89 //
90 // ASSERT if RsaContext is NULL
91 //
92 ASSERT (RsaContext != NULL);
93
94
95 RsaKey = (RSA *)RsaContext;
96 //
97 // Set RSA Key Components by converting octet string to OpenSSL BN representation.
98 // NOTE: For RSA public key (used in signature verification), only public components
99 // (N, e) are needed.
100 //
101 switch (KeyTag) {
102
103 //
104 // RSA Public Modulus (N)
105 //
106 case RsaKeyN:
107 if (RsaKey->n != NULL) {
108 BN_free (RsaKey->n);
109 }
110 RsaKey->n = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->n);
111 break;
112
113 //
114 // RSA Public Exponent (e)
115 //
116 case RsaKeyE:
117 if (RsaKey->e != NULL) {
118 BN_free (RsaKey->e);
119 }
120 RsaKey->e = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->e);
121 break;
122
123 //
124 // RSA Private Exponent (d)
125 //
126 case RsaKeyD:
127 if (RsaKey->d != NULL) {
128 BN_free (RsaKey->d);
129 }
130 RsaKey->d = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->d);
131 break;
132
133 //
134 // RSA Secret Prime Factor of Modulus (p)
135 //
136 case RsaKeyP:
137 if (RsaKey->p != NULL) {
138 BN_free (RsaKey->p);
139 }
140 RsaKey->p = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->p);
141 break;
142
143 //
144 // RSA Secret Prime Factor of Modules (q)
145 //
146 case RsaKeyQ:
147 if (RsaKey->q != NULL) {
148 BN_free (RsaKey->q);
149 }
150 RsaKey->q = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->q);
151 break;
152
153 //
154 // p's CRT Exponent (== d mod (p - 1))
155 //
156 case RsaKeyDp:
157 if (RsaKey->dmp1 != NULL) {
158 BN_free (RsaKey->dmp1);
159 }
160 RsaKey->dmp1 = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->dmp1);
161 break;
162
163 //
164 // q's CRT Exponent (== d mod (q - 1))
165 //
166 case RsaKeyDq:
167 if (RsaKey->dmq1 != NULL) {
168 BN_free (RsaKey->dmq1);
169 }
170 RsaKey->dmq1 = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->dmq1);
171 break;
172
173 //
174 // The CRT Coefficient (== 1/q mod p)
175 //
176 case RsaKeyQInv:
177 if (RsaKey->iqmp != NULL) {
178 BN_free (RsaKey->iqmp);
179 }
180 RsaKey->iqmp = BN_bin2bn (BigNumber, (int)BnLength, RsaKey->iqmp);
181 break;
182
183 default:
184 return FALSE;
185 }
186
187 return TRUE;
188 }
189
190
191 /**
192 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
193 RSA PKCS#1.
194
195 If RsaContext is NULL, then ASSERT().
196 If MessageHash is NULL, then ASSERT().
197 If Signature is NULL, then ASSERT().
198 If HashLength is not equal to the size of MD5, SHA-1 or SHA-256 digest, then ASSERT().
199
200 @param[in] RsaContext Pointer to RSA context for signature verification.
201 @param[in] MessageHash Pointer to octet message hash to be checked.
202 @param[in] HashLength Length of the message hash in bytes.
203 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
204 @param[in] SigLength Length of signature in bytes.
205
206 @return TRUE Valid signature encoded in PKCS1-v1_5.
207 @return FALSE Invalid signature or invalid RSA context.
208
209 **/
210 BOOLEAN
211 EFIAPI
212 RsaPkcs1Verify (
213 IN VOID *RsaContext,
214 IN CONST UINT8 *MessageHash,
215 IN UINTN HashLength,
216 IN UINT8 *Signature,
217 IN UINTN SigLength
218 )
219 {
220 INTN Length;
221
222 //
223 // ASSERT if RsaContext, MessageHash or Signature is NULL
224 //
225 ASSERT (RsaContext != NULL);
226 ASSERT (MessageHash != NULL);
227 ASSERT (Signature != NULL);
228
229 //
230 // ASSERT if unsupported hash length:
231 // Only MD5, SHA-1 or SHA-256 digest size is supported
232 //
233 ASSERT ((HashLength == MD5_DIGEST_SIZE) || (HashLength == SHA1_DIGEST_SIZE) ||
234 (HashLength == SHA256_DIGEST_SIZE));
235
236 //
237 // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key
238 //
239 Length = RSA_public_decrypt (
240 (int)SigLength,
241 Signature,
242 Signature,
243 RsaContext,
244 RSA_PKCS1_PADDING
245 );
246
247 //
248 // Invalid RSA Key or PKCS#1 Padding Checking Failed (if Length < 0)
249 // NOTE: Length should be the addition of HashLength and some DER value.
250 // Ignore more strict length checking here.
251 //
252 if (Length < (INTN) HashLength) {
253 return FALSE;
254 }
255
256 //
257 // Validate the MessageHash and Decoded Signature
258 // NOTE: The decoded Signature should be the DER encoding of the DigestInfo value
259 // DigestInfo ::= SEQUENCE {
260 // digestAlgorithm AlgorithmIdentifier
261 // digest OCTET STRING
262 // }
263 // Then Memory Comparing should skip the DER value of the underlying SEQUENCE
264 // type and AlgorithmIdentifier.
265 //
266 if (CompareMem (MessageHash, Signature + Length - HashLength, HashLength) == 0) {
267 //
268 // Valid RSA PKCS#1 Signature
269 //
270 return TRUE;
271 } else {
272 //
273 // Failed to verification
274 //
275 return FALSE;
276 }
277 }