]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
QUIC QTX: Add ciphertext size calculation function
authorHugo Landau <hlandau@openssl.org>
Wed, 12 Jul 2023 19:12:07 +0000 (20:12 +0100)
committerTomas Mraz <tomas@openssl.org>
Fri, 21 Jul 2023 06:43:52 +0000 (08:43 +0200)
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21458)

include/internal/quic_record_tx.h
ssl/quic/quic_record_tx.c

index b4c9bb8d26774ac5b559a96b146d1d041331a5ce..20fc5e268b4e118c99b23a7bb24c54462a04c7fc 100644 (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);
 
 
index 243f8a4dd8652eb551498c1f738a57072ca7a25c..68142ad6cf8fdcfffc0e8280ae02551df02f07c7 100644 (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;