Skip to content

Commit 6942a28

Browse files
vvfedorenkokuba-moo
authored andcommitted
net/tls: make inline helpers protocol-aware
Inline functions defined in tls.h have a lot of AES-specific constants. Remove these constants and change argument to struct tls_prot_info to have an access to cipher type in later patches Signed-off-by: Vadim Fedorenko <vfedorenko@novek.ru> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 594e31b commit 6942a28

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

include/net/tls.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -502,31 +502,30 @@ static inline void tls_advance_record_sn(struct sock *sk,
502502
tls_err_abort(sk, EBADMSG);
503503

504504
if (prot->version != TLS_1_3_VERSION)
505-
tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
505+
tls_bigint_increment(ctx->iv + prot->salt_size,
506506
prot->iv_size);
507507
}
508508

509509
static inline void tls_fill_prepend(struct tls_context *ctx,
510510
char *buf,
511511
size_t plaintext_len,
512-
unsigned char record_type,
513-
int version)
512+
unsigned char record_type)
514513
{
515514
struct tls_prot_info *prot = &ctx->prot_info;
516515
size_t pkt_len, iv_size = prot->iv_size;
517516

518517
pkt_len = plaintext_len + prot->tag_size;
519-
if (version != TLS_1_3_VERSION) {
518+
if (prot->version != TLS_1_3_VERSION) {
520519
pkt_len += iv_size;
521520

522521
memcpy(buf + TLS_NONCE_OFFSET,
523-
ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
522+
ctx->tx.iv + prot->salt_size, iv_size);
524523
}
525524

526525
/* we cover nonce explicit here as well, so buf should be of
527526
* size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
528527
*/
529-
buf[0] = version == TLS_1_3_VERSION ?
528+
buf[0] = prot->version == TLS_1_3_VERSION ?
530529
TLS_RECORD_TYPE_DATA : record_type;
531530
/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
532531
buf[1] = TLS_1_2_VERSION_MINOR;
@@ -539,30 +538,29 @@ static inline void tls_fill_prepend(struct tls_context *ctx,
539538
static inline void tls_make_aad(char *buf,
540539
size_t size,
541540
char *record_sequence,
542-
int record_sequence_size,
543541
unsigned char record_type,
544-
int version)
542+
struct tls_prot_info *prot)
545543
{
546-
if (version != TLS_1_3_VERSION) {
547-
memcpy(buf, record_sequence, record_sequence_size);
544+
if (prot->version != TLS_1_3_VERSION) {
545+
memcpy(buf, record_sequence, prot->rec_seq_size);
548546
buf += 8;
549547
} else {
550-
size += TLS_CIPHER_AES_GCM_128_TAG_SIZE;
548+
size += prot->tag_size;
551549
}
552550

553-
buf[0] = version == TLS_1_3_VERSION ?
551+
buf[0] = prot->version == TLS_1_3_VERSION ?
554552
TLS_RECORD_TYPE_DATA : record_type;
555553
buf[1] = TLS_1_2_VERSION_MAJOR;
556554
buf[2] = TLS_1_2_VERSION_MINOR;
557555
buf[3] = size >> 8;
558556
buf[4] = size & 0xFF;
559557
}
560558

561-
static inline void xor_iv_with_seq(int version, char *iv, char *seq)
559+
static inline void xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq)
562560
{
563561
int i;
564562

565-
if (version == TLS_1_3_VERSION) {
563+
if (prot->version == TLS_1_3_VERSION) {
566564
for (i = 0; i < 8; i++)
567565
iv[i + 4] ^= seq[i];
568566
}

net/tls/tls_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static int tls_device_record_close(struct sock *sk,
327327
/* fill prepend */
328328
tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]),
329329
record->len - prot->overhead_size,
330-
record_type, prot->version);
330+
record_type);
331331
return ret;
332332
}
333333

