Skip to content

Commit 4367d76

Browse files
committed
Merge branch 'tls-expand-tls_cipher_size_desc-to-simplify-getsockopt-setsockopt'
Sabrina Dubroca says: ==================== tls: expand tls_cipher_size_desc to simplify getsockopt/setsockopt Commit 2d2c5ea ("net/tls: Describe ciphers sizes by const structs") introduced tls_cipher_size_desc to describe the size of the fields of the per-cipher crypto_info structs, and commit ea7a9d8 ("net/tls: Use cipher sizes structs") used it, but only in tls_device.c and tls_device_fallback.c, and skipped converting similar code in tls_main.c and tls_sw.c. This series expands tls_cipher_size_desc (renamed to tls_cipher_desc to better fit this expansion) to fully describe a cipher: - offset of the fields within the per-cipher crypto_info - size of the full struct (for copies to/from userspace) - offload flag - algorithm name used by SW crypto With these additions, we can remove ~350L of switch (crypto_info->cipher_type) { ... } from tls_set_device_offload, tls_sw_fallback_init, do_tls_getsockopt_conf, do_tls_setsockopt_conf, tls_set_sw_offload (mainly do_tls_getsockopt_conf and tls_set_sw_offload). This series also adds the ARIA ciphers to the tls selftests, and some more getsockopt/setsockopt tests to cover more of the code changed by this series. ==================== Link: https://lore.kernel.org/r/cover.1692977948.git.sd@queasysnail.net Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 5447b08 + f3e444e commit 4367d76

8 files changed

Lines changed: 278 additions & 435 deletions

File tree

include/net/tls.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@
5151

5252
struct tls_rec;
5353

54-
struct tls_cipher_size_desc {
55-
unsigned int iv;
56-
unsigned int key;
57-
unsigned int salt;
58-
unsigned int tag;
59-
unsigned int rec_seq;
60-
};
61-
62-
extern const struct tls_cipher_size_desc tls_cipher_size_desc[];
63-
6454
/* Maximum data size carried in a TLS record */
6555
#define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14)
6656

net/tls/tls.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,59 @@
5151
#define TLS_DEC_STATS(net, field) \
5252
SNMP_DEC_STATS((net)->mib.tls_statistics, field)
5353

54+
struct tls_cipher_desc {
55+
unsigned int nonce;
56+
unsigned int iv;
57+
unsigned int key;
58+
unsigned int salt;
59+
unsigned int tag;
60+
unsigned int rec_seq;
61+
unsigned int iv_offset;
62+
unsigned int key_offset;
63+
unsigned int salt_offset;
64+
unsigned int rec_seq_offset;
65+
char *cipher_name;
66+
bool offloadable;
67+
size_t crypto_info;
68+
};
69+
70+
#define TLS_CIPHER_MIN TLS_CIPHER_AES_GCM_128
71+
#define TLS_CIPHER_MAX TLS_CIPHER_ARIA_GCM_256
72+
extern const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MIN];
73+
74+
static inline const struct tls_cipher_desc *get_cipher_desc(u16 cipher_type)
75+
{
76+
if (cipher_type < TLS_CIPHER_MIN || cipher_type > TLS_CIPHER_MAX)
77+
return NULL;
78+
79+
return &tls_cipher_desc[cipher_type - TLS_CIPHER_MIN];
80+
}
81+
82+
static inline char *crypto_info_iv(struct tls_crypto_info *crypto_info,
83+
const struct tls_cipher_desc *cipher_desc)
84+
{
85+
return (char *)crypto_info + cipher_desc->iv_offset;
86+
}
87+
88+
static inline char *crypto_info_key(struct tls_crypto_info *crypto_info,
89+
const struct tls_cipher_desc *cipher_desc)
90+
{
91+
return (char *)crypto_info + cipher_desc->key_offset;
92+
}
93+
94+
static inline char *crypto_info_salt(struct tls_crypto_info *crypto_info,
95+
const struct tls_cipher_desc *cipher_desc)
96+
{
97+
return (char *)crypto_info + cipher_desc->salt_offset;
98+
}
99+
100+
static inline char *crypto_info_rec_seq(struct tls_crypto_info *crypto_info,
101+
const struct tls_cipher_desc *cipher_desc)
102+
{
103+
return (char *)crypto_info + cipher_desc->rec_seq_offset;
104+
}
105+
106+
54107
/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
55108
* allocated or mapped for each TLS record. After encryption, the records are
56109
* stores in a linked list.

