]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/HashInstanceLibSha384/HashInstanceLibSha384.c
SecurityPkg: HashLib: Add SHA384, SHA512 HashLib
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSha384 / HashInstanceLibSha384.c
1 /** @file
2 This library is BaseCrypto SHA384 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
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.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 SHA384 to digest list.
27
28 @param DigestList digest list
29 @param Sha384Digest SHA384 digest
30 **/
31 VOID
32 Tpm2SetSha384ToDigestList (
33 IN TPML_DIGEST_VALUES *DigestList,
34 IN UINT8 *Sha384Digest
35 )
36 {
37 DigestList->count = 1;
38 DigestList->digests[0].hashAlg = TPM_ALG_SHA384;
39 CopyMem (
40 DigestList->digests[0].digest.sha384,
41 Sha384Digest,
42 SHA384_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 Sha384HashInit (
57 OUT HASH_HANDLE *HashHandle
58 )
59 {
60 VOID *Sha384Ctx;
61 UINTN CtxSize;
62
63 CtxSize = Sha384GetContextSize ();
64 Sha384Ctx = AllocatePool (CtxSize);
65 ASSERT (Sha384Ctx != NULL);
66
67 Sha384Init (Sha384Ctx);
68
69 *HashHandle = (HASH_HANDLE)Sha384Ctx;
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 Sha384HashUpdate (
86 IN HASH_HANDLE HashHandle,
87 IN VOID *DataToHash,
88 IN UINTN DataToHashLen
89 )
90 {
91 VOID *Sha384Ctx;
92
93 Sha384Ctx = (VOID *)HashHandle;
94 Sha384Update (Sha384Ctx, 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 Sha384HashFinal (
110 IN HASH_HANDLE HashHandle,
111 OUT TPML_DIGEST_VALUES *DigestList
112 )
113 {
114 UINT8 Digest[SHA384_DIGEST_SIZE];
115 VOID *Sha384Ctx;
116
117 Sha384Ctx = (VOID *)HashHandle;
118 Sha384Final (Sha384Ctx, Digest);
119
120 FreePool (Sha384Ctx);
121
122 Tpm2SetSha384ToDigestList (DigestList, Digest);
123
124 return EFI_SUCCESS;
125 }
126
127 HASH_INTERFACE mSha384InternalHashInstance = {
128 HASH_ALGORITHM_SHA384_GUID,
129 Sha384HashInit,
130 Sha384HashUpdate,
131 Sha384HashFinal,
132 };
133
134 /**
135 The function register SHA384 instance.
136
137 @retval EFI_SUCCESS SHA384 instance is registered, or system dose not surpport registr SHA384 instance
138 **/
139 EFI_STATUS
140 EFIAPI
141 HashInstanceLibSha384Constructor (
142 VOID
143 )
144 {
145 EFI_STATUS Status;
146
147 Status = RegisterHashInterfaceLib (&mSha384InternalHashInstance);
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 }