-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathhpke_kdf.c
More file actions
691 lines (604 loc) · 23.2 KB
/
Copy pathhpke_kdf.c
File metadata and controls
691 lines (604 loc) · 23.2 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//
// hpke_kdf.c
//
// HPKE KDF primitives and the key-schedule support that composes them:
// LabeledExtract/LabeledExpand (two-stage HKDF) and the OneStage SHAKE-backed
// equivalent, plus suite_id construction, CombineSecrets_{One,Two}Stage, and
// SecretExport.
//
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
#include "precomp.h"
#include "hpke_internal.h"
static PCSYMCRYPT_MAC
SYMCRYPT_CALL
SymCryptHpkeHkdfMacFromId(
SYMCRYPT_HPKE_KDF_ID kdfId )
{
switch ( kdfId )
{
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA256:
return SymCryptGetMacAlgorithm( SYMCRYPT_MAC_ID_HMAC_SHA256 );
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA384:
return SymCryptGetMacAlgorithm( SYMCRYPT_MAC_ID_HMAC_SHA384 );
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA512:
return SymCryptGetMacAlgorithm( SYMCRYPT_MAC_ID_HMAC_SHA512 );
default:
SYMCRYPT_ASSERT( FALSE );
return NULL;
}
}
//
// Get the hash output size (Nh) for the KDF from its ID.
//
UINT16
SYMCRYPT_CALL
SymCryptHpkeKdfOutputSizeFromId(
SYMCRYPT_HPKE_KDF_ID kdfId )
{
switch ( kdfId )
{
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA256: return 32;
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA384: return 48;
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA512: return 64;
case SYMCRYPT_HPKE_KDF_ID_SHAKE128: return 32;
case SYMCRYPT_HPKE_KDF_ID_SHAKE256: return 64;
default:
SYMCRYPT_ASSERT( FALSE );
return 0;
}
}
VOID
SYMCRYPT_CALL
SymCryptHpkeBuildKemSuiteId(
UINT16 kemId,
_Out_writes_bytes_( SYMCRYPT_HPKE_KEM_SUITE_ID_SIZE ) PBYTE pbSuiteId )
{
// suite_id = "KEM" || I2OSP(kem_id, 2)
memcpy( pbSuiteId, "KEM", 3 );
SYMCRYPT_STORE_MSBFIRST16( pbSuiteId + 3, kemId );
}
static VOID
SYMCRYPT_CALL
SymCryptHpkeBuildHpkeSuiteId(
UINT16 kemId,
UINT16 kdfId,
UINT16 aeadId,
_Out_writes_bytes_( SYMCRYPT_HPKE_SUITE_ID_SIZE ) PBYTE pbSuiteId )
{
// suite_id = "HPKE" || I2OSP(kem_id, 2) || I2OSP(kdf_id, 2) || I2OSP(aead_id, 2)
memcpy( pbSuiteId, "HPKE", 4 );
SYMCRYPT_STORE_MSBFIRST16( pbSuiteId + 4, kemId );
SYMCRYPT_STORE_MSBFIRST16( pbSuiteId + 6, kdfId );
SYMCRYPT_STORE_MSBFIRST16( pbSuiteId + 8, aeadId );
}
//
// LabeledExtract / LabeledExpand: two-stage HKDF wrappers. SHAKE-based KDFs
// use the OneStage path below.
//
// Longest current label is "shared_secret" (13 chars).
//
#define SYMCRYPT_HPKE_MAX_LABEL_SIZE (13)
//
// labeled_ikm = "HPKE-v1" || suite_id || label || ikm
// Maximum: version(7) + suite_id(10) + label(13) + ikm(128) = 158
//
#define SYMCRYPT_HPKE_LABELED_IKM_MAX \
(SYMCRYPT_HPKE_VERSION_LABEL_SIZE + SYMCRYPT_HPKE_SUITE_ID_SIZE + SYMCRYPT_HPKE_MAX_LABEL_SIZE + SYMCRYPT_HPKE_KDF_MAX_IKM_SIZE)
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHpkeLabeledExtract(
SYMCRYPT_HPKE_KDF_ID kdfId,
_In_reads_bytes_( cbSuiteId ) PCBYTE pbSuiteId,
SIZE_T cbSuiteId,
_In_reads_bytes_opt_( cbSalt ) PCBYTE pbSalt,
SIZE_T cbSalt,
_In_reads_bytes_( cbLabel ) PCBYTE pbLabel,
SIZE_T cbLabel,
_In_reads_bytes_opt_( cbIkm ) PCBYTE pbIkm,
SIZE_T cbIkm,
_Out_writes_bytes_( cbPrk ) PBYTE pbPrk,
SIZE_T cbPrk )
{
SYMCRYPT_ERROR scError;
BYTE labeledIkm[SYMCRYPT_HPKE_LABELED_IKM_MAX];
if ( cbIkm > SYMCRYPT_HPKE_KDF_MAX_IKM_SIZE )
{
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
SYMCRYPT_ASSERT( cbSuiteId <= SYMCRYPT_HPKE_SUITE_ID_SIZE );
SYMCRYPT_ASSERT( cbLabel <= SYMCRYPT_HPKE_MAX_LABEL_SIZE );
//
// Build labeled_ikm = "HPKE-v1" || suite_id || label || ikm
//
PBYTE pbCurr = labeledIkm;
memcpy( pbCurr, SYMCRYPT_HPKE_VERSION_LABEL, SYMCRYPT_HPKE_VERSION_LABEL_SIZE );
pbCurr += SYMCRYPT_HPKE_VERSION_LABEL_SIZE;
memcpy( pbCurr, pbSuiteId, cbSuiteId );
pbCurr += cbSuiteId;
memcpy( pbCurr, pbLabel, cbLabel );
pbCurr += cbLabel;
if ( cbIkm > 0 )
{
memcpy( pbCurr, pbIkm, cbIkm );
pbCurr += cbIkm;
}
SIZE_T cbLabeledIkm = (SIZE_T)( pbCurr - labeledIkm );
SYMCRYPT_ASSERT( cbLabeledIkm <= sizeof(labeledIkm) );
//
// Extract(salt, labeled_ikm) via SymCrypt HKDF API.
// When pbSalt is NULL and cbSalt is 0, SymCryptHkdfExtractPrk
// uses a string of zeroes of length HashLen per the HMAC specification.
//
scError = SymCryptHkdfExtractPrk(
SymCryptHpkeHkdfMacFromId( kdfId ), labeledIkm, cbLabeledIkm, pbSalt, cbSalt, pbPrk, cbPrk );
cleanup:
SymCryptWipeKnownSize( labeledIkm, sizeof(labeledIkm) );
return scError;
}
//
// labeled_info = I2OSP(L, 2) || "HPKE-v1" || suite_id || label || info
// Prefix max: 2 + version(7) + suite_id(10) + label(13) = 32. Caller info up
// to SYMCRYPT_HPKE_MAX_INFO_SIZE; small info uses the stack buffer below,
// larger info heap-allocates.
//
#define SYMCRYPT_HPKE_LABELED_INFO_PREFIX_MAX \
(2 + SYMCRYPT_HPKE_VERSION_LABEL_SIZE + SYMCRYPT_HPKE_SUITE_ID_SIZE + SYMCRYPT_HPKE_MAX_LABEL_SIZE)
#define SYMCRYPT_HPKE_LABELED_INFO_STACK_THRESHOLD (128)
#define SYMCRYPT_HPKE_LABELED_INFO_STACK_SIZE \
(SYMCRYPT_HPKE_LABELED_INFO_PREFIX_MAX + SYMCRYPT_HPKE_LABELED_INFO_STACK_THRESHOLD)
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHpkeLabeledExpand(
SYMCRYPT_HPKE_KDF_ID kdfId,
_In_reads_bytes_( cbSuiteId ) PCBYTE pbSuiteId,
SIZE_T cbSuiteId,
_In_reads_bytes_( cbPrk ) PCBYTE pbPrk,
SIZE_T cbPrk,
_In_reads_bytes_( cbLabel ) PCBYTE pbLabel,
SIZE_T cbLabel,
_In_reads_bytes_opt_( cbInfo ) PCBYTE pbInfo,
SIZE_T cbInfo,
_Out_writes_bytes_( cbResult ) PBYTE pbResult,
UINT16 cbResult )
{
SYMCRYPT_ERROR scError;
SYMCRYPT_HKDF_EXPANDED_KEY hkdfKey;
BYTE stackBuf[SYMCRYPT_HPKE_LABELED_INFO_STACK_SIZE];
PBYTE pbLabeledInfo = NULL;
PBYTE pbHeapBuf = NULL;
SIZE_T cbLabeledInfo = 0;
if ( cbInfo > SYMCRYPT_HPKE_MAX_INFO_SIZE )
{
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
SYMCRYPT_ASSERT( cbLabel <= SYMCRYPT_HPKE_MAX_LABEL_SIZE );
//
// Compute labeled_info size and select stack or heap buffer
//
cbLabeledInfo = 2 + SYMCRYPT_HPKE_VERSION_LABEL_SIZE + cbSuiteId + cbLabel + cbInfo;
if ( cbLabeledInfo <= sizeof(stackBuf) )
{
pbLabeledInfo = stackBuf;
}
else
{
pbHeapBuf = (PBYTE) SymCryptCallbackAlloc( cbLabeledInfo );
if ( pbHeapBuf == NULL )
{
scError = SYMCRYPT_MEMORY_ALLOCATION_FAILURE;
goto cleanup;
}
pbLabeledInfo = pbHeapBuf;
}
//
// Build labeled_info = I2OSP(L, 2) || "HPKE-v1" || suite_id || label || info
//
PBYTE pbCurr = pbLabeledInfo;
SYMCRYPT_STORE_MSBFIRST16( pbCurr, cbResult );
pbCurr += 2;
memcpy( pbCurr, SYMCRYPT_HPKE_VERSION_LABEL, SYMCRYPT_HPKE_VERSION_LABEL_SIZE );
pbCurr += SYMCRYPT_HPKE_VERSION_LABEL_SIZE;
memcpy( pbCurr, pbSuiteId, cbSuiteId );
pbCurr += cbSuiteId;
memcpy( pbCurr, pbLabel, cbLabel );
pbCurr += cbLabel;
if ( cbInfo > 0 )
{
memcpy( pbCurr, pbInfo, cbInfo );
pbCurr += cbInfo;
}
SYMCRYPT_ASSERT( (SIZE_T)( pbCurr - pbLabeledInfo ) == cbLabeledInfo );
//
// Expand(prk, labeled_info, L) via SymCrypt HKDF API
//
scError = SymCryptHkdfPrkExpandKey( &hkdfKey, SymCryptHpkeHkdfMacFromId( kdfId ), pbPrk, cbPrk );
if ( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
scError = SymCryptHkdfDerive( &hkdfKey, pbLabeledInfo, cbLabeledInfo, pbResult, cbResult );
cleanup:
SymCryptWipeKnownSize( &hkdfKey, sizeof(hkdfKey) );
if ( pbHeapBuf != NULL )
{
SymCryptWipe( pbHeapBuf, cbLabeledInfo );
SymCryptCallbackFree( pbHeapBuf );
}
else
{
SymCryptWipeKnownSize( stackBuf, sizeof(stackBuf) );
}
return scError;
}
//
// One-stage KDF state and primitives for SHAKE-backed HPKE KDFs.
//
typedef union _SYMCRYPT_HPKE_ONESTAGE_STATE
{
SYMCRYPT_SHAKE128_STATE shake128;
SYMCRYPT_SHAKE256_STATE shake256;
} SYMCRYPT_HPKE_ONESTAGE_STATE, *PSYMCRYPT_HPKE_ONESTAGE_STATE;
static VOID
SYMCRYPT_CALL
SymCryptHpkeOneStageInit(
_In_ SYMCRYPT_HPKE_KDF_ID kdfId,
_Out_ PSYMCRYPT_HPKE_ONESTAGE_STATE pState )
{
switch ( kdfId )
{
case SYMCRYPT_HPKE_KDF_ID_SHAKE128:
SymCryptShake128Init( &pState->shake128 );
break;
case SYMCRYPT_HPKE_KDF_ID_SHAKE256:
SymCryptShake256Init( &pState->shake256 );
break;
default:
SYMCRYPT_ASSERT( FALSE );
break;
}
}
static VOID
SYMCRYPT_CALL
SymCryptHpkeOneStageAppend(
_In_ SYMCRYPT_HPKE_KDF_ID kdfId,
_Inout_ PSYMCRYPT_HPKE_ONESTAGE_STATE pState,
_In_reads_bytes_( cbData ) PCBYTE pbData,
SIZE_T cbData )
{
switch ( kdfId )
{
case SYMCRYPT_HPKE_KDF_ID_SHAKE128:
SymCryptShake128Append( &pState->shake128, pbData, cbData );
break;
case SYMCRYPT_HPKE_KDF_ID_SHAKE256:
SymCryptShake256Append( &pState->shake256, pbData, cbData );
break;
default:
SYMCRYPT_ASSERT( FALSE );
break;
}
}
static VOID
SYMCRYPT_CALL
SymCryptHpkeOneStageExtract(
_In_ SYMCRYPT_HPKE_KDF_ID kdfId,
_Inout_ PSYMCRYPT_HPKE_ONESTAGE_STATE pState,
_Out_writes_bytes_( cbResult ) PBYTE pbResult,
SIZE_T cbResult,
BOOLEAN bWipe )
{
switch ( kdfId )
{
case SYMCRYPT_HPKE_KDF_ID_SHAKE128:
SymCryptShake128Extract( &pState->shake128, pbResult, cbResult, bWipe );
break;
case SYMCRYPT_HPKE_KDF_ID_SHAKE256:
SymCryptShake256Extract( &pState->shake256, pbResult, cbResult, bWipe );
break;
default:
SYMCRYPT_ASSERT( FALSE );
break;
}
}
//
// LabeledDerive_OneStage from draft-ietf-hpke-hpke-03:
// KDF( ikm || "HPKE-v1" || suite_id ||
// I2OSP(len(label), 2) || label || I2OSP(L, 2) || context, L )
//
VOID
SYMCRYPT_CALL
SymCryptHpkeLabeledDeriveOneStage(
SYMCRYPT_HPKE_KDF_ID kdfId,
_In_reads_bytes_( cbSuiteId ) PCBYTE pbSuiteId,
SIZE_T cbSuiteId,
_In_reads_bytes_( cbIkm ) PCBYTE pbIkm,
SIZE_T cbIkm,
_In_reads_bytes_( cbLabel ) PCBYTE pbLabel,
UINT16 cbLabel,
_In_reads_bytes_opt_( cbContext ) PCBYTE pbContext,
SIZE_T cbContext,
_Out_writes_bytes_( cbResult ) PBYTE pbResult,
UINT16 cbResult )
{
SYMCRYPT_HPKE_ONESTAGE_STATE state;
BYTE lengthBuf[2];
SymCryptHpkeOneStageInit( kdfId, &state );
if ( cbIkm > 0 )
{
SymCryptHpkeOneStageAppend( kdfId, &state, pbIkm, cbIkm );
}
SymCryptHpkeOneStageAppend( kdfId, &state, (PCBYTE) SYMCRYPT_HPKE_VERSION_LABEL, SYMCRYPT_HPKE_VERSION_LABEL_SIZE );
SymCryptHpkeOneStageAppend( kdfId, &state, pbSuiteId, cbSuiteId );
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, cbLabel );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
if ( cbLabel > 0 )
{
SymCryptHpkeOneStageAppend( kdfId, &state, pbLabel, cbLabel );
}
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, cbResult );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
if ( cbContext > 0 )
{
SymCryptHpkeOneStageAppend( kdfId, &state, pbContext, cbContext );
}
SymCryptHpkeOneStageExtract( kdfId, &state, pbResult, cbResult, TRUE );
}
//
// CombineSecrets_OneStage from draft-ietf-hpke-hpke-03 for SHAKE-backed KDFs.
// Output is consumed in order as key, base_nonce, and exporter_secret.
//
VOID
SYMCRYPT_CALL
SymCryptHpkeCombineSecretsOneStage(
_Inout_ PSYMCRYPT_HPKECONTEXT pHpkeContext,
BYTE mode,
_In_reads_bytes_( cbSharedSecret ) PCBYTE pbSharedSecret,
UINT16 cbSharedSecret,
_In_reads_bytes_opt_( cbInfo ) PCBYTE pbInfo,
UINT16 cbInfo,
_In_reads_bytes_opt_( cbPsk ) PCBYTE pbPsk,
UINT16 cbPsk,
_In_reads_bytes_opt_( cbPskId ) PCBYTE pbPskId,
UINT16 cbPskId )
{
SYMCRYPT_HPKE_KDF_ID kdfId = (SYMCRYPT_HPKE_KDF_ID) pHpkeContext->ciphersuite.kdfId;
UINT16 cbKdfOutput = SymCryptHpkeKdfOutputSizeFromId( kdfId );
UINT16 cbSecret = pHpkeContext->aeadParams.cbKey + pHpkeContext->aeadParams.cbNonce + cbKdfOutput;
BYTE lengthBuf[2];
BYTE hpkeSuiteId[SYMCRYPT_HPKE_SUITE_ID_SIZE];
SYMCRYPT_HPKE_ONESTAGE_STATE state;
SymCryptHpkeBuildHpkeSuiteId(
pHpkeContext->ciphersuite.kemId, pHpkeContext->ciphersuite.kdfId,
pHpkeContext->ciphersuite.aeadId, hpkeSuiteId );
//
// CombineSecrets_OneStage from draft-ietf-hpke-hpke-03:
// secrets = I2OSP(len(psk), 2) || psk ||
// I2OSP(len(shared_secret), 2) || shared_secret
// context = mode || I2OSP(len(psk_id), 2) || psk_id ||
// I2OSP(len(info), 2) || info
// L = Nk + Nn + Nh
// secret = LabeledDerive(suite_id, secrets, "secret", context, L)
// The output is consumed in order as key, base_nonce, and exporter_secret.
//
SymCryptHpkeOneStageInit( kdfId, &state );
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, (UINT16) cbPsk );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
if ( cbPsk > 0 )
{
SymCryptHpkeOneStageAppend( kdfId, &state, pbPsk, cbPsk );
}
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, (UINT16) cbSharedSecret );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
SymCryptHpkeOneStageAppend( kdfId, &state, pbSharedSecret, cbSharedSecret );
SymCryptHpkeOneStageAppend( kdfId, &state, (PCBYTE) SYMCRYPT_HPKE_VERSION_LABEL, SYMCRYPT_HPKE_VERSION_LABEL_SIZE );
SymCryptHpkeOneStageAppend( kdfId, &state, hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE );
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, 6 );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
SymCryptHpkeOneStageAppend( kdfId, &state, (PCBYTE)"secret", 6 );
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, cbSecret );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
SymCryptHpkeOneStageAppend( kdfId, &state, &mode, sizeof(mode) );
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, (UINT16) cbPskId );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
if ( cbPskId > 0 )
{
SymCryptHpkeOneStageAppend( kdfId, &state, pbPskId, cbPskId );
}
SYMCRYPT_STORE_MSBFIRST16( lengthBuf, (UINT16) cbInfo );
SymCryptHpkeOneStageAppend( kdfId, &state, lengthBuf, 2 );
if ( cbInfo > 0 )
{
SymCryptHpkeOneStageAppend( kdfId, &state, pbInfo, cbInfo );
}
if ( pHpkeContext->aeadParams.cbKey > 0 )
{
SymCryptHpkeOneStageExtract( kdfId, &state, pHpkeContext->key, pHpkeContext->aeadParams.cbKey, FALSE );
}
if ( pHpkeContext->aeadParams.cbNonce > 0 )
{
SymCryptHpkeOneStageExtract( kdfId, &state, pHpkeContext->baseNonce, pHpkeContext->aeadParams.cbNonce, FALSE );
}
SymCryptHpkeOneStageExtract( kdfId, &state, pHpkeContext->exporterSecret, cbKdfOutput, TRUE );
}
//
// CombineSecrets_TwoStage from draft-ietf-hpke-hpke-03 for HKDF-based KDFs.
//
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHpkeCombineSecretsTwoStage(
_Inout_ PSYMCRYPT_HPKECONTEXT pHpkeContext,
BYTE mode,
_In_reads_bytes_( cbSharedSecret ) PCBYTE pbSharedSecret,
UINT16 cbSharedSecret,
_In_reads_bytes_opt_( cbInfo ) PCBYTE pbInfo,
UINT16 cbInfo,
_In_reads_bytes_opt_( cbPsk ) PCBYTE pbPsk,
UINT16 cbPsk,
_In_reads_bytes_opt_( cbPskId ) PCBYTE pbPskId,
UINT16 cbPskId )
{
SYMCRYPT_ERROR scError;
SYMCRYPT_HPKE_KDF_ID kdfId = (SYMCRYPT_HPKE_KDF_ID) pHpkeContext->ciphersuite.kdfId;
UINT16 cbKdfOutput = SymCryptHpkeKdfOutputSizeFromId( kdfId );
// ks_context = mode(1) || psk_id_hash(Nh) || info_hash(Nh)
// Maximum size: 1 + 2*64 = 129
BYTE ksContext[1 + 2 * SYMCRYPT_HPKE_KDF_MAX_HASH_SIZE];
UINT16 cbKsContext = 1 + 2 * cbKdfOutput;
BYTE secret[SYMCRYPT_HPKE_KDF_MAX_HASH_SIZE];
BYTE hpkeSuiteId[SYMCRYPT_HPKE_SUITE_ID_SIZE];
SymCryptHpkeBuildHpkeSuiteId(
pHpkeContext->ciphersuite.kemId, pHpkeContext->ciphersuite.kdfId,
pHpkeContext->ciphersuite.aeadId, hpkeSuiteId );
//
// Step 1: psk_id_hash = LabeledExtract("", "psk_id_hash", psk_id)
//
ksContext[0] = mode;
scError = SymCryptHpkeLabeledExtract(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
NULL, 0, // empty salt
(PCBYTE)"psk_id_hash", 11,
pbPskId, cbPskId,
ksContext + 1, cbKdfOutput );
if ( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
//
// Step 2: info_hash = LabeledExtract("", "info_hash", info)
//
scError = SymCryptHpkeLabeledExtract(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
NULL, 0,
(PCBYTE)"info_hash", 9,
pbInfo, cbInfo,
ksContext + 1 + cbKdfOutput, cbKdfOutput );
if ( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
//
// Step 3: secret = LabeledExtract(shared_secret, "secret", psk)
//
scError = SymCryptHpkeLabeledExtract(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
pbSharedSecret, cbSharedSecret, // shared_secret as salt
(PCBYTE)"secret", 6,
pbPsk, cbPsk,
secret, cbKdfOutput );
if ( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
//
// Step 4: key = LabeledExpand(secret, "key", ks_context, Nk)
// (Skip for Export-Only mode where Nk = 0)
//
if ( pHpkeContext->aeadParams.cbKey > 0 )
{
scError = SymCryptHpkeLabeledExpand(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
secret, cbKdfOutput,
(PCBYTE)"key", 3,
ksContext, cbKsContext,
pHpkeContext->key, pHpkeContext->aeadParams.cbKey );
if ( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
}
//
// Step 5: base_nonce = LabeledExpand(secret, "base_nonce", ks_context, Nn)
// (Skip for Export-Only mode where Nn = 0)
//
if ( pHpkeContext->aeadParams.cbNonce > 0 )
{
scError = SymCryptHpkeLabeledExpand(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
secret, cbKdfOutput,
(PCBYTE)"base_nonce", 10,
ksContext, cbKsContext,
pHpkeContext->baseNonce, pHpkeContext->aeadParams.cbNonce );
if ( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
}
//
// Step 6: exporter_secret = LabeledExpand(secret, "exp", ks_context, Nh)
//
scError = SymCryptHpkeLabeledExpand(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
secret, cbKdfOutput,
(PCBYTE)"exp", 3,
ksContext, cbKsContext,
pHpkeContext->exporterSecret, cbKdfOutput );
cleanup:
SymCryptWipeKnownSize( ksContext, sizeof(ksContext) );
SymCryptWipeKnownSize( secret, sizeof(secret) );
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptHpkeSecretExport(
_In_ PCSYMCRYPT_HPKECONTEXT pHpkeContext,
_In_reads_bytes_opt_( cbExporterContext ) PCBYTE pbExporterContext,
SIZE_T cbExporterContext,
_Out_writes_bytes_( cbResult ) PBYTE pbResult,
UINT16 cbResult )
{
SYMCRYPT_CHECK_MAGIC( pHpkeContext );
SYMCRYPT_ASSERT( pbExporterContext != NULL || cbExporterContext == 0 );
if ( pHpkeContext->state == SYMCRYPT_HPKE_CONTEXT_STATE_UNINITIALIZED )
{
return SYMCRYPT_INVALID_ARGUMENT;
}
if ( cbExporterContext > SYMCRYPT_HPKE_MAX_EXPORT_SIZE ||
cbResult > SYMCRYPT_HPKE_MAX_EXPORT_SIZE )
{
return SYMCRYPT_INVALID_ARGUMENT;
}
BYTE hpkeSuiteId[SYMCRYPT_HPKE_SUITE_ID_SIZE];
SymCryptHpkeBuildHpkeSuiteId(
pHpkeContext->ciphersuite.kemId, pHpkeContext->ciphersuite.kdfId,
pHpkeContext->ciphersuite.aeadId, hpkeSuiteId );
SYMCRYPT_HPKE_KDF_ID kdfId = (SYMCRYPT_HPKE_KDF_ID) pHpkeContext->ciphersuite.kdfId;
switch ( kdfId )
{
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA256:
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA384:
case SYMCRYPT_HPKE_KDF_ID_HKDF_SHA512:
return SymCryptHpkeLabeledExpand(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
pHpkeContext->exporterSecret,
SymCryptHpkeKdfOutputSizeFromId( kdfId ),
(PCBYTE)"sec", 3,
pbExporterContext,
cbExporterContext,
pbResult, cbResult );
case SYMCRYPT_HPKE_KDF_ID_SHAKE128:
case SYMCRYPT_HPKE_KDF_ID_SHAKE256:
SymCryptHpkeLabeledDeriveOneStage(
kdfId,
hpkeSuiteId, SYMCRYPT_HPKE_SUITE_ID_SIZE,
pHpkeContext->exporterSecret,
SymCryptHpkeKdfOutputSizeFromId( kdfId ),
(PCBYTE)"sec", 3,
pbExporterContext,
cbExporterContext,
pbResult, cbResult );
return SYMCRYPT_NO_ERROR;
default:
SYMCRYPT_ASSERT( FALSE );
return SYMCRYPT_NOT_IMPLEMENTED;
}
}