2 Implementation of The runtime cryptographic library instance (for IPF).
4 Copyright (c) 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
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 ASSERT().
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 Performs SHA-256 digest on a data buffer of the specified length. This function can
222 be called multiple times to compute the digest of long or discontinuous data streams.
224 If Sha256Context is NULL, then ASSERT().
226 @param[in, out] Sha256Context Pointer to the SHA-256 context.
227 @param[in] Data Pointer to the buffer containing the data to be hashed.
228 @param[in] DataLength Length of Data buffer in bytes.
230 @retval TRUE SHA-256 data digest succeeded.
231 @retval FALSE Invalid SHA-256 context. After Sha256Final function has been called, the
232 SHA-256 context cannot be reused.
238 IN OUT VOID
*Sha256Context
,
243 if (!InternalIsCryptServiveAvailable ()) {
247 return mCryptProtocol
->Sha256Update (Sha256Context
, Data
, DataLength
);
251 Completes SHA-256 hash computation and retrieves the digest value into the specified
252 memory. After this function has been called, the SHA-256 context cannot be used again.
254 If Sha256Context is NULL, then ASSERT().
255 If HashValue is NULL, then ASSERT().
257 @param[in, out] Sha256Context Pointer to SHA-256 context
258 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
261 @retval TRUE SHA-256 digest computation succeeded.
262 @retval FALSE SHA-256 digest computation failed.
268 IN OUT VOID
*Sha256Context
,
272 if (!InternalIsCryptServiveAvailable ()) {
276 return mCryptProtocol
->Sha256Final (Sha256Context
, HashValue
);
280 Allocates and Initializes one RSA Context for subsequent use.
282 @return Pointer to the RSA Context that has been initialized.
283 If the allocations fails, RsaNew() returns NULL.
292 if (!InternalIsCryptServiveAvailable ()) {
296 return mCryptProtocol
->RsaNew ();
300 Release the specified RSA Context.
302 @param[in] RsaContext Pointer to the RSA context to be released.
311 if (!InternalIsCryptServiveAvailable ()) {
315 mCryptProtocol
->RsaFree (RsaContext
);
319 Sets the tag-designated RSA key component into the established RSA context from
320 the user-specified nonnegative integer (octet string format represented in RSA
323 If RsaContext is NULL, then ASSERT().
325 @param[in, out] RsaContext Pointer to RSA context being set.
326 @param[in] KeyTag Tag of RSA key component being set.
327 @param[in] BigNumber Pointer to octet integer buffer.
328 @param[in] BnLength Length of big number buffer in bytes.
330 @return TRUE RSA key component was set successfully.
331 @return FALSE Invalid RSA key component tag.
337 IN OUT VOID
*RsaContext
,
338 IN RSA_KEY_TAG KeyTag
,
339 IN CONST UINT8
*BigNumber
,
343 if (!InternalIsCryptServiveAvailable ()) {
347 return mCryptProtocol
->RsaSetKey (RsaContext
, KeyTag
, BigNumber
, BnLength
);
351 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
354 If RsaContext is NULL, then ASSERT().
355 If MessageHash is NULL, then ASSERT().
356 If Signature is NULL, then ASSERT().
357 If HashLength is not equal to the size of MD5, SHA-1 or SHA-256 digest, then ASSERT().
359 @param[in] RsaContext Pointer to RSA context for signature verification.
360 @param[in] MessageHash Pointer to octet message hash to be checked.
361 @param[in] HashLength Length of the message hash in bytes.
362 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
363 @param[in] SigLength Length of signature in bytes.
365 @return TRUE Valid signature encoded in PKCS1-v1_5.
366 @return FALSE Invalid signature or invalid RSA context.
373 IN CONST UINT8
*MessageHash
,
379 if (!InternalIsCryptServiveAvailable ()) {
383 return mCryptProtocol
->RsaPkcs1Verify (