Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/symcrypt/lib/cshake_pattern.c
15010 views
1
//
2
// cshake_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 cSHAKE128 and cSHAKE256
13
//
14
// See the symcrypt.h file for documentation on what the various functions do.
15
//
16
17
18
//
19
// SymCryptCShake
20
//
21
VOID
22
SYMCRYPT_CALL
23
SYMCRYPT_Xxx(
24
_In_reads_( cbFunctionNameString ) PCBYTE pbFunctionNameString,
25
SIZE_T cbFunctionNameString,
26
_In_reads_( cbCustomizationString ) PCBYTE pbCustomizationString,
27
SIZE_T cbCustomizationString,
28
_In_reads_( cbData ) PCBYTE pbData,
29
SIZE_T cbData,
30
_Out_writes_( cbResult ) PBYTE pbResult,
31
SIZE_T cbResult)
32
{
33
SYMCRYPT_XXX_STATE state;
34
35
SYMCRYPT_XxxInit(&state,
36
pbFunctionNameString, cbFunctionNameString,
37
pbCustomizationString, cbCustomizationString);
38
39
SYMCRYPT_XxxAppend(&state, pbData, cbData);
40
SYMCRYPT_XxxExtract(&state, pbResult, cbResult, TRUE);
41
}
42
43
44
//
45
// SymCryptCShakeInit
46
//
47
VOID
48
SYMCRYPT_CALL
49
SYMCRYPT_XxxInit(
50
_Out_ PSYMCRYPT_XXX_STATE pState,
51
_In_reads_( cbFunctionNameString ) PCBYTE pbFunctionNameString,
52
SIZE_T cbFunctionNameString,
53
_In_reads_( cbCustomizationString ) PCBYTE pbCustomizationString,
54
SIZE_T cbCustomizationString)
55
{
56
C_ASSERT( sizeof(SYMCRYPT_XXX_STATE) == sizeof(SYMCRYPT_SHAKEXXX_STATE) );
57
58
SYMCRYPT_SHAKEXXX_INIT( (SYMCRYPT_SHAKEXXX_STATE*)pState );
59
60
// Perform cSHAKE processing of input strings when any of the input strings is non-empty
61
if (cbFunctionNameString != 0 || cbCustomizationString != 0)
62
{
63
// cSHAKE and SHAKE have different paddings. pState->paddingValue
64
// is set to SYMCRYPT_SHAKE_PADDING_VALUE in the SHAKE initialization above.
65
// We update the padding value here because at least one of the input strings
66
// is non-empty and cSHAKE will not default to SHAKE.
67
pState->ks.paddingValue = SYMCRYPT_CSHAKE_PADDING_VALUE;
68
69
SymCryptCShakeEncodeInputStrings(&pState->ks,
70
pbFunctionNameString, cbFunctionNameString,
71
pbCustomizationString, cbCustomizationString);
72
}
73
74
SYMCRYPT_SET_MAGIC(pState);
75
}
76
77
//
78
// SymCryptCShakeAppend
79
//
80
VOID
81
SYMCRYPT_CALL
82
SYMCRYPT_XxxAppend(
83
_Inout_ PSYMCRYPT_XXX_STATE pState,
84
_In_reads_( cbData ) PCBYTE pbData,
85
SIZE_T cbData )
86
{
87
// Fixing of the padding value
88
//
89
// SymCryptKeccakAppend will reset the state, switch to absorb mode,
90
// and append data to the empty state if the state was in squeeze mode
91
// when Append is called. This behavior is equivalent to initializing
92
// cSHAKE with empty input strings, which makes cSHAKE a SHAKE instance.
93
//
94
// cSHAKE and SHAKE have different paddings, so we have to update the
95
// padding value in case it was cSHAKE padding before.
96
if (pState->ks.squeezeMode)
97
{
98
pState->ks.paddingValue = SYMCRYPT_SHAKE_PADDING_VALUE;
99
}
100
101
SymCryptKeccakAppend(&pState->ks, pbData, cbData);
102
}
103
104
//
105
// SymCryptCShakeExtract
106
//
107
VOID
108
SYMCRYPT_CALL
109
SYMCRYPT_XxxExtract(
110
_Inout_ PSYMCRYPT_XXX_STATE pState,
111
_Out_writes_(cbResult) PBYTE pbResult,
112
SIZE_T cbResult,
113
BOOLEAN bWipe)
114
{
115
SymCryptKeccakExtract(&pState->ks, pbResult, cbResult, bWipe);
116
117
if (bWipe)
118
{
119
// If the state was wiped, set the state as if cSHAKE was initialized
120
// with empty strings, which is equivalent to empty SHAKE state.
121
// We have no way to store the Function Name string and Customization
122
// string information to go back to the initial cSHAKE state.
123
pState->ks.paddingValue = SYMCRYPT_SHAKE_PADDING_VALUE;
124
}
125
}
126
127
//
128
// SymCryptCShakeResult
129
//
130
VOID
131
SYMCRYPT_CALL
132
SYMCRYPT_XxxResult(
133
_Inout_ PSYMCRYPT_XXX_STATE pState,
134
_Out_writes_( SYMCRYPT_CSHAKEXXX_RESULT_SIZE ) PBYTE pbResult)
135
{
136
SymCryptKeccakExtract(&pState->ks, pbResult, SYMCRYPT_CSHAKEXXX_RESULT_SIZE, TRUE);
137
138
// Revert to cSHAKE initialized with empty strings state, i.e., empty SHAKE state
139
pState->ks.paddingValue = SYMCRYPT_SHAKE_PADDING_VALUE;
140
}
141
142
//
143
// SymCryptCShakeStateCopy
144
//
145
VOID
146
SYMCRYPT_CALL
147
SYMCRYPT_XxxStateCopy(_In_ const SYMCRYPT_XXX_STATE* pSrc, _Out_ SYMCRYPT_XXX_STATE* pDst)
148
{
149
SYMCRYPT_CHECK_MAGIC(pSrc);
150
*pDst = *pSrc;
151
SYMCRYPT_SET_MAGIC(pDst);
152
}
153
154