net/tls/tls_device_fallback.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ static int tls_enc_record(struct aead_request *aead_req,
4949
struct crypto_aead *aead, char *aad,
5050
char *iv, __be64 rcd_sn,
5151
struct scatter_walk *in,
52-
struct scatter_walk *out, int *in_len)
52+
struct scatter_walk *out, int *in_len,
53+
struct tls_prot_info *prot)
5354
{
5455
unsigned char buf[TLS_HEADER_SIZE + TLS_CIPHER_AES_GCM_128_IV_SIZE];
5556
struct scatterlist sg_in[3];
@@ -73,8 +74,7 @@ static int tls_enc_record(struct aead_request *aead_req,
7374
len -= TLS_CIPHER_AES_GCM_128_IV_SIZE;
7475

7576
tls_make_aad(aad, len - TLS_CIPHER_AES_GCM_128_TAG_SIZE,
76-
(char *)&rcd_sn, sizeof(rcd_sn), buf[0],
77-
TLS_1_2_VERSION);
77+
(char *)&rcd_sn, buf[0], prot);
7878

7979
memcpy(iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, buf + TLS_HEADER_SIZE,
8080
TLS_CIPHER_AES_GCM_128_IV_SIZE);
@@ -140,7 +140,7 @@ static struct aead_request *tls_alloc_aead_request(struct crypto_aead *aead,
140140
static int tls_enc_records(struct aead_request *aead_req,
141141
struct crypto_aead *aead, struct scatterlist *sg_in,
142142
struct scatterlist *sg_out, char *aad, char *iv,
143-
u64 rcd_sn, int len)
143+
u64 rcd_sn, int len, struct tls_prot_info *prot)
144144
{
145145
struct scatter_walk out, in;
146146
int rc;
@@ -150,7 +150,7 @@ static int tls_enc_records(struct aead_request *aead_req,
150150

151151
do {
152152
rc = tls_enc_record(aead_req, aead, aad, iv,
153-
cpu_to_be64(rcd_sn), &in, &out, &len);
153+
cpu_to_be64(rcd_sn), &in, &out, &len, prot);
154154
rcd_sn++;
155155

156156
} while (rc == 0 && len);
@@ -348,7 +348,8 @@ static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
348348
payload_len, sync_size, dummy_buf);
349349

350350
if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv,
351-
rcd_sn, sync_size + payload_len) < 0)
351+
rcd_sn, sync_size + payload_len,
352+
&tls_ctx->prot_info) < 0)
352353
goto free_nskb;
353354

354355
complete_skb(nskb, skb, tcp_payload_offset);

net/tls/tls_sw.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ static int tls_do_encryption(struct sock *sk,
505505
memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
506506
prot->iv_size + prot->salt_size);
507507

508-
xor_iv_with_seq(prot->version, rec->iv_data, tls_ctx->tx.rec_seq);
508+
xor_iv_with_seq(prot, rec->iv_data, tls_ctx->tx.rec_seq);
509509

510510
sge->offset += prot->prepend_size;
511511
sge->length -= prot->prepend_size;
@@ -748,14 +748,13 @@ static int tls_push_record(struct sock *sk, int flags,
748748
sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
749749

750750
tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size,
751-
tls_ctx->tx.rec_seq, prot->rec_seq_size,
752-
record_type, prot->version);
751+
tls_ctx->tx.rec_seq, record_type, prot);
753752

754753
tls_fill_prepend(tls_ctx,
755754
page_address(sg_page(&msg_en->sg.data[i])) +
756755
msg_en->sg.data[i].offset,
757756
msg_pl->sg.size + prot->tail_size,
758-
record_type, prot->version);
757+
record_type);
759758

760759
tls_ctx->pending_open_record_frags = false;
761760

@@ -1471,13 +1470,12 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
14711470
else
14721471
memcpy(iv + iv_offset, tls_ctx->rx.iv, prot->salt_size);
14731472

1474-
xor_iv_with_seq(prot->version, iv, tls_ctx->rx.rec_seq);
1473+
xor_iv_with_seq(prot, iv, tls_ctx->rx.rec_seq);
14751474

14761475
/* Prepare AAD */
14771476
tls_make_aad(aad, rxm->full_len - prot->overhead_size +
14781477
prot->tail_size,
1479-
tls_ctx->rx.rec_seq, prot->rec_seq_size,
1480-
ctx->control, prot->version);
1478+
tls_ctx->rx.rec_seq, ctx->control, prot);
14811479

14821480
/* Prepare sgin */
14831481
sg_init_table(sgin, n_sgin);

0 commit comments

Comments
 (0)