Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#include "nx_crypto_hkdf.h"
#include "nx_crypto_hmac.h"
...
...
_nx_crypto_method_hkdf_init(struct NX_CRYPTO_METHOD_STRUCT *, UCHAR *, NX_CRYPTO_KEY_SIZE, void **, void *, ULONG)
...
...
_nx_crypto_method_hkdf_cleanup(void *)
...
...
_nx_crypto_method_hkdf_operation(UINT, void *, struct NX_CRYPTO_METHOD_STRUCT *, UCHAR *, NX_CRYPTO_KEY_SIZE, UCHAR *, ULONG, UCHAR *, UCHAR *, ULONG, void *, ULONG, void *, void (*)(void *, UINT))
...
...
_nx_crypto_hkdf_extract(NX_CRYPTO_HKDF *)
...
...
_nx_crypto_hkdf_expand(NX_CRYPTO_HKDF *, UCHAR *, UINT)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocrypto_libraries/src/nx_crypto_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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Crypto Component */ /** */ /** HMAC-based Extract-and-Expand Key Derivation Function (HKDF) */ /** */... /**************************************************************************/ /**************************************************************************/ #include "nx_crypto_hkdf.h" #include "nx_crypto_hmac.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_hkdf_init PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is the common crypto method initialization routine */ /* for the Microsoft implementation of the HKDF cryptographic */ /* algorithm. */ /* */ /* INPUT */ /* */ /* method Pointer to crypto method */ /* key Pointer to key */ /* key_size_in_bits Length of key size in bits */ /* handler Returned crypto handler */ /* crypto_metadata Metadata area */ /* crypto_metadata_size Size of the metadata area */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_hkdf_init(struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, VOID **handle, VOID *crypto_metadata, ULONG crypto_metadata_size) { NX_CRYPTO_HKDF *hkdf; NX_CRYPTO_PARAMETER_NOT_USED(handle); NX_CRYPTO_STATE_CHECK /* We don't need a key in the HKDF init. */ if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) { return(NX_CRYPTO_POINTER_ERROR); }if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) { ... } /* Verify the metadata addrsss is 4-byte aligned. */ if((((ULONG)crypto_metadata) & 0x3) != 0) { return(NX_CRYPTO_METADATA_UNALIGNED); }if ((((ULONG)crypto_metadata) & 0x3) != 0) { ... } if(crypto_metadata_size < sizeof(NX_CRYPTO_HKDF)) { return(NX_CRYPTO_INVALID_BUFFER_SIZE); }if (crypto_metadata_size < sizeof(NX_CRYPTO_HKDF)) { ... } hkdf = (NX_CRYPTO_HKDF *)crypto_metadata; /* Initialize IKM with key data. */ hkdf->nx_crypto_hkdf_ikm = key; hkdf->nx_crypto_hkdf_ikm_length = (key_size_in_bits << 3); /* Initialize HMAC and HASH methods. */ hkdf->nx_crypto_hmac_method = NX_CRYPTO_NULL; hkdf->nx_crypto_hash_method = NX_CRYPTO_NULL; return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_hkdf_cleanup PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function cleans up the crypto metadata for the HKDF operation. */ /* */ /* INPUT */ /* */ /* crypto_metadata Crypto metadata */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* NX_CRYPTO_MEMSET Set the memory */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_hkdf_cleanup(VOID *crypto_metadata) { #ifdef NX_SECURE_KEY_CLEAR NX_CRYPTO_METHOD *hmac_method; NX_CRYPTO_HKDF *hkdf; UINT status;/* ... */ #endif NX_CRYPTO_STATE_CHECK #ifdef NX_SECURE_KEY_CLEAR if (!crypto_metadata) return (NX_CRYPTO_SUCCESS); /* Clear the HMAC state. */ hkdf = (NX_CRYPTO_HKDF *)crypto_metadata; hmac_method = hkdf->nx_crypto_hmac_method; if(hmac_method) { status = hmac_method -> nx_crypto_cleanup(hmac_method); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hmac_method) { ... } /* Clean up the HKDF metadata. */ NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_HKDF));/* ... */ #else NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); #endif/* NX_SECURE_KEY_CLEAR */ return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_hkdf_operation PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function provides the generic NetX Crypto API for the HKDF */ /* operation. */ /* */ /* INPUT */ /* */ /* op Operation Type */ /* Encrypt, Decrypt, Authenticate*/ /* handler Pointer to crypto context */ /* key Pointer to key */ /* key_size_in_bits Length of key size in bits */ /* input Input Stream */ /* input_length_in_byte Input Stream Length */ /* iv_ptr Initialized Vector */ /* output Output Stream */ /* output_length_in_byte Output Stream Length */ /* crypto_metadata Metadata area */ /* crypto_metadata_size Size of the metadata area */ /* packet_ptr Pointer to packet */ /* nx_crypto_hw_process_callback Callback function pointer */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_crypto_hkdf_extract Calculate the HKDF key */ /* _nx_crypto_hkdf_expand Generate HKDF key material */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), improved */ /* buffer length verification, */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_hkdf_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ VOID *handle, /* Crypto handler */ struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) { NX_CRYPTO_HKDF *hkdf; UINT status; NX_CRYPTO_PARAMETER_NOT_USED(handle); NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr); NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); NX_CRYPTO_STATE_CHECK /* Verify the metadata addrsss is 4-byte aligned. */ if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) { return(NX_CRYPTO_POINTER_ERROR); }if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) { ... } if(crypto_metadata_size < sizeof(NX_CRYPTO_HKDF)) { return(NX_CRYPTO_INVALID_BUFFER_SIZE); }if (crypto_metadata_size < sizeof(NX_CRYPTO_HKDF)) { ... } /* Get our control block for HKDF. */ hkdf = (NX_CRYPTO_HKDF *)(crypto_metadata); status = NX_CRYPTO_SUCCESS; switch (op) { case NX_CRYPTO_HKDF_SET_HMAC: hkdf->nx_crypto_hmac_method = method; if(hkdf->nx_crypto_hash_method != NX_CRYPTO_NULL) { /* Set the HMAC hash method. */ status = hkdf->nx_crypto_hmac_method->nx_crypto_operation(NX_CRYPTO_HMAC_SET_HASH, NX_CRYPTO_NULL, hkdf->nx_crypto_hash_method, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, hkdf->nx_crypto_hmac_metadata, sizeof(hkdf->nx_crypto_hmac_metadata), NX_CRYPTO_NULL, NX_CRYPTO_NULL); }if (hkdf->nx_crypto_hash_method != NX_CRYPTO_NULL) { ... } break;case NX_CRYPTO_HKDF_SET_HMAC: case NX_CRYPTO_HKDF_SET_HASH: hkdf->nx_crypto_hash_method = method; if(hkdf->nx_crypto_hmac_method != NX_CRYPTO_NULL) { /* Set the HMAC hash method. */ status = hkdf->nx_crypto_hmac_method->nx_crypto_operation(NX_CRYPTO_HMAC_SET_HASH, NX_CRYPTO_NULL, method, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, hkdf->nx_crypto_hmac_metadata, sizeof(hkdf->nx_crypto_hmac_metadata), NX_CRYPTO_NULL, NX_CRYPTO_NULL); }if (hkdf->nx_crypto_hmac_method != NX_CRYPTO_NULL) { ... } break;case NX_CRYPTO_HKDF_SET_HASH: case NX_CRYPTO_HKDF_SET_PRK: if(key == NX_CRYPTO_NULL) { return(NX_CRYPTO_POINTER_ERROR); }if (key == NX_CRYPTO_NULL) { ... } if ((key_size_in_bits >> 3) > sizeof(hkdf->nx_crypto_hkdf_prk)) { return(NX_CRYPTO_SIZE_ERROR); }if ((key_size_in_bits >> 3) > sizeof(hkdf->nx_crypto_hkdf_prk)) { ... } /* Set the PRK and return. */ NX_CRYPTO_MEMCPY(hkdf->nx_crypto_hkdf_prk, key, (key_size_in_bits >> 3)); /* Use case of memcpy is verified. */ hkdf->nx_crypto_hkdf_prk_size = (key_size_in_bits >> 3); break;case NX_CRYPTO_HKDF_SET_PRK: case NX_CRYPTO_HKDF_EXTRACT: if(key == NX_CRYPTO_NULL) { return(NX_CRYPTO_POINTER_ERROR); }if (key == NX_CRYPTO_NULL) { ... } if(hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL || hkdf->nx_crypto_hmac_method == NX_CRYPTO_NULL) { return(NX_CRYPTO_METHOD_INITIALIZATION_FAILURE); }if (hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL || hkdf->nx_crypto_hmac_method == NX_CRYPTO_NULL) { ... } /* Key is our "salt". The IKM should have been passed in the init function.*/ hkdf->nx_crypto_hkdf_salt = key; hkdf->nx_crypto_hkdf_salt_length = (key_size_in_bits >> 3); /* Initialize IKM with input data. */ hkdf->nx_crypto_hkdf_ikm = input; hkdf->nx_crypto_hkdf_ikm_length = input_length_in_byte; /* Our output size is the output size of the hash. */ hkdf->nx_crypto_hkdf_prk_size = hkdf->nx_crypto_hmac_method->nx_crypto_block_size_in_bytes; status = _nx_crypto_hkdf_extract(hkdf); if(status == NX_CRYPTO_SUCCESS) { if (output_length_in_byte < hkdf->nx_crypto_hkdf_prk_size) { return(NX_CRYPTO_SIZE_ERROR); }if (output_length_in_byte < hkdf->nx_crypto_hkdf_prk_size) { ... } /* Copy the PRK into output. */ NX_CRYPTO_MEMCPY(output, hkdf->nx_crypto_hkdf_prk, hkdf->nx_crypto_hkdf_prk_size); /* Use case of memcpy is verified. */ }if (status == NX_CRYPTO_SUCCESS) { ... } break; case NX_CRYPTO_HKDF_EXTRACT: case NX_CRYPTO_HKDF_EXPAND: if(key == NX_CRYPTO_NULL) { return(NX_CRYPTO_POINTER_ERROR); }if (key == NX_CRYPTO_NULL) { ... } if(hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL || hkdf->nx_crypto_hmac_method == NX_CRYPTO_NULL) { return(NX_CRYPTO_METHOD_INITIALIZATION_FAILURE); }if (hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL || hkdf->nx_crypto_hmac_method == NX_CRYPTO_NULL) { ... } /* Key is our "info". The PRK should have been initialized by the call to NX_CRYPTO_HKDF_EXTRACT.*/ hkdf->nx_crypto_hkdf_info = key; hkdf->nx_crypto_hkdf_info_size = (key_size_in_bits >> 3); status = _nx_crypto_hkdf_expand(hkdf, output, output_length_in_byte); break;case NX_CRYPTO_HKDF_EXPAND: default: break;default }switch (op) { ... } return(status); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_hkdf_extract PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function performs the HKDF-extract operation detailed in RFC */ /* 5869. The output key is placed in the HKDF structure passed in. */ /* */ /* INPUT */ /* */ /* hkdf HKDF structure */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [hash method] Perform selected HMAC hash */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_crypto_hkdf_extract(NX_CRYPTO_HKDF *hkdf) { UINT status; UINT hash_size; /* the length of hmac output */ UCHAR *salt; UINT salt_len; UCHAR *ikm; UINT ikm_len; UCHAR *metadata; UINT metadata_size; UCHAR *hmac_output; VOID *handler = NX_CRYPTO_NULL; NX_CRYPTO_METHOD *hmac_method = hkdf -> nx_crypto_hmac_method; NX_CRYPTO_STATE_CHECK /* From RFC 5869: * HKDF-Extract(salt, IKM) -> PRK * * Options: * Hash a hash function; HashLen denotes the length of the * hash function output in octets * * Inputs: * salt optional salt value (a non-secret random value); * if not provided, it is set to a string of HashLen zeros. * IKM input keying material * * Output: * PRK a pseudorandom key (of HashLen octets) * * The output PRK is calculated as follows: * * PRK = HMAC-Hash(salt, IKM) * *//* ... */ /* Validate pointers. */ if (hmac_method == NX_CRYPTO_NULL || hmac_method -> nx_crypto_operation == NX_CRYPTO_NULL || hmac_method -> nx_crypto_cleanup == NX_CRYPTO_NULL || hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL) { return(NX_CRYPTO_INVALID_PARAMETER); }if (hmac_method == NX_CRYPTO_NULL || hmac_method -> nx_crypto_operation == NX_CRYPTO_NULL || hmac_method -> nx_crypto_cleanup == NX_CRYPTO_NULL || hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL) { ... } /* Initialize temporary variables. */ salt = hkdf->nx_crypto_hkdf_salt; salt_len = hkdf->nx_crypto_hkdf_salt_length; ikm = hkdf->nx_crypto_hkdf_ikm; ikm_len = hkdf->nx_crypto_hkdf_ikm_length; hash_size = hkdf -> nx_crypto_hash_method -> nx_crypto_ICV_size_in_bits >> 3; metadata = hkdf->nx_crypto_hmac_metadata; metadata_size = sizeof(hkdf->nx_crypto_hmac_metadata); hmac_output = hkdf->nx_crypto_hkdf_prk; /* Make sure we can store our output key. */ if (hash_size > sizeof(hkdf->nx_crypto_hkdf_prk)) { return(NX_CRYPTO_INVALID_PARAMETER); }if (hash_size > sizeof(hkdf->nx_crypto_hkdf_prk)) { ... } /* Assign the output size to our HKDF structure. */ hkdf->nx_crypto_hkdf_prk_size = hash_size; /* Initialize hash method (check key sizes, etc.). */ if (hmac_method -> nx_crypto_init) { status = hmac_method -> nx_crypto_init(hmac_method, salt, (NX_CRYPTO_KEY_SIZE)(salt_len << 3), &handler, metadata, metadata_size); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hmac_method -> nx_crypto_init) { ... } /* Generate the output PRK. */ status = hmac_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE, handler, hmac_method, salt, (NX_CRYPTO_KEY_SIZE)(salt_len << 3), ikm, ikm_len, NX_CRYPTO_NULL, hmac_output, hash_size, metadata, metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); return(status); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_hkdf_expand PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function performs the HKDF-expand operation detailed in RFC */ /* 5869. The hdkf parameter contains the input key (PRK) and other */ /* parameters needed to generate the desired output data. */ /* */ /* INPUT */ /* */ /* hkdf HKDF structure */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [hash method] Perform selected HMAC hash */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), improved */ /* buffer length verification, */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_crypto_hkdf_expand(NX_CRYPTO_HKDF *hkdf, UCHAR *output, UINT desired_length) { UINT offset; UINT N_count; UINT hash_size; /* the length of hmac output */ UCHAR *prk; UINT prk_len; UCHAR *info; UINT info_len; UCHAR *metadata; UINT metadata_size; VOID *handler = NX_CRYPTO_NULL; UINT output_len; /* T(i) */ UCHAR *temp_T; UINT temp_T_size, T_len, T_bytes_to_hash; UINT i; UINT status; NX_CRYPTO_METHOD *hmac_method = hkdf -> nx_crypto_hmac_method; NX_CRYPTO_STATE_CHECK /* From RFC 5869: * HKDF-Expand(PRK, info, L) -> OKM * * Options: * Hash a hash function; HashLen denotes the length of the * hash function output in octets * Inputs: * PRK a pseudorandom key of at least HashLen octets * (usually, the output from the extract step) * info optional context and application specific information * (can be a zero-length string) * L length of output keying material in octets * (<= 255*HashLen) * * Output: * OKM output keying material (of L octets) * * The output OKM is calculated as follows: * * N = ceil(L/HashLen) * T = T(1) | T(2) | T(3) | ... | T(N) * OKM = first L octets of T * * where: * T(0) = empty string (zero length) * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01) * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02) * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03) * ... * * (where the constant concatenated to the end of each T(n) is a * single octet.) * *//* ... */ /* Validate pointers. */ if (hmac_method == NX_CRYPTO_NULL || hmac_method -> nx_crypto_operation == NX_CRYPTO_NULL || hmac_method -> nx_crypto_cleanup == NX_CRYPTO_NULL || hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL || output == NX_CRYPTO_NULL) { return(NX_CRYPTO_INVALID_PARAMETER); }if (hmac_method == NX_CRYPTO_NULL || hmac_method -> nx_crypto_operation == NX_CRYPTO_NULL || hmac_method -> nx_crypto_cleanup == NX_CRYPTO_NULL || hkdf->nx_crypto_hash_method == NX_CRYPTO_NULL || output == NX_CRYPTO_NULL) { ... } /* Initialize temporary variables. */ prk = hkdf->nx_crypto_hkdf_prk; prk_len = hkdf->nx_crypto_hkdf_prk_size; info = hkdf->nx_crypto_hkdf_info; info_len = hkdf->nx_crypto_hkdf_info_size; temp_T = hkdf->nx_crypto_hkdf_temp_T; temp_T_size = sizeof(hkdf->nx_crypto_hkdf_temp_T); hash_size = hkdf-> nx_crypto_hash_method -> nx_crypto_ICV_size_in_bits >> 3; metadata = hkdf->nx_crypto_hmac_metadata; metadata_size = sizeof(hkdf->nx_crypto_hmac_metadata); /* Assign the output size to our HKDF structure. */ hkdf->nx_crypto_hkdf_prk_size = hash_size; /* Assign T(0), the empty string. */ NX_CRYPTO_MEMSET(temp_T, 0, temp_T_size); T_len = 0; /* Get our L count for our loop. */ N_count = 1 + ((desired_length) / hash_size); /* Loop through T(i) to calculate output material (OKM). * NOTE: We start at 1 so the counter is correct. Add one * to N_count to get the full amount of data. *//* ... */ for (i = 1; i < N_count + 1; ++i) { if ((T_len + info_len + 1) > temp_T_size) { return(NX_CRYPTO_SIZE_ERROR); }if ((T_len + info_len + 1) > temp_T_size) { ... } /* Concatenate T(i-1) (in temp_T after the hash above), info, and counter octet to feed into digest. */ NX_CRYPTO_MEMCPY(&temp_T[T_len], info, info_len); /* Use case of memcpy is verified. */ /* Concatenate counter octet. */ temp_T[T_len + info_len] = (UCHAR)(i & 0xFF); /* Initialize hash method. */ if (hmac_method -> nx_crypto_init) { status = hmac_method -> nx_crypto_init(hmac_method, prk, (NX_CRYPTO_KEY_SIZE)(prk_len << 3), &handler, metadata, metadata_size); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hmac_method -> nx_crypto_init) { ... } /* The number of bytes we want to hash is a combination of T_len (0 or <hash size>) the length of "info", and add 1 for the counter octet. *//* ... */ T_bytes_to_hash = T_len + info_len + 1; /* Calculate T(i) = HMAC(PRK, T(i-1) | info | i) */ status = hmac_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE, handler, hmac_method, prk, (NX_CRYPTO_KEY_SIZE)(prk_len << 3), temp_T, T_bytes_to_hash, NX_CRYPTO_NULL, temp_T, temp_T_size, metadata, metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Updated the length of T(i) */ T_len = hash_size; /* Get our output offset. */ offset = (i - 1) * hash_size; /* Output block is the size of the digest unless the remaining desired length is smaller than the digest length. *//* ... */ if ((desired_length - offset) < hash_size) { output_len = (desired_length - offset); }if ((desired_length - offset) < hash_size) { ... } else { output_len = hash_size; }else { ... } /* Make sure we only copy the desired data length into the output. */ if (hash_size > desired_length) { output_len = desired_length; }if (hash_size > desired_length) { ... } /* Copy T(i) into output. */ NX_CRYPTO_MEMCPY(&output[offset], temp_T, output_len); /* Use case of memcpy is verified. */ }for (i = 1; i < N_count + 1; ++i) { ... } return(NX_CRYPTO_SUCCESS); }{ ... } ...