Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#include "nx_secure_dtls.h"
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
Files
netxduo
addons
common
crypto_libraries
nx_secure
inc
ports
src
ports
threadx
filex
usbx
HAL
CMSIS
lan8742
SourceVuSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_1_3_generate_keys.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 Secure Component */ /** */ /** Transport Layer Security (TLS) - Generate Session Keys */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_tls.h" #ifdef NX_SECURE_ENABLE_DTLS #include "nx_secure_dtls.h" #endif /* NX_SECURE_ENABLE_DTLS */ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) /* Some secret generation requires a string of zeroes with a length equivalent to the key/hash size. This array is used for that with a memset. *//* ... */ static UCHAR _nx_secure_tls_zeroes[NX_SECURE_TLS_MAX_KEY_SIZE]; static UINT _nx_secure_tls_1_3_generate_handshake_secrets(NX_SECURE_TLS_SESSION *tls_session); static UINT _nx_secure_tls_1_3_generate_session_secrets(NX_SECURE_TLS_SESSION *tls_session); static UINT _nx_secure_tls_hkdf_expand_label(NX_SECURE_TLS_SESSION *tls_session, UCHAR *secret, UINT secret_len, UCHAR *label, UINT label_len, UCHAR *context, UINT context_len, UINT length, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method); static UINT _nx_secure_tls_derive_secret(NX_SECURE_TLS_SESSION *tls_session, UCHAR *secret, UINT secret_len, UCHAR *label, UINT label_len, UCHAR *message_hash, UINT message_hash_len, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method); static UINT _nx_secure_tls_hkdf_extract(NX_SECURE_TLS_SESSION *tls_session, UCHAR *salt, UINT salt_len, UCHAR *ikm, UINT ikm_len, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method);/* ... */ #endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_1_3_generate_psk_secrets PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 to generate the secrets and keys */ /* needed for PSK binder generation. Since each "external" PSK needs */ /* a binder generated from the early secret (which is generated using */ /* that PSK), each PSK gets a separate "early secret" and "binder key".*/ /* Therefore, this function will be called for *every* PSK provided by */ /* the application, each time with a different PSK and hash method. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) UINT _nx_secure_tls_1_3_generate_psk_secret(NX_SECURE_TLS_SESSION *tls_session, NX_SECURE_TLS_PSK_STORE *psk_entry, const NX_CRYPTO_METHOD *hash_method) { UINT status; UINT hash_length; UCHAR *psk_secret; UINT psk_secret_length; UCHAR *label; UINT label_length; UINT is_resumption_psk = NX_FALSE; /* Get the hash length so we know how much data we are generating. */ hash_length = (hash_method->nx_crypto_ICV_size_in_bits >> 3); /* The PSK is the input to the early secret. */ psk_secret = (UCHAR *)psk_entry->nx_secure_tls_psk_data; psk_secret_length = psk_entry->nx_secure_tls_psk_data_size; NX_SECURE_MEMSET(_nx_secure_tls_zeroes, 0, sizeof(_nx_secure_tls_zeroes)); /* Perform an HKDF-Extract to get the "early secret". */ /* Salt: 0 string, IKM: PSK secret. */ status = _nx_secure_tls_hkdf_extract(tls_session, _nx_secure_tls_zeroes, hash_length, psk_secret, psk_secret_length, psk_entry->nx_secure_tls_psk_early_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } psk_entry->nx_secure_tls_psk_early_secret_size = hash_length; /*----- Binder key value. -----*/ /* Get the appropriate label for our secret derivation. */ label = (UCHAR *)((is_resumption_psk)? "res binder" : "ext binder" ); label_length = 10; /* Ext/Res binder key has an empty messages context. */ status = _nx_secure_tls_derive_secret(tls_session, psk_entry->nx_secure_tls_psk_early_secret, psk_entry->nx_secure_tls_psk_early_secret_size, label, label_length, (UCHAR *)"", 0, psk_entry->nx_secure_tls_psk_binder_key, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } psk_entry->nx_secure_tls_psk_binder_key_size = hash_length; /* Generate Finished Key. According to RFC 8446 Section 4.2.11.2, we generate the PSK binder in the same * fashion as the Finished message, but using the binder key as input to the HKDF expansion. Thus, generate a "finished" * key for this specific PSK entry to use in the binder generation. *//* ... */ status = _nx_secure_tls_hkdf_expand_label(tls_session, psk_entry->nx_secure_tls_psk_binder_key, psk_entry->nx_secure_tls_psk_binder_key_size, (UCHAR *)"finished", 8, (UCHAR *)"", 0, hash_length, psk_entry->nx_secure_tls_psk_finished_key, hash_length, hash_method); psk_entry->nx_secure_tls_psk_finished_key_size = hash_length; return(status); }_nx_secure_tls_1_3_generate_psk_secret (NX_SECURE_TLS_SESSION *tls_session, NX_SECURE_TLS_PSK_STORE *psk_entry, const NX_CRYPTO_METHOD *hash_method) { ... } /* ... */#endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_1_3_generate_handshake_keys PORTABLE C */ /* 6.1.8 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 to generate the symmetric */ /* encryption keys used to protect TLS handshake messages. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* 08-02-2021 Timothy Stapko Modified comment(s), added */ /* cleanup for session cipher, */ /* resulting in version 6.1.8 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) UINT _nx_secure_tls_1_3_generate_handshake_keys(NX_SECURE_TLS_SESSION *tls_session) { UINT status; UCHAR *key_block; ULONG key_block_size; NX_SECURE_TLS_KEY_SECRETS *secrets; const NX_CRYPTO_METHOD *session_cipher_method = NX_NULL; const NX_CRYPTO_METHOD *hash_method = NX_NULL; UINT key_size; UINT iv_size; UINT key_offset; UINT hash_size; /* From RFC 8446, Section 7.3: The traffic keying material is generated from an input traffic secret value using: [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length) [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length) [sender] denotes the sending side. The value of Secret for each record type is shown in the table below. +-------------------+---------------------------------------+ | Record Type | Secret | +-------------------+---------------------------------------+ | 0-RTT Application | client_early_traffic_secret | | | | | Handshake | [sender]_handshake_traffic_secret | | | | | Application Data | [sender]_application_traffic_secret_N | +-------------------+---------------------------------------+ *//* ... */ /* Generate handshake secrets. */ status = _nx_secure_tls_1_3_generate_handshake_secrets(tls_session); /* Get our generated secrets. */ secrets = &tls_session->nx_secure_tls_key_material.nx_secure_tls_key_secrets; if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } /* Get our session cipher method so we can get key sizes. */ session_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_session_cipher; hash_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash; /* Lookup ciphersuite data for key size. We need 2 keys for each session. */ key_size = session_cipher_method -> nx_crypto_key_size_in_bits >> 3; /* Lookup initialization vector size. */ /* IV size for AES-128-GCM is 12 bytes! */ iv_size = 12; // session_cipher_method -> nx_crypto_IV_size_in_bits >> 3; /* Working pointers into our key material blocks - we need a place to store generated keys. */ key_block = tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data; key_block_size = sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data); /* Assign MAC secrets to TLS Session. */ tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_mac_secret = secrets->tls_client_handshake_traffic_secret; tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_mac_secret = secrets->tls_server_handshake_traffic_secret; /* To generate handshake keys, we need the [sender]_handshake_traffic_secret. */ /* Generate client traffic key. */ key_offset = 0; status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_client_handshake_traffic_secret, secrets->tls_client_handshake_traffic_secret_len, (UCHAR *)"key", 3, (UCHAR *)"", 0, key_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_key = &key_block[key_offset]; key_offset += key_size; /* Generate client traffic IV. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_client_handshake_traffic_secret, secrets->tls_client_handshake_traffic_secret_len, (UCHAR *)"iv", 2, (UCHAR *)"", 0, iv_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_iv = &key_block[key_offset]; key_offset += iv_size; /* Generate server-side key. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_server_handshake_traffic_secret, secrets->tls_server_handshake_traffic_secret_len, (UCHAR *)"key", 3, (UCHAR *)"", 0, key_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_key = &key_block[key_offset]; key_offset += key_size; /* Generate server-side IV. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_server_handshake_traffic_secret, secrets->tls_server_handshake_traffic_secret_len, (UCHAR *)"iv", 2, (UCHAR *)"", 0, iv_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_iv = &key_block[key_offset]; key_offset += iv_size; /* We now have the session keys so we can generate the Finished keys for both client and server. */ /* From RFC 8446 (TLS 1.3): finished_key = HKDF-Expand-Label(BaseKey, "finished", "", Hash.length) *//* ... */ /* Get hash size for this ciphersuite. */ hash_size = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size; /* Generate server-side Finished Key. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_server_handshake_traffic_secret, secrets->tls_server_handshake_traffic_secret_len, (UCHAR *)"finished", 8, (UCHAR *)"", 0, hash_size, &secrets->tls_server_finished_key[0], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_server_finished_key_len = hash_size; /* Generate client-side Finished Key. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_client_handshake_traffic_secret, secrets->tls_client_handshake_traffic_secret_len, (UCHAR *)"finished", 8, (UCHAR *)"", 0, hash_size, &secrets->tls_client_finished_key[0], (key_block_size - key_offset), hash_method); secrets->tls_client_finished_key_len = hash_size; if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } /* Now, we can initialize our crypto routines and turn on encryption. */ /* Initialize the crypto method used in the session cipher. */ if (session_cipher_method -> nx_crypto_init != NULL) { if (tls_session -> nx_secure_tls_session_cipher_client_initialized && session_cipher_method -> nx_crypto_cleanup) { status = session_cipher_method -> nx_crypto_cleanup(tls_session -> nx_secure_session_cipher_metadata_area_client); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } tls_session -> nx_secure_tls_session_cipher_client_initialized = 0; }if (tls_session -> nx_secure_tls_session_cipher_client_initialized && session_cipher_method -> nx_crypto_cleanup) { ... } /* Set client write key. */ status = session_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)session_cipher_method, tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_key, session_cipher_method -> nx_crypto_key_size_in_bits, &tls_session -> nx_secure_session_cipher_handler_client, tls_session -> nx_secure_session_cipher_metadata_area_client, tls_session -> nx_secure_session_cipher_metadata_size); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } tls_session -> nx_secure_tls_session_cipher_client_initialized = 1; if (tls_session -> nx_secure_tls_session_cipher_server_initialized && session_cipher_method -> nx_crypto_cleanup) { status = session_cipher_method -> nx_crypto_cleanup(tls_session -> nx_secure_session_cipher_metadata_area_server); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } tls_session -> nx_secure_tls_session_cipher_server_initialized = 0; }if (tls_session -> nx_secure_tls_session_cipher_server_initialized && session_cipher_method -> nx_crypto_cleanup) { ... } /* Set server write key. */ status = session_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)session_cipher_method, tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_key, session_cipher_method -> nx_crypto_key_size_in_bits, &tls_session -> nx_secure_session_cipher_handler_server, tls_session -> nx_secure_session_cipher_metadata_area_server, tls_session -> nx_secure_session_cipher_metadata_size); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } tls_session -> nx_secure_tls_session_cipher_server_initialized = 1; }if (session_cipher_method -> nx_crypto_init != NULL) { ... } return(NX_SUCCESS); }_nx_secure_tls_1_3_generate_handshake_keys (NX_SECURE_TLS_SESSION *tls_session) { ... } /* ... */ #endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_1_3_generate_session_keys PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 to generate the symmetric */ /* encryption keys used to protect application data. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) UINT _nx_secure_tls_1_3_generate_session_keys(NX_SECURE_TLS_SESSION *tls_session) { UINT status; UCHAR *key_block; ULONG key_block_size; NX_SECURE_TLS_KEY_SECRETS *secrets; const NX_CRYPTO_METHOD *session_cipher_method = NX_NULL; const NX_CRYPTO_METHOD *hash_method = NX_NULL; UINT key_size; UINT iv_size; UINT key_offset; /* From RFC 8446, Section 7.3: The traffic keying material is generated from an input traffic secret value using: [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length) [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length) [sender] denotes the sending side. The value of Secret for each record type is shown in the table below. +-------------------+---------------------------------------+ | Record Type | Secret | +-------------------+---------------------------------------+ | 0-RTT Application | client_early_traffic_secret | | | | | Handshake | [sender]_handshake_traffic_secret | | | | | Application Data | [sender]_application_traffic_secret_N | +-------------------+---------------------------------------+ *//* ... */ /* Generate handshake secrets. */ status = _nx_secure_tls_1_3_generate_session_secrets(tls_session); /* Get our generated secrets. */ secrets = &tls_session->nx_secure_tls_key_material.nx_secure_tls_key_secrets; if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } /* Get our session cipher method so we can get key sizes. */ session_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_session_cipher; hash_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash; /* Lookup ciphersuite data for key size. We need 2 keys for each session. */ key_size = session_cipher_method -> nx_crypto_key_size_in_bits >> 3; /* Lookup initialization vector size. */ // iv_size = session_cipher_method -> nx_crypto_IV_size_in_bits >> 3; /* IV size for AES-128-GCM is 12 bytes. */ iv_size = 12; // session_cipher_method -> nx_crypto_IV_size_in_bits >> 3; /* Working pointers into our key material blocks - we need a place to store generated keys. * Whenever we generate session keys in TLS 1.3 we are coming from an existing encrypted * context so save the keys to the "on-deck" space to be enabled later. *//* ... */ key_block = tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data; key_block_size = sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_key_material_data); /* Assign MAC secrets to TLS Session. */ tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_write_mac_secret = secrets->tls_client_application_traffic_secret_0; tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_write_mac_secret = secrets->tls_server_application_traffic_secret_0; /* To generate handshake keys, we need the [sender]_handshake_traffic_secret. */ /* Generate client traffic key. */ key_offset = 0; status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_client_application_traffic_secret_0, secrets->tls_client_application_traffic_secret_0_len, (UCHAR *)"key", 3, (UCHAR *)"", 0, key_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } /* Save the generated keys to the on-deck space (don't initialize yet). */ tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_next_write_key = &key_block[key_offset]; key_offset += key_size; /* Generate client traffic IV. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_client_application_traffic_secret_0, secrets->tls_client_application_traffic_secret_0_len, (UCHAR *)"iv", 2, (UCHAR *)"", 0, iv_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_next_iv = &key_block[key_offset]; key_offset += iv_size; /* Generate server-side key. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_server_application_traffic_secret_0, secrets->tls_server_application_traffic_secret_0_len, (UCHAR *)"key", 3, (UCHAR *)"", 0, key_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_next_write_key = &key_block[key_offset]; key_offset += key_size; /* Generate server-side IV. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_server_application_traffic_secret_0, secrets->tls_server_application_traffic_secret_0_len, (UCHAR *)"iv", 2, (UCHAR *)"", 0, iv_size, &key_block[key_offset], (key_block_size - key_offset), hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_next_iv = &key_block[key_offset]; key_offset += iv_size; if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } return(NX_SUCCESS); }_nx_secure_tls_1_3_generate_session_keys (NX_SECURE_TLS_SESSION *tls_session) { ... } /* ... */ #endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_generate_session_psk PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 to generate a PSK for use in */ /* session resumptions, using the nonce provided in the */ /* NewSessionTicket message. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* ticket_psk PSK control block for output */ /* nonce Pointer to session nonce */ /* nonce_len Length of nonce */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) UINT _nx_secure_tls_1_3_session_psk_generate(NX_SECURE_TLS_SESSION *tls_session, NX_SECURE_TLS_PSK_STORE *ticket_psk, UCHAR *nonce, UINT nonce_len) { NX_SECURE_TLS_KEY_SECRETS *secrets; UINT status; UINT hash_length; const NX_CRYPTO_METHOD *hash_method; /* Session PSK is generated as follows (From RFC 8446): * HKDF-Expand-Label(resumption_master_secret, * "resumption", ticket_nonce, Hash.length) * *//* ... */ /* Get a pointer to our key secrets for this session. */ secrets = &tls_session->nx_secure_tls_key_material.nx_secure_tls_key_secrets; if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } /* Get the hash method so we know how much data we are generating. */ hash_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash; hash_length = (hash_method->nx_crypto_ICV_size_in_bits >> 3); /* Generate the PSK by running HKDF-Expand-Label with the resumption secret and the passed-in nonce. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secrets->tls_resumption_master_secret, secrets->tls_resumption_master_secret_len, (UCHAR *)"resumption", 10, nonce, nonce_len, hash_length, ticket_psk->nx_secure_tls_psk_data, sizeof(ticket_psk->nx_secure_tls_psk_data), hash_method); /* Set the length of our PSK. */ ticket_psk->nx_secure_tls_psk_data_size = hash_length; return(status); }_nx_secure_tls_1_3_session_psk_generate (NX_SECURE_TLS_SESSION *tls_session, NX_SECURE_TLS_PSK_STORE *ticket_psk, UCHAR *nonce, UINT nonce_len) { ... } /* ... */#endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_1_3_generate_handshake_secrets PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 in generating key material. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) static UINT _nx_secure_tls_1_3_generate_handshake_secrets(NX_SECURE_TLS_SESSION *tls_session) { UINT status; NX_SECURE_TLS_KEY_SECRETS *secrets; UINT hash_length; const NX_CRYPTO_METHOD *hash_method; UCHAR *psk_secret; UINT psk_secret_length; UCHAR *label; UINT label_length; UINT is_resumption_psk = NX_FALSE; if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } /* Get the hash method so we know how much data we are generating. */ hash_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash; hash_length = (hash_method->nx_crypto_ICV_size_in_bits >> 3); /* Get a pointer to our key secrets for this session. */ secrets = &tls_session->nx_secure_tls_key_material.nx_secure_tls_key_secrets; /* From RFC 8446, section 7.1: Keys are derived from two input secrets using the HKDF-Extract and Derive-Secret functions. The general pattern for adding a new secret is to use HKDF-Extract with the Salt being the current secret state and the Input Keying Material (IKM) being the new secret to be added. In this version of TLS 1.3, the two input secrets are: - PSK (a pre-shared key established externally or derived from the resumption_master_secret value from a previous connection) - (EC)DHE shared secret (Section 7.4) This produces a full key derivation schedule shown in the diagram below. In this diagram, the following formatting conventions apply: - HKDF-Extract is drawn as taking the Salt argument from the top and the IKM argument from the left, with its output to the bottom and the name of the output on the right. - Derive-Secret's Secret argument is indicated by the incoming arrow. For instance, the Early Secret is the Secret for generating the client_early_traffic_secret. - "0" indicates a string of Hash.length bytes set to zero. 0 | v PSK -> HKDF-Extract = Early Secret | +-----> Derive-Secret(., "ext binder" | "res binder", "") | = binder_key | +-----> Derive-Secret(., "c e traffic", ClientHello) | = client_early_traffic_secret | +-----> Derive-Secret(., "e exp master", ClientHello) | = early_exporter_master_secret v Derive-Secret(., "derived", "") | v (EC)DHE -> HKDF-Extract = Handshake Secret | +-----> Derive-Secret(., "c hs traffic", | ClientHello...ServerHello) | = client_handshake_traffic_secret | +-----> Derive-Secret(., "s hs traffic", | ClientHello...ServerHello) | = server_handshake_traffic_secret v Derive-Secret(., "derived", "") | v 0 -> HKDF-Extract = Master Secret | +-----> Derive-Secret(., "c ap traffic", | ClientHello...server Finished) | = client_application_traffic_secret_0 | +-----> Derive-Secret(., "s ap traffic", | ClientHello...server Finished) | = server_application_traffic_secret_0 | +-----> Derive-Secret(., "exp master", | ClientHello...server Finished) | = exporter_master_secret | +-----> Derive-Secret(., "res master", ClientHello...client Finished) = resumption_master_secret The general pattern here is that the secrets shown down the left side of the diagram are just raw entropy without context, whereas the secrets down the right side include Handshake Context and therefore can be used to derive working keys without additional context. Note that the different calls to Derive-Secret may take different Messages arguments, even with the same secret. In a 0-RTT exchange, Derive-Secret is called with four distinct transcripts; in a 1-RTT-only exchange, it is called with three distinct transcripts. If a given secret is not available, then the 0-value consisting of a string of Hash.length bytes set to zeros is used. Note that this does not mean skipping rounds, so if PSK is not in use, Early Secret will still be HKDF-Extract(0, 0). For the computation of the binder_key, the label is "ext binder" for external PSKs (those provisioned outside of TLS) and "res binder" for resumption PSKs (those provisioned as the resumption master secret of a previous handshake). The different labels prevent the substitution of one type of PSK for the other. There are multiple potential Early Secret values, depending on which PSK the server ultimately selects. The client will need to compute one for each potential PSK; if no PSK is selected, it will then need to compute the Early Secret corresponding to the zero PSK. Once all the values which are to be derived from a given secret have been computed, that secret SHOULD be erased. *//* ... */ /* Go through all secrets, generate those that haven't been generated yet. */ /* If available, the chosen PSK is fed into the key generation process. */ /* If PSK is not in use, Early Secret will still be HKDF-Extract(0, 0). So set PSK as "0". */ psk_secret = _nx_secure_tls_zeroes; psk_secret_length = hash_length; if(tls_session->nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size > 0) { psk_secret = tls_session->nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data; psk_secret_length = tls_session->nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size; }if (tls_session->nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size > 0) { ... } NX_SECURE_MEMSET(_nx_secure_tls_zeroes, 0, sizeof(_nx_secure_tls_zeroes)); if(secrets->tls_early_secret_len == 0) { /* Perform an HKDF-Extract to get the "early secret". */ /* Salt: 0 string, IKM: PSK secret. */ status = _nx_secure_tls_hkdf_extract(tls_session, _nx_secure_tls_zeroes, hash_length, psk_secret, psk_secret_length, secrets->tls_early_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_early_secret_len = hash_length; }if (secrets->tls_early_secret_len == 0) { ... } /* Generate keys and secrets based on the "early secret". */ if(secrets->tls_early_secret_len != 0) { /*----- Binder key value. -----*/ /* Get the appropriate label for our secret derivation. */ label = (UCHAR *)((is_resumption_psk)? "res binder" : "ext binder"); label_length = 10; /* Ext/Res binder key has an empty messages context. */ status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_early_secret, secrets->tls_early_secret_len, label, label_length, (UCHAR *)"", 0, secrets->tls_binder_key, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_binder_key_len = hash_length; /*----- Early traffic secret. -----*/ label = (UCHAR *)"c e traffic"; label_length = 11; /* Context is hash of ClientHello. */ status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_early_secret, secrets->tls_early_secret_len, label, label_length, (UCHAR *)"FIXME", 5, secrets->tls_client_early_traffic_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_client_early_traffic_secret_len = hash_length; /*----- Early exporter master secret. -----*/ label = (UCHAR *)"e exp master"; label_length = 12; /* Context is hash of ClientHello. */ status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_early_secret, secrets->tls_early_secret_len, label, label_length, (UCHAR *)"FIXME", 5, secrets->tls_early_exporter_master_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_early_exporter_master_secret_len = hash_length; }if (secrets->tls_early_secret_len != 0) { ... } /* Handshake secret - special case! Needs a pre-master secret from the ECDHE exchange and the early secret from above. */ if(secrets->tls_handshake_secret_len == 0 && tls_session->nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size != 0) { /* Start by deriving the salt from the early secret. Context is empty! */ status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_early_secret, secrets->tls_early_secret_len, (UCHAR *)"derived", 7, (UCHAR *)"", 0, secrets->tls_handshake_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } /* Now perform an HKDF-Extract to get the handshake secret. Salt: derived secret from above, IKM: ECDHE pre-master secret *//* ... */ status = _nx_secure_tls_hkdf_extract(tls_session, secrets->tls_handshake_secret, hash_length, tls_session->nx_secure_tls_key_material.nx_secure_tls_pre_master_secret, tls_session->nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size, secrets->tls_handshake_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_handshake_secret_len = hash_length; }if (secrets->tls_handshake_secret_len == 0 && tls_session->nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size != 0) { ... } /* Generate keys and secrets based on the "handshake secret". */ if(secrets->tls_handshake_secret_len != 0) { /*----- Client handshake traffic secret. -----*/ label = (UCHAR *)"c hs traffic"; label_length = 12; status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_handshake_secret, secrets->tls_handshake_secret_len, label, label_length, tls_session->nx_secure_tls_key_material.nx_secure_tls_transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_SERVERHELLO], hash_length, secrets->tls_client_handshake_traffic_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_client_handshake_traffic_secret_len = hash_length; /*----- Server handshake traffic secret. -----*/ label = (UCHAR *)"s hs traffic"; label_length = 12; status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_handshake_secret, secrets->tls_handshake_secret_len, label, label_length, tls_session->nx_secure_tls_key_material.nx_secure_tls_transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_SERVERHELLO], hash_length, secrets->tls_server_handshake_traffic_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_server_handshake_traffic_secret_len = hash_length; }if (secrets->tls_handshake_secret_len != 0) { ... } return(NX_SUCCESS); }_nx_secure_tls_1_3_generate_handshake_secrets (NX_SECURE_TLS_SESSION *tls_session) { ... } /* ... */#endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_1_3_generate_session_secrets PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 in generating key material. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) static UINT _nx_secure_tls_1_3_generate_session_secrets(NX_SECURE_TLS_SESSION *tls_session) { UINT status; NX_SECURE_TLS_KEY_SECRETS *secrets; UINT hash_length; const NX_CRYPTO_METHOD *hash_method; UCHAR *label; UINT label_length; UCHAR (*transcript_hashes)[NX_SECURE_TLS_MAX_HASH_SIZE] = tls_session->nx_secure_tls_key_material.nx_secure_tls_transcript_hashes; if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { /* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */ return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); }if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... } /* Get the hash method so we know how much data we are generating. */ hash_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash; hash_length = (hash_method->nx_crypto_ICV_size_in_bits >> 3); /* Get a pointer to our key secrets for this session. */ secrets = &tls_session->nx_secure_tls_key_material.nx_secure_tls_key_secrets; /* Make sure our zeroes string is initialized. */ NX_SECURE_MEMSET(_nx_secure_tls_zeroes, 0, sizeof(_nx_secure_tls_zeroes)); /* Application Master secret - special case! Needs a secret derived from the previous secret. */ if(secrets->tls_master_secret_len == 0 && secrets->tls_handshake_secret_len != 0) { /* Start by deriving the salt from the handshake secret. Context is empty! */ status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_handshake_secret, secrets->tls_handshake_secret_len, (UCHAR *)"derived", 7, (UCHAR *)"", 0, secrets->tls_master_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } /* Now perform an HKDF-Extract to get the application master secret. Salt: derived secret from above, IKM: 0 string *//* ... */ status = _nx_secure_tls_hkdf_extract(tls_session, secrets->tls_master_secret, hash_length, _nx_secure_tls_zeroes, hash_length, secrets->tls_master_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_master_secret_len = hash_length; }if (secrets->tls_master_secret_len == 0 && secrets->tls_handshake_secret_len != 0) { ... } /* Derive secrets based on the application master secret. */ if(secrets->tls_master_secret_len != 0) { /*----- Client application traffic secret 0. -----*/ label = (UCHAR *)"c ap traffic"; label_length = 12; status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_master_secret, secrets->tls_master_secret_len, label, label_length, transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_SERVER_FINISHED], hash_length, secrets->tls_client_application_traffic_secret_0, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_client_application_traffic_secret_0_len = hash_length; /*----- Server application traffic secret 0. -----*/ label = (UCHAR *)"s ap traffic"; label_length = 12; status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_master_secret, secrets->tls_master_secret_len, label, label_length, transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_SERVER_FINISHED], hash_length, secrets->tls_server_application_traffic_secret_0, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_server_application_traffic_secret_0_len = hash_length; /*----- Exporter master secret. -----*/ label = (UCHAR *)"exp master"; label_length = 10; status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_master_secret, secrets->tls_master_secret_len, label, label_length, transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_SERVER_FINISHED], hash_length, secrets->tls_exporter_master_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_early_exporter_master_secret_len = hash_length; /*----- Resumption master secret. -----*/ label = (UCHAR *)"res master"; label_length = 10; status = _nx_secure_tls_derive_secret(tls_session, secrets->tls_master_secret, secrets->tls_master_secret_len, label, label_length, transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_CLIENT_FINISHED], hash_length, secrets->tls_resumption_master_secret, hash_length, hash_method); if(status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } secrets->tls_resumption_master_secret_len = hash_length; }if (secrets->tls_master_secret_len != 0) { ... } return(NX_SUCCESS); }_nx_secure_tls_1_3_generate_session_secrets (NX_SECURE_TLS_SESSION *tls_session) { ... } /* ... */#endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_derive_secret PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 in generating key material. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) static UCHAR _nx_secure_tls_temp_hash[100]; static UINT _nx_secure_tls_derive_secret(NX_SECURE_TLS_SESSION *tls_session, UCHAR *secret, UINT secret_len, UCHAR *label, UINT label_len, UCHAR *message_hash, UINT message_hash_len, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method) { UINT status; UINT hash_length; /* From RFC 8446, section 7.1: Derive-Secret(Secret, Label, Messages) = HKDF-Expand-Label(Secret, Label, Transcript-Hash(Messages), Hash.length) *//* ... */ /* Get session hash routine. */ hash_length = (hash_method->nx_crypto_ICV_size_in_bits >> 3); /* Our "messages" parameter is actually the ongoing hash of handshake messages stored in the TLS session context. In some contexts, the message hash will be of 0 length! *//* ... */ if(message_hash_len == 0) { /* Point the message hash at our temporary buffer. */ message_hash = &_nx_secure_tls_temp_hash[0]; message_hash_len = hash_length; /* Context has 0 length, so generate a hash on the empty string to feed into expand label call below. * Utilize the temporary "hash scratch" data buffer to initialize and calculate the hash. *//* ... */ if (hash_method -> nx_crypto_init) { status = hash_method -> nx_crypto_init((NX_CRYPTO_METHOD*)hash_method, NX_NULL, 0, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_handler, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hash_method -> nx_crypto_init) { ... } if (hash_method -> nx_crypto_operation != NX_NULL) { status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_INITIALIZE, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_handler, (NX_CRYPTO_METHOD*)hash_method, NX_NULL, 0, NX_NULL, 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size, NX_NULL, NX_NULL); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hash_method -> nx_crypto_operation != NX_NULL) { ... } if (hash_method -> nx_crypto_operation != NX_NULL) { status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_handler, (NX_CRYPTO_METHOD*)hash_method, NX_NULL, 0, (UCHAR *)"", 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size, NX_NULL, NX_NULL); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hash_method -> nx_crypto_operation != NX_NULL) { ... } /* Generate a hash using our temporary copy of the hash metadata, place it into the TLS Session transcript hash array. */ if (hash_method -> nx_crypto_operation != NX_NULL) { status = hash_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_handler, (NX_CRYPTO_METHOD*)hash_method, NX_NULL, 0, NX_NULL, 0, NX_NULL, message_hash, hash_length, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size, NX_NULL, NX_NULL); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hash_method -> nx_crypto_operation != NX_NULL) { ... } }if (message_hash_len == 0) { ... } /* Now derive the output by calling HKDF-Expand-Label. */ status = _nx_secure_tls_hkdf_expand_label(tls_session, secret, secret_len, label, label_len, message_hash, message_hash_len, hash_length, output, output_length, hash_method); return(status); }_nx_secure_tls_derive_secret (NX_SECURE_TLS_SESSION *tls_session, UCHAR *secret, UINT secret_len, UCHAR *label, UINT label_len, UCHAR *message_hash, UINT message_hash_len, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method) { ... } /* ... */ #endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_hkdf_expand_label PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 in generating key material. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) /* Buffer for HKDF output. The HKDF temporary can technically be as big as 514 bytes: 2 (length) + 1 (label length byte) + 255 (label) + 1 (context length byte) + 255 (context). However, 100 bytes is sufficient for the mandatory ciphersuite. *//* ... */ static UCHAR _nx_secure_tls_hkdf_temp_output[100]; static UINT _nx_secure_tls_hkdf_expand_label(NX_SECURE_TLS_SESSION *tls_session, UCHAR *secret, UINT secret_len, UCHAR *label, UINT label_len, UCHAR *context, UINT context_len, UINT length, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method) { UINT status; UINT data_len; const NX_CRYPTO_METHOD *session_hkdf_method = NX_NULL; const NX_CRYPTO_METHOD *session_hmac_method = NX_NULL; /*VOID *handler = NX_NULL;*/ /* From RFC 8446, section 7.1: HKDF-Expand-Label(Secret, Label, Context, Length) = HKDF-Expand(Secret, HkdfLabel, Length) Where HkdfLabel is specified as: struct { uint16 length = Length; opaque label<7..255> = "tls13 " + Label; opaque context<0..255> = Context; } HkdfLabel; *//* ... */ if (sizeof(_nx_secure_tls_hkdf_temp_output) < (10u + label_len + context_len)) { /* Buffer too small. */ return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL); }if (sizeof(_nx_secure_tls_hkdf_temp_output) < (10u + label_len + context_len)) { ... } /* Get our HKDF method and hash routine. */ /*session_hash_method = ciphersuite->nx_secure_tls_hash;*/ session_hkdf_method = tls_session->nx_secure_tls_crypto_table->nx_secure_tls_hkdf_method; session_hmac_method = tls_session->nx_secure_tls_crypto_table->nx_secure_tls_hmac_method; /* Now build the HkdfLabel from our inputs. */ _nx_secure_tls_hkdf_temp_output[0] = (UCHAR)((length & 0xFF00) >> 8); _nx_secure_tls_hkdf_temp_output[1] = (UCHAR)(length & 0x00FF); data_len = 2; /* Add the length of the label (single octet). */ _nx_secure_tls_hkdf_temp_output[data_len] = (UCHAR)(6 + label_len); data_len = data_len + 1; /* Now copy in label with TLS 1.3 prefix. */ NX_CRYPTO_MEMCPY(&_nx_secure_tls_hkdf_temp_output[data_len], "tls13 ", 6); /* Use case of memcpy is verified. */ data_len += 6; NX_CRYPTO_MEMCPY(&_nx_secure_tls_hkdf_temp_output[data_len], label, label_len); /* Use case of memcpy is verified. */ data_len += label_len; /* Add the length of the context (single octet). */ _nx_secure_tls_hkdf_temp_output[data_len] = (UCHAR)(context_len); data_len = data_len + 1; /* Now copy in context. */ NX_CRYPTO_MEMCPY(&_nx_secure_tls_hkdf_temp_output[data_len], context, context_len); /* Use case of memcpy is verified. */ data_len += context_len; /* Initialize the HKDF context. */ status = session_hkdf_method->nx_crypto_init((NX_CRYPTO_METHOD*)session_hkdf_method, NX_NULL, 0, NX_NULL, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Set the hash and HMAC routines for the HKDF. */ status = session_hkdf_method->nx_crypto_operation(NX_CRYPTO_HKDF_SET_HMAC, NX_NULL, (NX_CRYPTO_METHOD*)session_hmac_method, NX_NULL, 0, NX_NULL, 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size, NX_NULL, NX_NULL); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } status = session_hkdf_method->nx_crypto_operation(NX_CRYPTO_HKDF_SET_HASH, NX_NULL, (NX_CRYPTO_METHOD*)hash_method, NX_NULL, 0,NX_NULL, 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size, NX_NULL, NX_NULL); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Set the PRK for the HKDF-expand operation. */ status = session_hkdf_method->nx_crypto_operation(NX_CRYPTO_HKDF_SET_PRK, NX_NULL, (NX_CRYPTO_METHOD*)session_hkdf_method, (UCHAR*)(secret), /* Input HKDF label. */ (secret_len << 3), NX_NULL, 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size, NX_NULL, NX_NULL); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Now perform the HKDF operation. */ status = session_hkdf_method->nx_crypto_operation(NX_CRYPTO_HKDF_EXPAND, NX_NULL, (NX_CRYPTO_METHOD*)session_hkdf_method, (UCHAR*)(_nx_secure_tls_hkdf_temp_output), /* Input HKDF label. */ (data_len << 3), NX_NULL, 0, NX_NULL, (UCHAR *)output, output_length, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size, NX_NULL, NX_NULL); return(status); }_nx_secure_tls_hkdf_expand_label (NX_SECURE_TLS_SESSION *tls_session, UCHAR *secret, UINT secret_len, UCHAR *label, UINT label_len, UCHAR *context, UINT context_len, UINT length, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method) { ... } /* ... */#endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_hkdf_extract PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is used by TLS 1.3 in generating key material. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* salt HKDF salt parameter */ /* salt_len Length of salt */ /* ikm HKDF input key material */ /* ikm_len Length of IKM */ /* output Output buffer */ /* output_length Desired output length */ /* hash_method Hash routine for HMAC */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* 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 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) /*static UCHAR _nx_secure_tls_hkdf_label[524];*/ static UINT _nx_secure_tls_hkdf_extract(NX_SECURE_TLS_SESSION *tls_session, UCHAR *salt, UINT salt_len, UCHAR *ikm, UINT ikm_len, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method) { UINT status; const NX_CRYPTO_METHOD *session_hkdf_method = NX_NULL; const NX_CRYPTO_METHOD *session_hmac_method = NX_NULL; /* Get our HKDF method and hash routine. */ session_hkdf_method = tls_session->nx_secure_tls_crypto_table->nx_secure_tls_hkdf_method; session_hmac_method = tls_session->nx_secure_tls_crypto_table->nx_secure_tls_hmac_method; /* Initialize the HKDF context with our IKM. */ status = session_hkdf_method->nx_crypto_init((NX_CRYPTO_METHOD*)session_hkdf_method, ikm, ikm_len << 3, NX_NULL, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Set the hash and HMAC routines for the HKDF. */ status = session_hkdf_method->nx_crypto_operation(NX_CRYPTO_HKDF_SET_HMAC, NX_NULL, (NX_CRYPTO_METHOD*)session_hmac_method, NX_NULL, 0, NX_NULL, 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size, NX_NULL, NX_NULL); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } status = session_hkdf_method->nx_crypto_operation(NX_CRYPTO_HKDF_SET_HASH, NX_NULL, (NX_CRYPTO_METHOD*)hash_method, NX_NULL, 0,NX_NULL, 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size, NX_NULL, NX_NULL); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Now perform the HKDF operation. */ status = session_hkdf_method->nx_crypto_operation(NX_CRYPTO_HKDF_EXTRACT, NX_NULL, (NX_CRYPTO_METHOD*)session_hkdf_method, salt, salt_len << 3, ikm, ikm_len, NX_NULL, (UCHAR *)output, output_length, tls_session -> nx_secure_tls_prf_metadata_area, tls_session -> nx_secure_tls_prf_metadata_size, NX_NULL, NX_NULL); return(status); }_nx_secure_tls_hkdf_extract (NX_SECURE_TLS_SESSION *tls_session, UCHAR *salt, UINT salt_len, UCHAR *ikm, UINT ikm_len, UCHAR *output, UINT output_length, const NX_CRYPTO_METHOD *hash_method) { ... } /* ... */#endif ...
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.