Debian Codegraph @

crt/s2n/tests/cbmc/proofs/s2n_hash_free/s2n_hash_free_harness.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_fips.h"
17 #include "crypto/s2n_hash.h"
18
19 #include <cbmc_proof/make_common_datastructures.h>
20
21 #include <assert.h>
22
23 void s2n_hash_free_harness()
24 {
25 /* Non-deterministic inputs. */
26 struct s2n_hash_state *state = cbmc_allocate_s2n_hash_state();
27
28 /* Assumptions. */
29 __CPROVER_assume(s2n_result_is_ok(s2n_hash_state_validate(state)));
30
31 struct rc_keys_from_hash_state saved_hash_state;
32 save_rc_keys_from_hash_state(state, &saved_hash_state);
33
34 /* Operation under verification. */
35 assert(s2n_hash_free(state) == S2N_SUCCESS);
36 if (state != NULL) {
37 assert(state->hash_impl->free != NULL);
38 assert(state->digest.high_level.evp.ctx == NULL);
39 assert(state->digest.high_level.evp_md5_secondary.ctx == NULL);
40 assert_rc_decrement_on_hash_state(&saved_hash_state);
41 assert(state->is_ready_for_input == 0);
42 }
43
44 /* Cleanup after expected error cases, for memory leak check. */
45 if (state != NULL) {
46 /* 1. `free` leftover EVP_MD_CTX objects */
47 S2N_EVP_MD_CTX_FREE(state->digest.high_level.evp.ctx);
48 S2N_EVP_MD_CTX_FREE(state->digest.high_level.evp_md5_secondary.ctx);
49
50 /* 2. `free` leftover reference-counted keys (i.e. those with non-zero ref-count),
51 since they are not automatically `free`d until their ref count reaches 0. */
52 free_rc_keys_from_hash_state(&saved_hash_state);
53 }
54 /* 3. free our heap-allocated `state` since `s2n_hash_free` only `free`s the contents. */
55 free(state);
56 }