1 /** @file
2 Ihis library is BaseCrypto SHA1 hash instance.
3 It can be registered to BaseCrypto router, to serve as hash engine.
4
5 Copyright (c) 2013, 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 SHA1 to digest list.
27
28 @param DigestList digest list
29 @param Sha1Digest SHA1 digest
30 **/
31 VOID
Tpm2SetSha1ToDigestList(IN TPML_DIGEST_VALUES * DigestList,IN UINT8 * Sha1Digest)32 Tpm2SetSha1ToDigestList (
33 IN TPML_DIGEST_VALUES *DigestList,
34 IN UINT8 *Sha1Digest
35 )
36 {
37 DigestList->count = 1;
38 DigestList->digests[0].hashAlg = TPM_ALG_SHA1;
39 CopyMem (
40 DigestList->digests[0].digest.sha1,
41 Sha1Digest,
42 SHA1_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
Sha1HashInit(OUT HASH_HANDLE * HashHandle)56 Sha1HashInit (
57 OUT HASH_HANDLE *HashHandle
58 )
59 {
60 VOID *Sha1Ctx;
61 UINTN CtxSize;
62
63 CtxSize = Sha1GetContextSize ();
64 Sha1Ctx = AllocatePool (CtxSize);
65 ASSERT (Sha1Ctx != NULL);
66
67 Sha1Init (Sha1Ctx);
68
69 *HashHandle = (HASH_HANDLE)Sha1Ctx;
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
Sha1HashUpdate(IN HASH_HANDLE HashHandle,IN VOID * DataToHash,IN UINTN DataToHashLen)85 Sha1HashUpdate (
86 IN HASH_HANDLE HashHandle,
87 IN VOID *DataToHash,
88 IN UINTN DataToHashLen
89 )
90 {
91 VOID *Sha1Ctx;
92
93 Sha1Ctx = (VOID *)HashHandle;
94 Sha1Update (Sha1Ctx, 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
Sha1HashFinal(IN HASH_HANDLE HashHandle,OUT TPML_DIGEST_VALUES * DigestList)109 Sha1HashFinal (
110 IN HASH_HANDLE HashHandle,
111 OUT TPML_DIGEST_VALUES *DigestList
112 )
113 {
114 UINT8 Digest[SHA1_DIGEST_SIZE];
115 VOID *Sha1Ctx;
116
117 Sha1Ctx = (VOID *)HashHandle;
118 Sha1Final (Sha1Ctx, Digest);
119
120 FreePool (Sha1Ctx);
121
122 Tpm2SetSha1ToDigestList (DigestList, Digest);
123
124 return EFI_SUCCESS;
125 }
126
127 HASH_INTERFACE mSha1InternalHashInstance = {
128 HASH_ALGORITHM_SHA1_GUID,
129 Sha1HashInit,
130 Sha1HashUpdate,
131 Sha1HashFinal,
132 };
133
134 /**
135 The function register SHA1 instance.
136
137 @retval EFI_SUCCESS SHA1 instance is registered, or system dose not surpport registr SHA1 instance
138 **/
139 EFI_STATUS
140 EFIAPI
HashInstanceLibSha1Constructor(VOID)141 HashInstanceLibSha1Constructor (
142 VOID
143 )
144 {
145 EFI_STATUS Status;
146
147 Status = RegisterHashInterfaceLib (&mSha1InternalHashInstance);
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 }