]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/HashInstanceLibSha512/HashInstanceLibSha512.c
UefiCpuPkg/CpuMpPei: support stack guard feature
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSha512 / HashInstanceLibSha512.c
CommitLineData
8d7aef3d
ZC
1/** @file
2 This library is BaseCrypto SHA512 hash instance.
3 It can be registered to BaseCrypto router, to serve as hash engine.
4
5Copyright (c) 2018, Intel Corporation. All rights reserved. <BR>
6This program and the accompanying materials
7are licensed and made available under the terms and conditions of the BSD License
8which accompanies this distribution. The full text of the license may be found at
9http://opensource.org/licenses/bsd-license.php
10
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12WITHOUT 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**/
30VOID
31Tpm2SetSha512ToDigestList (
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**/
53EFI_STATUS
54EFIAPI
55Sha512HashInit (
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**/
82EFI_STATUS
83EFIAPI
84Sha512HashUpdate (
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**/
106EFI_STATUS
107EFIAPI
108Sha512HashFinal (
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
126HASH_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**/
138EFI_STATUS
139EFIAPI
140HashInstanceLibSha512Constructor (
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}