Support 256 bit TLS keys with device offload
Subsystems (5)
v2 cover Support 256 bit TLS keys with device offload
Hey,
This series adds support for 256 bit TLS keys with device offload, and a cleanup patch to remove repeating code:
- Patches #1-2 add cipher sizes descriptors which allow reducing the amount of code duplications.
- Patch #3 allows 256 bit keys to be TX offloaded in the tls module (RX already supported).
- Patch #4 adds 256 bit keys support to the mlx5 driver.
Changelog -v1->v2: https://lore.kernel.org/all/[email protected]/
* Add a missing '=' in initializer
* Add missing unlocks in resync_handle_seq_match()
Thanks,
Gal
Gal Pressman (3):
net/tls: Use cipher sizes structs
net/tls: Support 256 bit keys with TX device offload net/mlx5e: Support 256 bit keys with kTLS device offload
Tariq Toukan (1):
net/tls: Describe ciphers sizes by const structs
.../mellanox/mlx5/core/en_accel/ktls.h | 7 +- .../mellanox/mlx5/core/en_accel/ktls_rx.c | 45 +++++++++-- .../mellanox/mlx5/core/en_accel/ktls_tx.c | 41 ++++++++-- .../mellanox/mlx5/core/en_accel/ktls_txrx.c | 27 ++++++- .../mellanox/mlx5/core/en_accel/ktls_utils.h | 8 +- include/net/tls.h | 10 +++ net/tls/tls_device.c | 61 ++++++++------ net/tls/tls_device_fallback.c | 79 +++++++++++++------ net/tls/tls_main.c | 17 ++++ 9 files changed, 227 insertions(+), 68 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/4 net/tls: Describe ciphers sizes by const structs
From: Tariq Toukan <[email protected]>
Introduce cipher sizes descriptor. It helps reducing the amount of code duplications and repeated switch/cases that assigns the proper sizes according to the cipher type.
--- include/net/tls.h | 10 ++++++++++ net/tls/tls_main.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+)
include/net/tls.h +10 -0
@@ -51,6 +51,16 @@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_main.c +17 -0
@@ -58,6 +58,23 @@ enum { TLS_NUM_PROTS, }; +#define CIPHER_SIZE_DESC (cipher) [cipher] = { \ + .iv = cipher ## _IV_SIZE, \ + .key = cipher ## _KEY_SIZE, \ + .salt = cipher ## _SALT_SIZE, \ + .tag = cipher ## _TAG_SIZE, \ + .rec_seq = cipher ## _REC_SEQ_SIZE, \ +} + +const struct tls_cipher_size_desc tls_cipher_size_desc[] = { +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 ), +}; +static const struct proto *saved_tcpv6_prot;static DEFINE_MUTEX (tcpv6_prot_mutex);static const struct proto *saved_tcpv4_prot;
Signature · 1 line
2.25.1
PATCH v2 2/4 net/tls: Use cipher sizes structs
In reply to Gal PressmanUse the newly introduced cipher sizes structs instead of the repeated switch cases churn.
--- net/tls/tls_device.c | 55 +++++++++++++------------- net/tls/tls_device_fallback.c | 72 +++++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 51 deletions(-)
net/tls/tls_device.c +29 -26
@@ -902,17 +902,27 @@ static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx, }static int -tls_device_reencrypt (struct sock *sk,struct tls_sw_context_rx *sw_ctx) +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;int err, offset, copy, data_len, pos;struct sk_buff *skb, *skb_iter;struct scatterlist sg[1 ];struct strp_msg *rxm;char *orig_buf, *buf; +switch (tls_ctx->crypto_recv.info.cipher_type) { +case TLS_CIPHER_AES_GCM_128 : +break ; +default : +return -EINVAL ; + } + cipher_sz = &tls_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 + -TLS_CIPHER_AES_GCM_128_IV_SIZE , sk->sk_allocation); + orig_buf =kmalloc (rxm->full_len +TLS_HEADER_SIZE + cipher_sz->iv, + sk->sk_allocation);if (!orig_buf)return -ENOMEM ; buf = orig_buf;
@@ -927,10 +937,8 @@ tls_device_reencrypt(struct sock *sk, struct tls_sw_context_rx *sw_ctx)sg_init_table (sg,1 );sg_set_buf (&sg[0 ], buf, - rxm->full_len +TLS_HEADER_SIZE + -TLS_CIPHER_AES_GCM_128_IV_SIZE ); - err =skb_copy_bits (skb, offset, buf, -TLS_HEADER_SIZE +TLS_CIPHER_AES_GCM_128_IV_SIZE ); + rxm->full_len +TLS_HEADER_SIZE + cipher_sz->iv); + err =skb_copy_bits (skb, offset, buf,TLS_HEADER_SIZE + cipher_sz->iv);if (err) goto free_buf;
@@ -941,7 +949,7 @@ tls_device_reencrypt(struct sock *sk, struct tls_sw_context_rx *sw_ctx)else err =0 ; - data_len = rxm->full_len -TLS_CIPHER_AES_GCM_128_TAG_SIZE ; + data_len = rxm->full_len - cipher_sz->tag;if (skb_pagelen (skb) > offset) { copy =min_t (int,skb_pagelen (skb) - offset, data_len);
@@ -1024,7 +1032,7 @@ int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)* likely have initial fragments decrypted, and final ones not * decrypted. We need to reencrypt that single SKB. */ -return tls_device_reencrypt (sk, sw_ctx); +return tls_device_reencrypt (sk, tls_ctx); }/* Return immediately if the record is either entirely plaintext or
@@ -1041,7 +1049,7 @@ int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx) } ctx->resync_nh_reset =1 ; -return tls_device_reencrypt (sk, sw_ctx); +return tls_device_reencrypt (sk, tls_ctx); } static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
@@ -1062,9 +1070,9 @@ static void tls_device_attach(struct tls_context *ctx, struct sock *sk,int tls_set_device_offload (struct sock *sk,struct tls_context *ctx) { -u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;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;struct tls_record_info *start_marker_record;struct tls_offload_context_tx *offload_ctx;struct tls_crypto_info *crypto_info;
@@ -1099,12 +1107,7 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)switch (crypto_info->cipher_type) {case TLS_CIPHER_AES_GCM_128 : - 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 = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; - rec_seq_size =TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE ; - salt_size =TLS_CIPHER_AES_GCM_128_SALT_SIZE ; rec_seq = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;break ;
@@ -1112,31 +1115,31 @@ 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];/* Sanity-check the rec_seq_size for stack allocations */ -if (rec_seq_size >TLS_MAX_REC_SEQ_SIZE ) { +if (cipher_sz->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 + nonce_size; - prot->tag_size = tag_size; + prot->prepend_size =TLS_HEADER_SIZE + cipher_sz->iv; + prot->tag_size = cipher_sz->tag; prot->overhead_size = prot->prepend_size + prot->tag_size; - prot->iv_size = iv_size; - prot->salt_size = salt_size; - ctx->tx.iv =kmalloc (iv_size +TLS_CIPHER_AES_GCM_128_SALT_SIZE , -GFP_KERNEL ); + prot->iv_size = cipher_sz->iv; + prot->salt_size = cipher_sz->salt; + ctx->tx.iv =kmalloc (cipher_sz->iv + cipher_sz->salt,GFP_KERNEL );if (!ctx->tx.iv) { rc = -ENOMEM ; goto release_netdev; } -memcpy (ctx->tx.iv +TLS_CIPHER_AES_GCM_128_SALT_SIZE , iv, iv_size); +memcpy (ctx->tx.iv + cipher_sz->salt, iv, cipher_sz->iv); - prot->rec_seq_size = rec_seq_size; - ctx->tx.rec_seq =kmemdup (rec_seq, rec_seq_size,GFP_KERNEL ); + prot->rec_seq_size = cipher_sz->rec_seq; + ctx->tx.rec_seq =kmemdup (rec_seq, cipher_sz->rec_seq,GFP_KERNEL );if (!ctx->tx.rec_seq) { rc = -ENOMEM ; goto free_iv;
net/tls/tls_device_fallback.c +47 -25
146 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 3/4 net/tls: Support 256 bit keys with TX device offload
In reply to Gal PressmanAdd the missing clause for 256 bit keys in tls_set_device_offload(), and the needed adjustments in tls_device_fallback.c.
--- net/tls/tls_device.c | 6 ++++++ net/tls/tls_device_fallback.c | 7 +++++++ 2 files changed, 13 insertions(+)
net/tls/tls_device.c +6 -0
@@ -914,6 +914,7 @@ tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)switch (tls_ctx->crypto_recv.info.cipher_type) {case TLS_CIPHER_AES_GCM_128 : +case TLS_CIPHER_AES_GCM_256 :break ;default :return -EINVAL ;
@@ -1111,6 +1112,11 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) 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;
net/tls/tls_device_fallback.c +7 -0
@@ -64,6 +64,7 @@ static int tls_enc_record(struct aead_request *aead_req,switch (prot->cipher_type) {case TLS_CIPHER_AES_GCM_128 : +case TLS_CIPHER_AES_GCM_256 :break ;default :return -EINVAL ;
@@ -341,6 +342,9 @@ static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,case TLS_CIPHER_AES_GCM_128 : salt = tls_ctx->crypto_send.aes_gcm_128.salt;break ; +case TLS_CIPHER_AES_GCM_256 : + salt = tls_ctx->crypto_send.aes_gcm_256.salt; +break ;default :return NULL ; }
@@ -484,6 +488,9 @@ int tls_sw_fallback_init(struct sock *sk,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 :return -EINVAL ; }
Signature · 1 line
2.25.1
PATCH v2 4/4 net/mlx5e: Support 256 bit keys with kTLS device offload
In reply to Gal PressmanAdd support for 256 bit TLS keys using device offload.
--- .../mellanox/mlx5/core/en_accel/ktls.h | 7 ++- .../mellanox/mlx5/core/en_accel/ktls_rx.c | 45 ++++++++++++++++--- .../mellanox/mlx5/core/en_accel/ktls_tx.c | 41 ++++++++++++++--- .../mellanox/mlx5/core/en_accel/ktls_txrx.c | 27 +++++++++-- .../mellanox/mlx5/core/en_accel/ktls_utils.h | 8 +++- 5 files changed, 111 insertions(+), 17 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls.h +6 -1
@@ -25,7 +25,8 @@ static inline bool mlx5e_is_ktls_device(struct mlx5_core_dev *mdev)if (!MLX5_CAP_GEN (mdev, log_max_dek))return false; -return MLX5_CAP_TLS (mdev, tls_1_2_aes_gcm_128); +return (MLX5_CAP_TLS (mdev, tls_1_2_aes_gcm_128) || +MLX5_CAP_TLS (mdev, tls_1_2_aes_gcm_256)); } static inline bool mlx5e_ktls_type_check(struct mlx5_core_dev *mdev,
@@ -36,6 +37,10 @@ static inline bool mlx5e_ktls_type_check(struct mlx5_core_dev *mdev,if (crypto_info->version ==TLS_1_2_VERSION )return MLX5_CAP_TLS (mdev, tls_1_2_aes_gcm_128);break ; +case TLS_CIPHER_AES_GCM_256 : +if (crypto_info->version ==TLS_1_2_VERSION ) +return MLX5_CAP_TLS (mdev, tls_1_2_aes_gcm_256); +break ; }return false;
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_rx.c +40 -5
69 lines not on this page · raw message
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +35 -6
64 lines not on this page · raw message
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_txrx.c +23 -4
@@ -21,7 +21,7 @@ enum {static void fill_static_params (struct mlx5_wqe_tls_static_params_seg *params, -struct tls12_crypto_info_aes_gcm_128 *info, +union mlx5e_crypto_info *crypto_info,u32 key_id,u32 resync_tcp_sn) {char *initial_rn, *gcm_iv;
@@ -32,7 +32,26 @@ fill_static_params(struct mlx5_wqe_tls_static_params_seg *params, ctx = params->ctx; -EXTRACT_INFO_FIELDS ; +switch (crypto_info->crypto_info.cipher_type) { +case TLS_CIPHER_AES_GCM_128 : { +struct tls12_crypto_info_aes_gcm_128 *info = + &crypto_info->crypto_info_128; + +EXTRACT_INFO_FIELDS ; +break ; + } +case TLS_CIPHER_AES_GCM_256 : { +struct tls12_crypto_info_aes_gcm_256 *info = + &crypto_info->crypto_info_256; + +EXTRACT_INFO_FIELDS ; +break ; + } +default : +WARN_ONCE (1 ,"Unsupported cipher type %u\n" , + crypto_info->crypto_info.cipher_type); +return ; + } gcm_iv =MLX5_ADDR_OF (tls_static_params, ctx, gcm_iv); initial_rn =MLX5_ADDR_OF (tls_static_params, ctx, initial_record_number);
@@ -54,7 +73,7 @@ fill_static_params(struct mlx5_wqe_tls_static_params_seg *params,void mlx5e_ktls_build_static_params (struct mlx5e_set_tls_static_params_wqe *wqe,u16 pc,u32 sqn, -struct tls12_crypto_info_aes_gcm_128 *info, +union mlx5e_crypto_info *crypto_info,u32 tis_tir_num,u32 key_id,u32 resync_tcp_sn,bool fence,enum tls_offload_ctx_dir direction) {
@@ -75,7 +94,7 @@ mlx5e_ktls_build_static_params(struct mlx5e_set_tls_static_params_wqe *wqe, ucseg->flags =MLX5_UMR_INLINE ; ucseg->bsf_octowords =cpu_to_be16 (MLX5_ST_SZ_BYTES (tls_static_params) /16 ); -fill_static_params (&wqe->params, info, key_id, resync_tcp_sn); +fill_static_params (&wqe->params, crypto_info, key_id, resync_tcp_sn); } static void
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_utils.h +7 -1
@@ -27,6 +27,12 @@ int mlx5e_ktls_add_rx(struct net_device *netdev, struct sock *sk,void mlx5e_ktls_del_rx (struct net_device *netdev,struct tls_context *tls_ctx);void mlx5e_ktls_rx_resync (struct net_device *netdev,struct sock *sk,u32 seq,u8 *rcd_sn); +union mlx5e_crypto_info { +struct tls_crypto_info crypto_info; +struct tls12_crypto_info_aes_gcm_128 crypto_info_128; +struct tls12_crypto_info_aes_gcm_256 crypto_info_256; +}; +struct mlx5e_set_tls_static_params_wqe {struct mlx5_wqe_ctrl_seg ctrl;struct mlx5_wqe_umr_ctrl_seg uctrl;
@@ -72,7 +78,7 @@ struct mlx5e_get_tls_progress_params_wqe {void mlx5e_ktls_build_static_params (struct mlx5e_set_tls_static_params_wqe *wqe,u16 pc,u32 sqn, -struct tls12_crypto_info_aes_gcm_128 *info, +union mlx5e_crypto_info *crypto_info,u32 tis_tir_num,u32 key_id,u32 resync_tcp_sn,bool fence,enum tls_offload_ctx_dir direction);void
Signature · 1 line
2.25.1
This series was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Tue, 20 Sep 2022 16:01:46 +0300 you wrote:
Hey,
This series adds support for 256 bit TLS keys with device offload, and a cleanup patch to remove repeating code:
- Patches #1-2 add cipher sizes descriptors which allow reducing the amount of code duplications.
- Patch #3 allows 256 bit keys to be TX offloaded in the tls module (RX already supported).
- Patch #4 adds 256 bit keys support to the mlx5 driver.[...]
Here is the summary with links:
- [net-next,v2,1/4] net/tls: Describe ciphers sizes by const structs
https://git.kernel.org/netdev/net-next/c/2d2c5ea24243 - [net-next,v2,2/4] net/tls: Use cipher sizes structs https://git.kernel.org/netdev/net-next/c/ea7a9d88ba21 - [net-next,v2,3/4] net/tls: Support 256 bit keys with TX device offload https://git.kernel.org/netdev/net-next/c/56e5a6d3aa91 - [net-next,v2,4/4] net/mlx5e: Support 256 bit keys with kTLS device offload https://git.kernel.org/netdev/net-next/c/4960c414db35
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html