Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
_nx_secure_tls_record_block_buffer
...
...
_nx_secure_tls_record_payload_encrypt(NX_SECURE_TLS_SESSION *, NX_PACKET *, ULONG *, UCHAR)
...
_nx_secure_tls_record_data_encrypt_init(NX_SECURE_TLS_SESSION *, NX_PACKET *, ULONG *, UCHAR, UINT *, const NX_CRYPTO_METHOD *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_record_payload_encrypt.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_tls.h" static UINT _nx_secure_tls_record_data_encrypt_init(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet, ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE], UCHAR record_type, UINT *data_offset, const NX_CRYPTO_METHOD *session_cipher_method); UCHAR _nx_secure_tls_record_block_buffer[NX_SECURE_TLS_MAX_CIPHER_BLOCK_SIZE]; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_record_payload_encrypt PORTABLE C */ /* 6.1.8 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function encrypts the payload of an outgoing TLS record using */ /* the session keys generated and ciphersuite determined during the */ /* TLS handshake. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* send_packet Pointer to packet data */ /* sequence_num Current TLS/DTLS message num */ /* record_type TLS record type */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [nx_crypto_operation] Decryption ciphers */ /* */ /* CALLED BY */ /* */ /* _nx_secure_dtls_send_record Send DTLS encrypted record */ /* _nx_secure_tls_send_record Send TLS encrypted record */ /* */ /* 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, */ /* fixed data copy in chained */ /* packet, */ /* resulting in version 6.1 */ /* 08-02-2021 Timothy Stapko Modified comment(s), called */ /* NX_CRYPTO_ENCRYPT_CALCULATE */ /* to finalize the encryption */ /* of this record, resulting */ /* in version 6.1.8 */ /* */... /**************************************************************************/ UINT _nx_secure_tls_record_payload_encrypt(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet, ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE], UCHAR record_type) { UINT status; UCHAR *iv; const NX_CRYPTO_METHOD *session_cipher_method; UINT block_size; USHORT iv_size; NX_PACKET *current_packet; ULONG current_length; ULONG rounded_length; ULONG copy_length; ULONG remainder_length; UINT data_offset = 0; VOID *handler = NX_NULL; VOID *crypto_method_metadata; UINT icv_size = 0; UCHAR *icv_ptr = NX_NULL; 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) { ... } /* Select metadata based on the current mode. */ if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { /* The socket is a TLS server, so use the server cipher to encrypt. */ iv = tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_iv; handler = tls_session -> nx_secure_session_cipher_handler_server; crypto_method_metadata = tls_session -> nx_secure_session_cipher_metadata_area_server; }if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { ... } else { /* The socket is a TLS client, so use the client cipher to encrypt. */ iv = tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_iv; handler = tls_session -> nx_secure_session_cipher_handler_client; crypto_method_metadata = tls_session -> nx_secure_session_cipher_metadata_area_client; }else { ... } /* Select the encryption algorithm based on the ciphersuite. Then, using the session keys and the chosen cipher, encrypt the data in place. *//* ... */ session_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_session_cipher; if (session_cipher_method -> nx_crypto_operation == NX_NULL) { /* No encryption needed. */ return(NX_SUCCESS); }if (session_cipher_method -> nx_crypto_operation == NX_NULL) { ... } block_size = session_cipher_method -> nx_crypto_block_size_in_bytes; /* Get the size of the IV used by the session cipher. */ iv_size = session_cipher_method -> nx_crypto_IV_size_in_bits >> 3; /* Make sure our block size is small enough to fit into our buffer. */ NX_ASSERT((iv_size <= NX_SECURE_TLS_MAX_CIPHER_BLOCK_SIZE) && (block_size <= NX_SECURE_TLS_MAX_CIPHER_BLOCK_SIZE)); status = _nx_secure_tls_record_data_encrypt_init(tls_session, send_packet, sequence_num, record_type, &data_offset, session_cipher_method); if (status) { return(status); }if (status) { ... } /* Iterate through the packet chain using a temporary pointer. */ current_packet = send_packet; /* Loop through all packets in the chain. */ do { /* Get our current packet length. Use the data_offset from any previous iterations. */ current_length = (ULONG)(current_packet -> nx_packet_append_ptr - current_packet -> nx_packet_prepend_ptr) - data_offset; /* See if there are more packets in the chain. */ if (current_packet -> nx_packet_next == NX_NULL) { if (current_length) { /* Encrypt any remaining data in the current packet since it is our last. */ /* Offset should be such that the remaining data in the packet is remainder_length bytes. Add the block-boundary length to the previous offset to get this new offset. Also, add padding as this is the last bit of data to be encrypted (all previous packets should have been encrypted without padding). *//* ... */ status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_UPDATE, handler, (NX_CRYPTO_METHOD *)session_cipher_method, NX_NULL, 0, &current_packet -> nx_packet_prepend_ptr[data_offset], current_length, NX_NULL, &current_packet -> nx_packet_prepend_ptr[data_offset], current_length, crypto_method_metadata, tls_session -> nx_secure_session_cipher_metadata_size, NX_NULL, NX_NULL); if (status != NX_SUCCESS) { return(status); }if (status != NX_SUCCESS) { ... } /* CBC-mode ciphers need to have their IV's updated after encryption. */ if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) { /* New IV is the last encrypted block of the output. */ NX_SECURE_MEMCPY(iv, &current_packet -> nx_packet_prepend_ptr[current_length + data_offset - iv_size], iv_size); /* Use case of memcpy is verified. */ }if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) { ... } }if (current_length) { ... } }if (current_packet -> nx_packet_next == NX_NULL) { ... } else { /* Figure out how much we can encrypt. Get an evenly-divisible block of data and the remainder. *//* ... */ if (block_size == 0) { remainder_length = 0; }if (block_size == 0) { ... } else { remainder_length = (ULONG)((current_length % block_size)); }else { ... } rounded_length = current_length - remainder_length; if (rounded_length > 0) { /* Encrypt remaining data in the current packet from our previous offset to evenly-divisible block boundary. */ status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_UPDATE, handler, (NX_CRYPTO_METHOD *)session_cipher_method, NX_NULL, 0, &current_packet -> nx_packet_prepend_ptr[data_offset], rounded_length, NX_NULL, &current_packet -> nx_packet_prepend_ptr[data_offset], rounded_length, crypto_method_metadata, tls_session -> nx_secure_session_cipher_metadata_size, NX_NULL, NX_NULL); if (status != NX_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(_nx_secure_tls_record_block_buffer, 0, block_size); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_SUCCESS) { ... } }if (rounded_length > 0) { ... } if (remainder_length) { /* Copy data into temporary buffer for encryption. Pointers: packet2->prepend_ptr[data_offset]---v Lengths: | current length | remainder | block size - remainder | packet 2 remainder | Data: |**Packet 1 data***************|**Packet 2 data*************************************| Temporary: | record block buffer [block size] | *//* ... */ /* Offset for remainder bytes is rounded_length + data_offset. */ NX_SECURE_MEMCPY(&_nx_secure_tls_record_block_buffer[0], &current_packet -> nx_packet_prepend_ptr[rounded_length + data_offset], remainder_length); /* Use case of memcpy is verified. */ copy_length = (ULONG)(current_packet -> nx_packet_next -> nx_packet_append_ptr - current_packet -> nx_packet_next -> nx_packet_prepend_ptr); if (copy_length > (ULONG)(block_size - remainder_length)) { copy_length = (ULONG)(block_size - remainder_length); }if (copy_length > (ULONG)(block_size - remainder_length)) { ... } NX_SECURE_MEMCPY(&_nx_secure_tls_record_block_buffer[remainder_length], current_packet -> nx_packet_next -> nx_packet_prepend_ptr, copy_length); /* Use case of memcpy is verified. */ /* Encrypt the remainder block. */ status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_UPDATE, handler, (NX_CRYPTO_METHOD *)session_cipher_method, NX_NULL, 0, _nx_secure_tls_record_block_buffer, (remainder_length + copy_length), NX_NULL, _nx_secure_tls_record_block_buffer, (remainder_length + copy_length), crypto_method_metadata, tls_session -> nx_secure_session_cipher_metadata_size, NX_NULL, NX_NULL); if (status != NX_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(_nx_secure_tls_record_block_buffer, 0, block_size); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_SUCCESS) { ... } /* Copy data from temporary buffer back into packets. */ NX_SECURE_MEMCPY(&current_packet -> nx_packet_prepend_ptr[rounded_length + data_offset], &_nx_secure_tls_record_block_buffer[0], remainder_length); /* Use case of memcpy is verified. */ NX_SECURE_MEMCPY(current_packet -> nx_packet_next -> nx_packet_prepend_ptr, &_nx_secure_tls_record_block_buffer[remainder_length], copy_length); /* Use case of memcpy is verified. */ /* CBC-mode ciphers need to have their IV's updated after encryption. */ if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) { /* New IV is the last encrypted block of the output. */ NX_SECURE_MEMCPY(iv, &_nx_secure_tls_record_block_buffer, iv_size); /* Use case of memcpy is verified. */ }if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) { ... } #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(_nx_secure_tls_record_block_buffer, 0, block_size); #endif /* NX_SECURE_KEY_CLEAR */ /* Finally, our new offset for the next round is the number of bytes we already encrypted (along with the remainder bytes) in the next packet. *//* ... */ data_offset = copy_length; }if (remainder_length) { ... } else { data_offset = 0; }else { ... } }else { ... } /* Move to the next packet. */ current_packet = current_packet -> nx_packet_next; ...} while (current_packet != NX_NULL); if (session_cipher_method -> nx_crypto_ICV_size_in_bits > 0) { /* Get icv_size and icv_ptr for AEAD cipher */ if ((session_cipher_method -> nx_crypto_ICV_size_in_bits >> 3) > sizeof(_nx_secure_tls_record_block_buffer)) { return(NX_SIZE_ERROR); }if ((session_cipher_method -> nx_crypto_ICV_size_in_bits >> 3) > sizeof(_nx_secure_tls_record_block_buffer)) { ... } icv_size = session_cipher_method -> nx_crypto_ICV_size_in_bits >> 3; icv_ptr = _nx_secure_tls_record_block_buffer; }if (session_cipher_method -> nx_crypto_ICV_size_in_bits > 0) { ... } /* Call NX_CRYPTO_ENCRYPT_CALCULATE to finalize the encryption of this record. */ status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_CALCULATE, handler, (NX_CRYPTO_METHOD*)session_cipher_method, NX_NULL, NX_NULL, NX_NULL, 0, NX_NULL, icv_ptr, icv_size, crypto_method_metadata, tls_session -> nx_secure_session_cipher_metadata_size, NX_NULL, NX_NULL); if (status) { return(status); }if (status) { ... } if (icv_ptr && icv_size) { /* Append data for AEAD cipher */ status = nx_packet_data_append(send_packet, icv_ptr, icv_size, tls_session -> nx_secure_tls_packet_pool, NX_WAIT_FOREVER); if (status) { return(status); }if (status) { ... } }if (icv_ptr && icv_size) { ... } return(NX_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_record_data_encrypt_init PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is a static helper function used to initialize */ /* metadata for encryption. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* send_packet Pointer to packet data */ /* sequence_num Current TLS/DTLS message num */ /* record_type TLS record type */ /* data_offset Data offset for Initial Vector*/ /* session_cipher_method Pointer to cipher method */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [nx_crypto_operation] Decryption ciphers */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_record_payload_encrypt Encrypt payload */ /* */ /* 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 */ /* */... /**************************************************************************/ static UINT _nx_secure_tls_record_data_encrypt_init(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet, ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE], UCHAR record_type, UINT *data_offset, const NX_CRYPTO_METHOD *session_cipher_method) { UINT status; UCHAR *iv; VOID *handler = NX_NULL; VOID *crypto_method_metadata; UINT block_size; USHORT iv_size; UCHAR padding_length; #ifdef NX_SECURE_ENABLE_AEAD_CIPHER UCHAR additional_data[13]; UINT additional_data_size = 0; UCHAR nonce[13]; #if (NX_SECURE_TLS_TLS_1_3_ENABLED) UINT record_length; #endif UINT message_length;/* ... */ #else NX_PARAMETER_NOT_USED(sequence_num); NX_PARAMETER_NOT_USED(record_type);/* ... */ #endif /* NX_SECURE_ENABLE_AEAD_CIPHER */ /* Select IV based on the current mode. */ if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { /* The socket is a TLS server, so use the server cipher to encrypt. */ iv = tls_session -> nx_secure_tls_key_material.nx_secure_tls_server_iv; handler = tls_session -> nx_secure_session_cipher_handler_server; crypto_method_metadata = tls_session -> nx_secure_session_cipher_metadata_area_server; }if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { ... } else { /* The socket is a TLS client, so use the client cipher to encrypt. */ iv = tls_session -> nx_secure_tls_key_material.nx_secure_tls_client_iv; handler = tls_session -> nx_secure_session_cipher_handler_client; crypto_method_metadata = tls_session -> nx_secure_session_cipher_metadata_area_client; }else { ... } /* Offset into current packet data. */ *data_offset = 0; /* See if we need to add any data to the beginning of the payload such as an IV (e.g. for AES-CBC mode). */ /* !!! NOTE: This relies on nx_secure_tls_packet_allocate reserving block_size bytes between NX_PACKET.nx_packet_prepend_ptr and nx_packet_append_ptr !!! *//* ... */ #ifdef NX_SECURE_ENABLE_AEAD_CIPHER if ((session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CCM_8) || (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CCM_12) || (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CCM_16) || (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_GCM_16) || NX_SECURE_AEAD_CIPHER_CHECK(session_cipher_method -> nx_crypto_algorithm)) { #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (tls_session -> nx_secure_tls_1_3) { /* Each AEAD algorithm will specify a range of possible lengths for the per-record nonce, from N_MIN bytes to N_MAX bytes of input [RFC5116]. The length of the TLS per-record nonce (iv_length) is set to the larger of 8 bytes and N_MIN for the AEAD algorithm (see [RFC5116], Section 4). An AEAD algorithm where N_MAX is less than 8 bytes MUST NOT be used with TLS. The per-record nonce for the AEAD construction is formed as follows: 1. The 64-bit record sequence number is encoded in network byte order and padded to the left with zeros to iv_length. 2. The padded sequence number is XORed with either the static client_write_iv or server_write_iv (depending on the role). The resulting quantity (of length iv_length) is used as the per-record nonce. Note: This is a different construction from that in TLS 1.2, which specified a partially explicit nonce. *//* ... */ /* The length of the nonce is 12 bytes. */ nonce[0] = 12; /* Copy client_write_IV or server_write_IV. */ NX_SECURE_MEMCPY(&nonce[1], iv, 12); /* Use case of memcpy is verified. */ /* Correct the endianness of our sequence number and XOR with * the IV. Pad to the left with zeroes. *//* ... */ nonce[1] = (UCHAR)(nonce[1] ^ 0); nonce[2] = (UCHAR)(nonce[2] ^ 0); nonce[3] = (UCHAR)(nonce[3] ^ 0); nonce[4] = (UCHAR)(nonce[4] ^ 0); nonce[5] = (UCHAR)(nonce[5] ^ (sequence_num[1] >> 24)); nonce[6] = (UCHAR)(nonce[6] ^ (sequence_num[1] >> 16)); nonce[7] = (UCHAR)(nonce[7] ^ (sequence_num[1] >> 8)); nonce[8] = (UCHAR)(nonce[8] ^ (sequence_num[1])); nonce[9] = (UCHAR)(nonce[9] ^ (sequence_num[0] >> 24)); nonce[10] = (UCHAR)(nonce[10] ^ (sequence_num[0] >> 16)); nonce[11] = (UCHAR)(nonce[11] ^ (sequence_num[0] >> 8)); nonce[12] = (UCHAR)(nonce[12] ^ (sequence_num[0])); /* additional_data = record header * record header = TLSCiphertext.opaque_type || TLSCiphertext.legacy_record_version || TLSCiphertext.length *//* ... */ /* There is no explicit ICV in TLS 1.3 AEAD. */ record_length = send_packet -> nx_packet_length + (session_cipher_method -> nx_crypto_ICV_size_in_bits >> 3); message_length = send_packet -> nx_packet_length; additional_data[0] = record_type; additional_data[1] = (UCHAR)(0x03); additional_data[2] = (UCHAR)(0x03); additional_data[3] = (UCHAR)((record_length) >> 8); additional_data[4] = (UCHAR)(record_length); /* We have 5 bytes of additional data. */ additional_data_size = 5; }if (tls_session -> nx_secure_tls_1_3) { ... } else #endif { /* AEAD ciphers structure: struct { opaque nonce_explicit[SecurityParameters.record_iv_length]; aead-ciphered struct { opaque content[TLSCompressed.length]; }; } GenericAEADCipher; *//* ... */ /* The nonce of the CCM cipher is passed into crypto method using iv_ptr. struct { uint32 client_write_IV; // low order 32-bits uint64 seq_num; // TLS sequence number } CCMClientNonce. struct { uint32 server_write_IV; // low order 32-bits uint64 seq_num; // TLS sequence number } CCMServerNonce. *//* ... */ /* The length of CCMClientNonce or CCMServerNonce is 12 bytes. */ nonce[0] = 12; /* Copy client_write_IV or server_write_IV. */ NX_SECURE_MEMCPY(&nonce[1], iv, 4); /* Use case of memcpy is verified. */ /* Correct the endianness of our sequence number before hashing. */ nonce[5] = (UCHAR)(sequence_num[1] >> 24); nonce[6] = (UCHAR)(sequence_num[1] >> 16); nonce[7] = (UCHAR)(sequence_num[1] >> 8); nonce[8] = (UCHAR)(sequence_num[1]); nonce[9] = (UCHAR)(sequence_num[0] >> 24); nonce[10] = (UCHAR)(sequence_num[0] >> 16); nonce[11] = (UCHAR)(sequence_num[0] >> 8); nonce[12] = (UCHAR)(sequence_num[0]); /* additional_data = seq_num + TLSCompressed.type + TLSCompressed.version + TLSCompressed.length; *//* ... */ message_length = send_packet -> nx_packet_length - 8; NX_SECURE_MEMCPY(additional_data, &nonce[5], 8); /* Use case of memcpy is verified. */ additional_data[8] = record_type; additional_data[9] = (UCHAR)(tls_session -> nx_secure_tls_protocol_version >> 8); additional_data[10] = (UCHAR)(tls_session -> nx_secure_tls_protocol_version); additional_data[11] = (UCHAR)(message_length >> 8); additional_data[12] = (UCHAR)(message_length); /* We have 13 bytes of additional data (8 bytes seq num + 5 bytes header). */ additional_data_size = 13; /* Copy our IV into our data buffer at the head of the payload. */ NX_SECURE_MEMCPY(send_packet -> nx_packet_prepend_ptr, &nonce[5], 8); /* Use case of memcpy is verified. */ *data_offset = 8; }else { ... } /* Initialize crypto algorithm. */ status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_INITIALIZE, handler, (NX_CRYPTO_METHOD*)session_cipher_method, NX_NULL, 0, additional_data, additional_data_size, nonce, NX_NULL, message_length, crypto_method_metadata, tls_session -> nx_secure_session_cipher_metadata_size, NX_NULL, NX_NULL); return(status); }if ((session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CCM_8) || (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CCM_12) || (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CCM_16) || (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_GCM_16) || NX_SECURE_AEAD_CIPHER_CHECK(session_cipher_method -> nx_crypto_algorithm)) { ... } /* ... */#endif /* NX_SECURE_ENABLE_AEAD_CIPHER */ block_size = session_cipher_method -> nx_crypto_block_size_in_bytes; /* Get the size of the IV used by the session cipher. */ iv_size = session_cipher_method -> nx_crypto_IV_size_in_bits >> 3; if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) { /* CBC mode has a specific structure for encrypted data, so handle that here: block-ciphered struct { opaque IV[CipherSpec.block_length]; opaque content[TLSCompressed.length]; opaque MAC[CipherSpec.hash_size]; uint8 padding[GenericBlockCipher.padding_length]; uint8 padding_length; } GenericBlockCipher; *//* ... */ /* TLS 1.0 does not use an explicit IV in CBC-mode ciphers, so don't include it in the record. *//* ... */ if (tls_session -> nx_secure_tls_protocol_version != NX_SECURE_TLS_VERSION_TLS_1_0) { if (iv_size > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_prepend_ptr))) { /* Packet buffer too small. */ return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL); }if (iv_size > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_prepend_ptr))) { ... } /* IV size is equal to the AES block size. Copy our IV into our data buffer at the head of the payload. *//* ... */ NX_SECURE_MEMCPY(send_packet -> nx_packet_prepend_ptr, iv, iv_size); /* Use case of memcpy is verified. */ *data_offset = iv_size; }if (tls_session -> nx_secure_tls_protocol_version != NX_SECURE_TLS_VERSION_TLS_1_0) { ... } if (iv_size != block_size) { /* Invalid size. */ return(NX_SECURE_TLS_INVALID_STATE); }if (iv_size != block_size) { ... } /* Initialize crypto algorithm. */ status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_INITIALIZE, handler, (NX_CRYPTO_METHOD*)session_cipher_method, NX_NULL, 0, NX_NULL, 0, iv, NX_NULL, 0, crypto_method_metadata, tls_session -> nx_secure_session_cipher_metadata_size, NX_NULL, NX_NULL); if (status) { return(status); }if (status) { ... } }if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) { ... } /* Padding - final output must be an integral multiple of the block size (16 bytes for AES * modes used in TLS). If the data is not a multiple, the padding consists of bytes each * with the value of the length of the padding (e.g. for 3 bytes, the padding would be 0x03, * 0x03, 0x03). *//* ... */ if (block_size > 0) { padding_length = (UCHAR)(block_size - (send_packet -> nx_packet_length % block_size)); NX_SECURE_MEMSET(_nx_secure_tls_record_block_buffer, padding_length - 1, padding_length); status = nx_packet_data_append(send_packet, _nx_secure_tls_record_block_buffer, padding_length, tls_session -> nx_secure_tls_packet_pool, NX_WAIT_FOREVER); #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(_nx_secure_tls_record_block_buffer, 0, block_size); #endif /* NX_SECURE_KEY_CLEAR */ if (status) { return(status); }if (status) { ... } }if (block_size > 0) { ... } return(NX_SUCCESS); }{ ... }