Debian Codegraph @

crt/s2n/tests/cbmc/include/cbmc_proof/cbmc_utils.h

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"). You may not use
5 * this file except in compliance with the License. A copy of the License is
6 * located at
7 *
8 * http://aws.amazon.com/apache2.0/
9 *
10 * or in the "license" file accompanying this file. This file is distributed on an
11 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 * implied. See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #pragma once
17
18 #include <cbmc_proof/nondet.h>
19 #include <crypto/s2n_hash.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include "stuffer/s2n_stuffer.h"
23 #include "utils/s2n_blob.h"
24
25 #define IMPLIES(a, b) (!(a) || (b))
26 #define CBMC_ENSURE_REF(cond) \
27 do { \
28 if (!(cond)) { return; } \
29 } while (0)
30
31 struct store_byte_from_buffer {
32 size_t idx;
33 uint8_t byte;
34 };
35
36 /**
37 * Reference counted keys (EVP_PKEY and EC_KEY) store the reference count
38 * within the key itself, thus making it potentially unavailable after the
39 * key get deallocated.
40 * In the `rc_keys_from_evp_pkey_ctx`, we store the reference count outside of the key,
41 * so we can assert properties on them regardless of whether keys was deallocated or not.
42 */
43 struct rc_keys_from_evp_pkey_ctx {
44 EVP_PKEY *pkey;
45 int pkey_refs;
46 EC_KEY *pkey_eckey;
47 int pkey_eckey_refs;
48 };
49
50 /**
51 * In the `rc_keys_from_hash_state`, we store two `rc_keys_from_evp_pkey_ctx` objects:
52 * one for the `evp` field and another for `evp_md5_secondary` field.
53 */
54 struct rc_keys_from_hash_state {
55 struct rc_keys_from_evp_pkey_ctx evp;
56 struct rc_keys_from_evp_pkey_ctx evp_md5;
57 };
58
59 /**
60 * Asserts two s2n_stuffer instances are equivalent, except for read_cursor,
61 * which might change after a read operation. In order to be considered equivalent,
62 * all members (except for read_cursor) from both instances must match, including
63 * all bytes from their underlying blobs (i.e., blob.data field). Prior to using
64 * this function, it is necessary to select a non-deterministic byte from the
65 * rhs s2n_blob instance (use save_byte_from_blob function), so it can properly
66 * assert all bytes from blob.data match.
67 */
68 void assert_stuffer_immutable_fields_after_read(const struct s2n_stuffer *lhs, const struct s2n_stuffer *rhs,
69 const struct store_byte_from_buffer *stored_byte_from_rhs);
70
71 /**
72 * Asserts two s2n_blob instances are equivalent. In order to be considered equivalent,
73 * all members from both instances must match, including all bytes from their underlying
74 * buffers (i.e., *data field). Prior to using this function, it is necessary to select
75 * a non-deterministic byte from the rhs s2n_blob instance (use save_byte_from_blob function),
76 * so it can properly assert all bytes from *data match.
77 */
78 void assert_blob_equivalence(const struct s2n_blob *lhs, const struct s2n_blob *rhs,
79 const struct store_byte_from_buffer *stored_byte_from_rhs);
80
81 /**
82 * Asserts two s2n_stuffer instances are equivalent. In order to be considered equivalent,
83 * all members from both instances must match, including all bytes from their underlying
84 * blobs (i.e., blob.data field). Prior to using this function, it is necessary to select
85 * a non-deterministic byte from the rhs s2n_blob instance (use save_byte_from_blob function),
86 * so it can properly assert all bytes from blob.data match.
87 */
88 void assert_stuffer_equivalence(const struct s2n_stuffer *lhs, const struct s2n_stuffer *rhs,
89 const struct store_byte_from_buffer *stored_byte_from_rhs);
90
91 /**
92 * Asserts whether all bytes from two arrays of same length match.
93 */
94 void assert_bytes_match(const uint8_t *const a, const uint8_t *const b, const size_t len);
95
96 /**
97 * Asserts whether all bytes from an array are equal to c.
98 */
99 void assert_all_bytes_are(const uint8_t *const a, const uint8_t c, const size_t len);
100
101 /**
102 * Asserts whether all bytes from an array are equal to 0.
103 */
104 void assert_all_zeroes(const uint8_t *const a, const size_t len);
105
106 /**
107 * Asserts whether the byte in storage correspond to the byte in the same position in buffer.
108 */
109 void assert_byte_from_buffer_matches(const uint8_t *const buffer, const struct store_byte_from_buffer *const b);
110
111 /**
112 * Asserts whether the byte in storage correspond to the byte in the same position in buffer.
113 */
114 void assert_byte_from_blob_matches(const struct s2n_blob *blob, const struct store_byte_from_buffer *const b);
115
116 /**
117 * Assert that the reference counts in `storage` have decremented by 1 (if they were > 1).
118 * We cannot check anything if the previous reference count was 1, because the key is deallocated in that case.
119 */
120 void assert_rc_decrement_on_evp_pkey_ctx(struct rc_keys_from_evp_pkey_ctx *storage);
121
122 /**
123 * Assert that the reference counts in `storage` have decremented by 1 (if they were > 1).
124 * We cannot check anything if the previous reference count was 1, because the key is deallocated in that case.
125 */
126 void assert_rc_unchanged_on_evp_pkey_ctx(struct rc_keys_from_evp_pkey_ctx *storage);
127
128 /**
129 * Assert that the reference counts in `storage` are unchanged.
130 */
131 void assert_rc_decrement_on_hash_state(struct rc_keys_from_hash_state *storage);
132
133 /**
134 * Assert that the reference counts in `storage` are unchanged.
135 */
136 void assert_rc_unchanged_on_hash_state(struct rc_keys_from_hash_state *storage);
137
138 /**
139 * Save reference-counted keys from an `EVP_PKEY_CTX`.
140 * Afterwards, one can assert that keys have changed as expected (e.g. after `s2n_hash_free`), i.e.,
141 * reference counts have either decreased by 1: see `assert_rc_decrement_on_evp_pkey_ctx`.
142 * Additionally, one can also `free` the keys that are left with non-zero reference counts,
143 * if that's expected, using `free_rc_keys_from_evp_pkey_ctx` for memory leak check to go through.
144 */
145 void save_rc_keys_from_evp_pkey_ctx(const EVP_PKEY_CTX *pctx, struct rc_keys_from_evp_pkey_ctx *storage);
146
147 /**
148 * Save reference-counted keys from an `s2n_hash_state`.
149 * Afterwards, one can assert that keys have changed as expected (e.g. after `s2n_hash_free`), i.e.,
150 * reference counts have either decreased by 1: see `assert_rc_decrement_on_hash_state`.
151 * Additionally, one can also `free` the keys that are left with non-zero reference counts,
152 * if that's expected, using `free_rc_keys_from_hash_state` for memory leak check to go through.
153 */
154 void save_rc_keys_from_hash_state(const struct s2n_hash_state *state, struct rc_keys_from_hash_state *storage);
155
156 /**
157 * Nondeterministically selects a byte from array and stores it into a store_array_list_byte
158 * structure. Afterwards, one can prove using the assert_byte_from_buffer_matches function
159 * whether no byte in the array has changed.
160 */
161 void save_byte_from_array(const uint8_t *const array, const size_t size, struct store_byte_from_buffer *const storage);
162
163 /**
164 * Nondeterministically selects a byte from blob and stores it into a store_array_list_byte
165 * structure. Afterwards, one can prove using the assert_byte_from_blob_matches function
166 * whether no byte in the blob has changed.
167 */
168 void save_byte_from_blob(const struct s2n_blob *blob, struct store_byte_from_buffer *storage);
169
170 /**
171 * Free all ref-counted keys in `storage` that earlier had a reference count != 1.
172 */
173 void free_rc_keys_from_evp_pkey_ctx(struct rc_keys_from_evp_pkey_ctx *pctx);
174
175 /**
176 * Free all ref-counted keys in `storage` that earlier had a reference count != 1.
177 */
178 void free_rc_keys_from_hash_state(struct rc_keys_from_hash_state *storage);
179
180 /**
181 * Standard stub function to compare two items.
182 */
183 int nondet_compare(const void *const a, const void *const b);
184
185 /**
186 * Standard stub function to compare two items.
187 */
188 int uninterpreted_compare(const void *const a, const void *const b);
189
190 /**
191 * Standard stub function to compare two items.
192 */
193 bool nondet_equals(const void *const a, const void *const b);
194
195 /**
196 * Standard stub function to compare two items.
197 * Also enforces uninterpreted_hasher() to be equal for equal values.
198 */
199 bool uninterpreted_equals(const void *const a, const void *const b);
200
201 /**
202 * uninterpreted_equals(), but with an extra assertion that a and b are both not null
203 */
204 bool uninterpreted_equals_assert_inputs_nonnull(const void *const a, const void *const b);
205
206 /**
207 * Standard stub function to hash one item.
208 */
209 uint64_t nondet_hasher(const void *a);
210
211 /**
212 * Standard stub function to hash one item.
213 */
214 uint64_t uninterpreted_hasher(const void *a);
215
216 /**
217 * Standard stub function of a predicate.
218 */
219 bool uninterpreted_predicate_fn(uint8_t value);
220
221 /**
222 * Non-deterministically set initialized (in s2n_mem) to true.
223 */
224 void nondet_s2n_mem_init();