From: Hugo Landau Date: Wed, 12 Jul 2023 19:12:07 +0000 (+0100) Subject: QUIC QTX: Add ciphertext size calculation function X-Git-Tag: openssl-3.2.0-alpha1~377 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41d39984e948322700a9b48ed6c6e8426bed3a9d;p=thirdparty%2Fopenssl.git QUIC QTX: Add ciphertext size calculation function Reviewed-by: Paul Dale Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/21458) --- diff --git a/include/internal/quic_record_tx.h b/include/internal/quic_record_tx.h index b4c9bb8d267..20fc5e268b4 100644 --- a/include/internal/quic_record_tx.h +++ b/include/internal/quic_record_tx.h @@ -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); diff --git a/ssl/quic/quic_record_tx.c b/ssl/quic/quic_record_tx.c index 243f8a4dd86..68142ad6cf8 100644 --- a/ssl/quic/quic_record_tx.c +++ b/ssl/quic/quic_record_tx.c @@ -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;