]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/HashInstanceLibSha512/HashInstanceLibSha512.c
83133702d511e44d318ab03ab662e20604d251d4
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSha512 / HashInstanceLibSha512.c
1 /** @file
2 This library is BaseCrypto SHA512 hash instance.
3 It can be registered to BaseCrypto router, to serve as hash engine.
4
5 Copyright (c) 2018, 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/DebugLib.h>
20 #include <Library/BaseCryptLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/HashLib.h>
23
24 /**
25 The function set SHA512 to digest list.
26
27 @param DigestList digest list
28 @param Sha512Digest SHA512 digest
29 **/
30 VOID
31 Tpm2SetSha512ToDigestList (
32 IN TPML_DIGEST_VALUES *DigestList,
33 IN UINT8 *Sha512Digest
34 )
35 {
36 DigestList->count = 1;
37 DigestList->digests[0].hashAlg = TPM_ALG_SHA512;
38 CopyMem (
39 DigestList->digests[0].digest.sha512,
40 Sha512Digest,
41 SHA512_DIGEST_SIZE
42 );
43 }
44
45 /**
46 Start hash sequence.
47
48 @param HashHandle Hash handle.
49
50 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
51 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
52 **/
53 EFI_STATUS
54 EFIAPI
55 Sha512HashInit (
56 OUT HASH_HANDLE *HashHandle
57 )
58 {
59 VOID *Sha512Ctx;
60 UINTN CtxSize;
61
62 CtxSize = Sha512GetContextSize ();
63 Sha512Ctx = AllocatePool (CtxSize);
64 ASSERT (Sha512Ctx != NULL);
65
66 Sha512Init (Sha512Ctx);
67
68 *HashHandle = (HASH_HANDLE)Sha512Ctx;
69
70 return EFI_SUCCESS;
71 }
72
73 /**
74 Update hash sequence data.
75
76 @param HashHandle Hash handle.
77 @param DataToHash Data to be hashed.
78 @param DataToHashLen Data size.
79
80 @retval EFI_SUCCESS Hash sequence updated.
81 **/
82 EFI_STATUS
83 EFIAPI
84 Sha512HashUpdate (
85 IN HASH_HANDLE HashHandle,
86 IN VOID *DataToHash,
87 IN UINTN DataToHashLen
88 )
89 {
90 VOID *Sha512Ctx;
91
92 Sha512Ctx = (VOID *)HashHandle;
93 Sha512Update (Sha512Ctx, DataToHash, DataToHashLen);
94
95 return EFI_SUCCESS;
96 }
97
98 /**
99 Complete hash sequence complete.
100
101 @param HashHandle Hash handle.
102 @param DigestList Digest list.
103
104 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
105 **/
106 EFI_STATUS
107 EFIAPI
108 Sha512HashFinal (
109 IN HASH_HANDLE HashHandle,
110 OUT TPML_DIGEST_VALUES *DigestList
111 )
112 {
113 UINT8 Digest[SHA512_DIGEST_SIZE];
114 VOID *Sha512Ctx;
115
116 Sha512Ctx = (VOID *)HashHandle;
117 Sha512Final (Sha512Ctx, Digest);
118
119 FreePool (Sha512Ctx);
120
121 Tpm2SetSha512ToDigestList (DigestList, Digest);
122
123 return EFI_SUCCESS;
124 }
125
126 HASH_INTERFACE mSha512InternalHashInstance = {
127 HASH_ALGORITHM_SHA512_GUID,
128 Sha512HashInit,
129 Sha512HashUpdate,
130 Sha512HashFinal,
131 };
132
133 /**
134 The function register SHA512 instance.
135
136 @retval EFI_SUCCESS SHA512 instance is registered, or system dose not surpport registr SHA512 instance
137 **/
138 EFI_STATUS
139 EFIAPI
140 HashInstanceLibSha512Constructor (
141 VOID
142 )
143 {
144 EFI_STATUS Status;
145
146 Status = RegisterHashInterfaceLib (&mSha512InternalHashInstance);
147 if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {
148 //
149 // Unsupported means platform policy does not need this instance enabled.
150 //
151 return EFI_SUCCESS;
152 }
153 return Status;
154 }