]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLibRuntimeCryptProtocol/RuntimeDxeIpfCryptLib.c
Fix issue that RsaPkcs1Verify() may not work in PEI phase.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLibRuntimeCryptProtocol / RuntimeDxeIpfCryptLib.c
1 /** @file
2 Implementation of The runtime cryptographic library instance (for IPF).
3
4 Copyright (c) 2010 - 2012, 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 <Uefi.h>
16
17 #include <Library/BaseLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/UefiRuntimeLib.h>
21
22 #include <Protocol/RuntimeCrypt.h>
23
24 #include <Guid/EventGroup.h>
25
26 EFI_RUNTIME_CRYPT_PROTOCOL *mCryptProtocol = NULL;
27 EFI_EVENT mIpfCryptLibVirtualNotifyEvent;
28
29 /**
30 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, which converts
31 pointer to new virtual address.
32
33 @param Event Event whose notification function is being invoked.
34 @param Context Pointer to the notification function's context
35
36 **/
37 VOID
38 EFIAPI
39 IpfCryptLibAddressChangeEvent (
40 IN EFI_EVENT Event,
41 IN VOID *Context
42 )
43 {
44 //
45 // Convert Address of Runtime Crypto Protocol.
46 //
47 EfiConvertPointer (0x0, (VOID **) &mCryptProtocol);
48 }
49
50 /**
51 Constructor of IPF Crypto Library Instance.
52 This function locates the Runtime Crypt Protocol and register notification
53 function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
54
55 @param ImageHandle The firmware allocated handle for the EFI image.
56 @param SystemTable A pointer to the EFI System Table.
57
58 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
59
60 **/
61 EFI_STATUS
62 EFIAPI
63 RuntimeDxeIpfCryptLibConstructor (
64 IN EFI_HANDLE ImageHandle,
65 IN EFI_SYSTEM_TABLE *SystemTable
66 )
67 {
68 EFI_STATUS Status;
69
70 //
71 // Locate Runtime Crypt Protocol Instance
72 //
73 Status = gBS->LocateProtocol (
74 &gEfiRuntimeCryptProtocolGuid,
75 NULL,
76 (VOID**) &mCryptProtocol
77 );
78 ASSERT_EFI_ERROR (Status);
79 ASSERT (mCryptProtocol != NULL);
80
81 //
82 // Register SetVirtualAddressMap () notify function
83 //
84 Status = gBS->CreateEventEx (
85 EVT_NOTIFY_SIGNAL,
86 TPL_NOTIFY,
87 IpfCryptLibAddressChangeEvent,
88 NULL,
89 &gEfiEventVirtualAddressChangeGuid,
90 &mIpfCryptLibVirtualNotifyEvent
91 );
92 ASSERT_EFI_ERROR (Status);
93
94 return Status;
95 }
96
97 /**
98 Destructor of IPF Crypto Library Instance.
99
100 @param ImageHandle The firmware allocated handle for the EFI image.
101 @param SystemTable A pointer to the EFI System Table.
102
103 @retval EFI_SUCCESS The destructor completed successfully.
104 @retval Other value The destructor did not complete successfully.
105
106 **/
107 EFI_STATUS
108 EFIAPI
109 RuntimeDxeIpfCryptLibDestructor (
110 IN EFI_HANDLE ImageHandle,
111 IN EFI_SYSTEM_TABLE *SystemTable
112 )
113 {
114 EFI_STATUS Status;
115
116 //
117 // Close the Set Virtual Address Map event
118 //
119 Status = gBS->CloseEvent (mIpfCryptLibVirtualNotifyEvent);
120 ASSERT_EFI_ERROR (Status);
121
122 return Status;
123 }
124
125 /**
126 Check whether crypto service provided by Runtime Crypt protocol is ready to use.
127
128 Crypto service is available if the call is in physical mode prior to
129 SetVirtualAddressMap() or virtual mode after SetVirtualAddressMap(). If either
130 of these two conditions are met, this routine will return TRUE; if neither of
131 these conditions are met, this routine will return FALSE.
132
133 @retval TRUE The Crypto service is ready to use.
134 @retval FALSE The Crypto service is not available.
135
136 **/
137 BOOLEAN
138 EFIAPI
139 InternalIsCryptServiveAvailable (
140 VOID
141 )
142 {
143 INT64 CpuMode;
144 BOOLEAN GoneVirtual;
145
146 CpuMode = AsmCpuVirtual();
147 if (CpuMode < 0) {
148 //
149 // CPU is in mixed mode, return failing the operation gracefully.
150 //
151 return FALSE;
152 }
153
154 GoneVirtual = EfiGoneVirtual();
155
156 if ((CpuMode > 0) && !GoneVirtual) {
157 //
158 // CPU is in virtual mode, but SetVirtualAddressMap() has not been called,
159 // so return failing the operation gracefully.
160 //
161 return FALSE;
162 }
163
164 if ((CpuMode == 0) && GoneVirtual) {
165 //
166 // CPU is in physical mode, but SetVirtualAddressMap() has been called,
167 // so return failing the operation gracefully.
168 //
169 return FALSE;
170 }
171
172 return TRUE;
173 }
174
175 /**
176 Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.
177
178 @return The size, in bytes, of the context buffer required for SHA-256 operations.
179
180 **/
181 UINTN
182 EFIAPI
183 Sha256GetContextSize (
184 VOID
185 )
186 {
187 if (!InternalIsCryptServiveAvailable ()) {
188 return 0;
189 }
190
191 return mCryptProtocol->Sha256GetContextSize ();
192 }
193
194 /**
195 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
196 subsequent use.
197
198 If Sha256Context is NULL, then return FALSE.
199
200 @param[in, out] Sha256Context Pointer to SHA-256 Context being initialized.
201
202 @retval TRUE SHA-256 context initialization succeeded.
203 @retval FALSE SHA-256 context initialization failed.
204
205 **/
206 BOOLEAN
207 EFIAPI
208 Sha256Init (
209 IN OUT VOID *Sha256Context
210 )
211 {
212 if (!InternalIsCryptServiveAvailable ()) {
213 return FALSE;
214 }
215
216 return mCryptProtocol->Sha256Init (Sha256Context);
217 }
218
219
220 /**
221 Makes a copy of an existing SHA-256 context.
222
223 Return FALSE to indicate this interface is not supported.
224
225 @param[in] Sha256Context Pointer to SHA-256 context being copied.
226 @param[out] NewSha256Context Pointer to new SHA-256 context.
227
228 @retval FALSE This interface is not supported.
229
230 **/
231 BOOLEAN
232 EFIAPI
233 Sha256Duplicate (
234 IN CONST VOID *Sha256Context,
235 OUT VOID *NewSha256Context
236 )
237 {
238 ASSERT (FALSE);
239 return FALSE;
240 }
241
242
243 /**
244 Performs SHA-256 digest on a data buffer of the specified length. This function can
245 be called multiple times to compute the digest of long or discontinuous data streams.
246
247 If Sha256Context is NULL, then return FALSE.
248
249 @param[in, out] Sha256Context Pointer to the SHA-256 context.
250 @param[in] Data Pointer to the buffer containing the data to be hashed.
251 @param[in] DataLength Length of Data buffer in bytes.
252
253 @retval TRUE SHA-256 data digest succeeded.
254 @retval FALSE Invalid SHA-256 context. After Sha256Final function has been called, the
255 SHA-256 context cannot be reused.
256
257 **/
258 BOOLEAN
259 EFIAPI
260 Sha256Update (
261 IN OUT VOID *Sha256Context,
262 IN CONST VOID *Data,
263 IN UINTN DataLength
264 )
265 {
266 if (!InternalIsCryptServiveAvailable ()) {
267 return FALSE;
268 }
269
270 return mCryptProtocol->Sha256Update (Sha256Context, Data, DataLength);
271 }
272
273 /**
274 Completes SHA-256 hash computation and retrieves the digest value into the specified
275 memory. After this function has been called, the SHA-256 context cannot be used again.
276
277 If Sha256Context is NULL, then return FALSE.
278 If HashValue is NULL, then return FALSE.
279
280 @param[in, out] Sha256Context Pointer to SHA-256 context
281 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
282 value (32 bytes).
283
284 @retval TRUE SHA-256 digest computation succeeded.
285 @retval FALSE SHA-256 digest computation failed.
286
287 **/
288 BOOLEAN
289 EFIAPI
290 Sha256Final (
291 IN OUT VOID *Sha256Context,
292 OUT UINT8 *HashValue
293 )
294 {
295 if (!InternalIsCryptServiveAvailable ()) {
296 return FALSE;
297 }
298
299 return mCryptProtocol->Sha256Final (Sha256Context, HashValue);
300 }
301
302 /**
303 Allocates and initializes one RSA context for subsequent use.
304
305 @return Pointer to the RSA context that has been initialized.
306 If the allocations fails, RsaNew() returns NULL.
307
308 **/
309 VOID *
310 EFIAPI
311 RsaNew (
312 VOID
313 )
314 {
315 if (!InternalIsCryptServiveAvailable ()) {
316 return FALSE;
317 }
318
319 return mCryptProtocol->RsaNew ();
320 }
321
322 /**
323 Release the specified RSA context.
324
325 @param[in] RsaContext Pointer to the RSA context to be released.
326
327 **/
328 VOID
329 EFIAPI
330 RsaFree (
331 IN VOID *RsaContext
332 )
333 {
334 if (!InternalIsCryptServiveAvailable ()) {
335 return;
336 }
337
338 mCryptProtocol->RsaFree (RsaContext);
339 }
340
341 /**
342 Sets the tag-designated key component into the established RSA context.
343
344 This function sets the tag-designated RSA key component into the established
345 RSA context from the user-specified non-negative integer (octet string format
346 represented in RSA PKCS#1).
347 If BigNumber is NULL, then the specified key componenet in RSA context is cleared.
348
349 If RsaContext is NULL, then return FALSE.
350
351 @param[in, out] RsaContext Pointer to RSA context being set.
352 @param[in] KeyTag Tag of RSA key component being set.
353 @param[in] BigNumber Pointer to octet integer buffer.
354 If NULL, then the specified key componenet in RSA
355 context is cleared.
356 @param[in] BnSize Size of big number buffer in bytes.
357 If BigNumber is NULL, then it is ignored.
358
359 @retval TRUE RSA key component was set successfully.
360 @retval FALSE Invalid RSA key component tag.
361
362 **/
363 BOOLEAN
364 EFIAPI
365 RsaSetKey (
366 IN OUT VOID *RsaContext,
367 IN RSA_KEY_TAG KeyTag,
368 IN CONST UINT8 *BigNumber,
369 IN UINTN BnSize
370 )
371 {
372 if (!InternalIsCryptServiveAvailable ()) {
373 return FALSE;
374 }
375
376 return mCryptProtocol->RsaSetKey (RsaContext, KeyTag, BigNumber, BnSize);
377 }
378
379 /**
380 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
381 RSA PKCS#1.
382
383 If RsaContext is NULL, then return FALSE.
384 If MessageHash is NULL, then return FALSE.
385 If Signature is NULL, then return FALSE.
386 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
387
388 @param[in] RsaContext Pointer to RSA context for signature verification.
389 @param[in] MessageHash Pointer to octet message hash to be checked.
390 @param[in] HashSize Size of the message hash in bytes.
391 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
392 @param[in] SigSize Size of signature in bytes.
393
394 @retval TRUE Valid signature encoded in PKCS1-v1_5.
395 @retval FALSE Invalid signature or invalid RSA context.
396
397 **/
398 BOOLEAN
399 EFIAPI
400 RsaPkcs1Verify (
401 IN VOID *RsaContext,
402 IN CONST UINT8 *MessageHash,
403 IN UINTN HashSize,
404 IN CONST UINT8 *Signature,
405 IN UINTN SigSize
406 )
407 {
408 if (!InternalIsCryptServiveAvailable ()) {
409 return FALSE;
410 }
411
412 return mCryptProtocol->RsaPkcs1Verify (
413 RsaContext,
414 MessageHash,
415 HashSize,
416 Signature,
417 SigSize
418 );
419 }