1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/** @file
cSHAKE-256 Digest Wrapper Implementations.
Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "CryptParallelHash.h"
#define CSHAKE256_SECURITY_STRENGTH 256
#define CSHAKE256_RATE_IN_BYTES 136
CONST CHAR8 mZeroPadding[CSHAKE256_RATE_IN_BYTES] = { 0 };
/**
CShake256 initial function.
Initializes user-supplied memory pointed by CShake256Context as cSHAKE-256 hash context for
subsequent use.
@param[out] CShake256Context Pointer to cSHAKE-256 context being initialized.
@param[in] OutputLen The desired number of output length in bytes.
@param[in] Name Pointer to the function name string.
@param[in] NameLen The length of the function name in bytes.
@param[in] Customization Pointer to the customization string.
@param[in] CustomizationLen The length of the customization string in bytes.
@retval TRUE cSHAKE-256 context initialization succeeded.
@retval FALSE cSHAKE-256 context initialization failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
CShake256Init (
OUT VOID *CShake256Context,
IN UINTN OutputLen,
IN CONST VOID *Name,
IN UINTN NameLen,
IN CONST VOID *Customization,
IN UINTN CustomizationLen
)
{
BOOLEAN Status;
UINT8 EncBuf[sizeof (UINTN) + 1];
UINTN EncLen;
UINTN AbsorbLen;
UINTN PadLen;
//
// Check input parameters.
//
if ((CShake256Context == NULL) || (OutputLen == 0) || ((NameLen != 0) && (Name == NULL)) || ((CustomizationLen != 0) && (Customization == NULL))) {
return FALSE;
}
//
// Initialize KECCAK context with pad value and block size.
//
if ((NameLen == 0) && (CustomizationLen == 0)) {
//
// When N and S are both empty strings, cSHAKE(X, L, N, S) is equivalent to
// SHAKE as defined in FIPS 202.
//
Status = (BOOLEAN)KeccakInit (
(Keccak1600_Ctx *)CShake256Context,
'\x1f',
(KECCAK1600_WIDTH - CSHAKE256_SECURITY_STRENGTH * 2) / 8,
OutputLen
);
return Status;
} else {
Status = (BOOLEAN)KeccakInit (
(Keccak1600_Ctx *)CShake256Context,
'\x04',
(KECCAK1600_WIDTH - CSHAKE256_SECURITY_STRENGTH * 2) / 8,
OutputLen
);
if (!Status) {
return FALSE;
}
AbsorbLen = 0;
//
// Absorb Absorb bytepad(.., rate).
//
EncLen = LeftEncode (EncBuf, CSHAKE256_RATE_IN_BYTES);
Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, EncBuf, EncLen);
if (!Status) {
return FALSE;
}
AbsorbLen += EncLen;
//
// Absorb encode_string(N).
//
EncLen = LeftEncode (EncBuf, NameLen * 8);
Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, EncBuf, EncLen);
if (!Status) {
return FALSE;
}
AbsorbLen += EncLen;
Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, Name, NameLen);
if (!Status) {
return FALSE;
}
AbsorbLen += NameLen;
//
// Absorb encode_string(S).
//
EncLen = LeftEncode (EncBuf, CustomizationLen * 8);
Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, EncBuf, EncLen);
if (!Status) {
return FALSE;
}
AbsorbLen += EncLen;
Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, Customization, CustomizationLen);
if (!Status) {
return FALSE;
}
AbsorbLen += CustomizationLen;
//
// Absorb zero padding up to rate.
//
PadLen = CSHAKE256_RATE_IN_BYTES - AbsorbLen % CSHAKE256_RATE_IN_BYTES;
Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, mZeroPadding, PadLen);
if (!Status) {
return FALSE;
}
return TRUE;
}
}
/**
Digests the input data and updates cSHAKE-256 context.
This function performs cSHAKE-256 digest on a data buffer of the specified size.
It can be called multiple times to compute the digest of long or discontinuous data streams.
cSHAKE-256 context should be already correctly initialized by CShake256Init(), and should not be finalized
by CShake256Final(). Behavior with invalid context is undefined.
@param[in, out] CShake256Context Pointer to the cSHAKE-256 context.
@param[in] Data Pointer to the buffer containing the data to be hashed.
@param[in] DataSize Size of Data buffer in bytes.
@retval TRUE cSHAKE-256 data digest succeeded.
@retval FALSE cSHAKE-256 data digest failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
CShake256Update (
IN OUT VOID *CShake256Context,
IN CONST VOID *Data,
IN UINTN DataSize
)
{
//
// Check input parameters.
//
if (CShake256Context == NULL) {
return FALSE;
}
//
// Check invalid parameters, in case that only DataLength was checked in OpenSSL.
//
if ((Data == NULL) && (DataSize != 0)) {
return FALSE;
}
return (BOOLEAN)(Sha3Update ((Keccak1600_Ctx *)CShake256Context, Data, DataSize));
}
/**
Completes computation of the cSHAKE-256 digest value.
This function completes cSHAKE-256 hash computation and retrieves the digest value into
the specified memory. After this function has been called, the cSHAKE-256 context cannot
be used again.
cSHAKE-256 context should be already correctly initialized by CShake256Init(), and should not be
finalized by CShake256Final(). Behavior with invalid cSHAKE-256 context is undefined.
@param[in, out] CShake256Context Pointer to the cSHAKE-256 context.
@param[out] HashValue Pointer to a buffer that receives the cSHAKE-256 digest
value.
@retval TRUE cSHAKE-256 digest computation succeeded.
@retval FALSE cSHAKE-256 digest computation failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
CShake256Final (
IN OUT VOID *CShake256Context,
OUT UINT8 *HashValue
)
{
//
// Check input parameters.
//
if ((CShake256Context == NULL) || (HashValue == NULL)) {
return FALSE;
}
//
// cSHAKE-256 Hash Finalization.
//
return (BOOLEAN)(Sha3Final ((Keccak1600_Ctx *)CShake256Context, HashValue));
}
/**
Computes the CSHAKE-256 message digest of a input data buffer.
This function performs the CSHAKE-256 message digest of a given data buffer, and places
the digest value into the specified memory.
@param[in] Data Pointer to the buffer containing the data to be hashed.
@param[in] DataSize Size of Data buffer in bytes.
@param[in] OutputLen Size of output in bytes.
@param[in] Name Pointer to the function name string.
@param[in] NameLen Size of the function name in bytes.
@param[in] Customization Pointer to the customization string.
@param[in] CustomizationLen Size of the customization string in bytes.
@param[out] HashValue Pointer to a buffer that receives the CSHAKE-256 digest
value.
@retval TRUE CSHAKE-256 digest computation succeeded.
@retval FALSE CSHAKE-256 digest computation failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
CShake256HashAll (
IN CONST VOID *Data,
IN UINTN DataSize,
IN UINTN OutputLen,
IN CONST VOID *Name,
IN UINTN NameLen,
IN CONST VOID *Customization,
IN UINTN CustomizationLen,
OUT UINT8 *HashValue
)
{
BOOLEAN Status;
Keccak1600_Ctx Ctx;
//
// Check input parameters.
//
if (HashValue == NULL) {
return FALSE;
}
if ((Data == NULL) && (DataSize != 0)) {
return FALSE;
}
Status = CShake256Init (&Ctx, OutputLen, Name, NameLen, Customization, CustomizationLen);
if (!Status) {
return FALSE;
}
Status = CShake256Update (&Ctx, Data, DataSize);
if (!Status) {
return FALSE;
}
return CShake256Final (&Ctx, HashValue);
}
|