Debian Codegraph @

crt/s2n/utils/s2n_blob.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 "utils/s2n_blob.h"
17
18 #include <ctype.h>
19 #include <string.h>
20
21 #include "api/s2n.h"
22 #include "error/s2n_errno.h"
23 #include "utils/s2n_safety.h"
24
25 S2N_RESULT s2n_blob_validate(const struct s2n_blob *b)
26 {
27 RESULT_ENSURE_REF(b);
28 RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->size == 0), S2N_ERR_SAFETY);
29 RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->allocated == 0), S2N_ERR_SAFETY);
30 RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable == 0, b->allocated == 0), S2N_ERR_SAFETY);
31 RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable == 1, b->allocated > 0 || b->size == 0), S2N_ERR_SAFETY);
32 RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable != 0, b->size <= b->allocated), S2N_ERR_SAFETY);
33 RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->allocated), S2N_ERR_SAFETY);
34 RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->size), S2N_ERR_SAFETY);
35 return S2N_RESULT_OK;
36 }
37
38 /**
39 * Initialize a blob to reference some data.
40 *
41 * `b` will not free `data`. The caller is responsible for making sure that
42 * `data` outlives `b`.
43 */
44 int s2n_blob_init(struct s2n_blob *b, uint8_t *data, uint32_t size)
45 {
46 POSIX_ENSURE_REF(b);
47 POSIX_ENSURE(S2N_MEM_IS_READABLE(data, size), S2N_ERR_SAFETY);
48 *b = (struct s2n_blob){ .data = data, .size = size, .allocated = 0, .growable = 0 };
49 POSIX_POSTCONDITION(s2n_blob_validate(b));
50 return S2N_SUCCESS;
51 }
52
53 int s2n_blob_zero(struct s2n_blob *b)
54 {
55 POSIX_PRECONDITION(s2n_blob_validate(b));
56 POSIX_CHECKED_MEMSET(b->data, 0, S2N_MAX(b->allocated, b->size));
57 POSIX_POSTCONDITION(s2n_blob_validate(b));
58 return S2N_SUCCESS;
59 }
60
61 /**
62 * Set `slice` to reference some portion of `b`.
63 *
64 * The caller is responsible for ensuring that the data pointed to by `b` outlives
65 * `slice`.
66 */
67 int s2n_blob_slice(const struct s2n_blob *b, struct s2n_blob *slice, uint32_t offset, uint32_t size)
68 {
69 POSIX_PRECONDITION(s2n_blob_validate(b));
70 POSIX_PRECONDITION(s2n_blob_validate(slice));
71
72 uint32_t slice_size = 0;
73 POSIX_GUARD(s2n_add_overflow(offset, size, &slice_size));
74 POSIX_ENSURE(b->size >= slice_size, S2N_ERR_SIZE_MISMATCH);
75 slice->data = (b->data) ? (b->data + offset) : NULL;
76 slice->size = size;
77 slice->growable = 0;
78 slice->allocated = 0;
79
80 POSIX_POSTCONDITION(s2n_blob_validate(slice));
81 return S2N_SUCCESS;
82 }
83
84 int s2n_blob_char_to_lower(struct s2n_blob *b)
85 {
86 POSIX_PRECONDITION(s2n_blob_validate(b));
87 for (size_t i = 0; i < b->size; i++) {
88 b->data[i] = tolower(b->data[i]);
89 }
90 POSIX_POSTCONDITION(s2n_blob_validate(b));
91 return S2N_SUCCESS;
92 }