LCOV - code coverage report
Current view: top level - crypto - s2n_sequence.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 22 22 100.0 %
Date: 2025-08-15 07:28:39 Functions: 2 2 100.0 %
Branches: 9 14 64.3 %

           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_sequence.h"
      17                 :            : 
      18                 :            : #include "error/s2n_errno.h"
      19                 :            : #include "tls/s2n_crypto.h"
      20                 :            : #include "utils/s2n_blob.h"
      21                 :            : 
      22                 :     396912 : #define SEQUENCE_NUMBER_POWER 8
      23                 :            : 
      24                 :            : int s2n_increment_sequence_number(struct s2n_blob *sequence_number)
      25                 :    4031120 : {
      26         [ +  - ]:    4032969 :     for (uint32_t j = sequence_number->size; j > 0; j--) {
      27                 :    4032969 :         uint32_t i = j - 1;
      28                 :    4032969 :         sequence_number->data[i] += 1;
      29         [ +  + ]:    4032969 :         if (sequence_number->data[i]) {
      30                 :    4031119 :             break;
      31                 :    4031119 :         }
      32                 :            : 
      33                 :            :         /* If a sequence number would exceed the maximum value, then we need to start a new session.
      34                 :            :          * This condition is very unlikely. It requires 2^64 - 1 records to be sent.
      35                 :            :          */
      36 [ +  + ][ +  - ]:       1850 :         S2N_ERROR_IF(i == 0, S2N_ERR_RECORD_LIMIT);
      37                 :            : 
      38                 :            :         /* seq[i] wrapped, so let it carry */
      39                 :       1850 :     }
      40                 :            : 
      41                 :    4031119 :     return 0;
      42                 :    4031120 : }
      43                 :            : 
      44                 :            : int s2n_sequence_number_to_uint64(struct s2n_blob *sequence_number, uint64_t *output)
      45                 :      49614 : {
      46 [ -  + ][ #  # ]:      49614 :     POSIX_ENSURE_REF(sequence_number);
      47                 :            : 
      48                 :      49614 :     uint8_t shift = 0;
      49                 :      49614 :     *output = 0;
      50                 :            : 
      51         [ +  + ]:     446526 :     for (uint32_t i = sequence_number->size; i > 0; i--) {
      52                 :     396912 :         *output += ((uint64_t) sequence_number->data[i - 1]) << shift;
      53                 :     396912 :         shift += SEQUENCE_NUMBER_POWER;
      54                 :     396912 :     }
      55                 :      49614 :     return S2N_SUCCESS;
      56                 :      49614 : }

Generated by: LCOV version 1.14