forked from microsoft/SymCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhkdf.c
231 lines (190 loc) · 6.84 KB
/
hkdf.c
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
//
// hkdf.c
//
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
//
// This module contains the routines to implement the HKDF
// function for the TLS protocol 1.3. It is used in
// the protocol's key derivation function.
//
//
#include "precomp.h"
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHkdfExpandKey(
_Out_ PSYMCRYPT_HKDF_EXPANDED_KEY pExpandedKey,
_In_ PCSYMCRYPT_MAC macAlgorithm,
_In_reads_(cbIkm) PCBYTE pbIkm,
SIZE_T cbIkm,
_In_reads_opt_(cbSalt) PCBYTE pbSalt,
SIZE_T cbSalt )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
SYMCRYPT_ALIGN BYTE rbPrk[SYMCRYPT_MAC_MAX_RESULT_SIZE] = { 0 };
SYMCRYPT_ASSERT( macAlgorithm->expandedKeySize <= sizeof( pExpandedKey->macKey ) );
scError = SymCryptHkdfExtractPrk( macAlgorithm, pbIkm, cbIkm, pbSalt, cbSalt, rbPrk, macAlgorithm->resultSize );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
scError = SymCryptHkdfPrkExpandKey( pExpandedKey, macAlgorithm, rbPrk, macAlgorithm->resultSize );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
cleanup:
SymCryptWipeKnownSize(&rbPrk[0], sizeof(rbPrk));
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHkdfExtractPrk(
_In_ PCSYMCRYPT_MAC macAlgorithm,
_In_reads_(cbIkm) PCBYTE pbIkm,
SIZE_T cbIkm,
_In_reads_opt_(cbSalt) PCBYTE pbSalt,
SIZE_T cbSalt,
_Out_writes_(cbPrk) PBYTE pbPrk,
SIZE_T cbPrk )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
SYMCRYPT_MAC_STATE state;
SYMCRYPT_MAC_EXPANDED_KEY key;
// Ensure that pbPrk is the correct size
if (cbPrk != macAlgorithm->resultSize)
{
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
// Calculation of PRK = HMAC-Hash(salt, IKM)
scError = macAlgorithm->expandKeyFunc( &key, pbSalt, cbSalt );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
macAlgorithm->initFunc( &state, &key );
macAlgorithm->appendFunc( &state, pbIkm, cbIkm );
macAlgorithm->resultFunc( &state, pbPrk );
cleanup:
SymCryptWipeKnownSize(&key, sizeof(key));
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHkdfPrkExpandKey(
_Out_ PSYMCRYPT_HKDF_EXPANDED_KEY pExpandedKey,
_In_ PCSYMCRYPT_MAC macAlgorithm,
_In_reads_(cbPrk) PCBYTE pbPrk,
SIZE_T cbPrk )
{
SYMCRYPT_ASSERT( macAlgorithm->expandedKeySize <= sizeof( pExpandedKey->macKey ) );
pExpandedKey->macAlg = macAlgorithm;
return macAlgorithm->expandKeyFunc( &pExpandedKey->macKey, pbPrk, cbPrk );
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHkdfDerive(
_In_ PCSYMCRYPT_HKDF_EXPANDED_KEY pExpandedKey,
_In_reads_opt_(cbInfo) PCBYTE pbInfo,
SIZE_T cbInfo,
_Out_writes_(cbResult) PBYTE pbResult,
SIZE_T cbResult)
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
SYMCRYPT_MAC_STATE state;
PCSYMCRYPT_MAC pMacAlgorithm = pExpandedKey->macAlg;
SYMCRYPT_ALIGN BYTE rbPartialResult[SYMCRYPT_MAC_MAX_RESULT_SIZE];
BYTE * pbCurr = pbResult;
SIZE_T cbMacResultSize = pMacAlgorithm->resultSize;
BYTE cntr = 0x01;
// Check that cbResult <= 255*HashLen
if (cbResult > 0xff * cbMacResultSize)
{
scError = SYMCRYPT_WRONG_DATA_SIZE;
goto cleanup;
}
// In the first iteration T(0) is the empty string
// Calculate T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
pMacAlgorithm->initFunc( &state, pExpandedKey );
pMacAlgorithm->appendFunc( &state, pbInfo, cbInfo );
pMacAlgorithm->appendFunc( &state, &cntr, sizeof(cntr) );
pMacAlgorithm->resultFunc( &state, rbPartialResult );
// Store the result in the output buffer
memcpy(pbCurr, rbPartialResult, SYMCRYPT_MIN(cbResult, cbMacResultSize));
if (cbResult <= cbMacResultSize)
{
goto cleanup;
}
// Update counters
cntr++;
pbCurr += cbMacResultSize;
cbResult -= cbMacResultSize;
while( cbResult > 0 )
{
// Calculate T(i) = HMAC-Hash(PRK, T(i-1) | info | 0xi)
pMacAlgorithm->initFunc( &state, pExpandedKey );
pMacAlgorithm->appendFunc( &state, rbPartialResult, cbMacResultSize );
pMacAlgorithm->appendFunc( &state, pbInfo, cbInfo );
pMacAlgorithm->appendFunc( &state, &cntr, sizeof(cntr) );
pMacAlgorithm->resultFunc( &state, rbPartialResult );
// Store the result in the output buffer
memcpy(pbCurr, rbPartialResult, SYMCRYPT_MIN(cbResult, cbMacResultSize));
if (cbResult <= cbMacResultSize)
{
goto cleanup;
}
// Update counters
cntr++;
pbCurr += cbMacResultSize;
cbResult -= cbMacResultSize;
}
cleanup:
SymCryptWipeKnownSize(&rbPartialResult[0], sizeof(rbPartialResult));
return scError;
}
//
// The full HKDF
//
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHkdf(
PCSYMCRYPT_MAC macAlgorithm,
_In_reads_(cbIkm) PCBYTE pbIkm,
SIZE_T cbIkm,
_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 scError = SYMCRYPT_NO_ERROR;
SYMCRYPT_HKDF_EXPANDED_KEY key;
// Create the expanded key
scError = SymCryptHkdfExpandKey(
&key,
macAlgorithm,
pbIkm,
cbIkm,
pbSalt,
cbSalt );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// Derive the key
scError = SymCryptHkdfDerive(
&key,
pbInfo,
cbInfo,
pbResult,
cbResult );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
cleanup:
SymCryptWipeKnownSize(&key, sizeof(key));
return scError;
}