Debian Codegraph @

crt/s2n/crypto/s2n_sequence.c

raw symbols SCIP
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 #define SEQUENCE_NUMBER_POWER 8
23
24 int s2n_increment_sequence_number(struct s2n_blob *sequence_number)
25 {
26 for (uint32_t j = sequence_number->size; j > 0; j--) {
27 uint32_t i = j - 1;
28 sequence_number->data[i] += 1;
29 if (sequence_number->data[i]) {
30 break;
31 }
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 S2N_ERROR_IF(i == 0, S2N_ERR_RECORD_LIMIT);
37
38 /* seq[i] wrapped, so let it carry */
39 }
40
41 return 0;
42 }
43
44 int s2n_sequence_number_to_uint64(struct s2n_blob *sequence_number, uint64_t *output)
45 {
46 POSIX_ENSURE_REF(sequence_number);
47
48 uint8_t shift = 0;
49 *output = 0;
50
51 for (uint32_t i = sequence_number->size; i > 0; i--) {
52 *output += ((uint64_t) sequence_number->data[i - 1]) << shift;
53 shift += SEQUENCE_NUMBER_POWER;
54 }
55 return S2N_SUCCESS;
56 }