1 /** @file 2 This library is BaseCrypto SHA256 hash instance. 3 It can be registered to BaseCrypto router, to serve as hash engine. 4 5 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR> 6 This program and the accompanying materials 7 are licensed and made available under the terms and conditions of the BSD License 8 which accompanies this distribution. The full text of the license may be found at 9 http://opensource.org/licenses/bsd-license.php 10 11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 13 14 **/ 15 16 #include <PiPei.h> 17 #include <Library/BaseLib.h> 18 #include <Library/BaseMemoryLib.h> 19 #include <Library/Tpm2CommandLib.h> 20 #include <Library/DebugLib.h> 21 #include <Library/BaseCryptLib.h> 22 #include <Library/MemoryAllocationLib.h> 23 #include <Library/HashLib.h> 24 25 /** 26 The function set SHA256 to digest list. 27 28 @param DigestList digest list 29 @param Sha256Digest SHA256 digest 30 **/ 31 VOID 32 Tpm2SetSha256ToDigestList ( 33 IN TPML_DIGEST_VALUES *DigestList, 34 IN UINT8 *Sha256Digest 35 ) 36 { 37 DigestList->count = 1; 38 DigestList->digests[0].hashAlg = TPM_ALG_SHA256; 39 CopyMem ( 40 DigestList->digests[0].digest.sha256, 41 Sha256Digest, 42 SHA256_DIGEST_SIZE 43 ); 44 } 45 46 /** 47 Start hash sequence. 48 49 @param HashHandle Hash handle. 50 51 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned. 52 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash. 53 **/ 54 EFI_STATUS 55 EFIAPI 56 Sha256HashInit ( 57 OUT HASH_HANDLE *HashHandle 58 ) 59 { 60 VOID *Sha256Ctx; 61 UINTN CtxSize; 62 63 CtxSize = Sha256GetContextSize (); 64 Sha256Ctx = AllocatePool (CtxSize); 65 ASSERT (Sha256Ctx != NULL); 66 67 Sha256Init (Sha256Ctx); 68 69 *HashHandle = (HASH_HANDLE)Sha256Ctx; 70 71 return EFI_SUCCESS; 72 } 73 74 /** 75 Update hash sequence data. 76 77 @param HashHandle Hash handle. 78 @param DataToHash Data to be hashed. 79 @param DataToHashLen Data size. 80 81 @retval EFI_SUCCESS Hash sequence updated. 82 **/ 83 EFI_STATUS 84 EFIAPI 85 Sha256HashUpdate ( 86 IN HASH_HANDLE HashHandle, 87 IN VOID *DataToHash, 88 IN UINTN DataToHashLen 89 ) 90 { 91 VOID *Sha256Ctx; 92 93 Sha256Ctx = (VOID *)HashHandle; 94 Sha256Update (Sha256Ctx, DataToHash, DataToHashLen); 95 96 return EFI_SUCCESS; 97 } 98 99 /** 100 Complete hash sequence complete. 101 102 @param HashHandle Hash handle. 103 @param DigestList Digest list. 104 105 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned. 106 **/ 107 EFI_STATUS 108 EFIAPI 109 Sha256HashFinal ( 110 IN HASH_HANDLE HashHandle, 111 OUT TPML_DIGEST_VALUES *DigestList 112 ) 113 { 114 UINT8 Digest[SHA256_DIGEST_SIZE]; 115 VOID *Sha256Ctx; 116 117 Sha256Ctx = (VOID *)HashHandle; 118 Sha256Final (Sha256Ctx, Digest); 119 120 FreePool (Sha256Ctx); 121 122 Tpm2SetSha256ToDigestList (DigestList, Digest); 123 124 return EFI_SUCCESS; 125 } 126 127 HASH_INTERFACE mSha256InternalHashInstance = { 128 HASH_ALGORITHM_SHA256_GUID, 129 Sha256HashInit, 130 Sha256HashUpdate, 131 Sha256HashFinal, 132 }; 133 134 /** 135 The function register SHA256 instance. 136 137 @retval EFI_SUCCESS SHA256 instance is registered, or system dose not surpport registr SHA256 instance 138 **/ 139 EFI_STATUS 140 EFIAPI 141 HashInstanceLibSha256Constructor ( 142 VOID 143 ) 144 { 145 EFI_STATUS Status; 146 147 Status = RegisterHashInterfaceLib (&mSha256InternalHashInstance); 148 if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) { 149 // 150 // Unsupported means platform policy does not need this instance enabled. 151 // 152 return EFI_SUCCESS; 153 } 154 return Status; 155 }