ReactOS 0.4.17-dev-1005-g171e1de
sskdf.c File Reference
#include "precomp.h"
Include dependency graph for sskdf.c:

Go to the source code of this file.

Macros

#define SYMCRYPT_SSKDF_KMAC_128_DEFAULT_SALT_SIZE   (164)
 
#define SYMCRYPT_SSKDF_KMAC_256_DEFAULT_SALT_SIZE   (132)
 
#define SYMCRYPT_SSKDF_DEFAULT_SALT_MAX   SYMCRYPT_SSKDF_KMAC_128_DEFAULT_SALT_SIZE
 

Functions

SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMacExpandSalt (_Out_ PSYMCRYPT_SSKDF_MAC_EXPANDED_SALT pExpandedSalt, _In_ PCSYMCRYPT_MAC macAlgorithm, _In_reads_opt_(cbSalt) PCBYTE pbSalt, SIZE_T cbSalt)
 
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMacDerive (_In_ PCSYMCRYPT_SSKDF_MAC_EXPANDED_SALT pExpandedSalt, SIZE_T cbMacOutputSize, _In_reads_(cbSecret) PCBYTE pbSecret, SIZE_T cbSecret, _In_reads_opt_(cbInfo) PCBYTE pbInfo, SIZE_T cbInfo, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
 
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMac (_In_ PCSYMCRYPT_MAC macAlgorithm, SIZE_T cbMacOutputSize, _In_reads_(cbSecret) PCBYTE pbSecret, SIZE_T cbSecret, _In_reads_opt_(cbSalt) PCBYTE pbSalt, SIZE_T cbSalt, _In_reads_opt_(cbInfo) PCBYTE pbInfo, SIZE_T cbInfo, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
 
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfHash (_In_ PCSYMCRYPT_HASH hashAlgorithm, SIZE_T cbHashOutputSize, _In_reads_(cbSecret) PCBYTE pbSecret, SIZE_T cbSecret, _In_reads_opt_(cbInfo) PCBYTE pbInfo, SIZE_T cbInfo, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
 

Variables

static const BYTE pbKmacCustomizationString [3] = { 'K', 'D', 'F' }
 

Macro Definition Documentation

◆ SYMCRYPT_SSKDF_DEFAULT_SALT_MAX

#define SYMCRYPT_SSKDF_DEFAULT_SALT_MAX   SYMCRYPT_SSKDF_KMAC_128_DEFAULT_SALT_SIZE

Definition at line 20 of file sskdf.c.

◆ SYMCRYPT_SSKDF_KMAC_128_DEFAULT_SALT_SIZE

#define SYMCRYPT_SSKDF_KMAC_128_DEFAULT_SALT_SIZE   (164)

Definition at line 18 of file sskdf.c.

◆ SYMCRYPT_SSKDF_KMAC_256_DEFAULT_SALT_SIZE

#define SYMCRYPT_SSKDF_KMAC_256_DEFAULT_SALT_SIZE   (132)

Definition at line 19 of file sskdf.c.

Function Documentation

◆ SymCryptSskdfHash()

SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfHash ( _In_ PCSYMCRYPT_HASH  hashAlgorithm,
SIZE_T  cbHashOutputSize,
_In_reads_(cbSecret) PCBYTE  pbSecret,
SIZE_T  cbSecret,
_In_reads_opt_(cbInfo) PCBYTE  pbInfo,
SIZE_T  cbInfo,
_Out_writes_(cbResult) PBYTE  pbResult,
SIZE_T  cbResult 
)

Definition at line 221 of file sskdf.c.

230{
232
233 PBYTE pbCurr = pbResult;
234
235 SIZE_T cbHashResultSize = hashAlgorithm->resultSize;
236 SIZE_T cbPartialResult;
237 UINT32 cntr = 1;
238 BYTE ctrBuf[4];
239
240 if ( cbHashOutputSize > 64 ||
241 cbHashOutputSize > 0 && cbHashOutputSize != hashAlgorithm->resultSize )
242 {
243 return SYMCRYPT_INVALID_ARGUMENT;
244 }
245
246 while ( cbResult > 0 )
247 {
248 SYMCRYPT_STORE_MSBFIRST32( ctrBuf, cntr );
249
250 cbPartialResult = SYMCRYPT_MIN( cbResult, cbHashResultSize );
251
252 // Calculate K(i) = H(counter || Z || FixedInfo)
253 SymCryptHashInit( hashAlgorithm, &state );
254 SymCryptHashAppend( hashAlgorithm, &state, ctrBuf, sizeof( ctrBuf ) );
255 SymCryptHashAppend( hashAlgorithm, &state, pbSecret, cbSecret );
256 SymCryptHashAppend( hashAlgorithm, &state, pbInfo, cbInfo );
257 SymCryptHashResult( hashAlgorithm, &state, pbCurr, cbPartialResult );
258
259 // Update counters
260 cntr++;
261 pbCurr += cbPartialResult;
262 cbResult -= cbPartialResult;
263 }
264
265 return SYMCRYPT_NO_ERROR;
266}
static int state
Definition: maze.c:121
BYTE * PBYTE
Definition: pedump.c:66
static const BYTE pbResult[]
VOID SYMCRYPT_CALL SymCryptHashAppend(_In_ PCSYMCRYPT_HASH pHash, _Inout_updates_bytes_(pHash->stateSize) PVOID pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
Definition: hash.c:182
VOID SYMCRYPT_CALL SymCryptHashInit(_In_ PCSYMCRYPT_HASH pHash, _Out_writes_bytes_(pHash->stateSize) PVOID pState)
Definition: hash.c:173
#define SYMCRYPT_STORE_MSBFIRST32(p, v)
Definition: symcrypt.h:311
VOID SYMCRYPT_CALL SymCryptHashResult(_In_ PCSYMCRYPT_HASH pHash, _Inout_updates_bytes_(pHash->stateSize) PVOID pState, _Out_writes_(SYMCRYPT_MIN(cbResult, pHash->resultSize)) PBYTE pbResult, SIZE_T cbResult)
Definition: hash.c:193
#define SYMCRYPT_MIN(_a, _b)
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t UINT32
Definition: typedefs.h:59
unsigned char BYTE
Definition: xxhash.c:193

◆ SymCryptSskdfMac()

SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMac ( _In_ PCSYMCRYPT_MAC  macAlgorithm,
SIZE_T  cbMacOutputSize,
_In_reads_(cbSecret) PCBYTE  pbSecret,
SIZE_T  cbSecret,
_In_reads_opt_(cbSalt) PCBYTE  pbSalt,
SIZE_T  cbSalt,
_In_reads_opt_(cbInfo) PCBYTE  pbInfo,
SIZE_T  cbInfo,
_Out_writes_(cbResult) PBYTE  pbResult,
SIZE_T  cbResult 
)

Definition at line 180 of file sskdf.c.

191{
192 SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
194
195 scError = SymCryptSskdfMacExpandSalt( &expandedSalt, macAlgorithm, pbSalt, cbSalt );
196
197 if ( scError != SYMCRYPT_NO_ERROR )
198 {
199 goto cleanup;
200 }
201
202 scError = SymCryptSskdfMacDerive(
203 &expandedSalt,
204 cbMacOutputSize,
205 pbSecret,
206 cbSecret,
207 pbInfo,
208 cbInfo,
209 pbResult,
210 cbResult );
211
212cleanup:
213
214 SymCryptWipeKnownSize( &expandedSalt, sizeof( expandedSalt ) );
215
216 return scError;
217}
static void cleanup(void)
Definition: main.c:1335
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMacDerive(_In_ PCSYMCRYPT_SSKDF_MAC_EXPANDED_SALT pExpandedSalt, SIZE_T cbMacOutputSize, _In_reads_(cbSecret) PCBYTE pbSecret, SIZE_T cbSecret, _In_reads_opt_(cbInfo) PCBYTE pbInfo, SIZE_T cbInfo, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
Definition: sskdf.c:87
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMacExpandSalt(_Out_ PSYMCRYPT_SSKDF_MAC_EXPANDED_SALT pExpandedSalt, _In_ PCSYMCRYPT_MAC macAlgorithm, _In_reads_opt_(cbSalt) PCBYTE pbSalt, SIZE_T cbSalt)
Definition: sskdf.c:27
FORCEINLINE VOID SYMCRYPT_CALL SymCryptWipeKnownSize(_Out_writes_bytes_(cbData) PVOID pbData, SIZE_T cbData)
SYMCRYPT_ERROR
Definition: symcrypt.h:227
PCSYMCRYPT_MAC macAlgorithm

◆ SymCryptSskdfMacDerive()

SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMacDerive ( _In_ PCSYMCRYPT_SSKDF_MAC_EXPANDED_SALT  pExpandedSalt,
SIZE_T  cbMacOutputSize,
_In_reads_(cbSecret) PCBYTE  pbSecret,
SIZE_T  cbSecret,
_In_reads_opt_(cbInfo) PCBYTE  pbInfo,
SIZE_T  cbInfo,
_Out_writes_(cbResult) PBYTE  pbResult,
SIZE_T  cbResult 
)

Definition at line 87 of file sskdf.c.

96{
97 SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
99 PCSYMCRYPT_MAC pMacAlgorithm = pExpandedSalt->macAlg;
100 PSYMCRYPT_MAC_RESULT_EX resultFuncEx = NULL;
101
103 PBYTE pbCurr = pbResult;
104
105 SIZE_T cbMacResultSize = pMacAlgorithm->resultSize;
106 SIZE_T cbBlock;
107 UINT32 cntr = 1;
108 BYTE ctrBuf[4];
109
110 if ( cbMacOutputSize > 64 )
111 {
112 scError = SYMCRYPT_INVALID_ARGUMENT;
113 goto cleanup;
114 }
115
116 if ( pMacAlgorithm == SymCryptKmac128Algorithm )
117 {
118 cbMacResultSize = cbMacOutputSize;
119 resultFuncEx = SymCryptKmac128ResultEx;
120 }
121 else if ( pMacAlgorithm == SymCryptKmac256Algorithm )
122 {
123 cbMacResultSize = cbMacOutputSize;
124 resultFuncEx = SymCryptKmac256ResultEx;
125 }
126 else if ( cbMacOutputSize > 0 && cbMacOutputSize != pMacAlgorithm->resultSize )
127 {
128 scError = SYMCRYPT_INVALID_ARGUMENT;
129 goto cleanup;
130 }
131
132 while ( cbResult > 0 )
133 {
134 SYMCRYPT_STORE_MSBFIRST32( ctrBuf, cntr );
135
136 // Calculate K(i) = H(counter || Z || FixedInfo)
137 pMacAlgorithm->initFunc( &state, &pExpandedSalt->macKey );
138 pMacAlgorithm->appendFunc( &state, ctrBuf, sizeof( ctrBuf ) );
139 pMacAlgorithm->appendFunc( &state, pbSecret, cbSecret );
140 pMacAlgorithm->appendFunc( &state, pbInfo, cbInfo );
141
142 cbBlock = SYMCRYPT_MIN( cbResult, cbMacResultSize );
143
144 if ( resultFuncEx != NULL )
145 {
146 if ( cbMacOutputSize > 0 )
147 {
148 resultFuncEx( &state, rbPartialResult, cbMacOutputSize );
149 }
150 else
151 {
152 // If the output size is not specified, calculate the full result
153 resultFuncEx( &state, pbResult, cbResult );
154 break;
155 }
156 }
157 else
158 {
159 pMacAlgorithm->resultFunc( &state, rbPartialResult );
160 }
161
162 // Store the result in the output buffer
163 memcpy( pbCurr, rbPartialResult, cbBlock );
164
165 // Update counters
166 cntr++;
167 pbCurr += cbBlock;
168 cbResult -= cbBlock;
169 }
170
171cleanup:
172
173 SymCryptWipeKnownSize( &rbPartialResult[0], sizeof( rbPartialResult ) );
174
175 return scError;
176}
#define NULL
Definition: types.h:112
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
PSYMCRYPT_MAC_RESULT resultFunc
PSYMCRYPT_MAC_APPEND appendFunc
PSYMCRYPT_MAC_INIT initFunc
const PCSYMCRYPT_MAC SymCryptKmac256Algorithm
Definition: kmac.c:96
const PCSYMCRYPT_MAC SymCryptKmac128Algorithm
Definition: kmac.c:38
VOID SYMCRYPT_CALL SymCryptKmac256ResultEx(_Inout_ PSYMCRYPT_KMAC256_STATE pState, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
VOID SYMCRYPT_CALL SymCryptKmac128ResultEx(_Inout_ PSYMCRYPT_KMAC128_STATE pState, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
#define SYMCRYPT_ALIGN
VOID(SYMCRYPT_CALL * PSYMCRYPT_MAC_RESULT_EX)(PVOID pState, PVOID pbResult, SIZE_T cbResult)
#define SYMCRYPT_MAC_MAX_RESULT_SIZE

Referenced by SymCryptSskdfMac().

◆ SymCryptSskdfMacExpandSalt()

SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSskdfMacExpandSalt ( _Out_ PSYMCRYPT_SSKDF_MAC_EXPANDED_SALT  pExpandedSalt,
_In_ PCSYMCRYPT_MAC  macAlgorithm,
_In_reads_opt_(cbSalt) PCBYTE  pbSalt,
SIZE_T  cbSalt 
)

Definition at line 27 of file sskdf.c.

32{
33 SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
34
35 const BYTE pbSaltDefault[SYMCRYPT_SSKDF_DEFAULT_SALT_MAX] = { 0 };
36
37 SYMCRYPT_ASSERT( macAlgorithm->expandedKeySize <= sizeof( pExpandedSalt->macKey ) );
38
39 pExpandedSalt->macAlg = macAlgorithm;
40
41 if ( pbSalt == NULL )
42 {
44 {
46 }
48 {
50 }
51 else
52 {
54 }
55
56 pbSalt = pbSaltDefault;
57 }
58
60 {
62 &pExpandedSalt->macKey.kmac128Key,
63 pbSalt,
64 cbSalt,
66 sizeof( pbKmacCustomizationString ) );
67 }
69 {
71 &pExpandedSalt->macKey.kmac256Key,
72 pbSalt,
73 cbSalt,
75 sizeof( pbKmacCustomizationString ) );
76 }
77 else
78 {
79 scError = macAlgorithm->expandKeyFunc( &pExpandedSalt->macKey, pbSalt, cbSalt );
80 }
81
82 return scError;
83}
#define SYMCRYPT_SSKDF_KMAC_128_DEFAULT_SALT_SIZE
Definition: sskdf.c:18
#define SYMCRYPT_SSKDF_KMAC_256_DEFAULT_SALT_SIZE
Definition: sskdf.c:19
#define SYMCRYPT_SSKDF_DEFAULT_SALT_MAX
Definition: sskdf.c:20
static const BYTE pbKmacCustomizationString[3]
Definition: sskdf.c:23
const PCSYMCRYPT_HASH * ppHashAlgorithm
PSYMCRYPT_MAC_EXPAND_KEY expandKeyFunc
#define SYMCRYPT_ASSERT(_x)
Definition: symcrypt.h:10807
SIZE_T SYMCRYPT_CALL SymCryptHashInputBlockSize(_In_ PCSYMCRYPT_HASH pHash)
Definition: hash.c:140
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptKmac256ExpandKeyEx(_Out_ PSYMCRYPT_KMAC256_EXPANDED_KEY pExpandedKey, _In_reads_bytes_(cbKey) PCBYTE pbKey, SIZE_T cbKey, _In_reads_bytes_(cbCustomizationString) PCBYTE pbCustomizationString, SIZE_T cbCustomizationString)
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptKmac128ExpandKeyEx(_Out_ PSYMCRYPT_KMAC128_EXPANDED_KEY pExpandedKey, _In_reads_bytes_(cbKey) PCBYTE pbKey, SIZE_T cbKey, _In_reads_bytes_(cbCustomizationString) PCBYTE pbCustomizationString, SIZE_T cbCustomizationString)

Referenced by SymCryptSskdfMac().

Variable Documentation

◆ pbKmacCustomizationString

const BYTE pbKmacCustomizationString[3] = { 'K', 'D', 'F' }
static

Definition at line 23 of file sskdf.c.

Referenced by SymCryptSskdfMacExpandSalt().