TianoCore EDK2 master
Loading...
Searching...
No Matches
Hash2DxeCrypto.c
Go to the documentation of this file.
1
10#include <Uefi.h>
11#include <Protocol/Hash2.h>
12#include <Library/BaseLib.h>
16#include <Library/DebugLib.h>
18
19#include "Driver.h"
20
30typedef
33 VOID
34 );
35
50typedef
51BOOLEAN
52(EFIAPI *EFI_HASH_INIT)(
53 OUT VOID *HashContext
54 );
55
76typedef
77BOOLEAN
78(EFIAPI *EFI_HASH_UPDATE)(
79 IN OUT VOID *HashContext,
80 IN CONST VOID *Data,
81 IN UINTN DataSize
82 );
83
106typedef
107BOOLEAN
108(EFIAPI *EFI_HASH_FINAL)(
109 IN OUT VOID *HashContext,
110 OUT UINT8 *HashValue
111 );
112
113typedef struct {
114 EFI_GUID *Guid;
115 UINT32 HashSize;
116 EFI_HASH_GET_CONTEXT_SIZE GetContextSize;
117 EFI_HASH_INIT Init;
118 EFI_HASH_UPDATE Update;
119 EFI_HASH_FINAL Final;
121
122EFI_HASH_INFO mHashInfo[] = {
123 { &gEfiHashAlgorithmSha256Guid, sizeof (EFI_SHA256_HASH2), Sha256GetContextSize, Sha256Init, Sha256Update, Sha256Final },
124 { &gEfiHashAlgorithmSha384Guid, sizeof (EFI_SHA384_HASH2), Sha384GetContextSize, Sha384Init, Sha384Update, Sha384Final },
125 { &gEfiHashAlgorithmSha512Guid, sizeof (EFI_SHA512_HASH2), Sha512GetContextSize, Sha512Init, Sha512Update, Sha512Final },
126};
127
142EFIAPI
145 IN CONST EFI_GUID *HashAlgorithm,
146 OUT UINTN *HashSize
147 );
148
170EFIAPI
173 IN CONST EFI_GUID *HashAlgorithm,
174 IN CONST UINT8 *Message,
175 IN UINTN MessageSize,
177 );
178
196EFIAPI
199 IN CONST EFI_GUID *HashAlgorithm
200 );
201
218EFIAPI
221 IN CONST UINT8 *Message,
222 IN UINTN MessageSize
223 );
224
242EFIAPI
246 );
247
248EFI_HASH2_PROTOCOL mHash2Protocol = {
254};
255
265 IN CONST EFI_GUID *HashAlgorithm
266 )
267{
268 UINTN Index;
269
270 for (Index = 0; Index < sizeof (mHashInfo)/sizeof (mHashInfo[0]); Index++) {
271 if (CompareGuid (HashAlgorithm, mHashInfo[Index].Guid)) {
272 return &mHashInfo[Index];
273 }
274 }
275
276 return NULL;
277}
278
293EFIAPI
296 IN CONST EFI_GUID *HashAlgorithm,
297 OUT UINTN *HashSize
298 )
299{
300 EFI_HASH_INFO *HashInfo;
301
302 if ((This == NULL) || (HashSize == NULL)) {
303 return EFI_INVALID_PARAMETER;
304 }
305
306 if (HashAlgorithm == NULL) {
307 return EFI_UNSUPPORTED;
308 }
309
310 HashInfo = GetHashInfo (HashAlgorithm);
311 if (HashInfo == NULL) {
312 return EFI_UNSUPPORTED;
313 }
314
315 *HashSize = HashInfo->HashSize;
316 return EFI_SUCCESS;
317}
318
340EFIAPI
343 IN CONST EFI_GUID *HashAlgorithm,
344 IN CONST UINT8 *Message,
345 IN UINTN MessageSize,
347 )
348{
349 EFI_HASH_INFO *HashInfo;
350 VOID *HashCtx;
351 UINTN CtxSize;
352 BOOLEAN Ret;
353 EFI_STATUS Status;
354 HASH2_INSTANCE_DATA *Instance;
355
356 Status = EFI_SUCCESS;
357
358 if ((This == NULL) || (Hash == NULL)) {
359 return EFI_INVALID_PARAMETER;
360 }
361
362 if (HashAlgorithm == NULL) {
363 return EFI_UNSUPPORTED;
364 }
365
366 HashInfo = GetHashInfo (HashAlgorithm);
367 if (HashInfo == NULL) {
368 return EFI_UNSUPPORTED;
369 }
370
371 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
372 if (Instance->HashContext != NULL) {
373 FreePool (Instance->HashContext);
374 }
375
376 Instance->HashInfoContext = NULL;
377 Instance->HashContext = NULL;
378
379 //
380 // Start hash sequence
381 //
382 CtxSize = HashInfo->GetContextSize ();
383 if (CtxSize == 0) {
384 return EFI_UNSUPPORTED;
385 }
386
387 HashCtx = AllocatePool (CtxSize);
388 if (HashCtx == NULL) {
389 return EFI_OUT_OF_RESOURCES;
390 }
391
392 Ret = HashInfo->Init (HashCtx);
393 if (!Ret) {
394 Status = EFI_OUT_OF_RESOURCES;
395 goto Done;
396 }
397
398 //
399 // Setup the context
400 //
401 Instance->HashContext = HashCtx;
402 Instance->HashInfoContext = HashInfo;
403
404 Ret = HashInfo->Update (HashCtx, Message, MessageSize);
405 if (!Ret) {
406 Status = EFI_OUT_OF_RESOURCES;
407 goto Done;
408 }
409
410 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);
411 if (!Ret) {
412 Status = EFI_OUT_OF_RESOURCES;
413 goto Done;
414 }
415
416Done:
417 //
418 // Cleanup the context
419 //
420 FreePool (HashCtx);
421 Instance->HashInfoContext = NULL;
422 Instance->HashContext = NULL;
423 return Status;
424}
425
443EFIAPI
446 IN CONST EFI_GUID *HashAlgorithm
447 )
448{
449 EFI_HASH_INFO *HashInfo;
450 VOID *HashCtx;
451 UINTN CtxSize;
452 BOOLEAN Ret;
453 HASH2_INSTANCE_DATA *Instance;
454
455 if (This == NULL) {
456 return EFI_INVALID_PARAMETER;
457 }
458
459 if (HashAlgorithm == NULL) {
460 return EFI_UNSUPPORTED;
461 }
462
463 HashInfo = GetHashInfo (HashAlgorithm);
464 if (HashInfo == NULL) {
465 return EFI_UNSUPPORTED;
466 }
467
468 //
469 // Consistency Check
470 //
471 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
472 if ((Instance->HashContext != NULL) || (Instance->HashInfoContext != NULL)) {
473 return EFI_ALREADY_STARTED;
474 }
475
476 //
477 // Start hash sequence
478 //
479 CtxSize = HashInfo->GetContextSize ();
480 if (CtxSize == 0) {
481 return EFI_UNSUPPORTED;
482 }
483
484 HashCtx = AllocatePool (CtxSize);
485 if (HashCtx == NULL) {
486 return EFI_OUT_OF_RESOURCES;
487 }
488
489 Ret = HashInfo->Init (HashCtx);
490 if (!Ret) {
491 FreePool (HashCtx);
492 return EFI_OUT_OF_RESOURCES;
493 }
494
495 //
496 // Setup the context
497 //
498 Instance->HashContext = HashCtx;
499 Instance->HashInfoContext = HashInfo;
500 Instance->Updated = FALSE;
501
502 return EFI_SUCCESS;
503}
504
521EFIAPI
524 IN CONST UINT8 *Message,
525 IN UINTN MessageSize
526 )
527{
528 EFI_HASH_INFO *HashInfo;
529 VOID *HashCtx;
530 BOOLEAN Ret;
531 HASH2_INSTANCE_DATA *Instance;
532
533 if (This == NULL) {
534 return EFI_INVALID_PARAMETER;
535 }
536
537 //
538 // Consistency Check
539 //
540 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
541 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL)) {
542 return EFI_NOT_READY;
543 }
544
545 HashInfo = Instance->HashInfoContext;
546 HashCtx = Instance->HashContext;
547
548 Ret = HashInfo->Update (HashCtx, Message, MessageSize);
549 if (!Ret) {
550 return EFI_OUT_OF_RESOURCES;
551 }
552
553 Instance->Updated = TRUE;
554
555 return EFI_SUCCESS;
556}
557
575EFIAPI
579 )
580{
581 EFI_HASH_INFO *HashInfo;
582 VOID *HashCtx;
583 BOOLEAN Ret;
584 HASH2_INSTANCE_DATA *Instance;
585
586 if ((This == NULL) || (Hash == NULL)) {
587 return EFI_INVALID_PARAMETER;
588 }
589
590 //
591 // Consistency Check
592 //
593 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
594 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL) ||
595 (!Instance->Updated))
596 {
597 return EFI_NOT_READY;
598 }
599
600 HashInfo = Instance->HashInfoContext;
601 HashCtx = Instance->HashContext;
602
603 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);
604
605 //
606 // Cleanup the context
607 //
608 FreePool (HashCtx);
609 Instance->HashInfoContext = NULL;
610 Instance->HashContext = NULL;
611 Instance->Updated = FALSE;
612
613 if (!Ret) {
614 return EFI_OUT_OF_RESOURCES;
615 }
616
617 return EFI_SUCCESS;
618}
UINT64 UINTN
UINTN EFIAPI Sha256GetContextSize(VOID)
Definition: CryptSha256.c:20
UINTN EFIAPI Sha384GetContextSize(VOID)
Definition: CryptSha512.c:20
BOOLEAN EFIAPI Sha512Final(IN OUT VOID *Sha512Context, OUT UINT8 *HashValue)
Definition: CryptSha512.c:389
UINTN EFIAPI Sha512GetContextSize(VOID)
Definition: CryptSha512.c:246
BOOLEAN EFIAPI Sha512Init(OUT VOID *Sha512Context)
Definition: CryptSha512.c:270
BOOLEAN EFIAPI Sha256Init(OUT VOID *Sha256Context)
Definition: CryptSha256.c:44
BOOLEAN EFIAPI Sha256Final(IN OUT VOID *Sha256Context, OUT UINT8 *HashValue)
Definition: CryptSha256.c:161
BOOLEAN EFIAPI Sha384Update(IN OUT VOID *Sha384Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha512.c:115
BOOLEAN EFIAPI Sha256Update(IN OUT VOID *Sha256Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha256.c:113
BOOLEAN EFIAPI Sha384Final(IN OUT VOID *Sha384Context, OUT UINT8 *HashValue)
Definition: CryptSha512.c:163
BOOLEAN EFIAPI Sha384Init(OUT VOID *Sha384Context)
Definition: CryptSha512.c:44
BOOLEAN EFIAPI Sha512Update(IN OUT VOID *Sha512Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha512.c:341
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS EFIAPI BaseCrypto2HashInit(IN CONST EFI_HASH2_PROTOCOL *This, IN CONST EFI_GUID *HashAlgorithm)
EFI_STATUS EFIAPI BaseCrypto2Hash(IN CONST EFI_HASH2_PROTOCOL *This, IN CONST EFI_GUID *HashAlgorithm, IN CONST UINT8 *Message, IN UINTN MessageSize, IN OUT EFI_HASH2_OUTPUT *Hash)
UINTN(EFIAPI * EFI_HASH_GET_CONTEXT_SIZE)(VOID)
EFI_STATUS EFIAPI BaseCrypto2GetHashSize(IN CONST EFI_HASH2_PROTOCOL *This, IN CONST EFI_GUID *HashAlgorithm, OUT UINTN *HashSize)
BOOLEAN(EFIAPI * EFI_HASH_UPDATE)(IN OUT VOID *HashContext, IN CONST VOID *Data, IN UINTN DataSize)
BOOLEAN(EFIAPI * EFI_HASH_INIT)(OUT VOID *HashContext)
EFI_HASH_INFO * GetHashInfo(IN CONST EFI_GUID *HashAlgorithm)
EFI_STATUS EFIAPI BaseCrypto2HashUpdate(IN CONST EFI_HASH2_PROTOCOL *This, IN CONST UINT8 *Message, IN UINTN MessageSize)
EFI_STATUS EFIAPI BaseCrypto2HashFinal(IN CONST EFI_HASH2_PROTOCOL *This, IN OUT EFI_HASH2_OUTPUT *Hash)
BOOLEAN(EFIAPI * EFI_HASH_FINAL)(IN OUT VOID *HashContext, OUT UINT8 *HashValue)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Base.h:213