Skip to content

Commit a2ef9b6

Browse files
Dave Watsondavem330
authored andcommitted
net: tls: Refactor tls aad space size calculation
TLS 1.3 has a different AAD size, use a variable in the code to make TLS 1.3 support easy. Signed-off-by: Dave Watson <davejwatson@fb.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent fb99bce commit a2ef9b6

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

include/net/tls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ struct cipher_context {
202202
char *iv;
203203
u16 rec_seq_size;
204204
char *rec_seq;
205+
u16 aad_size;
205206
};
206207

207208
union tls_crypto_context {

net/tls/tls_sw.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static int tls_do_decryption(struct sock *sk,
185185
int ret;
186186

187187
aead_request_set_tfm(aead_req, ctx->aead_recv);
188-
aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
188+
aead_request_set_ad(aead_req, tls_ctx->rx.aad_size);
189189
aead_request_set_crypt(aead_req, sgin, sgout,
190190
data_len + tls_ctx->rx.tag_size,
191191
(u8 *)iv_recv);
@@ -289,12 +289,12 @@ static struct tls_rec *tls_get_rec(struct sock *sk)
289289

290290
sg_init_table(rec->sg_aead_in, 2);
291291
sg_set_buf(&rec->sg_aead_in[0], rec->aad_space,
292-
sizeof(rec->aad_space));
292+
tls_ctx->tx.aad_size);
293293
sg_unmark_end(&rec->sg_aead_in[1]);
294294

295295
sg_init_table(rec->sg_aead_out, 2);
296296
sg_set_buf(&rec->sg_aead_out[0], rec->aad_space,
297-
sizeof(rec->aad_space));
297+
tls_ctx->tx.aad_size);
298298
sg_unmark_end(&rec->sg_aead_out[1]);
299299

300300
return rec;
@@ -455,7 +455,7 @@ static int tls_do_encryption(struct sock *sk,
455455
msg_en->sg.curr = start;
456456

457457
aead_request_set_tfm(aead_req, ctx->aead_send);
458-
aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
458+
aead_request_set_ad(aead_req, tls_ctx->tx.aad_size);
459459
aead_request_set_crypt(aead_req, rec->sg_aead_in,
460460
rec->sg_aead_out,
461461
data_len, rec->iv_data);
@@ -1317,7 +1317,7 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
13171317

13181318
aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
13191319
mem_size = aead_size + (nsg * sizeof(struct scatterlist));
1320-
mem_size = mem_size + TLS_AAD_SPACE_SIZE;
1320+
mem_size = mem_size + tls_ctx->rx.aad_size;
13211321
mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv);
13221322

13231323
/* Allocate a single block of memory which contains
@@ -1333,7 +1333,7 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
13331333
sgin = (struct scatterlist *)(mem + aead_size);
13341334
sgout = sgin + n_sgin;
13351335
aad = (u8 *)(sgout + n_sgout);
1336-
iv = aad + TLS_AAD_SPACE_SIZE;
1336+
iv = aad + tls_ctx->rx.aad_size;
13371337

13381338
/* Prepare IV */
13391339
err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
@@ -1352,7 +1352,7 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
13521352

13531353
/* Prepare sgin */
13541354
sg_init_table(sgin, n_sgin);
1355-
sg_set_buf(&sgin[0], aad, TLS_AAD_SPACE_SIZE);
1355+
sg_set_buf(&sgin[0], aad, tls_ctx->rx.aad_size);
13561356
err = skb_to_sgvec(skb, &sgin[1],
13571357
rxm->offset + tls_ctx->rx.prepend_size,
13581358
rxm->full_len - tls_ctx->rx.prepend_size);
@@ -1364,7 +1364,7 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
13641364
if (n_sgout) {
13651365
if (out_iov) {
13661366
sg_init_table(sgout, n_sgout);
1367-
sg_set_buf(&sgout[0], aad, TLS_AAD_SPACE_SIZE);
1367+
sg_set_buf(&sgout[0], aad, tls_ctx->rx.aad_size);
13681368

13691369
*chunk = 0;
13701370
err = tls_setup_from_iter(sk, out_iov, data_len,
@@ -2100,6 +2100,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
21002100
goto free_priv;
21012101
}
21022102

2103+
cctx->aad_size = TLS_AAD_SPACE_SIZE;
21032104
cctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
21042105
cctx->tag_size = tag_size;
21052106
cctx->overhead_size = cctx->prepend_size + cctx->tag_size;

0 commit comments

Comments
 (0)