1
10
13
14
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
134
135
136
142
143
144
145
146
147
148
149
150
151
152
153
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
180
181
182
183
184
185
186
187
188
189
190
191
192
193
197
198
199
206
207
208
209
210
211
213
217
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
247
248
249
250
251
252
258
259
260
261
262
263
264
265
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
311
312
313
314
315
316
318
319
320
324
325
326
327
328
329
330
331
332
333
334
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
362
363
364
365
366
367
368
369
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
444
445
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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
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
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
643
644
645
651
652
654
655
656
657
658
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
684
685
686
691
692
693
694
695
696
697
698
699
700
701
702
703
707
708
709
710
...
...
...
#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];
...
...
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)
{
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL) { ... }
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
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
{
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 { ... }
/* ... */
session_cipher_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_session_cipher;
if (session_cipher_method -> nx_crypto_operation == NX_NULL)
{
return(NX_SUCCESS);
}if (session_cipher_method -> nx_crypto_operation == NX_NULL) { ... }
block_size = session_cipher_method -> nx_crypto_block_size_in_bytes;
iv_size = session_cipher_method -> nx_crypto_IV_size_in_bits >> 3;
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) { ... }
current_packet = send_packet;
do
{
current_length = (ULONG)(current_packet -> nx_packet_append_ptr -
current_packet -> nx_packet_prepend_ptr) - data_offset;
if (current_packet -> nx_packet_next == NX_NULL)
{
if (current_length)
{
/* ... */
status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_UPDATE,
handler,
(NX_CRYPTO_METHOD *)session_cipher_method,
NX_NULL,
0,
¤t_packet -> nx_packet_prepend_ptr[data_offset],
current_length,
NX_NULL,
¤t_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) { ... }
if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC)
{
NX_SECURE_MEMCPY(iv, ¤t_packet -> nx_packet_prepend_ptr[current_length + data_offset - iv_size],
iv_size);
}if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) { ... }
}if (current_length) { ... }
}if (current_packet -> nx_packet_next == NX_NULL) { ... }
else
{
/* ... */
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)
{
status = session_cipher_method -> nx_crypto_operation(NX_CRYPTO_ENCRYPT_UPDATE,
handler,
(NX_CRYPTO_METHOD *)session_cipher_method,
NX_NULL,
0,
¤t_packet -> nx_packet_prepend_ptr[data_offset],
rounded_length,
NX_NULL,
¤t_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
return(status);
}if (status != NX_SUCCESS) { ... }
}if (rounded_length > 0) { ... }
if (remainder_length)
{
/* ... */
NX_SECURE_MEMCPY(&_nx_secure_tls_record_block_buffer[0],
¤t_packet -> nx_packet_prepend_ptr[rounded_length + data_offset],
remainder_length);
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);
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
return(status);
}if (status != NX_SUCCESS) { ... }
NX_SECURE_MEMCPY(¤t_packet -> nx_packet_prepend_ptr[rounded_length + data_offset],
&_nx_secure_tls_record_block_buffer[0], remainder_length);
NX_SECURE_MEMCPY(current_packet -> nx_packet_next -> nx_packet_prepend_ptr,
&_nx_secure_tls_record_block_buffer[remainder_length],
copy_length);
if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC)
{
NX_SECURE_MEMCPY(iv, &_nx_secure_tls_record_block_buffer, iv_size);
}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
/* ... */
data_offset = copy_length;
}if (remainder_length) { ... }
else
{
data_offset = 0;
}else { ... }
}else { ... }
current_packet = current_packet -> nx_packet_next;
...} while (current_packet != NX_NULL);
if (session_cipher_method -> nx_crypto_ICV_size_in_bits > 0)
{
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) { ... }
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)
{
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);
}{ ... }
...
...
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
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
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
{
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 { ... }
*data_offset = 0;
/* ... */
#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)
{
/* ... */
nonce[0] = 12;
NX_SECURE_MEMCPY(&nonce[1], iv, 12);
/* ... */
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]));
/* ... */
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);
additional_data_size = 5;
}if (tls_session -> nx_secure_tls_1_3) { ... }
else
#endif
{
/* ... */
/* ... */
nonce[0] = 12;
NX_SECURE_MEMCPY(&nonce[1], iv, 4);
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]);
/* ... */
message_length = send_packet -> nx_packet_length - 8;
NX_SECURE_MEMCPY(additional_data, &nonce[5], 8);
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);
additional_data_size = 13;
NX_SECURE_MEMCPY(send_packet -> nx_packet_prepend_ptr, &nonce[5], 8);
*data_offset = 8;
}else { ... }
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
block_size = session_cipher_method -> nx_crypto_block_size_in_bytes;
iv_size = session_cipher_method -> nx_crypto_IV_size_in_bits >> 3;
if (session_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC)
{
/* ... */
/* ... */
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)))
{
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))) { ... }
/* ... */
NX_SECURE_MEMCPY(send_packet -> nx_packet_prepend_ptr, iv, iv_size);
*data_offset = iv_size;
}if (tls_session -> nx_secure_tls_protocol_version != NX_SECURE_TLS_VERSION_TLS_1_0) { ... }
if (iv_size != block_size)
{
return(NX_SECURE_TLS_INVALID_STATE);
}if (iv_size != block_size) { ... }
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) { ... }
/* ... */
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
if (status)
{
return(status);
}if (status) { ... }
}if (block_size > 0) { ... }
return(NX_SUCCESS);
}{ ... }