net/tls/tls_device.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ static int
884884
tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)
885885
{
886886
struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
887-
const struct tls_cipher_size_desc *cipher_sz;
887+
const struct tls_cipher_desc *cipher_desc;
888888
int err, offset, copy, data_len, pos;
889889
struct sk_buff *skb, *skb_iter;
890890
struct scatterlist sg[1];
@@ -898,10 +898,10 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)
898898
default:
899899
return -EINVAL;
900900
}
901-
cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_recv.info.cipher_type];
901+
cipher_desc = get_cipher_desc(tls_ctx->crypto_recv.info.cipher_type);
902902

903903
rxm = strp_msg(tls_strp_msg(sw_ctx));
904-
orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv,
904+
orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_desc->iv,
905905
sk->sk_allocation);
906906
if (!orig_buf)
907907
return -ENOMEM;
@@ -917,8 +917,8 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)
917917

918918
sg_init_table(sg, 1);
919919
sg_set_buf(&sg[0], buf,
920-
rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv);
921-
err = skb_copy_bits(skb, offset, buf, TLS_HEADER_SIZE + cipher_sz->iv);
920+
rxm->full_len + TLS_HEADER_SIZE + cipher_desc->iv);
921+
err = skb_copy_bits(skb, offset, buf, TLS_HEADER_SIZE + cipher_desc->iv);
922922
if (err)
923923
goto free_buf;
924924

@@ -929,7 +929,7 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)
929929
else
930930
err = 0;
931931

932-
data_len = rxm->full_len - cipher_sz->tag;
932+
data_len = rxm->full_len - cipher_desc->tag;
933933

