@@ -120,6 +120,34 @@ static int skb_nsg(struct sk_buff *skb, int offset, int len)
120
120
return __skb_nsg (skb , offset , len , 0 );
121
121
}
122
122
123
+ static int padding_length (struct tls_sw_context_rx * ctx ,
124
+ struct tls_context * tls_ctx , struct sk_buff * skb )
125
+ {
126
+ struct strp_msg * rxm = strp_msg (skb );
127
+ int sub = 0 ;
128
+
129
+ /* Determine zero-padding length */
130
+ if (tls_ctx -> crypto_recv .info .version == TLS_1_3_VERSION ) {
131
+ char content_type = 0 ;
132
+ int err ;
133
+ int back = 17 ;
134
+
135
+ while (content_type == 0 ) {
136
+ if (back > rxm -> full_len )
137
+ return - EBADMSG ;
138
+ err = skb_copy_bits (skb ,
139
+ rxm -> offset + rxm -> full_len - back ,
140
+ & content_type , 1 );
141
+ if (content_type )
142
+ break ;
143
+ sub ++ ;
144
+ back ++ ;
145
+ }
146
+ ctx -> control = content_type ;
147
+ }
148
+ return sub ;
149
+ }
150
+
123
151
static void tls_decrypt_done (struct crypto_async_request * req , int err )
124
152
{
125
153
struct aead_request * aead_req = (struct aead_request * )req ;
@@ -142,7 +170,7 @@ static void tls_decrypt_done(struct crypto_async_request *req, int err)
142
170
tls_err_abort (skb -> sk , err );
143
171
} else {
144
172
struct strp_msg * rxm = strp_msg (skb );
145
-
173
+ rxm -> full_len -= padding_length ( ctx , tls_ctx , skb );
146
174
rxm -> offset += tls_ctx -> rx .prepend_size ;
147
175
rxm -> full_len -= tls_ctx -> rx .overhead_size ;
148
176
}
@@ -448,6 +476,8 @@ static int tls_do_encryption(struct sock *sk,
448
476
int rc ;
449
477
450
478
memcpy (rec -> iv_data , tls_ctx -> tx .iv , sizeof (rec -> iv_data ));
479
+ xor_iv_with_seq (tls_ctx -> crypto_send .info .version , rec -> iv_data ,
480
+ tls_ctx -> tx .rec_seq );
451
481
452
482
sge -> offset += tls_ctx -> tx .prepend_size ;
453
483
sge -> length -= tls_ctx -> tx .prepend_size ;
@@ -483,7 +513,8 @@ static int tls_do_encryption(struct sock *sk,
483
513
484
514
/* Unhook the record from context if encryption is not failure */
485
515
ctx -> open_rec = NULL ;
486
- tls_advance_record_sn (sk , & tls_ctx -> tx );
516
+ tls_advance_record_sn (sk , & tls_ctx -> tx ,
517
+ tls_ctx -> crypto_send .info .version );
487
518
return rc ;
488
519
}
489
520
@@ -640,7 +671,17 @@ static int tls_push_record(struct sock *sk, int flags,
640
671
641
672
i = msg_pl -> sg .end ;
642
673
sk_msg_iter_var_prev (i );
643
- sg_mark_end (sk_msg_elem (msg_pl , i ));
674
+
675
+ rec -> content_type = record_type ;
676
+ if (tls_ctx -> crypto_send .info .version == TLS_1_3_VERSION ) {
677
+ /* Add content type to end of message. No padding added */
678
+ sg_set_buf (& rec -> sg_content_type , & rec -> content_type , 1 );
679
+ sg_mark_end (& rec -> sg_content_type );
680
+ sg_chain (msg_pl -> sg .data , msg_pl -> sg .end + 1 ,
681
+ & rec -> sg_content_type );
682
+ } else {
683
+ sg_mark_end (sk_msg_elem (msg_pl , i ));
684
+ }
644
685
645
686
i = msg_pl -> sg .start ;
646
687
sg_chain (rec -> sg_aead_in , 2 , rec -> inplace_crypto ?
@@ -653,18 +694,22 @@ static int tls_push_record(struct sock *sk, int flags,
653
694
i = msg_en -> sg .start ;
654
695
sg_chain (rec -> sg_aead_out , 2 , & msg_en -> sg .data [i ]);
655
696
656
- tls_make_aad (rec -> aad_space , msg_pl -> sg .size ,
697
+ tls_make_aad (rec -> aad_space , msg_pl -> sg .size + tls_ctx -> tx . tail_size ,
657
698
tls_ctx -> tx .rec_seq , tls_ctx -> tx .rec_seq_size ,
658
- record_type );
699
+ record_type ,
700
+ tls_ctx -> crypto_send .info .version );
659
701
660
702
tls_fill_prepend (tls_ctx ,
661
703
page_address (sg_page (& msg_en -> sg .data [i ])) +
662
- msg_en -> sg .data [i ].offset , msg_pl -> sg .size ,
663
- record_type );
704
+ msg_en -> sg .data [i ].offset ,
705
+ msg_pl -> sg .size + tls_ctx -> tx .tail_size ,
706
+ record_type ,
707
+ tls_ctx -> crypto_send .info .version );
664
708
665
709
tls_ctx -> pending_open_record_frags = false;
666
710
667
- rc = tls_do_encryption (sk , tls_ctx , ctx , req , msg_pl -> sg .size , i );
711
+ rc = tls_do_encryption (sk , tls_ctx , ctx , req ,
712
+ msg_pl -> sg .size + tls_ctx -> tx .tail_size , i );
668
713
if (rc < 0 ) {
669
714
if (rc != - EINPROGRESS ) {
670
715
tls_err_abort (sk , EBADMSG );
@@ -1292,7 +1337,8 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1292
1337
u8 * aad , * iv , * mem = NULL ;
1293
1338
struct scatterlist * sgin = NULL ;
1294
1339
struct scatterlist * sgout = NULL ;
1295
- const int data_len = rxm -> full_len - tls_ctx -> rx .overhead_size ;
1340
+ const int data_len = rxm -> full_len - tls_ctx -> rx .overhead_size +
1341
+ tls_ctx -> rx .tail_size ;
1296
1342
1297
1343
if (* zc && (out_iov || out_sg )) {
1298
1344
if (out_iov )
@@ -1343,12 +1389,20 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1343
1389
kfree (mem );
1344
1390
return err ;
1345
1391
}
1346
- memcpy (iv , tls_ctx -> rx .iv , TLS_CIPHER_AES_GCM_128_SALT_SIZE );
1392
+ if (tls_ctx -> crypto_recv .info .version == TLS_1_3_VERSION )
1393
+ memcpy (iv , tls_ctx -> rx .iv , crypto_aead_ivsize (ctx -> aead_recv ));
1394
+ else
1395
+ memcpy (iv , tls_ctx -> rx .iv , TLS_CIPHER_AES_GCM_128_SALT_SIZE );
1396
+
1397
+ xor_iv_with_seq (tls_ctx -> crypto_recv .info .version , iv ,
1398
+ tls_ctx -> rx .rec_seq );
1347
1399
1348
1400
/* Prepare AAD */
1349
- tls_make_aad (aad , rxm -> full_len - tls_ctx -> rx .overhead_size ,
1401
+ tls_make_aad (aad , rxm -> full_len - tls_ctx -> rx .overhead_size +
1402
+ tls_ctx -> rx .tail_size ,
1350
1403
tls_ctx -> rx .rec_seq , tls_ctx -> rx .rec_seq_size ,
1351
- ctx -> control );
1404
+ ctx -> control ,
1405
+ tls_ctx -> crypto_recv .info .version );
1352
1406
1353
1407
/* Prepare sgin */
1354
1408
sg_init_table (sgin , n_sgin );
@@ -1405,6 +1459,7 @@ static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
1405
1459
{
1406
1460
struct tls_context * tls_ctx = tls_get_ctx (sk );
1407
1461
struct tls_sw_context_rx * ctx = tls_sw_ctx_rx (tls_ctx );
1462
+ int version = tls_ctx -> crypto_recv .info .version ;
1408
1463
struct strp_msg * rxm = strp_msg (skb );
1409
1464
int err = 0 ;
1410
1465
@@ -1417,13 +1472,17 @@ static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
1417
1472
err = decrypt_internal (sk , skb , dest , NULL , chunk , zc , async );
1418
1473
if (err < 0 ) {
1419
1474
if (err == - EINPROGRESS )
1420
- tls_advance_record_sn (sk , & tls_ctx -> rx );
1475
+ tls_advance_record_sn (sk , & tls_ctx -> rx ,
1476
+ version );
1421
1477
1422
1478
return err ;
1423
1479
}
1480
+
1481
+ rxm -> full_len -= padding_length (ctx , tls_ctx , skb );
1482
+
1424
1483
rxm -> offset += tls_ctx -> rx .prepend_size ;
1425
1484
rxm -> full_len -= tls_ctx -> rx .overhead_size ;
1426
- tls_advance_record_sn (sk , & tls_ctx -> rx );
1485
+ tls_advance_record_sn (sk , & tls_ctx -> rx , version );
1427
1486
ctx -> decrypted = true;
1428
1487
ctx -> saved_data_ready (sk );
1429
1488
} else {
@@ -1611,7 +1670,8 @@ int tls_sw_recvmsg(struct sock *sk,
1611
1670
to_decrypt = rxm -> full_len - tls_ctx -> rx .overhead_size ;
1612
1671
1613
1672
if (to_decrypt <= len && !is_kvec && !is_peek &&
1614
- ctx -> control == TLS_RECORD_TYPE_DATA )
1673
+ ctx -> control == TLS_RECORD_TYPE_DATA &&
1674
+ tls_ctx -> crypto_recv .info .version != TLS_1_3_VERSION )
1615
1675
zc = true;
1616
1676
1617
1677
err = decrypt_skb_update (sk , skb , & msg -> msg_iter ,
@@ -1835,9 +1895,12 @@ static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1835
1895
1836
1896
data_len = ((header [4 ] & 0xFF ) | (header [3 ] << 8 ));
1837
1897
1838
- cipher_overhead = tls_ctx -> rx .tag_size + tls_ctx -> rx .iv_size ;
1898
+ cipher_overhead = tls_ctx -> rx .tag_size ;
1899
+ if (tls_ctx -> crypto_recv .info .version != TLS_1_3_VERSION )
1900
+ cipher_overhead += tls_ctx -> rx .iv_size ;
1839
1901
1840
- if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead ) {
1902
+ if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
1903
+ tls_ctx -> rx .tail_size ) {
1841
1904
ret = - EMSGSIZE ;
1842
1905
goto read_failure ;
1843
1906
}
@@ -1846,12 +1909,12 @@ static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1846
1909
goto read_failure ;
1847
1910
}
1848
1911
1849
- if (header [1 ] != TLS_VERSION_MINOR (tls_ctx -> crypto_recv .info .version ) ||
1850
- header [2 ] != TLS_VERSION_MAJOR (tls_ctx -> crypto_recv .info .version )) {
1912
+ /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
1913
+ if (header [1 ] != TLS_1_2_VERSION_MINOR ||
1914
+ header [2 ] != TLS_1_2_VERSION_MAJOR ) {
1851
1915
ret = - EINVAL ;
1852
1916
goto read_failure ;
1853
1917
}
1854
-
1855
1918
#ifdef CONFIG_TLS_DEVICE
1856
1919
handle_device_resync (strp -> sk , TCP_SKB_CB (skb )-> seq + rxm -> offset ,
1857
1920
* (u64 * )tls_ctx -> rx .rec_seq );
@@ -2100,10 +2163,19 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2100
2163
goto free_priv ;
2101
2164
}
2102
2165
2103
- cctx -> aad_size = TLS_AAD_SPACE_SIZE ;
2166
+ if (crypto_info -> version == TLS_1_3_VERSION ) {
2167
+ nonce_size = 0 ;
2168
+ cctx -> aad_size = TLS_HEADER_SIZE ;
2169
+ cctx -> tail_size = 1 ;
2170
+ } else {
2171
+ cctx -> aad_size = TLS_AAD_SPACE_SIZE ;
2172
+ cctx -> tail_size = 0 ;
2173
+ }
2174
+
2104
2175
cctx -> prepend_size = TLS_HEADER_SIZE + nonce_size ;
2105
2176
cctx -> tag_size = tag_size ;
2106
- cctx -> overhead_size = cctx -> prepend_size + cctx -> tag_size ;
2177
+ cctx -> overhead_size = cctx -> prepend_size + cctx -> tag_size +
2178
+ cctx -> tail_size ;
2107
2179
cctx -> iv_size = iv_size ;
2108
2180
cctx -> iv = kmalloc (iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE ,
2109
2181
GFP_KERNEL );
0 commit comments