]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h
CryptoPkg: Add new hash algorithm ParallelHash256HashAll in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptParallelHash.h
CommitLineData
c1e66210
ZL
1/** @file\r
2 ParallelHash related function and type declaration.\r
3\r
4Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.\r
8Licensed under the OpenSSL license (the "License"). You may not use\r
9this file except in compliance with the License. You can obtain a copy\r
10in the file LICENSE in the source distribution or at\r
11https://www.openssl.org/source/license.html\r
12\r
13Copyright 2022 The eXtended Keccak Code Package (XKCP)\r
14https://github.com/XKCP/XKCP\r
15Keccak, designed by Guido Bertoni, Joan Daemen, Michael Peeters and Gilles Van Assche.\r
16Implementation by the designers, hereby denoted as "the implementer".\r
17For more information, feedback or questions, please refer to the Keccak Team website:\r
18https://keccak.team/\r
19To the extent possible under law, the implementer has waived all copyright\r
20and related or neighboring rights to the source code in this file.\r
21http://creativecommons.org/publicdomain/zero/1.0/\r
22**/\r
23\r
24#include "InternalCryptLib.h"\r
25\r
26#define KECCAK1600_WIDTH 1600\r
27\r
28//\r
29// This struct referring to m_sha3.c from opessl and modified its type name.\r
30//\r
31typedef struct {\r
32 uint64_t A[5][5];\r
33 size_t block_size; /* cached ctx->digest->block_size */\r
34 size_t md_size; /* output length, variable in XOF */\r
35 size_t num; /* used bytes in below buffer */\r
36 unsigned char buf[KECCAK1600_WIDTH / 8 - 32];\r
37 unsigned char pad;\r
38} Keccak1600_Ctx;\r
39\r
40/**\r
41 SHA3_absorb can be called multiple times, but at each invocation\r
42 largest multiple of |r| out of |len| bytes are processed. Then\r
43 remaining amount of bytes is returned. This is done to spare caller\r
44 trouble of calculating the largest multiple of |r|. |r| can be viewed\r
45 as blocksize. It is commonly (1600 - 256*n)/8, e.g. 168, 136, 104,\r
46 72, but can also be (1600 - 448)/8 = 144. All this means that message\r
47 padding and intermediate sub-block buffering, byte- or bitwise, is\r
48 caller's responsibility.\r
49**/\r
50size_t\r
51SHA3_absorb (\r
52 uint64_t A[5][5],\r
53 const unsigned char *inp,\r
54 size_t len,\r
55 size_t r\r
56 );\r
57\r
58/**\r
59 SHA3_squeeze is called once at the end to generate |out| hash value\r
60 of |len| bytes.\r
61**/\r
62void\r
63SHA3_squeeze (\r
64 uint64_t A[5][5],\r
65 unsigned char *out,\r
66 size_t len,\r
67 size_t r\r
68 );\r
69\r
70/**\r
71 Encode function from XKCP.\r
72\r
73 Encodes the input as a byte string in a way that can be unambiguously parsed\r
74 from the beginning of the string by inserting the length of the byte string\r
75 before the byte string representation of input.\r
76\r
77 @param[out] EncBuf Result of left encode.\r
78 @param[in] Value Input of left encode.\r
79\r
80 @retval EncLen Size of encode result in bytes.\r
81**/\r
82UINTN\r
83EFIAPI\r
84LeftEncode (\r
85 OUT UINT8 *EncBuf,\r
86 IN UINTN Value\r
87 );\r
88\r
89/**\r
90 Encode function from XKCP.\r
91\r
92 Encodes the input as a byte string in a way that can be unambiguously parsed\r
93 from the end of the string by inserting the length of the byte string after\r
94 the byte string representation of input.\r
95\r
96 @param[out] EncBuf Result of right encode.\r
97 @param[in] Value Input of right encode.\r
98\r
99 @retval EncLen Size of encode result in bytes.\r
100**/\r
101UINTN\r
102EFIAPI\r
103RightEncode (\r
104 OUT UINT8 *EncBuf,\r
105 IN UINTN Value\r
106 );\r
107\r
108/**\r
109 Keccak initial fuction.\r
110\r
111 Set up state with specified capacity.\r
112\r
113 @param[out] Context Pointer to the context being initialized.\r
114 @param[in] Pad Delimited Suffix.\r
115 @param[in] BlockSize Size of context block.\r
116 @param[in] MessageDigestLen Size of message digest in bytes.\r
117\r
118 @retval 1 Initialize successfully.\r
119 @retval 0 Fail to initialize.\r
120**/\r
121UINT8\r
122EFIAPI\r
123KeccakInit (\r
124 OUT Keccak1600_Ctx *Context,\r
125 IN UINT8 Pad,\r
126 IN UINTN BlockSize,\r
127 IN UINTN MessageDigstLen\r
128 );\r
129\r
130/**\r
131 Sha3 update fuction.\r
132\r
133 This function performs Sha3 digest on a data buffer of the specified size.\r
134 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
135\r
136 @param[in,out] Context Pointer to the Keccak context.\r
137 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
138 @param[in] DataSize Size of Data buffer in bytes.\r
139\r
140 @retval 1 Update successfully.\r
141**/\r
142UINT8\r
143EFIAPI\r
144Sha3Update (\r
145 IN OUT Keccak1600_Ctx *Context,\r
146 IN const VOID *Data,\r
147 IN UINTN DataSize\r
148 );\r
149\r
150/**\r
151 Completes computation of Sha3 message digest.\r
152\r
153 This function completes sha3 hash computation and retrieves the digest value into\r
154 the specified memory. After this function has been called, the keccak context cannot\r
155 be used again.\r
156\r
157 @param[in, out] Context Pointer to the keccak context.\r
158 @param[out] MessageDigest Pointer to a buffer that receives the message digest.\r
159\r
160 @retval 1 Meaasge digest computation succeeded.\r
161**/\r
162UINT8\r
163EFIAPI\r
164Sha3Final (\r
165 IN OUT Keccak1600_Ctx *Context,\r
166 OUT UINT8 *MessageDigest\r
167 );\r
168\r
169/**\r
170 Computes the CSHAKE-256 message digest of a input data buffer.\r
171\r
172 This function performs the CSHAKE-256 message digest of a given data buffer, and places\r
173 the digest value into the specified memory.\r
174\r
175 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
176 @param[in] DataSize Size of Data buffer in bytes.\r
177 @param[in] OutputLen Size of output in bytes.\r
178 @param[in] Name Pointer to the function name string.\r
179 @param[in] NameLen Size of the function name in bytes.\r
180 @param[in] Customization Pointer to the customization string.\r
181 @param[in] CustomizationLen Size of the customization string in bytes.\r
182 @param[out] HashValue Pointer to a buffer that receives the CSHAKE-256 digest\r
183 value.\r
184\r
185 @retval TRUE CSHAKE-256 digest computation succeeded.\r
186 @retval FALSE CSHAKE-256 digest computation failed.\r
187 @retval FALSE This interface is not supported.\r
188\r
189**/\r
190BOOLEAN\r
191EFIAPI\r
192CShake256HashAll (\r
193 IN CONST VOID *Data,\r
194 IN UINTN DataSize,\r
195 IN UINTN OutputLen,\r
196 IN CONST VOID *Name,\r
197 IN UINTN NameLen,\r
198 IN CONST VOID *Customization,\r
199 IN UINTN CustomizationLen,\r
200 OUT UINT8 *HashValue\r
201 );\r