934934
if (skb_pagelen(skb) > offset) {
935935
copy = min_t(int, skb_pagelen(skb) - offset, data_len);
@@ -1046,7 +1046,7 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
10461046
{
10471047
struct tls_context *tls_ctx = tls_get_ctx(sk);
10481048
struct tls_prot_info *prot = &tls_ctx->prot_info;
1049-
const struct tls_cipher_size_desc *cipher_sz;
1049+
const struct tls_cipher_desc *cipher_desc;
10501050
struct tls_record_info *start_marker_record;
10511051
struct tls_offload_context_tx *offload_ctx;
10521052
struct tls_crypto_info *crypto_info;
@@ -1079,46 +1079,32 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
10791079
goto release_netdev;
10801080
}
10811081

1082-
switch (crypto_info->cipher_type) {
1083-
case TLS_CIPHER_AES_GCM_128:
1084-
iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1085-
rec_seq =
1086-
((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1087-
break;
1088-
case TLS_CIPHER_AES_GCM_256:
1089-
iv = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->iv;
1090-
rec_seq =
1091-
((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->rec_seq;
1092-
break;
1093-
default:
1082+
cipher_desc = get_cipher_desc(crypto_info->cipher_type);
1083+
if (!cipher_desc || !cipher_desc->offloadable) {
10941084
rc = -EINVAL;
10951085
goto release_netdev;
10961086
}
1097-
cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type];
10981087

1099-
/* Sanity-check the rec_seq_size for stack allocations */
1100-
if (cipher_sz->rec_seq > TLS_MAX_REC_SEQ_SIZE) {
1101-
rc = -EINVAL;
1102-
goto release_netdev;
1103-
}
1088+
iv = crypto_info_iv(crypto_info, cipher_desc);
1089+
rec_seq = crypto_info_rec_seq(crypto_info, cipher_desc);
11041090

11051091
prot->version = crypto_info->version;
11061092
prot->cipher_type = crypto_info->cipher_type;
1107-
prot->prepend_size = TLS_HEADER_SIZE + cipher_sz->iv;
1108-
prot->tag_size = cipher_sz->tag;
1093+
prot->prepend_size = TLS_HEADER_SIZE + cipher_desc->iv;
1094+
prot->tag_size = cipher_desc->tag;
11091095
prot->overhead_size = prot->prepend_size + prot->tag_size;
1110-
prot->iv_size = cipher_sz->iv;
1111-
prot->salt_size = cipher_sz->salt;
1112-
ctx->tx.iv = kmalloc(cipher_sz->iv + cipher_sz->salt, GFP_KERNEL);
1096+
prot->iv_size = cipher_desc->iv;
1097+
prot->salt_size = cipher_desc->salt;
1098+
ctx->tx.iv = kmalloc(cipher_desc->iv + cipher_desc->salt, GFP_KERNEL);
11131099
if (!ctx->tx.iv) {
11141100
rc = -ENOMEM;
11151101
goto release_netdev;
11161102
}
11171103

1118-
memcpy(ctx->tx.iv + cipher_sz->salt, iv, cipher_sz->iv);
1104+
memcpy(ctx->tx.iv + cipher_desc->salt, iv, cipher_desc->iv);
11191105

1120-
prot->rec_seq_size = cipher_sz->rec_seq;
1121-
ctx->tx.rec_seq = kmemdup(rec_seq, cipher_sz->rec_seq, GFP_KERNEL);
1106+
prot->rec_seq_size = cipher_desc->rec_seq;
1107+
ctx->tx.rec_seq = kmemdup(rec_seq, cipher_desc->rec_seq, GFP_KERNEL);
11221108
if (!ctx->tx.rec_seq) {
11231109
rc = -ENOMEM;
11241110
goto free_iv;

net/tls/tls_device_fallback.c

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static int tls_enc_record(struct aead_request *aead_req,
5555
struct tls_prot_info *prot)
5656
{
5757
unsigned char buf[TLS_HEADER_SIZE + MAX_IV_SIZE];
58-
const struct tls_cipher_size_desc *cipher_sz;
58+
const struct tls_cipher_desc *cipher_desc;
5959
struct scatterlist sg_in[3];
6060
struct scatterlist sg_out[3];
6161
unsigned int buf_size;
@@ -69,9 +69,9 @@ static int tls_enc_record(struct aead_request *aead_req,
6969
default:
7070
return -EINVAL;
7171
}
72-
cipher_sz = &tls_cipher_size_desc[prot->cipher_type];
72+
cipher_desc = get_cipher_desc(prot->cipher_type);
7373

74-
buf_size = TLS_HEADER_SIZE + cipher_sz->iv;
74+
buf_size = TLS_HEADER_SIZE + cipher_desc->iv;
7575
len = min_t(int, *in_len, buf_size);
7676

7777
scatterwalk_copychunks(buf, in, len, 0);
@@ -85,11 +85,11 @@ static int tls_enc_record(struct aead_request *aead_req,
8585
scatterwalk_pagedone(out, 1, 1);
8686

8787
len = buf[4] | (buf[3] << 8);
88-
len -= cipher_sz->iv;
88+
len -= cipher_desc->iv;
8989

90-
tls_make_aad(aad, len - cipher_sz->tag, (char *)&rcd_sn, buf[0], prot);
90+
tls_make_aad(aad, len - cipher_desc->tag, (char *)&rcd_sn, buf[0], prot);
9191

92-
memcpy(iv + cipher_sz->salt, buf + TLS_HEADER_SIZE, cipher_sz->iv);
92+
memcpy(iv + cipher_desc->salt, buf + TLS_HEADER_SIZE, cipher_desc->iv);
9393

9494
sg_init_table(sg_in, ARRAY_SIZE(sg_in));
9595
sg_init_table(sg_out, ARRAY_SIZE(sg_out));
@@ -100,7 +100,7 @@ static int tls_enc_record(struct aead_request *aead_req,
100100

101101
*in_len -= len;
102102
if (*in_len < 0) {
103-
*in_len += cipher_sz->tag;
103+
*in_len += cipher_desc->tag;
104104
/* the input buffer doesn't contain the entire record.
105105
* trim len accordingly. The resulting authentication tag
106106
* will contain garbage, but we don't care, so we won't
@@ -121,7 +121,7 @@ static int tls_enc_record(struct aead_request *aead_req,
121121
scatterwalk_pagedone(out, 1, 1);
122122
}
123123

124-
len -= cipher_sz->tag;
124+
len -= cipher_desc->tag;
125125
aead_request_set_crypt(aead_req, sg_in, sg_out, len, iv);
126126

127127
rc = crypto_aead_encrypt(aead_req);
@@ -309,14 +309,14 @@ static void fill_sg_out(struct scatterlist sg_out[3], void *buf,
309309
int sync_size,
310310
void *dummy_buf)
311311
{
312-
const struct tls_cipher_size_desc *cipher_sz =
313-
&tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type];
312+
const struct tls_cipher_desc *cipher_desc =
313+
get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
314314

315315
sg_set_buf(&sg_out[0], dummy_buf, sync_size);
316316
sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len);
317317
/* Add room for authentication tag produced by crypto */
318318
dummy_buf += sync_size;
319-
sg_set_buf(&sg_out[2], dummy_buf, cipher_sz->tag);
319+
sg_set_buf(&sg_out[2], dummy_buf, cipher_desc->tag);
320320
}
321321

322322
static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
@@ -328,7 +328,7 @@ static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
328328
struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
329329
int tcp_payload_offset = skb_tcp_all_headers(skb);
330330
int payload_len = skb->len - tcp_payload_offset;
331-
const struct tls_cipher_size_desc *cipher_sz;
331+
const struct tls_cipher_desc *cipher_desc;
332332
void *buf, *iv, *aad, *dummy_buf, *salt;
333333
struct aead_request *aead_req;
334334
struct sk_buff *nskb = NULL;
@@ -348,16 +348,16 @@ static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
348348
default:
349349
goto free_req;
350350
}
351-
cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type];
352-
buf_len = cipher_sz->salt + cipher_sz->iv + TLS_AAD_SPACE_SIZE +
353-
sync_size + cipher_sz->tag;
351+
cipher_desc = get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
352+
buf_len = cipher_desc->salt + cipher_desc->iv + TLS_AAD_SPACE_SIZE +
353+
sync_size + cipher_desc->tag;
354354
buf = kmalloc(buf_len, GFP_ATOMIC);
355355
if (!buf)
356356
goto free_req;
357357

358358
iv = buf;
359-
memcpy(iv, salt, cipher_sz->salt);
360-
aad = buf + cipher_sz->salt + cipher_sz->iv;
359+
memcpy(iv, salt, cipher_desc->salt);
360+
aad = buf + cipher_desc->salt + cipher_desc->iv;
361361
dummy_buf = aad + TLS_AAD_SPACE_SIZE;
362362

363363
nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC);
@@ -471,37 +471,29 @@ int tls_sw_fallback_init(struct sock *sk,
471471
struct tls_offload_context_tx *offload_ctx,
472472
struct tls_crypto_info *crypto_info)
473473
{
474-
const struct tls_cipher_size_desc *cipher_sz;
475-
const u8 *key;
474+
const struct tls_cipher_desc *cipher_desc;
476475
int rc;
477476

477+
cipher_desc = get_cipher_desc(crypto_info->cipher_type);
478+
if (!cipher_desc || !cipher_desc->offloadable)
479+
return -EINVAL;
480+
478481
offload_ctx->aead_send =
479-
crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
482+
crypto_alloc_aead(cipher_desc->cipher_name, 0, CRYPTO_ALG_ASYNC);
480483
if (IS_ERR(offload_ctx->aead_send)) {
481484
rc = PTR_ERR(offload_ctx->aead_send);
482485
pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc);
483486
offload_ctx->aead_send = NULL;
484487
goto err_out;
485488
}
486489

487-
switch (crypto_info->cipher_type) {
488-
case TLS_CIPHER_AES_GCM_128:
489-
key = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->key;
490-
break;
491-
case TLS_CIPHER_AES_GCM_256:
492-
key = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->key;
493-
break;
494-
default:
495-
rc = -EINVAL;
496-
goto free_aead;
497-
}
498-
cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type];
499-
500-
rc = crypto_aead_setkey(offload_ctx->aead_send, key, cipher_sz->key);
490+
rc = crypto_aead_setkey(offload_ctx->aead_send,
491+
crypto_info_key(crypto_info, cipher_desc),
492+
cipher_desc->key);
501493
if (rc)
502494
goto free_aead;
503495

504-
rc = crypto_aead_setauthsize(offload_ctx->aead_send, cipher_sz->tag);
496+
rc = crypto_aead_setauthsize(offload_ctx->aead_send, cipher_desc->tag);
505497
if (rc)
506498
goto free_aead;
507499

0 commit comments

Comments
 (0)