2 Implementation of The runtime cryptographic library instance (for IPF).
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
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.
17 #include <Library/BaseLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/UefiRuntimeLib.h>
22 #include <Protocol/RuntimeCrypt.h>
24 #include <Guid/EventGroup.h>
26 EFI_RUNTIME_CRYPT_PROTOCOL
*mCryptProtocol
= NULL
;
27 EFI_EVENT mIpfCryptLibVirtualNotifyEvent
;
30 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, which converts
31 pointer to new virtual address.
33 @param Event Event whose notification function is being invoked.
34 @param Context Pointer to the notification function's context
39 IpfCryptLibAddressChangeEvent (
45 // Convert Address of Runtime Crypto Protocol.
47 EfiConvertPointer (0x0, (VOID
**) &mCryptProtocol
);
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.
55 @param ImageHandle The firmware allocated handle for the EFI image.
56 @param SystemTable A pointer to the EFI System Table.
58 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
63 RuntimeDxeIpfCryptLibConstructor (
64 IN EFI_HANDLE ImageHandle
,
65 IN EFI_SYSTEM_TABLE
*SystemTable
71 // Locate Runtime Crypt Protocol Instance
73 Status
= gBS
->LocateProtocol (
74 &gEfiRuntimeCryptProtocolGuid
,
76 (VOID
**) &mCryptProtocol
78 ASSERT_EFI_ERROR (Status
);
79 ASSERT (mCryptProtocol
!= NULL
);
82 // Register SetVirtualAddressMap () notify function
84 Status
= gBS
->CreateEventEx (
87 IpfCryptLibAddressChangeEvent
,
89 &gEfiEventVirtualAddressChangeGuid
,
90 &mIpfCryptLibVirtualNotifyEvent
92 ASSERT_EFI_ERROR (Status
);
98 Destructor of IPF Crypto Library Instance.
100 @param ImageHandle The firmware allocated handle for the EFI image.
101 @param SystemTable A pointer to the EFI System Table.
103 @retval EFI_SUCCESS The destructor completed successfully.
104 @retval Other value The destructor did not complete successfully.
109 RuntimeDxeIpfCryptLibDestructor (
110 IN EFI_HANDLE ImageHandle
,
111 IN EFI_SYSTEM_TABLE
*SystemTable
117 // Close the Set Virtual Address Map event
119 Status
= gBS
->CloseEvent (mIpfCryptLibVirtualNotifyEvent
);
120 ASSERT_EFI_ERROR (Status
);
126 Check whether crypto service provided by Runtime Crypt protocol is ready to use.
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.
133 @retval TRUE The Crypto service is ready to use.
134 @retval FALSE The Crypto service is not available.
139 InternalIsCryptServiveAvailable (
146 CpuMode
= AsmCpuVirtual();
149 // CPU is in mixed mode, return failing the operation gracefully.
154 GoneVirtual
= EfiGoneVirtual();
156 if ((CpuMode
> 0) && !GoneVirtual
) {
158 // CPU is in virtual mode, but SetVirtualAddressMap() has not been called,
159 // so return failing the operation gracefully.
164 if ((CpuMode
== 0) && GoneVirtual
) {
166 // CPU is in physical mode, but SetVirtualAddressMap() has been called,
167 // so return failing the operation gracefully.
176 Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.
178 @return The size, in bytes, of the context buffer required for SHA-256 operations.
183 Sha256GetContextSize (
187 if (!InternalIsCryptServiveAvailable ()) {
191 return mCryptProtocol
->Sha256GetContextSize ();
195 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
198 If Sha256Context is NULL, then return FALSE.
200 @param[in, out] Sha256Context Pointer to SHA-256 Context being initialized.
202 @retval TRUE SHA-256 context initialization succeeded.
203 @retval FALSE SHA-256 context initialization failed.
209 IN OUT VOID
*Sha256Context
212 if (!InternalIsCryptServiveAvailable ()) {
216 return mCryptProtocol
->Sha256Init (Sha256Context
);
221 Makes a copy of an existing SHA-256 context.
223 Return FALSE to indicate this interface is not supported.
225 @param[in] Sha256Context Pointer to SHA-256 context being copied.
226 @param[out] NewSha256Context Pointer to new SHA-256 context.
228 @retval FALSE This interface is not supported.
234 IN CONST VOID
*Sha256Context
,
235 OUT VOID
*NewSha256Context
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.
247 If Sha256Context is NULL, then return FALSE.
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.
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.
261 IN OUT VOID
*Sha256Context
,
266 if (!InternalIsCryptServiveAvailable ()) {
270 return mCryptProtocol
->Sha256Update (Sha256Context
, Data
, DataLength
);
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.
277 If Sha256Context is NULL, then return FALSE.
278 If HashValue is NULL, then return FALSE.
280 @param[in, out] Sha256Context Pointer to SHA-256 context
281 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
284 @retval TRUE SHA-256 digest computation succeeded.
285 @retval FALSE SHA-256 digest computation failed.
291 IN OUT VOID
*Sha256Context
,
295 if (!InternalIsCryptServiveAvailable ()) {
299 return mCryptProtocol
->Sha256Final (Sha256Context
, HashValue
);
303 Allocates and initializes one RSA context for subsequent use.
305 @return Pointer to the RSA context that has been initialized.
306 If the allocations fails, RsaNew() returns NULL.
315 if (!InternalIsCryptServiveAvailable ()) {
319 return mCryptProtocol
->RsaNew ();
323 Release the specified RSA context.
325 @param[in] RsaContext Pointer to the RSA context to be released.
334 if (!InternalIsCryptServiveAvailable ()) {
338 mCryptProtocol
->RsaFree (RsaContext
);
342 Sets the tag-designated key component into the established RSA context.
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.
349 If RsaContext is NULL, then return FALSE.
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
356 @param[in] BnSize Size of big number buffer in bytes.
357 If BigNumber is NULL, then it is ignored.
359 @retval TRUE RSA key component was set successfully.
360 @retval FALSE Invalid RSA key component tag.
366 IN OUT VOID
*RsaContext
,
367 IN RSA_KEY_TAG KeyTag
,
368 IN CONST UINT8
*BigNumber
,
372 if (!InternalIsCryptServiveAvailable ()) {
376 return mCryptProtocol
->RsaSetKey (RsaContext
, KeyTag
, BigNumber
, BnSize
);
380 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
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.
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.
394 @retval TRUE Valid signature encoded in PKCS1-v1_5.
395 @retval FALSE Invalid signature or invalid RSA context.
402 IN CONST UINT8
*MessageHash
,
408 if (!InternalIsCryptServiveAvailable ()) {
412 return mCryptProtocol
->RsaPkcs1Verify (