tls: expand tls_cipher_size_desc to simplify getsockopt/setsockopt
Subsystems (4)
Sabrina Dubroca
maintainercover tls: expand tls_cipher_size_desc to simplify getsockopt/setsockopt
Commit 2d2c5ea24243 ("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 ea7a9d88ba21 ("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.
Sabrina Dubroca (17):
selftests: tls: add test variants for aria-gcm
selftests: tls: add getsockopt test
selftests: tls: test some invalid inputs for setsockopt
tls: move tls_cipher_size_desc to net/tls/tls.h
tls: add TLS_CIPHER_ARIA_GCM_* to tls_cipher_size_desc
tls: reduce size of tls_cipher_size_desc
tls: rename tls_cipher_size_desc to tls_cipher_desc
tls: extend tls_cipher_desc to fully describe the ciphers
tls: validate cipher descriptions at compile time
tls: expand use of tls_cipher_desc in tls_set_device_offload
tls: allocate the fallback aead after checking that the cipher is
valid
tls: expand use of tls_cipher_desc in tls_sw_fallback_init
tls: get crypto_info size from tls_cipher_desc in
do_tls_setsockopt_conf
tls: use tls_cipher_desc to simplify do_tls_getsockopt_conf
tls: use tls_cipher_desc to get per-cipher sizes in tls_set_sw_offload
tls: use tls_cipher_desc to access per-cipher crypto_info in
tls_set_sw_offload
tls: get cipher_name from cipher_desc in tls_set_sw_offload
include/net/tls.h | 10 -- net/tls/tls.h | 53 ++++++ net/tls/tls_device.c | 52 ++---- net/tls/tls_device_fallback.c | 62 +++---- net/tls/tls_main.c | 272 ++++++++--------------------- net/tls/tls_sw.c | 179 +++---------------- tools/testing/selftests/net/config | 1 + tools/testing/selftests/net/tls.c | 84 +++++++++ 8 files changed, 278 insertions(+), 435 deletions(-)
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 1/17 selftests: tls: add test variants for aria-gcm
Only supported for TLS1.2.
--- tools/testing/selftests/net/config | 1 + tools/testing/selftests/net/tls.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
tools/testing/selftests/net/config +1 -0
@@ -51,3 +51,4 @@ CONFIG_AMT=m CONFIG_VXLAN=m CONFIG_IP_SCTP=m CONFIG_NETFILTER_XT_MATCH_POLICY=m +CONFIG_CRYPTO_ARIA=y
tools/testing/selftests/net/tls.c +24 -0
@@ -36,6 +36,8 @@ struct tls_crypto_info_keys {struct tls12_crypto_info_sm4_ccm sm4ccm;struct tls12_crypto_info_aes_ccm_128 aesccm128;struct tls12_crypto_info_aes_gcm_256 aesgcm256; +struct tls12_crypto_info_aria_gcm_128 ariagcm128; +struct tls12_crypto_info_aria_gcm_256 ariagcm256; };size_t len; };
@@ -76,6 +78,16 @@ static void tls_crypto_info_init(uint16_t tls_version, uint16_t cipher_type, tls12->aesgcm256.info.version = tls_version; tls12->aesgcm256.info.cipher_type = cipher_type;break ; +case TLS_CIPHER_ARIA_GCM_128 : + tls12->len =sizeof (struct tls12_crypto_info_aria_gcm_128 ); + tls12->ariagcm128.info.version = tls_version; + tls12->ariagcm128.info.cipher_type = cipher_type; +break ; +case TLS_CIPHER_ARIA_GCM_256 : + tls12->len =sizeof (struct tls12_crypto_info_aria_gcm_256 ); + tls12->ariagcm256.info.version = tls_version; + tls12->ariagcm256.info.cipher_type = cipher_type; +break ;default :break ; }
@@ -312,6 +324,18 @@ FIXTURE_VARIANT_ADD(tls, 13_nopad) .nopad = true, }; +FIXTURE_VARIANT_ADD(tls, 12_aria_gcm) +{ + .tls_version = TLS_1_2_VERSION, + .cipher_type =TLS_CIPHER_ARIA_GCM_128 , +}; + +FIXTURE_VARIANT_ADD(tls, 12_aria_gcm_256) +{ + .tls_version =TLS_1_2_VERSION , + .cipher_type = TLS_CIPHER_ARIA_GCM_256, +}; +FIXTURE_SETUP (tls) {struct tls_crypto_info_keys tls12;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 2/17 selftests: tls: add getsockopt test
In reply to Sabrina DubrocaThe kernel accepts fetching either just the version and cipher type, or exactly the per-cipher struct. Also check that getsockopt returns what we just passed to the kernel.
--- tools/testing/selftests/net/tls.c | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
tools/testing/selftests/net/tls.c +35 -0
@@ -30,6 +30,7 @@ static int fips_enabled;struct tls_crypto_info_keys {union { +struct tls_crypto_info crypto_info;struct tls12_crypto_info_aes_gcm_128 aes128;struct tls12_crypto_info_chacha20_poly1305 chacha20;struct tls12_crypto_info_sm4_gcm sm4gcm;
@@ -1496,6 +1497,40 @@ TEST_F(tls, shutdown_reuse)EXPECT_EQ (errno,EISCONN ); } +TEST_F(tls, getsockopt) +{ +struct tls_crypto_info_keys expect, get; +socklen_t len; + +/* get only the version/cipher */ + len =sizeof (struct tls_crypto_info ); +memrnd (&get,sizeof (get)); +EXPECT_EQ (getsockopt (self->fd,SOL_TLS ,TLS_TX , &get, &len),0 ); +EXPECT_EQ (len,sizeof (struct tls_crypto_info )); +EXPECT_EQ (get.crypto_info.version, variant->tls_version); +EXPECT_EQ (get.crypto_info.cipher_type, variant->cipher_type); + +/* get the full crypto_info */ +tls_crypto_info_init (variant->tls_version, variant->cipher_type, &expect); + len = expect.len; +memrnd (&get,sizeof (get)); +EXPECT_EQ (getsockopt (self->fd,SOL_TLS ,TLS_TX , &get, &len),0 ); +EXPECT_EQ (len, expect.len); +EXPECT_EQ (get.crypto_info.version, variant->tls_version); +EXPECT_EQ (get.crypto_info.cipher_type, variant->cipher_type); +EXPECT_EQ (memcmp (&get, &expect, expect.len),0 ); + +/* short get should fail */ + len =sizeof (struct tls_crypto_info ) -1 ; +EXPECT_EQ (getsockopt (self->fd,SOL_TLS ,TLS_TX , &get, &len),-1 ); +EXPECT_EQ (errno,EINVAL ); + +/* partial get of the cipher data should fail */ + len = expect.len -1 ; +EXPECT_EQ (getsockopt (self->fd,SOL_TLS ,TLS_TX , &get, &len),-1 ); +EXPECT_EQ (errno,EINVAL ); +} +FIXTURE (tls_err) {int fd, cfd;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 3/17 selftests: tls: test some invalid inputs for setsockopt
In reply to Sabrina DubrocaThis test will need to be updated if new ciphers are added.
--- tools/testing/selftests/net/tls.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
tools/testing/selftests/net/tls.c +25 -0
@@ -241,6 +241,31 @@ TEST_F(tls_basic, base_base)EXPECT_EQ (memcmp (buf, test_str, send_len),0 ); }; +TEST_F(tls_basic, bad_cipher) +{ +struct tls_crypto_info_keys tls12; + + tls12.crypto_info.version =200 ; + tls12.crypto_info.cipher_type =TLS_CIPHER_AES_GCM_128 ; +EXPECT_EQ (setsockopt (self->fd,SOL_TLS ,TLS_TX , &tls12,sizeof (struct tls12_crypto_info_aes_gcm_128 )),-1 ); + + tls12.crypto_info.version =TLS_1_2_VERSION ; + tls12.crypto_info.cipher_type =50 ; +EXPECT_EQ (setsockopt (self->fd,SOL_TLS ,TLS_TX , &tls12,sizeof (struct tls12_crypto_info_aes_gcm_128 )),-1 ); + + tls12.crypto_info.version =TLS_1_2_VERSION ; + tls12.crypto_info.cipher_type =59 ; +EXPECT_EQ (setsockopt (self->fd,SOL_TLS ,TLS_TX , &tls12,sizeof (struct tls12_crypto_info_aes_gcm_128 )),-1 ); + + tls12.crypto_info.version =TLS_1_2_VERSION ; + tls12.crypto_info.cipher_type =10 ; +EXPECT_EQ (setsockopt (self->fd,SOL_TLS ,TLS_TX , &tls12,sizeof (struct tls12_crypto_info_aes_gcm_128 )),-1 ); + + tls12.crypto_info.version =TLS_1_2_VERSION ; + tls12.crypto_info.cipher_type =70 ; +EXPECT_EQ (setsockopt (self->fd,SOL_TLS ,TLS_TX , &tls12,sizeof (struct tls12_crypto_info_aes_gcm_128 )),-1 ); +} +FIXTURE (tls) {int fd, cfd;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 4/17 tls: move tls_cipher_size_desc to net/tls/tls.h
In reply to Sabrina DubrocaIt's only used in net/tls/*, no need to bloat include/net/tls.h.
--- include/net/tls.h | 10 ---------- net/tls/tls.h | 10 ++++++++++ 2 files changed, 10 insertions(+), 10 deletions(-)
include/net/tls.h +0 -10
@@ -51,16 +51,6 @@struct tls_rec ; -struct tls_cipher_size_desc { -unsigned int iv; -unsigned int key; -unsigned int salt; -unsigned int tag; -unsigned int rec_seq; -}; - -extern const struct tls_cipher_size_desc tls_cipher_size_desc[]; -/* Maximum data size carried in a TLS record */ #define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14)
net/tls/tls.h +10 -0
@@ -51,6 +51,16 @@#define TLS_DEC_STATS (net, field) \ SNMP_DEC_STATS((net)->mib.tls_statistics, field) +struct tls_cipher_size_desc { +unsigned int iv; +unsigned int key; +unsigned int salt; +unsigned int tag; +unsigned int rec_seq; +}; + +extern const struct tls_cipher_size_desc tls_cipher_size_desc[]; +/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages * allocated or mapped for each TLS record. After encryption, the records are * stores in a linked list.
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 5/17 tls: add TLS_CIPHER_ARIA_GCM_* to tls_cipher_size_desc
In reply to Sabrina Dubroca--- net/tls/tls_main.c | 2 ++ 1 file changed, 2 insertions(+)
net/tls/tls_main.c +2 -0
@@ -73,6 +73,8 @@ const struct tls_cipher_size_desc tls_cipher_size_desc[] = {CIPHER_SIZE_DESC (TLS_CIPHER_CHACHA20_POLY1305 ),CIPHER_SIZE_DESC (TLS_CIPHER_SM4_GCM ),CIPHER_SIZE_DESC (TLS_CIPHER_SM4_CCM ), +CIPHER_SIZE_DESC (TLS_CIPHER_ARIA_GCM_128 ), + CIPHER_SIZE_DESC(TLS_CIPHER_ARIA_GCM_256), };static const struct proto *saved_tcpv6_prot;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 6/17 tls: reduce size of tls_cipher_size_desc
In reply to Sabrina Dubrocatls_cipher_size_desc indexes ciphers by their type, but we're not using indices 0..50 of the array. Each struct tls_cipher_size_desc is 20B, so that's a lot of unused memory. We can reindex the array starting at the lowest used cipher_type.
Introduce the get_cipher_size_desc helper to find the right item and avoid out-of-bounds accesses, and make tls_cipher_size_desc's size explicit so that gcc reminds us to update TLS_CIPHER_MIN/MAX when we add a new cipher.
--- net/tls/tls.h | 13 ++++++++++++- net/tls/tls_device.c | 4 ++-- net/tls/tls_device_fallback.c | 8 ++++---- net/tls/tls_main.c | 4 ++-- 4 files changed, 20 insertions(+), 9 deletions(-)
net/tls/tls.h +12 -1
@@ -59,7 +59,18 @@ struct tls_cipher_size_desc {unsigned int rec_seq; }; -extern const struct tls_cipher_size_desc tls_cipher_size_desc[]; +#define TLS_CIPHER_MIN TLS_CIPHER_AES_GCM_128 +#define TLS_CIPHER_MAX TLS_CIPHER_ARIA_GCM_256 +extern const struct tls_cipher_size_desc tls_cipher_size_desc[TLS_CIPHER_MAX +1 -TLS_CIPHER_MIN ]; + +static inline const struct tls_cipher_size_desc *get_cipher_size_desc (u16 cipher_type) +{ +if (cipher_type <TLS_CIPHER_MIN || cipher_type >TLS_CIPHER_MAX ) +return NULL ; + +return &tls_cipher_size_desc[cipher_type -TLS_CIPHER_MIN ]; +} +/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages * allocated or mapped for each TLS record. After encryption, the records are
net/tls/tls_device.c +2 -2
@@ -898,7 +898,7 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)default :return -EINVAL ; } - cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_recv.info.cipher_type]; + cipher_sz =get_cipher_size_desc (tls_ctx->crypto_recv.info.cipher_type); rxm =strp_msg (tls_strp_msg (sw_ctx)); orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv,
@@ -1094,7 +1094,7 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) rc = -EINVAL ; goto release_netdev; } - cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type]; + cipher_sz =get_cipher_size_desc (crypto_info->cipher_type);/* Sanity-check the rec_seq_size for stack allocations */ if (cipher_sz->rec_seq >TLS_MAX_REC_SEQ_SIZE ) {
net/tls/tls_device_fallback.c +4 -4
@@ -69,7 +69,7 @@ static int tls_enc_record(struct aead_request *aead_req,default :return -EINVAL ; } - cipher_sz = &tls_cipher_size_desc[prot->cipher_type]; + cipher_sz =get_cipher_size_desc (prot->cipher_type); buf_size =TLS_HEADER_SIZE + cipher_sz->iv; len =min_t (int, *in_len, buf_size);
@@ -310,7 +310,7 @@ static void fill_sg_out(struct scatterlist sg_out[3], void *buf, void *dummy_buf) {const struct tls_cipher_size_desc *cipher_sz = - &tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type]; +get_cipher_size_desc (tls_ctx->crypto_send.info.cipher_type);sg_set_buf (&sg_out[0 ], dummy_buf, sync_size);sg_set_buf (&sg_out[1 ], nskb->data + tcp_payload_offset, payload_len);
@@ -348,7 +348,7 @@ static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,default : goto free_req; } - cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type]; + cipher_sz =get_cipher_size_desc (tls_ctx->crypto_send.info.cipher_type); buf_len = cipher_sz->salt + cipher_sz->iv +TLS_AAD_SPACE_SIZE + sync_size + cipher_sz->tag; buf =kmalloc (buf_len,GFP_ATOMIC );
@@ -495,7 +495,7 @@ int tls_sw_fallback_init(struct sock *sk, rc = -EINVAL ; goto free_aead; } - cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type]; + cipher_sz =get_cipher_size_desc (crypto_info->cipher_type); rc =crypto_aead_setkey (offload_ctx->aead_send, key, cipher_sz->key);if (rc)
net/tls/tls_main.c +2 -2
@@ -58,7 +58,7 @@ enum { TLS_NUM_PROTS, }; -#define CIPHER_SIZE_DESC (cipher) [cipher] = { \ +#define CIPHER_SIZE_DESC (cipher) [cipher - TLS_CIPHER_MIN] = { \ .iv = cipher ## _IV_SIZE, \ .key = cipher ## _KEY_SIZE, \ .salt = cipher ## _SALT_SIZE, \
@@ -66,7 +66,7 @@ enum { .rec_seq = cipher ## _REC_SEQ_SIZE, \ } -const struct tls_cipher_size_desc tls_cipher_size_desc[] = { +const struct tls_cipher_size_desc tls_cipher_size_desc[TLS_CIPHER_MAX +1 -TLS_CIPHER_MIN ] = {CIPHER_SIZE_DESC (TLS_CIPHER_AES_GCM_128 ),CIPHER_SIZE_DESC (TLS_CIPHER_AES_GCM_256 ),CIPHER_SIZE_DESC (TLS_CIPHER_AES_CCM_128 ),
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 7/17 tls: rename tls_cipher_size_desc to tls_cipher_desc
In reply to Sabrina DubrocaWe're going to add other fields to it to fully describe a cipher, so the "_size" name won't match the contents.
--- net/tls/tls.h | 8 +++---- net/tls/tls_device.c | 34 ++++++++++++++-------------- net/tls/tls_device_fallback.c | 42 +++++++++++++++++------------------ net/tls/tls_main.c | 20 ++++++++--------- 4 files changed, 52 insertions(+), 52 deletions(-)
net/tls/tls.h +4 -4
@@ -51,7 +51,7 @@#define TLS_DEC_STATS (net, field) \ SNMP_DEC_STATS((net)->mib.tls_statistics, field) -struct tls_cipher_size_desc { +struct tls_cipher_desc {unsigned int iv;unsigned int key;unsigned int salt;
@@ -61,14 +61,14 @@ struct tls_cipher_size_desc {#define TLS_CIPHER_MIN TLS_CIPHER_AES_GCM_128#define TLS_CIPHER_MAX TLS_CIPHER_ARIA_GCM_256 -extern const struct tls_cipher_size_desc tls_cipher_size_desc[TLS_CIPHER_MAX +1 -TLS_CIPHER_MIN ]; +extern const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX +1 -TLS_CIPHER_MIN ]; -static inline const struct tls_cipher_size_desc *get_cipher_size_desc (u16 cipher_type) +static inline const struct tls_cipher_desc *get_cipher_desc (u16 cipher_type) {if (cipher_type <TLS_CIPHER_MIN || cipher_type >TLS_CIPHER_MAX )return NULL ; -return &tls_cipher_size_desc[cipher_type -TLS_CIPHER_MIN ]; +return &tls_cipher_desc[cipher_type -TLS_CIPHER_MIN ]; }
net/tls/tls_device.c +17 -17
@@ -884,7 +884,7 @@ static int tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx) { struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx); -const struct tls_cipher_size_desc *cipher_sz; +const struct tls_cipher_desc *cipher_desc;int err, offset, copy, data_len, pos;struct sk_buff *skb, *skb_iter;struct scatterlist sg[1 ];
@@ -898,10 +898,10 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)default :return -EINVAL ; } - cipher_sz =get_cipher_size_desc (tls_ctx->crypto_recv.info.cipher_type); + cipher_desc =get_cipher_desc (tls_ctx->crypto_recv.info.cipher_type); rxm =strp_msg (tls_strp_msg (sw_ctx)); - orig_buf =kmalloc (rxm->full_len +TLS_HEADER_SIZE + cipher_sz->iv, + orig_buf =kmalloc (rxm->full_len +TLS_HEADER_SIZE + cipher_desc->iv, sk->sk_allocation);if (!orig_buf)return -ENOMEM ;
@@ -917,8 +917,8 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)sg_init_table (sg,1 );sg_set_buf (&sg[0 ], buf, - rxm->full_len +TLS_HEADER_SIZE + cipher_sz->iv); - err =skb_copy_bits (skb, offset, buf,TLS_HEADER_SIZE + cipher_sz->iv); + rxm->full_len +TLS_HEADER_SIZE + cipher_desc->iv); + err =skb_copy_bits (skb, offset, buf,TLS_HEADER_SIZE + cipher_desc->iv);if (err) goto free_buf;
@@ -929,7 +929,7 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)else err =0 ; - data_len = rxm->full_len - cipher_sz->tag; + data_len = rxm->full_len - cipher_desc->tag;if (skb_pagelen (skb) > offset) { 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) {struct tls_context *tls_ctx =tls_get_ctx (sk);struct tls_prot_info *prot = &tls_ctx->prot_info; -const struct tls_cipher_size_desc *cipher_sz; +const struct tls_cipher_desc *cipher_desc;struct tls_record_info *start_marker_record;struct tls_offload_context_tx *offload_ctx;struct tls_crypto_info *crypto_info;
@@ -1094,31 +1094,31 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) rc = -EINVAL ; goto release_netdev; } - cipher_sz =get_cipher_size_desc (crypto_info->cipher_type); + cipher_desc =get_cipher_desc (crypto_info->cipher_type);/* Sanity-check the rec_seq_size for stack allocations */ -if (cipher_sz->rec_seq >TLS_MAX_REC_SEQ_SIZE ) { +if (cipher_desc->rec_seq >TLS_MAX_REC_SEQ_SIZE ) { rc = -EINVAL ; goto release_netdev; } prot->version = crypto_info->version; prot->cipher_type = crypto_info->cipher_type; - prot->prepend_size =TLS_HEADER_SIZE + cipher_sz->iv; - prot->tag_size = cipher_sz->tag; + prot->prepend_size =TLS_HEADER_SIZE + cipher_desc->iv; + prot->tag_size = cipher_desc->tag; prot->overhead_size = prot->prepend_size + prot->tag_size; - prot->iv_size = cipher_sz->iv; - prot->salt_size = cipher_sz->salt; - ctx->tx.iv =kmalloc (cipher_sz->iv + cipher_sz->salt,GFP_KERNEL ); + prot->iv_size = cipher_desc->iv; + prot->salt_size = cipher_desc->salt; + ctx->tx.iv =kmalloc (cipher_desc->iv + cipher_desc->salt,GFP_KERNEL );if (!ctx->tx.iv) { rc = -ENOMEM ; goto release_netdev; } -memcpy (ctx->tx.iv + cipher_sz->salt, iv, cipher_sz->iv); +memcpy (ctx->tx.iv + cipher_desc->salt, iv, cipher_desc->iv); - prot->rec_seq_size = cipher_sz->rec_seq; - ctx->tx.rec_seq =kmemdup (rec_seq, cipher_sz->rec_seq,GFP_KERNEL ); + prot->rec_seq_size = cipher_desc->rec_seq; + ctx->tx.rec_seq =kmemdup (rec_seq, cipher_desc->rec_seq,GFP_KERNEL );if (!ctx->tx.rec_seq) { rc = -ENOMEM ; goto free_iv;
net/tls/tls_device_fallback.c +21 -21
119 lines not on this page · raw message
net/tls/tls_main.c +10 -10
@@ -58,7 +58,7 @@ enum { TLS_NUM_PROTS, }; -#define CIPHER_SIZE_DESC (cipher) [cipher - TLS_CIPHER_MIN] = { \ +#define CIPHER_DESC (cipher) [cipher - TLS_CIPHER_MIN] = { \ .iv = cipher ## _IV_SIZE, \ .key = cipher ## _KEY_SIZE, \ .salt = cipher ## _SALT_SIZE, \
@@ -66,15 +66,15 @@ enum { .rec_seq = cipher ## _REC_SEQ_SIZE, \ } -const struct tls_cipher_size_desc tls_cipher_size_desc[TLS_CIPHER_MAX +1 -TLS_CIPHER_MIN ] = { -CIPHER_SIZE_DESC (TLS_CIPHER_AES_GCM_128 ), -CIPHER_SIZE_DESC (TLS_CIPHER_AES_GCM_256 ), -CIPHER_SIZE_DESC (TLS_CIPHER_AES_CCM_128 ), -CIPHER_SIZE_DESC (TLS_CIPHER_CHACHA20_POLY1305 ), -CIPHER_SIZE_DESC (TLS_CIPHER_SM4_GCM ), -CIPHER_SIZE_DESC (TLS_CIPHER_SM4_CCM ), -CIPHER_SIZE_DESC (TLS_CIPHER_ARIA_GCM_128 ), -CIPHER_SIZE_DESC (TLS_CIPHER_ARIA_GCM_256 ), +const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX +1 -TLS_CIPHER_MIN ] = { +CIPHER_DESC (TLS_CIPHER_AES_GCM_128 ), +CIPHER_DESC (TLS_CIPHER_AES_GCM_256 ), +CIPHER_DESC (TLS_CIPHER_AES_CCM_128 ), +CIPHER_DESC (TLS_CIPHER_CHACHA20_POLY1305 ), +CIPHER_DESC (TLS_CIPHER_SM4_GCM ), +CIPHER_DESC (TLS_CIPHER_SM4_CCM ), +CIPHER_DESC (TLS_CIPHER_ARIA_GCM_128 ), +CIPHER_DESC (TLS_CIPHER_ARIA_GCM_256 ), };static const struct proto *saved_tcpv6_prot;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 8/17 tls: extend tls_cipher_desc to fully describe the ciphers
In reply to Sabrina Dubroca - add nonce, usually equal to iv_size but not for chacha
- add offsets into the crypto_info for each field
- add algorithm name
- add offloadable flag
Also add helpers to access each field of a crypto_info struct described by a tls_cipher_desc.
--- net/tls/tls.h | 32 ++++++++++++++++++++++++++++++++ net/tls/tls_main.c | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 9 deletions(-)
net/tls/tls.h +32 -0
@@ -52,11 +52,19 @@ SNMP_DEC_STATS((net)->mib.tls_statistics, field)struct tls_cipher_desc { +unsigned int nonce;unsigned int iv;unsigned int key;unsigned int salt;unsigned int tag;unsigned int rec_seq; +unsigned int iv_offset; +unsigned int key_offset; +unsigned int salt_offset; +unsigned int rec_seq_offset; +char *cipher_name; +bool offloadable; +size_t crypto_info; };#define TLS_CIPHER_MIN TLS_CIPHER_AES_GCM_128
@@ -71,6 +79,30 @@ static inline const struct tls_cipher_desc *get_cipher_desc(u16 cipher_type)return &tls_cipher_desc[cipher_type -TLS_CIPHER_MIN ]; } +static inline char *crypto_info_iv (struct tls_crypto_info *crypto_info, +const struct tls_cipher_desc *cipher_desc) +{ +return (char *)crypto_info + cipher_desc->iv_offset; +} + +static inline char *crypto_info_key (struct tls_crypto_info *crypto_info, +const struct tls_cipher_desc *cipher_desc) +{ +return (char *)crypto_info + cipher_desc->key_offset; +} + +static inline char *crypto_info_salt (struct tls_crypto_info *crypto_info, +const struct tls_cipher_desc *cipher_desc) +{ +return (char *)crypto_info + cipher_desc->salt_offset; +} + +static inline char *crypto_info_rec_seq (struct tls_crypto_info *crypto_info, +const struct tls_cipher_desc *cipher_desc) +{ +return (char *)crypto_info + cipher_desc->rec_seq_offset; +} +/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages * allocated or mapped for each TLS record. After encryption, the records are
net/tls/tls_main.c +32 -9
55 lines not on this page · raw message
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 9/17 tls: validate cipher descriptions at compile time
In reply to Sabrina Dubroca--- net/tls/tls_main.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
net/tls/tls_main.c +18 -0
@@ -58,6 +58,15 @@ enum { TLS_NUM_PROTS, }; +#define CHECK_CIPHER_DESC (cipher,ci) \ + static_assert(cipher ## _IV_SIZE <= MAX_IV_SIZE); \ + static_assert(cipher ## _REC_SEQ_SIZE <= TLS_MAX_REC_SEQ_SIZE); \ + static_assert(cipher ## _TAG_SIZE == TLS_TAG_SIZE); \ + static_assert(sizeof_field(struct ci, iv) == cipher ## _IV_SIZE); \ + static_assert(sizeof_field(struct ci, key) == cipher ## _KEY_SIZE); \ + static_assert(sizeof_field(struct ci, salt) == cipher ## _SALT_SIZE); \ + static_assert(sizeof_field(struct ci, rec_seq) == cipher ## _REC_SEQ_SIZE); +#define __CIPHER_DESC (ci) \ .iv_offset = offsetof(struct ci, iv), \ .key_offset = offsetof(struct ci, key), \
@@ -100,6 +109,15 @@ const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MINCIPHER_DESC (TLS_CIPHER_ARIA_GCM_256 , tls12_crypto_info_aria_gcm_256,"gcm(aria)" , false), }; +CHECK_CIPHER_DESC (TLS_CIPHER_AES_GCM_128 , tls12_crypto_info_aes_gcm_128); +CHECK_CIPHER_DESC (TLS_CIPHER_AES_GCM_256 , tls12_crypto_info_aes_gcm_256); +CHECK_CIPHER_DESC (TLS_CIPHER_AES_CCM_128 , tls12_crypto_info_aes_ccm_128); +CHECK_CIPHER_DESC (TLS_CIPHER_CHACHA20_POLY1305 , tls12_crypto_info_chacha20_poly1305); +CHECK_CIPHER_DESC (TLS_CIPHER_SM4_GCM , tls12_crypto_info_sm4_gcm); +CHECK_CIPHER_DESC (TLS_CIPHER_SM4_CCM , tls12_crypto_info_sm4_ccm); +CHECK_CIPHER_DESC (TLS_CIPHER_ARIA_GCM_128 , tls12_crypto_info_aria_gcm_128); +CHECK_CIPHER_DESC (TLS_CIPHER_ARIA_GCM_256 , tls12_crypto_info_aria_gcm_256); +static const struct proto *saved_tcpv6_prot;static DEFINE_MUTEX (tcpv6_prot_mutex);static const struct proto *saved_tcpv4_prot;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 10/17 tls: expand use of tls_cipher_desc in tls_set_device_offload
In reply to Sabrina Dubrocatls_set_device_offload is already getting iv and rec_seq sizes from tls_cipher_desc. We can now also check if the cipher_type coming from userspace is valid and can be offloaded.
We can also remove the runtime check on rec_seq, since we validate it at compile time.
--- net/tls/tls_device.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-)
net/tls/tls_device.c +4 -18
@@ -1079,29 +1079,15 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) goto release_netdev; } -switch (crypto_info->cipher_type) { -case TLS_CIPHER_AES_GCM_128 : - iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; - rec_seq = - ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; -break ; -case TLS_CIPHER_AES_GCM_256 : - iv = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->iv; - rec_seq = - ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->rec_seq; -break ; -default : - rc = -EINVAL ; - goto release_netdev; - } cipher_desc =get_cipher_desc (crypto_info->cipher_type); - -/* Sanity-check the rec_seq_size for stack allocations */ -if (cipher_desc->rec_seq >TLS_MAX_REC_SEQ_SIZE ) { +if (!cipher_desc || !cipher_desc->offloadable) { rc = -EINVAL ; goto release_netdev; } + iv =crypto_info_iv (crypto_info, cipher_desc); + rec_seq =crypto_info_rec_seq (crypto_info, cipher_desc); + prot->version = crypto_info->version; prot->cipher_type = crypto_info->cipher_type; prot->prepend_size =TLS_HEADER_SIZE + cipher_desc->iv;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 11/17 tls: allocate the fallback aead after checking that the cipher is valid
In reply to Sabrina DubrocaNo need to allocate the aead if we're going to fail afterwards.
--- net/tls/tls_device_fallback.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
net/tls/tls_device_fallback.c +10 -10
@@ -475,15 +475,6 @@ int tls_sw_fallback_init(struct sock *sk,const u8 *key;int rc; - offload_ctx->aead_send = -crypto_alloc_aead ("gcm(aes)" ,0 ,CRYPTO_ALG_ASYNC ); -if (IS_ERR (offload_ctx->aead_send)) { - rc =PTR_ERR (offload_ctx->aead_send); -pr_err_ratelimited ("crypto_alloc_aead failed rc=%d\n" , rc); - offload_ctx->aead_send =NULL ; - goto err_out; - } -switch (crypto_info->cipher_type) {case TLS_CIPHER_AES_GCM_128 : key = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->key;
@@ -493,10 +484,19 @@ int tls_sw_fallback_init(struct sock *sk,break ;default : rc = -EINVAL ; - goto free_aead; + goto err_out; } cipher_desc =get_cipher_desc (crypto_info->cipher_type); + offload_ctx->aead_send = +crypto_alloc_aead ("gcm(aes)" ,0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (offload_ctx->aead_send)) { + rc =PTR_ERR (offload_ctx->aead_send); +pr_err_ratelimited ("crypto_alloc_aead failed rc=%d\n" , rc); + offload_ctx->aead_send =NULL ; + goto err_out; + } + rc =crypto_aead_setkey (offload_ctx->aead_send, key, cipher_desc->key);if (rc) goto free_aead;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 12/17 tls: expand use of tls_cipher_desc in tls_sw_fallback_init
In reply to Sabrina Dubrocatls_sw_fallback_init already gets the key and tag size from tls_cipher_desc. We can now also check that the cipher type is valid, and stop hard-coding the algorithm name passed to crypto_alloc_aead.
--- net/tls/tls_device_fallback.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)
net/tls/tls_device_fallback.c +6 -14
@@ -472,24 +472,14 @@ int tls_sw_fallback_init(struct sock *sk, struct tls_crypto_info *crypto_info) {const struct tls_cipher_desc *cipher_desc; -const u8 *key;int rc; -switch (crypto_info->cipher_type) { -case TLS_CIPHER_AES_GCM_128 : - key = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->key; -break ; -case TLS_CIPHER_AES_GCM_256 : - key = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->key; -break ; -default : - rc = -EINVAL ; - goto err_out; - } cipher_desc =get_cipher_desc (crypto_info->cipher_type); +if (!cipher_desc || !cipher_desc->offloadable) +return -EINVAL ; offload_ctx->aead_send = -crypto_alloc_aead ("gcm(aes)" ,0 ,CRYPTO_ALG_ASYNC ); +crypto_alloc_aead (cipher_desc->cipher_name,0 ,CRYPTO_ALG_ASYNC );if (IS_ERR (offload_ctx->aead_send)) { rc =PTR_ERR (offload_ctx->aead_send);pr_err_ratelimited ("crypto_alloc_aead failed rc=%d\n" , rc);
@@ -497,7 +487,9 @@ int tls_sw_fallback_init(struct sock *sk, goto err_out; } - rc =crypto_aead_setkey (offload_ctx->aead_send, key, cipher_desc->key); + rc =crypto_aead_setkey (offload_ctx->aead_send, +crypto_info_key (crypto_info, cipher_desc), + cipher_desc->key);if (rc) goto free_aead;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 13/17 tls: get crypto_info size from tls_cipher_desc in do_tls_setsockopt_conf
In reply to Sabrina DubrocaWe can simplify do_tls_setsockopt_conf using tls_cipher_desc. Also use get_cipher_desc's result to check if the cipher_type coming from userspace is valid.
--- net/tls/tls_main.c | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-)
net/tls/tls_main.c +8 -31
@@ -739,7 +739,7 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,struct tls_crypto_info *crypto_info;struct tls_crypto_info *alt_crypto_info;struct tls_context *ctx =tls_get_ctx (sk); -size_t optsize; +const struct tls_cipher_desc *cipher_desc;int rc =0 ;int conf;
@@ -780,46 +780,23 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval, } } -switch (crypto_info->cipher_type) { -case TLS_CIPHER_AES_GCM_128 : - optsize =sizeof (struct tls12_crypto_info_aes_gcm_128 ); -break ; -case TLS_CIPHER_AES_GCM_256 : { - optsize =sizeof (struct tls12_crypto_info_aes_gcm_256 ); -break ; + cipher_desc =get_cipher_desc (crypto_info->cipher_type); +if (!cipher_desc) { + rc = -EINVAL ; + goto err_crypto_info; } -case TLS_CIPHER_AES_CCM_128 : - optsize =sizeof (struct tls12_crypto_info_aes_ccm_128 ); -break ; -case TLS_CIPHER_CHACHA20_POLY1305 : - optsize =sizeof (struct tls12_crypto_info_chacha20_poly1305 ); -break ; -case TLS_CIPHER_SM4_GCM : - optsize =sizeof (struct tls12_crypto_info_sm4_gcm ); -break ; -case TLS_CIPHER_SM4_CCM : - optsize =sizeof (struct tls12_crypto_info_sm4_ccm ); -break ; + +switch (crypto_info->cipher_type) {case TLS_CIPHER_ARIA_GCM_128 : -if (crypto_info->version !=TLS_1_2_VERSION ) { - rc = -EINVAL ; - goto err_crypto_info; - } - optsize =sizeof (struct tls12_crypto_info_aria_gcm_128 ); -break ;case TLS_CIPHER_ARIA_GCM_256 :if (crypto_info->version !=TLS_1_2_VERSION ) { rc = -EINVAL ; goto err_crypto_info; } - optsize =sizeof (struct tls12_crypto_info_aria_gcm_256 );break ; -default : - rc = -EINVAL ; - goto err_crypto_info; } -if (optlen != optsize) { +if (optlen != cipher_desc->crypto_info) { rc = -EINVAL ; goto err_crypto_info; }
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 14/17 tls: use tls_cipher_desc to simplify do_tls_getsockopt_conf
In reply to Sabrina DubrocaEvery cipher uses the same code to update its crypto_info struct based on the values contained in the cctx, with only the struct type and size/offset changing. We can get those from tls_cipher_desc, and use a single pair of memcpy and final copy_to_user.
--- net/tls/tls_main.c | 174 +++------------------------------------------ 1 file changed, 11 insertions(+), 163 deletions(-)
net/tls/tls_main.c +11 -163
@@ -435,6 +435,7 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval, int __user *optlen, int tx) { int rc = 0; +const struct tls_cipher_desc *cipher_desc;struct tls_context *ctx =tls_get_ctx (sk);struct tls_crypto_info *crypto_info;struct cipher_context *cctx;
@@ -473,172 +474,19 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval, goto out; } -switch (crypto_info->cipher_type) { -case TLS_CIPHER_AES_GCM_128 : { -struct tls12_crypto_info_aes_gcm_128 * - crypto_info_aes_gcm_128 = -container_of (crypto_info, - struct tls12_crypto_info_aes_gcm_128, - info); - -if (len !=sizeof (*crypto_info_aes_gcm_128)) { - rc = -EINVAL ; - goto out; - } -memcpy (crypto_info_aes_gcm_128->iv, - cctx->iv +TLS_CIPHER_AES_GCM_128_SALT_SIZE , -TLS_CIPHER_AES_GCM_128_IV_SIZE ); -memcpy (crypto_info_aes_gcm_128->rec_seq, cctx->rec_seq, -TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE ); -if (copy_to_user (optval, - crypto_info_aes_gcm_128, -sizeof (*crypto_info_aes_gcm_128))) - rc = -EFAULT ; -break ; - } -case TLS_CIPHER_AES_GCM_256 : { -struct tls12_crypto_info_aes_gcm_256 * - crypto_info_aes_gcm_256 = -container_of (crypto_info, - struct tls12_crypto_info_aes_gcm_256, - info); - -if (len !=sizeof (*crypto_info_aes_gcm_256)) { - rc = -EINVAL ; - goto out; - } -memcpy (crypto_info_aes_gcm_256->iv, - cctx->iv +TLS_CIPHER_AES_GCM_256_SALT_SIZE , -TLS_CIPHER_AES_GCM_256_IV_SIZE ); -memcpy (crypto_info_aes_gcm_256->rec_seq, cctx->rec_seq, -TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE ); -if (copy_to_user (optval, - crypto_info_aes_gcm_256, -sizeof (*crypto_info_aes_gcm_256))) - rc = -EFAULT ; -break ; - } -case TLS_CIPHER_AES_CCM_128 : { -struct tls12_crypto_info_aes_ccm_128 *aes_ccm_128 = -container_of (crypto_info, - struct tls12_crypto_info_aes_ccm_128, info); - -if (len !=sizeof (*aes_ccm_128)) { - rc = -EINVAL ; - goto out; - } -memcpy (aes_ccm_128->iv, - cctx->iv +TLS_CIPHER_AES_CCM_128_SALT_SIZE , -TLS_CIPHER_AES_CCM_128_IV_SIZE ); -memcpy (aes_ccm_128->rec_seq, cctx->rec_seq, -TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE ); -if (copy_to_user (optval, aes_ccm_128,sizeof (*aes_ccm_128))) - rc = -EFAULT ; -break ; - } -case TLS_CIPHER_CHACHA20_POLY1305 : { -struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305 = -container_of (crypto_info, - struct tls12_crypto_info_chacha20_poly1305, - info); - -if (len !=sizeof (*chacha20_poly1305)) { - rc = -EINVAL ; - goto out; - } -memcpy (chacha20_poly1305->iv, - cctx->iv +TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE , -TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE ); -memcpy (chacha20_poly1305->rec_seq, cctx->rec_seq, -TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE ); -if (copy_to_user (optval, chacha20_poly1305, -sizeof (*chacha20_poly1305))) - rc = -EFAULT ; -break ; + cipher_desc =get_cipher_desc (crypto_info->cipher_type); +if (!cipher_desc || len != cipher_desc->crypto_info) { + rc = -EINVAL ; + goto out; } -case TLS_CIPHER_SM4_GCM : { -struct tls12_crypto_info_sm4_gcm *sm4_gcm_info = -container_of (crypto_info, - struct tls12_crypto_info_sm4_gcm, info); -if (len !=sizeof (*sm4_gcm_info)) { - rc = -EINVAL ; - goto out; - } -memcpy (sm4_gcm_info->iv, - cctx->iv +TLS_CIPHER_SM4_GCM_SALT_SIZE , -TLS_CIPHER_SM4_GCM_IV_SIZE ); -memcpy (sm4_gcm_info->rec_seq, cctx->rec_seq, -TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE ); -if (copy_to_user (optval, sm4_gcm_info,sizeof (*sm4_gcm_info))) - rc = -EFAULT ; -break ; - } -case TLS_CIPHER_SM4_CCM : { -struct tls12_crypto_info_sm4_ccm *sm4_ccm_info = -container_of (crypto_info, - struct tls12_crypto_info_sm4_ccm, info); +memcpy (crypto_info_iv (crypto_info, cipher_desc), + cctx->iv + cipher_desc->salt, cipher_desc->iv); +memcpy (crypto_info_rec_seq (crypto_info, cipher_desc), + cctx->rec_seq, cipher_desc->rec_seq); -if (len !=sizeof (*sm4_ccm_info)) { - rc = -EINVAL ; - goto out; - } -memcpy (sm4_ccm_info->iv, - cctx->iv +TLS_CIPHER_SM4_CCM_SALT_SIZE , -TLS_CIPHER_SM4_CCM_IV_SIZE ); -memcpy (sm4_ccm_info->rec_seq, cctx->rec_seq, -TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE ); -if (copy_to_user (optval, sm4_ccm_info,sizeof (*sm4_ccm_info))) - rc = -EFAULT ; -break ; - } -case TLS_CIPHER_ARIA_GCM_128 : { -struct tls12_crypto_info_aria_gcm_128 * - crypto_info_aria_gcm_128 = -container_of (crypto_info, - struct tls12_crypto_info_aria_gcm_128, - info); - -if (len !=sizeof (*crypto_info_aria_gcm_128)) { - rc = -EINVAL ; - goto out; - } -memcpy (crypto_info_aria_gcm_128->iv, - cctx->iv +TLS_CIPHER_ARIA_GCM_128_SALT_SIZE , -TLS_CIPHER_ARIA_GCM_128_IV_SIZE ); -memcpy (crypto_info_aria_gcm_128->rec_seq, cctx->rec_seq, -TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE ); -if (copy_to_user (optval, - crypto_info_aria_gcm_128, -sizeof (*crypto_info_aria_gcm_128))) - rc = -EFAULT ; -break ; - } -case TLS_CIPHER_ARIA_GCM_256 : { -struct tls12_crypto_info_aria_gcm_256 * - crypto_info_aria_gcm_256 = -container_of (crypto_info, - struct tls12_crypto_info_aria_gcm_256, - info); - -if (len !=sizeof (*crypto_info_aria_gcm_256)) { - rc = -EINVAL ; - goto out; - } -memcpy (crypto_info_aria_gcm_256->iv, - cctx->iv +TLS_CIPHER_ARIA_GCM_256_SALT_SIZE , -TLS_CIPHER_ARIA_GCM_256_IV_SIZE ); -memcpy (crypto_info_aria_gcm_256->rec_seq, cctx->rec_seq, -TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE ); -if (copy_to_user (optval, - crypto_info_aria_gcm_256, -sizeof (*crypto_info_aria_gcm_256))) - rc = -EFAULT ; -break ; - } -default : - rc = -EINVAL ; - } +if (copy_to_user (optval, crypto_info, cipher_desc->crypto_info)) + rc = -EFAULT ; out:return rc;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 15/17 tls: use tls_cipher_desc to get per-cipher sizes in tls_set_sw_offload
In reply to Sabrina DubrocaWe can get rid of some local variables, but we have to keep nonce_size because tls1.3 uses nonce_size = 0 for all ciphers.
We can also drop the runtime sanity checks on iv/rec_seq/tag size, since we have compile time checks on those values.
--- net/tls/tls_sw.c | 79 ++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 63 deletions(-)
net/tls/tls_sw.c +16 -63
@@ -2590,10 +2590,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls_sw_context_rx *sw_ctx_rx =NULL ;struct cipher_context *cctx;struct crypto_aead **aead; -u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;struct crypto_tfm *tfm;char *iv, *rec_seq, *key, *salt, *cipher_name; -size_t keysize; +const struct tls_cipher_desc *cipher_desc; +u16 nonce_size;int rc =0 ;if (!ctx) {
@@ -2652,16 +2652,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; gcm_128_info = (void *)crypto_info; - nonce_size =TLS_CIPHER_AES_GCM_128_IV_SIZE ; - tag_size =TLS_CIPHER_AES_GCM_128_TAG_SIZE ; - iv_size =TLS_CIPHER_AES_GCM_128_IV_SIZE ; iv = gcm_128_info->iv; - rec_seq_size =TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE ; rec_seq = gcm_128_info->rec_seq; - keysize =TLS_CIPHER_AES_GCM_128_KEY_SIZE ; key = gcm_128_info->key; salt = gcm_128_info->salt; - salt_size =TLS_CIPHER_AES_GCM_128_SALT_SIZE ; cipher_name ="gcm(aes)" ;break ; }
@@ -2669,16 +2663,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_aes_gcm_256 *gcm_256_info; gcm_256_info = (void *)crypto_info; - nonce_size =TLS_CIPHER_AES_GCM_256_IV_SIZE ; - tag_size =TLS_CIPHER_AES_GCM_256_TAG_SIZE ; - iv_size =TLS_CIPHER_AES_GCM_256_IV_SIZE ; iv = gcm_256_info->iv; - rec_seq_size =TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE ; rec_seq = gcm_256_info->rec_seq; - keysize =TLS_CIPHER_AES_GCM_256_KEY_SIZE ; key = gcm_256_info->key; salt = gcm_256_info->salt; - salt_size =TLS_CIPHER_AES_GCM_256_SALT_SIZE ; cipher_name ="gcm(aes)" ;break ; }
@@ -2686,16 +2674,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_aes_ccm_128 *ccm_128_info; ccm_128_info = (void *)crypto_info; - nonce_size =TLS_CIPHER_AES_CCM_128_IV_SIZE ; - tag_size =TLS_CIPHER_AES_CCM_128_TAG_SIZE ; - iv_size =TLS_CIPHER_AES_CCM_128_IV_SIZE ; iv = ccm_128_info->iv; - rec_seq_size =TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE ; rec_seq = ccm_128_info->rec_seq; - keysize =TLS_CIPHER_AES_CCM_128_KEY_SIZE ; key = ccm_128_info->key; salt = ccm_128_info->salt; - salt_size =TLS_CIPHER_AES_CCM_128_SALT_SIZE ; cipher_name ="ccm(aes)" ;break ; }
@@ -2703,16 +2685,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305_info; chacha20_poly1305_info = (void *)crypto_info; - nonce_size =0 ; - tag_size =TLS_CIPHER_CHACHA20_POLY1305_TAG_SIZE ; - iv_size =TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE ; iv = chacha20_poly1305_info->iv; - rec_seq_size =TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE ; rec_seq = chacha20_poly1305_info->rec_seq; - keysize =TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE ; key = chacha20_poly1305_info->key; salt = chacha20_poly1305_info->salt; - salt_size =TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE ; cipher_name ="rfc7539(chacha20,poly1305)" ;break ; }
@@ -2720,16 +2696,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_sm4_gcm *sm4_gcm_info; sm4_gcm_info = (void *)crypto_info; - nonce_size =TLS_CIPHER_SM4_GCM_IV_SIZE ; - tag_size =TLS_CIPHER_SM4_GCM_TAG_SIZE ; - iv_size =TLS_CIPHER_SM4_GCM_IV_SIZE ; iv = sm4_gcm_info->iv; - rec_seq_size =TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE ; rec_seq = sm4_gcm_info->rec_seq; - keysize =TLS_CIPHER_SM4_GCM_KEY_SIZE ; key = sm4_gcm_info->key; salt = sm4_gcm_info->salt; - salt_size =TLS_CIPHER_SM4_GCM_SALT_SIZE ; cipher_name ="gcm(sm4)" ;break ; }
@@ -2737,16 +2707,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_sm4_ccm *sm4_ccm_info; sm4_ccm_info = (void *)crypto_info; - nonce_size =TLS_CIPHER_SM4_CCM_IV_SIZE ; - tag_size =TLS_CIPHER_SM4_CCM_TAG_SIZE ; - iv_size =TLS_CIPHER_SM4_CCM_IV_SIZE ; iv = sm4_ccm_info->iv; - rec_seq_size =TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE ; rec_seq = sm4_ccm_info->rec_seq; - keysize =TLS_CIPHER_SM4_CCM_KEY_SIZE ; key = sm4_ccm_info->key; salt = sm4_ccm_info->salt; - salt_size =TLS_CIPHER_SM4_CCM_SALT_SIZE ; cipher_name ="ccm(sm4)" ;break ; }
@@ -2754,16 +2718,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_aria_gcm_128 *aria_gcm_128_info; aria_gcm_128_info = (void *)crypto_info; - nonce_size =TLS_CIPHER_ARIA_GCM_128_IV_SIZE ; - tag_size =TLS_CIPHER_ARIA_GCM_128_TAG_SIZE ; - iv_size =TLS_CIPHER_ARIA_GCM_128_IV_SIZE ; iv = aria_gcm_128_info->iv; - rec_seq_size =TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE ; rec_seq = aria_gcm_128_info->rec_seq; - keysize =TLS_CIPHER_ARIA_GCM_128_KEY_SIZE ; key = aria_gcm_128_info->key; salt = aria_gcm_128_info->salt; - salt_size =TLS_CIPHER_ARIA_GCM_128_SALT_SIZE ; cipher_name ="gcm(aria)" ;break ; }
@@ -2771,16 +2729,10 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct tls12_crypto_info_aria_gcm_256 *gcm_256_info; gcm_256_info = (void *)crypto_info; - nonce_size =TLS_CIPHER_ARIA_GCM_256_IV_SIZE ; - tag_size =TLS_CIPHER_ARIA_GCM_256_TAG_SIZE ; - iv_size =TLS_CIPHER_ARIA_GCM_256_IV_SIZE ; iv = gcm_256_info->iv; - rec_seq_size =TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE ; rec_seq = gcm_256_info->rec_seq; - keysize =TLS_CIPHER_ARIA_GCM_256_KEY_SIZE ; key = gcm_256_info->key; salt = gcm_256_info->salt; - salt_size =TLS_CIPHER_ARIA_GCM_256_SALT_SIZE ; cipher_name ="gcm(aria)" ;break ; }
@@ -2789,6 +2741,9 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) goto free_priv; } + cipher_desc =get_cipher_desc (crypto_info->cipher_type); + nonce_size = cipher_desc->nonce; +if (crypto_info->version ==TLS_1_3_VERSION ) { nonce_size =0 ; prot->aad_size =TLS_HEADER_SIZE ;
@@ -2799,9 +2754,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) }/* Sanity-check the sizes for stack allocations. */ -if (iv_size >MAX_IV_SIZE || nonce_size >MAX_IV_SIZE || - rec_seq_size >TLS_MAX_REC_SEQ_SIZE || tag_size !=TLS_TAG_SIZE || - prot->aad_size >TLS_MAX_AAD_SIZE ) { +if (nonce_size >MAX_IV_SIZE || prot->aad_size >TLS_MAX_AAD_SIZE ) { rc = -EINVAL ; goto free_priv; }
@@ -2809,21 +2762,22 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) prot->version = crypto_info->version; prot->cipher_type = crypto_info->cipher_type; prot->prepend_size =TLS_HEADER_SIZE + nonce_size; - prot->tag_size = tag_size; + prot->tag_size = cipher_desc->tag; prot->overhead_size = prot->prepend_size + prot->tag_size + prot->tail_size; - prot->iv_size = iv_size; - prot->salt_size = salt_size; - cctx->iv =kmalloc (iv_size + salt_size,GFP_KERNEL ); + prot->iv_size = cipher_desc->iv; + prot->salt_size = cipher_desc->salt; + cctx->iv =kmalloc (cipher_desc->iv + cipher_desc->salt,GFP_KERNEL );if (!cctx->iv) { rc = -ENOMEM ; goto free_priv; }/* Note: 128 & 256 bit salt are the same size */ - prot->rec_seq_size = rec_seq_size; -memcpy (cctx->iv, salt, salt_size); -memcpy (cctx->iv + salt_size, iv, iv_size); - cctx->rec_seq =kmemdup (rec_seq, rec_seq_size,GFP_KERNEL ); + prot->rec_seq_size = cipher_desc->rec_seq; +memcpy (cctx->iv, salt, cipher_desc->salt); +memcpy (cctx->iv + cipher_desc->salt, iv, cipher_desc->iv); + + cctx->rec_seq =kmemdup (rec_seq, cipher_desc->rec_seq,GFP_KERNEL );if (!cctx->rec_seq) { rc = -ENOMEM ; goto free_iv;
@@ -2840,8 +2794,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) ctx->push_pending_record = tls_sw_push_pending_record; - rc =crypto_aead_setkey (*aead, key, keysize); - + rc =crypto_aead_setkey (*aead, key, cipher_desc->key);if (rc) goto free_aead;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 16/17 tls: use tls_cipher_desc to access per-cipher crypto_info in tls_set_sw_offload
In reply to Sabrina DubrocaThe crypto_info_* helpers allow us to fetch pointers into the per-cipher crypto_info's data.
--- net/tls/tls_sw.c | 89 +++++++----------------------------------------- 1 file changed, 13 insertions(+), 76 deletions(-)
net/tls/tls_sw.c +13 -76
@@ -2648,94 +2648,26 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) }switch (crypto_info->cipher_type) { -case TLS_CIPHER_AES_GCM_128 : { -struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; - - gcm_128_info = (void *)crypto_info; - iv = gcm_128_info->iv; - rec_seq = gcm_128_info->rec_seq; - key = gcm_128_info->key; - salt = gcm_128_info->salt; +case TLS_CIPHER_AES_GCM_128 : +case TLS_CIPHER_AES_GCM_256 : cipher_name ="gcm(aes)" ;break ; - } -case TLS_CIPHER_AES_GCM_256 : { -struct tls12_crypto_info_aes_gcm_256 *gcm_256_info; - - gcm_256_info = (void *)crypto_info; - iv = gcm_256_info->iv; - rec_seq = gcm_256_info->rec_seq; - key = gcm_256_info->key; - salt = gcm_256_info->salt; - cipher_name ="gcm(aes)" ; -break ; - } -case TLS_CIPHER_AES_CCM_128 : { -struct tls12_crypto_info_aes_ccm_128 *ccm_128_info; - - ccm_128_info = (void *)crypto_info; - iv = ccm_128_info->iv; - rec_seq = ccm_128_info->rec_seq; - key = ccm_128_info->key; - salt = ccm_128_info->salt; +case TLS_CIPHER_AES_CCM_128 : cipher_name ="ccm(aes)" ;break ; - } -case TLS_CIPHER_CHACHA20_POLY1305 : { -struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305_info; - - chacha20_poly1305_info = (void *)crypto_info; - iv = chacha20_poly1305_info->iv; - rec_seq = chacha20_poly1305_info->rec_seq; - key = chacha20_poly1305_info->key; - salt = chacha20_poly1305_info->salt; +case TLS_CIPHER_CHACHA20_POLY1305 : cipher_name ="rfc7539(chacha20,poly1305)" ;break ; - } -case TLS_CIPHER_SM4_GCM : { -struct tls12_crypto_info_sm4_gcm *sm4_gcm_info; - - sm4_gcm_info = (void *)crypto_info; - iv = sm4_gcm_info->iv; - rec_seq = sm4_gcm_info->rec_seq; - key = sm4_gcm_info->key; - salt = sm4_gcm_info->salt; +case TLS_CIPHER_SM4_GCM : cipher_name ="gcm(sm4)" ;break ; - } -case TLS_CIPHER_SM4_CCM : { -struct tls12_crypto_info_sm4_ccm *sm4_ccm_info; - - sm4_ccm_info = (void *)crypto_info; - iv = sm4_ccm_info->iv; - rec_seq = sm4_ccm_info->rec_seq; - key = sm4_ccm_info->key; - salt = sm4_ccm_info->salt; +case TLS_CIPHER_SM4_CCM : cipher_name ="ccm(sm4)" ;break ; - } -case TLS_CIPHER_ARIA_GCM_128 : { -struct tls12_crypto_info_aria_gcm_128 *aria_gcm_128_info; - - aria_gcm_128_info = (void *)crypto_info; - iv = aria_gcm_128_info->iv; - rec_seq = aria_gcm_128_info->rec_seq; - key = aria_gcm_128_info->key; - salt = aria_gcm_128_info->salt; +case TLS_CIPHER_ARIA_GCM_128 : +case TLS_CIPHER_ARIA_GCM_256 : cipher_name ="gcm(aria)" ;break ; - } -case TLS_CIPHER_ARIA_GCM_256 : { -struct tls12_crypto_info_aria_gcm_256 *gcm_256_info; - - gcm_256_info = (void *)crypto_info; - iv = gcm_256_info->iv; - rec_seq = gcm_256_info->rec_seq; - key = gcm_256_info->key; - salt = gcm_256_info->salt; - cipher_name ="gcm(aria)" ; -break ; - }default : rc = -EINVAL ; goto free_priv;
@@ -2744,6 +2676,11 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) cipher_desc =get_cipher_desc (crypto_info->cipher_type); nonce_size = cipher_desc->nonce; + iv =crypto_info_iv (crypto_info, cipher_desc); + key =crypto_info_key (crypto_info, cipher_desc); + salt =crypto_info_salt (crypto_info, cipher_desc); + rec_seq =crypto_info_rec_seq (crypto_info, cipher_desc); +if (crypto_info->version ==TLS_1_3_VERSION ) { nonce_size =0 ; prot->aad_size =TLS_HEADER_SIZE ;
Signature · 1 line
2.40.1
Sabrina Dubroca
maintainerPATCH 17/17 tls: get cipher_name from cipher_desc in tls_set_sw_offload
In reply to Sabrina Dubrocatls_cipher_desc also contains the algorithm name needed by crypto_alloc_aead, use it.
Finally, use get_cipher_desc to check if the cipher_type coming from userspace is valid, and remove the cipher_type switch.
--- net/tls/tls_sw.c | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-)
net/tls/tls_sw.c +4 -25
@@ -2591,7 +2591,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)struct cipher_context *cctx;struct crypto_aead **aead;struct crypto_tfm *tfm; -char *iv, *rec_seq, *key, *salt, *cipher_name; +char *iv, *rec_seq, *key, *salt;const struct tls_cipher_desc *cipher_desc;u16 nonce_size;int rc =0 ;
@@ -2647,33 +2647,12 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) aead = &sw_ctx_rx->aead_recv; } -switch (crypto_info->cipher_type) { -case TLS_CIPHER_AES_GCM_128 : -case TLS_CIPHER_AES_GCM_256 : - cipher_name ="gcm(aes)" ; -break ; -case TLS_CIPHER_AES_CCM_128 : - cipher_name ="ccm(aes)" ; -break ; -case TLS_CIPHER_CHACHA20_POLY1305 : - cipher_name ="rfc7539(chacha20,poly1305)" ; -break ; -case TLS_CIPHER_SM4_GCM : - cipher_name ="gcm(sm4)" ; -break ; -case TLS_CIPHER_SM4_CCM : - cipher_name ="ccm(sm4)" ; -break ; -case TLS_CIPHER_ARIA_GCM_128 : -case TLS_CIPHER_ARIA_GCM_256 : - cipher_name ="gcm(aria)" ; -break ; -default : + cipher_desc =get_cipher_desc (crypto_info->cipher_type); +if (!cipher_desc) { rc = -EINVAL ; goto free_priv; } - cipher_desc =get_cipher_desc (crypto_info->cipher_type); nonce_size = cipher_desc->nonce; iv =crypto_info_iv (crypto_info, cipher_desc);
@@ -2721,7 +2700,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) }if (!*aead) { - *aead =crypto_alloc_aead (cipher_name,0 ,0 ); + *aead =crypto_alloc_aead (cipher_desc->cipher_name,0 ,0 );if (IS_ERR (*aead)) { rc =PTR_ERR (*aead); *aead =NULL ;
Signature · 1 line
2.40.1
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Fri, 25 Aug 2023 23:35:05 +0200 you wrote:
Commit
2d2c5ea24243("net/tls: Describe ciphers sizes by const structs") introducedtls_cipher_size_descto describe the size of the fields of the per-ciphercrypto_infostructs, and commitea7a9d88ba21("net/tls: Use cipher sizes structs") used it, but only intls_device.candtls_device_fallback.c, and skipped converting similar code intls_main.candtls_sw.c.[...]
Here is the summary with links:
- [net-next,01/17] selftests: tls: add test variants for aria-gcm
https://git.kernel.org/netdev/net-next/c/84e306b08340 - [net-next,02/17] selftests: tls: add getsockopt test https://git.kernel.org/netdev/net-next/c/f27ad62fe38c - [net-next,03/17] selftests: tls: test some invalid inputs for setsockopt https://git.kernel.org/netdev/net-next/c/4bfb6224ed80 - [net-next,04/17] tls: move tls_cipher_size_desc to net/tls/tls.h https://git.kernel.org/netdev/net-next/c/fd0fc6fdd889 - [net-next,05/17] tls: add TLS_CIPHER_ARIA_GCM_* to tls_cipher_size_desc https://git.kernel.org/netdev/net-next/c/200e23165109 - [net-next,06/17] tls: reduce size of tls_cipher_size_desc https://git.kernel.org/netdev/net-next/c/037303d67607 - [net-next,07/17] tls: rename tls_cipher_size_desc to tls_cipher_desc https://git.kernel.org/netdev/net-next/c/8db44ab26beb - [net-next,08/17] tls: extend tls_cipher_desc to fully describe the ciphers https://git.kernel.org/netdev/net-next/c/176a3f50bc6a - [net-next,09/17] tls: validate cipher descriptions at compile time https://git.kernel.org/netdev/net-next/c/0d98cc02022d - [net-next,10/17] tls: expand use of tls_cipher_desc in tls_set_device_offload https://git.kernel.org/netdev/net-next/c/3524dd4d5f1f - [net-next,11/17] tls: allocate the fallback aead after checking that the cipher is valid https://git.kernel.org/netdev/net-next/c/d2322cf5ed59 - [net-next,12/17] tls: expand use of tls_cipher_desc in tls_sw_fallback_init https://git.kernel.org/netdev/net-next/c/e907277aeb6c - [net-next,13/17] tls: get crypto_info size from tls_cipher_desc in do_tls_setsockopt_conf https://git.kernel.org/netdev/net-next/c/5f309ade49c7 - [net-next,14/17] tls: use tls_cipher_desc to simplify do_tls_getsockopt_conf https://git.kernel.org/netdev/net-next/c/077e05d13548 - [net-next,15/17] tls: use tls_cipher_desc to get per-cipher sizes in tls_set_sw_offload https://git.kernel.org/netdev/net-next/c/d9a6ca1a9758 - [net-next,16/17] tls: use tls_cipher_desc to access per-cipher crypto_info in tls_set_sw_offload https://git.kernel.org/netdev/net-next/c/48dfad27fd40 - [net-next,17/17] tls: get cipher_name from cipher_desc in tls_set_sw_offload https://git.kernel.org/netdev/net-next/c/f3e444e31f9f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html