ReactOS 0.4.17-dev-1005-g171e1de
kmac_pattern.c
Go to the documentation of this file.
1//
2// kmac_pattern.c
3//
4// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
5//
6
7#if 0
8#pragma makedep header
9#endif
10
11//
12// This source file implements KMAC128 and KMAC256
13//
14// See the symcrypt.h file for documentation on what the various functions do.
15//
16
17
18//
19// SymCryptKmac
20//
21VOID
25 _In_reads_bytes_( cbInput ) PCBYTE pbInput,
26 SIZE_T cbInput,
28{
30
32 SYMCRYPT_XxxAppend(&state, pbInput, cbInput);
34}
35
36//
37// SymCryptKmacEx
38//
39VOID
43 _In_reads_bytes_( cbInput ) PCBYTE pbInput,
44 SIZE_T cbInput,
46 SIZE_T cbResult)
47{
49
51 SYMCRYPT_XxxAppend(&state, pbInput, cbInput);
53}
54
55
56//
57// SymCryptKmacExpandKey
58//
65{
67}
68
69//
70// SymCryptKmacExpandKeyEx
71//
78 _In_reads_bytes_( cbCustomizationString ) PCBYTE pbCustomizationString,
79 SIZE_T cbCustomizationString)
80{
81 static const BYTE nameString[] = { 0x4b, 0x4d, 0x41, 0x43 }; // "KMAC"
82
84
85 SYMCRYPT_CSHAKEXXX_INIT( (SYMCRYPT_CSHAKEXXX_STATE*)pExpandedKey, nameString, sizeof(nameString), pbCustomizationString, cbCustomizationString);
86
88
89 // byte_pad( encode_string( K ) )
90 SymCryptKeccakAppendEncodeTimes8(pks, pks->inputBlockSize / 8, TRUE);
92
93 if (pks->stateIndex != 0)
94 {
96 }
97
98 return SYMCRYPT_NO_ERROR;
99}
100
101//
102// SymCryptKmacInit
103//
104VOID
109{
110 C_ASSERT(sizeof(*pState) == sizeof(*pExpandedKey));
111
113 memcpy(pState, pExpandedKey, sizeof(*pState));
115}
116
117//
118// SymCryptKmacAppend
119//
120VOID
125 SIZE_T cbData )
126{
127 SYMCRYPT_ASSERT(!pState->ks.squeezeMode);
129}
130
131//
132// SymCryptKmacExtract
133//
134VOID
138 _Out_writes_( cbOutput ) PBYTE pbOutput,
139 SIZE_T cbOutput,
140 BOOLEAN bWipe)
141{
142 // This function uses KMAC in XOF mode.
143 //
144 // If this is the first time Extract is being called, append right_encode(0)
145 // to indicate that we're in XOF mode. This padding will be applied only once
146 // as SymCryptKeccakExtract will transition the state to squeeze mode.
147 if (!pState->ks.squeezeMode)
148 {
150 }
151
152 SymCryptKeccakExtract(&pState->ks, pbOutput, cbOutput, bWipe);
153}
154
155//
156// SymCryptKmacResult
157//
158VOID
163{
165}
166
167
168//
169// SymCryptKmacResultEx
170//
171VOID
175 _Out_writes_( cbOutput ) PBYTE pbOutput,
176 SIZE_T cbOutput)
177{
178 // Result and ResultEx functions are used to extract data only once.
179 // KMAC requires the output length to be encoded and appended to the
180 // end of the input before the state switches to squeeze mode.
181 //
182 // If Result or ResultEx is called after an Extract call with bWipe=FALSE,
183 // this means KMAC was used in XOF mode and length padding has already been
184 // applied. In this case, Result and ResultEx functions extract data one last
185 // time in XOF mode and wipe the state afterwards.
186
187 if (!pState->ks.squeezeMode)
188 {
189 // Append right_encode(L)
191 }
192
193 SymCryptKeccakExtract(&pState->ks, pbOutput, cbOutput, TRUE);
194}
195
196//
197// SymCryptKmacKeyCopy
198//
199VOID
202{
204 *pDst = *pSrc;
205 SYMCRYPT_SET_MAGIC(pDst);
206}
207
208//
209// SymCryptKmacStateCopy
210//
211VOID
214{
216 *pDst = *pSrc;
217 SYMCRYPT_SET_MAGIC(pDst);
218}
unsigned char BOOLEAN
Definition: actypes.h:127
static int state
Definition: maze.c:121
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define C_ASSERT(e)
Definition: intsafe.h:73
#define SYMCRYPT_CSHAKEXXX_INIT
Definition: kmac.c:73
#define SYMCRYPT_KMACXXX_RESULT_SIZE
Definition: kmac.c:75
#define SYMCRYPT_CSHAKEXXX_STATE
Definition: kmac.c:74
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define _In_reads_bytes_(s)
Definition: no_sal2.h:170
#define _In_reads_(s)
Definition: no_sal2.h:168
#define _Inout_
Definition: no_sal2.h:162
#define _Out_writes_(s)
Definition: no_sal2.h:176
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _Out_writes_bytes_(s)
Definition: no_sal2.h:178
BYTE * PBYTE
Definition: pedump.c:66
#define SYMCRYPT_XxxExtract
Definition: sc_lib.h:138
VOID SYMCRYPT_CALL SymCryptKeccakAppendEncodeTimes8(_Inout_ SYMCRYPT_KECCAK_STATE *pState, UINT64 uValue, BOOLEAN bLeftEncode)
Definition: shake.c:310
VOID SYMCRYPT_CALL SymCryptKeccakZeroAppendBlock(_Inout_ PSYMCRYPT_KECCAK_STATE pState)
Definition: sha3.c:273
#define PSYMCRYPT_XXX_EXPANDED_KEY
Definition: sc_lib.h:132
#define SYMCRYPT_XxxInit
Definition: sc_lib.h:123
VOID SYMCRYPT_CALL SymCryptKeccakAppendEncodedString(_Inout_ PSYMCRYPT_KECCAK_STATE pState, _In_reads_(cbString) PCBYTE pbString, SIZE_T cbString)
Definition: shake.c:330
#define SYMCRYPT_XxxAppend
Definition: sc_lib.h:124
#define SYMCRYPT_XxxEx
Definition: sc_lib.h:134
#define SYMCRYPT_XxxExpandKey
Definition: sc_lib.h:136
#define SYMCRYPT_XXX_EXPANDED_KEY
Definition: sc_lib.h:131
VOID SYMCRYPT_CALL SymCryptKeccakAppend(_Inout_ PSYMCRYPT_KECCAK_STATE pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
Definition: sha3.c:285
#define SYMCRYPT_XXX_STATE
Definition: sc_lib.h:116
#define SYMCRYPT_Xxx
Definition: sc_lib.h:120
#define SYMCRYPT_XxxExpandKeyEx
Definition: sc_lib.h:137
#define PSYMCRYPT_XXX_STATE
Definition: sc_lib.h:117
#define SYMCRYPT_XxxKeyCopy
Definition: sc_lib.h:140
#define SYMCRYPT_XxxResultEx
Definition: sc_lib.h:139
#define PCSYMCRYPT_XXX_EXPANDED_KEY
Definition: sc_lib.h:133
#define SYMCRYPT_XxxResult
Definition: sc_lib.h:125
VOID SYMCRYPT_CALL SymCryptKeccakExtract(_Inout_ PSYMCRYPT_KECCAK_STATE pState, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult, BOOLEAN bWipe)
Definition: sha3.c:411
#define SYMCRYPT_XxxStateCopy
Definition: sc_lib.h:122
static const BYTE pbResult[]
#define SYMCRYPT_ASSERT(_x)
Definition: symcrypt.h:10807
SYMCRYPT_ERROR
Definition: symcrypt.h:227
#define SYMCRYPT_CALL
PCBYTE pbKey
SYMCRYPT_KECCAK_STATE
PCBYTE SIZE_T cbKey
PCBYTE PBYTE SIZE_T cbData
#define SYMCRYPT_SET_MAGIC(p)
PSYMCRYPT_COMMON_HASH_STATE pState
const BYTE * PCBYTE
PCBYTE pbData
#define SYMCRYPT_CHECK_MAGIC(p)
PCVOID pExpandedKey
ULONG_PTR SIZE_T
Definition: typedefs.h:80
unsigned char BYTE
Definition: xxhash.c:193