TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptCShake256.c
Go to the documentation of this file.
1
9#include "CryptParallelHash.h"
10
11#define CSHAKE256_SECURITY_STRENGTH 256
12#define CSHAKE256_RATE_IN_BYTES 136
13
14CONST CHAR8 mZeroPadding[CSHAKE256_RATE_IN_BYTES] = { 0 };
15
33BOOLEAN
34EFIAPI
36 OUT VOID *CShake256Context,
37 IN UINTN OutputLen,
38 IN CONST VOID *Name,
39 IN UINTN NameLen,
40 IN CONST VOID *Customization,
41 IN UINTN CustomizationLen
42 )
43{
44 BOOLEAN Status;
45 UINT8 EncBuf[sizeof (UINTN) + 1];
46 UINTN EncLen;
47 UINTN AbsorbLen;
48 UINTN PadLen;
49
50 //
51 // Check input parameters.
52 //
53 if ((CShake256Context == NULL) || (OutputLen == 0) || ((NameLen != 0) && (Name == NULL)) || ((CustomizationLen != 0) && (Customization == NULL))) {
54 return FALSE;
55 }
56
57 //
58 // Initialize KECCAK context with pad value and block size.
59 //
60 if ((NameLen == 0) && (CustomizationLen == 0)) {
61 //
62 // When N and S are both empty strings, cSHAKE(X, L, N, S) is equivalent to
63 // SHAKE as defined in FIPS 202.
64 //
65 Status = (BOOLEAN)KeccakInit (
66 (Keccak1600_Ctx *)CShake256Context,
67 '\x1f',
68 (KECCAK1600_WIDTH - CSHAKE256_SECURITY_STRENGTH * 2) / 8,
69 OutputLen
70 );
71
72 return Status;
73 } else {
74 Status = (BOOLEAN)KeccakInit (
75 (Keccak1600_Ctx *)CShake256Context,
76 '\x04',
77 (KECCAK1600_WIDTH - CSHAKE256_SECURITY_STRENGTH * 2) / 8,
78 OutputLen
79 );
80 if (!Status) {
81 return FALSE;
82 }
83
84 AbsorbLen = 0;
85 //
86 // Absorb Absorb bytepad(.., rate).
87 //
88 EncLen = LeftEncode (EncBuf, CSHAKE256_RATE_IN_BYTES);
89 Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, EncBuf, EncLen);
90 if (!Status) {
91 return FALSE;
92 }
93
94 AbsorbLen += EncLen;
95
96 //
97 // Absorb encode_string(N).
98 //
99 EncLen = LeftEncode (EncBuf, NameLen * 8);
100 Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, EncBuf, EncLen);
101 if (!Status) {
102 return FALSE;
103 }
104
105 AbsorbLen += EncLen;
106 Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, Name, NameLen);
107 if (!Status) {
108 return FALSE;
109 }
110
111 AbsorbLen += NameLen;
112
113 //
114 // Absorb encode_string(S).
115 //
116 EncLen = LeftEncode (EncBuf, CustomizationLen * 8);
117 Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, EncBuf, EncLen);
118 if (!Status) {
119 return FALSE;
120 }
121
122 AbsorbLen += EncLen;
123 Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, Customization, CustomizationLen);
124 if (!Status) {
125 return FALSE;
126 }
127
128 AbsorbLen += CustomizationLen;
129
130 //
131 // Absorb zero padding up to rate.
132 //
133 PadLen = CSHAKE256_RATE_IN_BYTES - AbsorbLen % CSHAKE256_RATE_IN_BYTES;
134 Status = (BOOLEAN)Sha3Update ((Keccak1600_Ctx *)CShake256Context, mZeroPadding, PadLen);
135 if (!Status) {
136 return FALSE;
137 }
138
139 return TRUE;
140 }
141}
142
160BOOLEAN
161EFIAPI
163 IN OUT VOID *CShake256Context,
164 IN CONST VOID *Data,
165 IN UINTN DataSize
166 )
167{
168 //
169 // Check input parameters.
170 //
171 if (CShake256Context == NULL) {
172 return FALSE;
173 }
174
175 //
176 // Check invalid parameters, in case that only DataLength was checked in OpenSSL.
177 //
178 if ((Data == NULL) && (DataSize != 0)) {
179 return FALSE;
180 }
181
182 return (BOOLEAN)(Sha3Update ((Keccak1600_Ctx *)CShake256Context, Data, DataSize));
183}
184
203BOOLEAN
204EFIAPI
206 IN OUT VOID *CShake256Context,
207 OUT UINT8 *HashValue
208 )
209{
210 //
211 // Check input parameters.
212 //
213 if ((CShake256Context == NULL) || (HashValue == NULL)) {
214 return FALSE;
215 }
216
217 //
218 // cSHAKE-256 Hash Finalization.
219 //
220 return (BOOLEAN)(Sha3Final ((Keccak1600_Ctx *)CShake256Context, HashValue));
221}
222
244BOOLEAN
245EFIAPI
247 IN CONST VOID *Data,
248 IN UINTN DataSize,
249 IN UINTN OutputLen,
250 IN CONST VOID *Name,
251 IN UINTN NameLen,
252 IN CONST VOID *Customization,
253 IN UINTN CustomizationLen,
254 OUT UINT8 *HashValue
255 )
256{
257 BOOLEAN Status;
258 Keccak1600_Ctx Ctx;
259
260 //
261 // Check input parameters.
262 //
263 if (HashValue == NULL) {
264 return FALSE;
265 }
266
267 if ((Data == NULL) && (DataSize != 0)) {
268 return FALSE;
269 }
270
271 Status = CShake256Init (&Ctx, OutputLen, Name, NameLen, Customization, CustomizationLen);
272 if (!Status) {
273 return FALSE;
274 }
275
276 Status = CShake256Update (&Ctx, Data, DataSize);
277 if (!Status) {
278 return FALSE;
279 }
280
281 return CShake256Final (&Ctx, HashValue);
282}
UINT64 UINTN
BOOLEAN EFIAPI CShake256Init(OUT VOID *CShake256Context, IN UINTN OutputLen, IN CONST VOID *Name, IN UINTN NameLen, IN CONST VOID *Customization, IN UINTN CustomizationLen)
BOOLEAN EFIAPI CShake256Final(IN OUT VOID *CShake256Context, OUT UINT8 *HashValue)
BOOLEAN EFIAPI CShake256Update(IN OUT VOID *CShake256Context, IN CONST VOID *Data, IN UINTN DataSize)
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)
UINTN EFIAPI LeftEncode(OUT UINT8 *EncBuf, IN UINTN Value)
Definition: CryptXkcp.c:35
UINT8 EFIAPI KeccakInit(OUT Keccak1600_Ctx *Context, IN UINT8 Pad, IN UINTN BlockSize, IN UINTN MessageDigstLen)
Definition: CryptSha3.c:31
UINT8 EFIAPI Sha3Final(IN OUT Keccak1600_Ctx *Context, OUT UINT8 *MessageDigest)
Definition: CryptSha3.c:137
UINT8 EFIAPI Sha3Update(IN OUT Keccak1600_Ctx *Context, IN const VOID *Data, IN UINTN DataSize)
Definition: CryptSha3.c:66
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284