QUIC QTX: Add ciphertext size calculation function

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21458)
This commit is contained in:
Hugo Landau 2023-07-12 20:12:07 +01:00 committed by Tomas Mraz
parent d49c6ca7b9
commit 41d39984e9
2 changed files with 28 additions and 7 deletions

View File

@ -130,6 +130,16 @@ int ossl_qtx_calculate_plaintext_payload_len(OSSL_QTX *qtx, uint32_t enc_level,
size_t ciphertext_len,
size_t *plaintext_len);
/*
* Given the value plaintext_len represented a plaintext packet payload length
* in bytes, determines how many ciphertext bytes it will encrypt to. The value
* output does not include packet headers. Returns 0 if the specified EL is not
* provisioned. The result is written to *ciphertext_len.
*/
int ossl_qtx_calculate_ciphertext_payload_len(OSSL_QTX *qtx, uint32_t enc_level,
size_t plaintext_len,
size_t *ciphertext_len);
uint32_t ossl_qrl_get_suite_cipher_tag_len(uint32_t suite_id);

View File

@ -383,19 +383,27 @@ static size_t iovec_cur_get_buffer(struct iovec_cur *cur,
}
/* Determines the size of the AEAD output given the input size. */
static size_t qtx_inflate_payload_len(OSSL_QTX *qtx, uint32_t enc_level,
size_t plaintext_len)
int ossl_qtx_calculate_ciphertext_payload_len(OSSL_QTX *qtx, uint32_t enc_level,
size_t plaintext_len,
size_t *ciphertext_len)
{
OSSL_QRL_ENC_LEVEL *el
= ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
size_t tag_len;
assert(el != NULL); /* Already checked by caller. */
if (el == NULL) {
*ciphertext_len = 0;
return 0;
}
/*
* We currently only support ciphers with a 1:1 mapping between plaintext
* and ciphertext size, save for authentication tag.
*/
return plaintext_len + ossl_qrl_get_suite_cipher_tag_len(el->suite_id);
tag_len = ossl_qrl_get_suite_cipher_tag_len(el->suite_id);
*ciphertext_len = plaintext_len + tag_len;
return 1;
}
/* Determines the size of the AEAD input given the output size. */
@ -611,9 +619,12 @@ static int qtx_write(OSSL_QTX *qtx, const OSSL_QTX_PKT *pkt, TXE *txe,
}
/* Determine encrypted payload length. */
payload_len = needs_encrypt ? qtx_inflate_payload_len(qtx, enc_level,
cur.bytes_remaining)
: cur.bytes_remaining;
if (needs_encrypt)
ossl_qtx_calculate_ciphertext_payload_len(qtx, enc_level,
cur.bytes_remaining,
&payload_len);
else
payload_len = cur.bytes_remaining;
/* Determine header length. */
hdr->data = NULL;