Debian Codegraph @

crt/s2n/tests/cbmc/sources/cbmc_utils.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"). 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 #include <assert.h>
17 #include <crypto/s2n_hash.h>
18 #include <utils/s2n_mem.h>
19 #include <utils/s2n_ensure.h>
20 #include <cbmc_proof/cbmc_utils.h>
21
22 /**
23 * CBMC has an internal representation in which each object has an index and a (signed) offset
24 * A buffer cannot be larger than the max size of the offset
25 * The Makefile is expected to set CBMC_OBJECT_BITS to the value of --object-bits
26 */
27 #define MAX_MALLOC (SIZE_MAX >> (CBMC_OBJECT_BITS + 1))
28
29 void assert_stuffer_immutable_fields_after_read(const struct s2n_stuffer *lhs, const struct s2n_stuffer *rhs,
30 const struct store_byte_from_buffer *stored_byte_from_rhs)
31 {
32 /* In order to be equivalent, either both are NULL or both are non-NULL */
33 if (lhs == rhs) {
34 return;
35 } else {
36 assert(lhs && rhs);
37 }
38 assert(lhs->write_cursor == rhs->write_cursor);
39 assert(lhs->high_water_mark == rhs->high_water_mark);
40 assert(lhs->alloced == rhs->alloced);
41 assert(lhs->growable == rhs->growable);
42 assert(lhs->tainted == rhs->tainted);
43 assert_blob_equivalence(&lhs->blob, &rhs->blob, stored_byte_from_rhs);
44 }
45
46 void assert_blob_equivalence(const struct s2n_blob *lhs, const struct s2n_blob *rhs,
47 const struct store_byte_from_buffer *stored_byte_from_rhs)
48 {
49 /* In order to be equivalent, either both are NULL or both are non-NULL */
50 if (lhs == rhs) {
51 return;
52 } else {
53 assert(lhs && rhs);
54 }
55 assert(lhs->size == rhs->size);
56 assert(lhs->allocated == rhs->allocated);
57 assert(lhs->growable == rhs->growable);
58 if (lhs->size > 0) { assert_byte_from_blob_matches(lhs, stored_byte_from_rhs); }
59 }
60
61 void assert_stuffer_equivalence(const struct s2n_stuffer *lhs, const struct s2n_stuffer *rhs,
62 const struct store_byte_from_buffer *stored_byte_from_rhs)
63 {
64 /* In order to be equivalent, either both are NULL or both are non-NULL */
65 if (lhs == rhs) {
66 return;
67 } else {
68 assert(lhs && rhs);
69 }
70 assert(lhs->read_cursor == rhs->read_cursor);
71 assert(lhs->write_cursor == rhs->write_cursor);
72 assert(lhs->high_water_mark == rhs->high_water_mark);
73 assert(lhs->alloced == rhs->alloced);
74 assert(lhs->growable == rhs->growable);
75 assert(lhs->tainted == rhs->tainted);
76 assert_blob_equivalence(&lhs->blob, &rhs->blob, stored_byte_from_rhs);
77 }
78
79 void assert_bytes_match(const uint8_t *const a, const uint8_t *const b, const size_t len)
80 {
81 assert(!a == !b);
82 if (len > 0 && a != NULL && b != NULL) {
83 size_t i;
84 CONTRACT_ASSUME(i < len && len < MAX_MALLOC); /* prevent spurious pointer overflows */
85 assert(a[ i ] == b[ i ]);
86 }
87 }
88
89 void assert_all_bytes_are(const uint8_t *const a, const uint8_t c, const size_t len)
90 {
91 if (len > 0 && a != NULL) {
92 size_t i;
93 CONTRACT_ASSUME(i < len);
94 assert(a[ i ] == c);
95 }
96 }
97
98 void assert_all_zeroes(const uint8_t *const a, const size_t len) { assert_all_bytes_are(a, 0, len); }
99
100 void assert_byte_from_buffer_matches(const uint8_t *const buffer, const struct store_byte_from_buffer *const b)
101 {
102 if (buffer && b) { assert(*(buffer + b->idx) == b->byte); }
103 }
104
105 void assert_byte_from_blob_matches(const struct s2n_blob *blob, const struct store_byte_from_buffer *const b)
106 {
107 if (blob && blob->size) { assert_byte_from_buffer_matches(blob->data, b); }
108 }
109
110 void save_byte_from_array(const uint8_t *const array, const size_t size, struct store_byte_from_buffer *const storage)
111 {
112 if (size > 0 && array && storage) {
113 storage->idx = nondet_size_t();
114 CONTRACT_ASSUME(storage->idx < size);
115 storage->byte = array[ storage->idx ];
116 }
117 }
118
119 void save_byte_from_blob(const struct s2n_blob *blob, struct store_byte_from_buffer *storage)
120 {
121 save_byte_from_array(blob->data, blob->size, storage);
122 }
123
124 int nondet_compare(const void *const a, const void *const b)
125 {
126 assert(a != NULL);
127 assert(b != NULL);
128 return nondet_int();
129 }
130
131 int __CPROVER_uninterpreted_compare(const void *const a, const void *const b);
132 bool __CPROVER_uninterpreted_equals(const void *const a, const void *const b);
133 uint64_t __CPROVER_uninterpreted_hasher(const void *const a);
134
135 int uninterpreted_compare(const void *const a, const void *const b)
136 {
137 assert(a != NULL);
138 assert(b != NULL);
139 int rval = __CPROVER_uninterpreted_compare(a, b);
140 /* Compare is reflexive */
141 CONTRACT_ASSUME(IMPLIES(a == b, rval == 0));
142 /* Compare is anti-symmetric*/
143 CONTRACT_ASSUME(__CPROVER_uninterpreted_compare(b, a) == -rval);
144 /* If two things are equal, their hashes are also equal */
145 if (rval == 0) { CONTRACT_ASSUME(__CPROVER_uninterpreted_hasher(a) == __CPROVER_uninterpreted_hasher(b)); }
146 return rval;
147 }
148
149 bool nondet_equals(const void *const a, const void *const b)
150 {
151 assert(a != NULL);
152 assert(b != NULL);
153 return nondet_bool();
154 }
155
156 /**
157 * Add assumptions that equality is reflexive and symmetric. Don't bother with
158 * transitivity because it doesn't cause any spurious proof failures on hash-table
159 */
160 bool uninterpreted_equals(const void *const a, const void *const b)
161 {
162 bool rval = __CPROVER_uninterpreted_equals(a, b);
163 /* Equals is reflexive */
164 CONTRACT_ASSUME(IMPLIES(a == b, rval));
165 /* Equals is symmetric */
166 CONTRACT_ASSUME(__CPROVER_uninterpreted_equals(b, a) == rval);
167 /* If two things are equal, their hashes are also equal */
168 if (rval) { CONTRACT_ASSUME(__CPROVER_uninterpreted_hasher(a) == __CPROVER_uninterpreted_hasher(b)); }
169 return rval;
170 }
171
172 bool uninterpreted_equals_assert_inputs_nonnull(const void *const a, const void *const b)
173 {
174 assert(a != NULL);
175 assert(b != NULL);
176 return uninterpreted_equals(a, b);
177 }
178
179 uint64_t nondet_hasher(const void *a)
180 {
181 assert(a != NULL);
182 return nondet_uint64_t();
183 }
184
185 /**
186 * Standard stub function to hash one item.
187 */
188 uint64_t uninterpreted_hasher(const void *a)
189 {
190 assert(a != NULL);
191 return __CPROVER_uninterpreted_hasher(a);
192 }
193
194 bool uninterpreted_predicate_fn(uint8_t value);
195
196 void nondet_s2n_mem_init()
197 {
198 if (nondet_bool()) { s2n_mem_init(); }
199 }
200
201 void assert_rc_decrement_on_evp_pkey_ctx(struct rc_keys_from_evp_pkey_ctx *storage)
202 {
203 if (storage->pkey_refs > 1) {
204 assert(storage->pkey->references == storage->pkey_refs - 1);
205 } else if (storage->pkey_refs == 1 && storage->pkey_eckey_refs > 1) {
206 assert(storage->pkey_eckey->references == storage->pkey_eckey_refs - 1);
207 }
208 }
209
210 void assert_rc_unchanged_on_evp_pkey_ctx(struct rc_keys_from_evp_pkey_ctx *storage)
211 {
212 if (storage->pkey) {
213 assert(storage->pkey->references == storage->pkey_refs);
214 }
215 if (storage->pkey_eckey) {
216 assert(storage->pkey_eckey->references == storage->pkey_eckey_refs);
217 }
218 }
219
220 void assert_rc_decrement_on_hash_state(struct rc_keys_from_hash_state *storage)
221 {
222 assert_rc_decrement_on_evp_pkey_ctx(&storage->evp);
223 assert_rc_decrement_on_evp_pkey_ctx(&storage->evp_md5);
224 }
225
226 void assert_rc_unchanged_on_hash_state(struct rc_keys_from_hash_state *storage)
227 {
228 assert_rc_unchanged_on_evp_pkey_ctx(&storage->evp);
229 assert_rc_unchanged_on_evp_pkey_ctx(&storage->evp_md5);
230 }
231
232 void save_abstract_evp_ctx(const EVP_PKEY_CTX *pctx, struct rc_keys_from_evp_pkey_ctx *storage)
233 {
234 storage->pkey = pctx->pkey;
235 if (storage->pkey) {
236 storage->pkey_refs = storage->pkey->references;
237 storage->pkey_eckey = storage->pkey->ec_key;
238 if (storage->pkey_eckey) {
239 storage->pkey_eckey_refs = storage->pkey_eckey->references;
240 }
241 }
242 }
243
244 void save_rc_keys_from_hash_state(const struct s2n_hash_state *state, struct rc_keys_from_hash_state *storage)
245 {
246 storage->evp.pkey = NULL;
247 storage->evp.pkey_eckey = NULL;
248 storage->evp_md5.pkey = NULL;
249 storage->evp_md5.pkey_eckey = NULL;
250 storage->evp.pkey_refs = storage->evp.pkey_eckey_refs = storage->evp_md5.pkey_refs = storage->evp_md5.pkey_eckey_refs = 0;
251
252 if (state) {
253 if (state->digest.high_level.evp.ctx) {
254 if (state->digest.high_level.evp.ctx->pctx) {
255 save_abstract_evp_ctx(state->digest.high_level.evp.ctx->pctx, &storage->evp);
256 }
257 }
258
259 if (state->digest.high_level.evp_md5_secondary.ctx) {
260 if (state->digest.high_level.evp_md5_secondary.ctx->pctx) {
261 save_abstract_evp_ctx(state->digest.high_level.evp_md5_secondary.ctx->pctx, &storage->evp_md5);
262 }
263 }
264 }
265 }
266
267 void free_rc_keys_from_evp_pkey_ctx(struct rc_keys_from_evp_pkey_ctx *pctx)
268 {
269 if (pctx->pkey && pctx->pkey_refs != 1) {
270 pctx->pkey->references = 1;
271 EVP_PKEY_free(pctx->pkey);
272 }
273 if (pctx->pkey_eckey && pctx->pkey_eckey_refs != 1) {
274 pctx->pkey_eckey->references = 1;
275 EC_KEY_free(pctx->pkey_eckey);
276 }
277 }
278
279 void free_rc_keys_from_hash_state(struct rc_keys_from_hash_state *storage)
280 {
281 free_rc_keys_from_evp_pkey_ctx(&storage->evp);
282 free_rc_keys_from_evp_pkey_ctx(&storage->evp_md5);
283 }