LCOV - code coverage report
Current view: top level - tls - s2n_record_read_composite.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 40 40 100.0 %
Date: 2026-08-22 07:27:53 Functions: 1 1 100.0 %
Branches: 24 60 40.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License").
       5                 :            :  * You may not use this file except in compliance with the License.
       6                 :            :  * A copy of the License is located at
       7                 :            :  *
       8                 :            :  *  http://aws.amazon.com/apache2.0
       9                 :            :  *
      10                 :            :  * or in the "license" file accompanying this file. This file is distributed
      11                 :            :  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
      12                 :            :  * express or implied. See the License for the specific language governing
      13                 :            :  * permissions and limitations under the License.
      14                 :            :  */
      15                 :            : 
      16                 :            : #include "crypto/s2n_cipher.h"
      17                 :            : #include "crypto/s2n_hmac.h"
      18                 :            : #include "crypto/s2n_sequence.h"
      19                 :            : #include "error/s2n_errno.h"
      20                 :            : #include "stuffer/s2n_stuffer.h"
      21                 :            : #include "tls/s2n_cipher_suites.h"
      22                 :            : #include "tls/s2n_connection.h"
      23                 :            : #include "tls/s2n_crypto.h"
      24                 :            : #include "tls/s2n_record_read.h"
      25                 :            : #include "utils/s2n_blob.h"
      26                 :            : #include "utils/s2n_safety.h"
      27                 :            : 
      28                 :            : int s2n_record_parse_composite(
      29                 :            :         const struct s2n_cipher_suite *cipher_suite,
      30                 :            :         struct s2n_connection *conn,
      31                 :            :         struct s2n_record_header *header,
      32                 :            :         uint8_t *implicit_iv,
      33                 :            :         struct s2n_hmac_state *mac,
      34                 :            :         uint8_t *sequence_number,
      35                 :            :         struct s2n_session_key *session_key)
      36                 :     107612 : {
      37                 :            :     /* Don't reduce encrypted length for explicit IV, composite decrypt expects it */
      38                 :     107612 :     struct s2n_blob iv = { .data = implicit_iv, .size = cipher_suite->record_alg->cipher->io.comp.record_iv_size };
      39                 :     107612 :     uint8_t ivpad[S2N_TLS_MAX_IV_LEN] = { 0 };
      40                 :            : 
      41                 :     107612 :     struct s2n_blob en = { .size = header->length, .data = s2n_stuffer_raw_read(&conn->in, header->length) };
      42 [ -  + ][ #  # ]:     107612 :     POSIX_ENSURE_REF(en.data);
      43                 :            : 
      44                 :     107612 :     uint16_t payload_length = header->length;
      45                 :     107612 :     uint8_t mac_digest_size = 0;
      46         [ -  + ]:     107612 :     POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size));
      47                 :            : 
      48 [ -  + ][ #  # ]:     107612 :     POSIX_ENSURE_GTE(payload_length, mac_digest_size);
      49                 :     107612 :     payload_length -= mac_digest_size;
      50                 :            : 
      51                 :            :     /* Compute non-payload parts of the MAC(seq num, type, proto vers, fragment length) for composite ciphers.
      52                 :            :      * Composite "decrypt" will MAC the actual payload data.
      53                 :            :      */
      54                 :            :     /* In the decrypt case, this outputs the MAC digest length:
      55                 :            :      * https://github.com/openssl/openssl/blob/master/crypto/evp/e_aes_cbc_hmac_sha1.c#L842 */
      56                 :     107612 :     int mac_size = 0;
      57         [ -  + ]:     107612 :     POSIX_GUARD(cipher_suite->record_alg->cipher->io.comp.initial_hmac(session_key, sequence_number, header->content_type, header->version, payload_length, &mac_size));
      58                 :            : 
      59 [ #  # ][ -  + ]:     107612 :     POSIX_ENSURE_GTE(payload_length, mac_size);
      60                 :     107612 :     payload_length -= mac_size;
      61                 :            :     /* Adjust payload_length for explicit IV */
      62         [ +  + ]:     107612 :     if (conn->actual_protocol_version > S2N_TLS10) {
      63                 :      74795 :         uint32_t out = 0;
      64         [ -  + ]:      74795 :         POSIX_GUARD(s2n_sub_overflow(payload_length, cipher_suite->record_alg->cipher->io.comp.record_iv_size, &out));
      65                 :      74795 :         payload_length = out;
      66                 :      74795 :     }
      67                 :            : 
      68                 :            :     /* Decrypt stuff! */
      69 [ -  + ][ #  # ]:     107612 :     POSIX_ENSURE_NE(en.size, 0);
      70 [ #  # ][ -  + ]:     107612 :     POSIX_ENSURE_EQ(en.size % iv.size, 0);
      71                 :            : 
      72                 :            :     /* Copy the last encrypted block to be the next IV */
      73 [ #  # ][ -  + ]:     107612 :     POSIX_CHECKED_MEMCPY(ivpad, en.data + en.size - iv.size, iv.size);
                 [ +  - ]
      74                 :            : 
      75                 :            :     /* This will: Skip the explicit IV(if applicable), decrypt the payload, verify the MAC and padding. */
      76         [ -  + ]:     107612 :     POSIX_GUARD((cipher_suite->record_alg->cipher->io.comp.decrypt(session_key, &iv, &en, &en)));
      77                 :            : 
      78 [ -  + ][ #  # ]:     107612 :     POSIX_CHECKED_MEMCPY(implicit_iv, ivpad, iv.size);
                 [ +  - ]
      79                 :            : 
      80                 :            :     /* Subtract the padding length */
      81 [ #  # ][ -  + ]:     107612 :     POSIX_ENSURE_GT(en.size, 0);
      82                 :     107612 :     uint32_t out = 0;
      83         [ -  + ]:     107612 :     POSIX_GUARD(s2n_sub_overflow(payload_length, en.data[en.size - 1] + 1, &out));
      84                 :     107612 :     payload_length = out;
      85                 :            : 
      86                 :     107612 :     struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN };
      87         [ -  + ]:     107612 :     POSIX_GUARD(s2n_increment_sequence_number(&seq));
      88                 :            : 
      89                 :            :     /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer
      90                 :            :      * for reading the plaintext data.
      91                 :            :      */
      92         [ -  + ]:     107612 :     POSIX_GUARD(s2n_stuffer_reread(&conn->in));
      93                 :            : 
      94                 :            :     /* Skip the IV, if any */
      95         [ +  + ]:     107612 :     if (conn->actual_protocol_version > S2N_TLS10) {
      96         [ -  + ]:      74795 :         POSIX_GUARD(s2n_stuffer_skip_read(&conn->in, cipher_suite->record_alg->cipher->io.comp.record_iv_size));
      97                 :      74795 :     }
      98                 :            : 
      99                 :            :     /* Truncate and wipe the MAC and any padding */
     100                 :     107612 :     uint32_t mac_len = 0;
     101         [ -  + ]:     107612 :     POSIX_GUARD(s2n_sub_overflow(s2n_stuffer_data_available(&conn->in), payload_length, &mac_len));
     102         [ -  + ]:     107612 :     POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, mac_len));
     103                 :     107612 :     conn->in_status = PLAINTEXT;
     104                 :            : 
     105                 :     107612 :     return 0;
     106                 :     107612 : }

Generated by: LCOV version 1.14