]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLibRuntimeCryptProtocol/RuntimeDxeIpfCryptLib.c
1. Remove conducting ASSERT in BaseCryptLib.
[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 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.
223
224 If Sha256Context is NULL, then return FALSE.
225
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.
229
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.
233
234 **/
235 BOOLEAN
236 EFIAPI
237 Sha256Update (
238 IN OUT VOID *Sha256Context,
239 IN CONST VOID *Data,
240 IN UINTN DataLength
241 )
242 {
243 if (!InternalIsCryptServiveAvailable ()) {
244 return FALSE;
245 }
246
247 return mCryptProtocol->Sha256Update (Sha256Context, Data, DataLength);
248 }
249
250 /**
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.
253
254 If Sha256Context is NULL, then return FALSE.
255 If HashValue is NULL, then return FALSE.
256
257 @param[in, out] Sha256Context Pointer to SHA-256 context
258 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
259 value (32 bytes).
260
261 @retval TRUE SHA-256 digest computation succeeded.
262 @retval FALSE SHA-256 digest computation failed.
263
264 **/
265 BOOLEAN
266 EFIAPI
267 Sha256Final (
268 IN OUT VOID *Sha256Context,
269 OUT UINT8 *HashValue
270 )
271 {
272 if (!InternalIsCryptServiveAvailable ()) {
273 return FALSE;
274 }
275
276 return mCryptProtocol->Sha256Final (Sha256Context, HashValue);
277 }
278
279 /**
280 Allocates and Initializes one RSA Context for subsequent use.
281
282 @return Pointer to the RSA Context that has been initialized.
283 If the allocations fails, RsaNew() returns NULL.
284
285 **/
286 VOID *
287 EFIAPI
288 RsaNew (
289 VOID
290 )
291 {
292 if (!InternalIsCryptServiveAvailable ()) {
293 return FALSE;
294 }
295
296 return mCryptProtocol->RsaNew ();
297 }
298
299 /**
300 Release the specified RSA Context.
301
302 @param[in] RsaContext Pointer to the RSA context to be released.
303
304 **/
305 VOID
306 EFIAPI
307 RsaFree (
308 IN VOID *RsaContext
309 )
310 {
311 if (!InternalIsCryptServiveAvailable ()) {
312 return;
313 }
314
315 mCryptProtocol->RsaFree (RsaContext);
316 }
317
318 /**
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
321 PKCS#1).
322
323 If RsaContext is NULL, then return FALSE.
324
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.
329
330 @return TRUE RSA key component was set successfully.
331 @return FALSE Invalid RSA key component tag.
332
333 **/
334 BOOLEAN
335 EFIAPI
336 RsaSetKey (
337 IN OUT VOID *RsaContext,
338 IN RSA_KEY_TAG KeyTag,
339 IN CONST UINT8 *BigNumber,
340 IN UINTN BnLength
341 )
342 {
343 if (!InternalIsCryptServiveAvailable ()) {
344 return FALSE;
345 }
346
347 return mCryptProtocol->RsaSetKey (RsaContext, KeyTag, BigNumber, BnLength);
348 }
349
350 /**
351 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
352 RSA PKCS#1.
353
354 If RsaContext is NULL, then return FALSE.
355 If MessageHash is NULL, then return FALSE.
356 If Signature is NULL, then return FALSE.
357 If HashLength is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
358
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.
364
365 @return TRUE Valid signature encoded in PKCS1-v1_5.
366 @return FALSE Invalid signature or invalid RSA context.
367
368 **/
369 BOOLEAN
370 EFIAPI
371 RsaPkcs1Verify (
372 IN VOID *RsaContext,
373 IN CONST UINT8 *MessageHash,
374 IN UINTN HashLength,
375 IN UINT8 *Signature,
376 IN UINTN SigLength
377 )
378 {
379 if (!InternalIsCryptServiveAvailable ()) {
380 return FALSE;
381 }
382
383 return mCryptProtocol->RsaPkcs1Verify (
384 RsaContext,
385 MessageHash,
386 HashLength,
387 Signature,
388 SigLength
389 );
390 }