Debian Codegraph @

crt/s2n/tests/sidetrail/working/s2n-cbc/stubs/s2n_hash.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 <stdint.h>
17 #include <stdlib.h>
18 #include <smack.h>
19 #include "crypto/s2n_hash.h"
20 #include "error/s2n_errno.h"
21 #include "ct-verif.h"
22 #include "sidetrail.h"
23 #include "utils/s2n_safety.h"
24
25 int s2n_hash_digest_size(s2n_hash_algorithm alg, uint8_t *out)
26 {
27 switch (alg) {
28 case S2N_HASH_NONE: *out = 0; break;
29 case S2N_HASH_MD5: *out = MD5_DIGEST_LENGTH; break;
30 case S2N_HASH_SHA1: *out = SHA_DIGEST_LENGTH; break;
31 case S2N_HASH_SHA224: *out = SHA224_DIGEST_LENGTH; break;
32 case S2N_HASH_SHA256: *out = SHA256_DIGEST_LENGTH; break;
33 case S2N_HASH_SHA384: *out = SHA384_DIGEST_LENGTH; break;
34 case S2N_HASH_SHA512: *out = SHA512_DIGEST_LENGTH; break;
35 case S2N_HASH_MD5_SHA1: *out = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH; break;
36 default:
37 POSIX_BAIL(S2N_ERR_HASH_INVALID_ALGORITHM);
38 }
39 return 0;
40 }
41
42 int s2n_hash_new(struct s2n_hash_state *state)
43 {
44 return SUCCESS;
45 }
46
47 S2N_RESULT s2n_hash_state_validate(struct s2n_hash_state *state)
48 {
49 return S2N_RESULT_OK;
50 }
51
52 int s2n_hash_init(struct s2n_hash_state *state, s2n_hash_algorithm alg)
53 {
54 __VERIFIER_ASSUME_LEAKAGE(0);
55 state->alg = alg;
56 state->currently_in_hash_block = 0;
57 return SUCCESS;
58 }
59
60 int num_blocks(int numBytes) {
61 /* Using div and mod directly in the stubs turned out to have significant runtime cost
62 * because the backend SMT solver does not handle these non-linear operations well.
63 * Instead, hardcode the div function.
64 * This trades off speed for generality because we have to assert that no input is larger
65 * the given bound. Padding length cannot be more than 256, (4 SHA1 blocks). Choosing a
66 * bound much larger than this ensures that for any padding size we can see the effect on
67 * the packet.
68 */
69 __VERIFIER_ASSUME_LEAKAGE(0);
70 if (numBytes < 1*BLOCK_SIZE) {BENIGN;return 0;}
71 if (numBytes < 2*BLOCK_SIZE) return 1;
72 if (numBytes < 3*BLOCK_SIZE) return 2;
73 if (numBytes < 4*BLOCK_SIZE) return 3;
74 if (numBytes < 5*BLOCK_SIZE) return 4;
75 if (numBytes < 6*BLOCK_SIZE) return 5;
76 if (numBytes < 7*BLOCK_SIZE) return 6;
77 if (numBytes < 8*BLOCK_SIZE) return 7;
78 if (numBytes < 9*BLOCK_SIZE) return 8;
79 if (numBytes < 10*BLOCK_SIZE) return 9;
80 if (numBytes < 11*BLOCK_SIZE) return 10;
81 if (numBytes < 12*BLOCK_SIZE) return 11;
82 if (numBytes < 13*BLOCK_SIZE) return 12;
83 if (numBytes < 14*BLOCK_SIZE) return 13;
84 if (numBytes < 15*BLOCK_SIZE) return 14;
85 if (numBytes < 16*BLOCK_SIZE) return 15;
86 if (numBytes < 17*BLOCK_SIZE) return 16;
87 if (numBytes == 17*BLOCK_SIZE) return 17;
88 __VERIFIER_assert(0);/* Unreachable */
89 }
90
91 int s2n_hash_update(struct s2n_hash_state *state, const void *data, uint32_t size)
92 {
93
94 /* The __VERIFIER_assert statements give better performance but don't add to our current spec.
95 * In particular, Boogie is bad about reestablishing invariants on values that have been put
96 * into memory, then read back out. Adding the asserts triggers Boogie to relearn the
97 * invariant properties.
98 * The proof should hold in their absense (albeit much more slowly).
99 */
100 /* cppcheck-suppress unsignedPositive */
101 __VERIFIER_assert(size >= 0);
102 __VERIFIER_assert(size <= MAX_SIZE);
103 __VERIFIER_assert(state->currently_in_hash_block < BLOCK_SIZE);
104
105 /* We model a hash function as having two forms of leakage:
106 * A per-byte-cost, which represents the cost of moving the information into the hash bufer
107 * A per-block-cost, which represents the cost of compressing one hash-block using the hash fn
108 * As discussed in the README, __VERIFIER_ASSUME_LEAKAGE allows us to annotate these
109 * timing costs into the stub
110 */
111 __VERIFIER_ASSUME_LEAKAGE(PER_BYTE_COST * size);
112
113 state->currently_in_hash_block += size;
114 int num_filled_blocks = num_blocks(state->currently_in_hash_block);
115 __VERIFIER_ASSUME_LEAKAGE(num_filled_blocks * PER_BLOCK_COST);
116
117 state->currently_in_hash_block = state->currently_in_hash_block - num_filled_blocks*BLOCK_SIZE;
118 __VERIFIER_assert(state->currently_in_hash_block < BLOCK_SIZE);
119
120 return SUCCESS;
121 }
122
123 int s2n_hash_digest(struct s2n_hash_state *state, void *out, uint32_t size)
124 {
125 __VERIFIER_ASSUME_LEAKAGE(0);
126 /* All the leakage comes from the hash_update we do once we've updated the size fields */
127
128 const int MARKER_BYTE_LENGTH = 1;
129 /* append the bit '1' to the message e.g. by adding 0x80 if message length is a multiple of 8 bits. */
130 uint32_t min_bytes_to_add = MARKER_BYTE_LENGTH;
131 min_bytes_to_add += LENGTH_FIELD_SIZE;
132
133 int bytes_to_add;
134 if(state->currently_in_hash_block + min_bytes_to_add <= BLOCK_SIZE){
135 BENIGN;
136 bytes_to_add = BLOCK_SIZE - state->currently_in_hash_block;
137 } else {
138 bytes_to_add = BLOCK_SIZE + (BLOCK_SIZE - state->currently_in_hash_block);
139 }
140
141 s2n_hash_update(state, NULL, bytes_to_add);
142 return SUCCESS;
143 }
144
145 int s2n_hash_copy(struct s2n_hash_state *to, struct s2n_hash_state *from)
146 {
147 __VERIFIER_ASSUME_LEAKAGE(0);
148 to->alg = from->alg;
149 to->currently_in_hash_block = from->currently_in_hash_block;
150 return SUCCESS;
151 }
152
153 int s2n_hash_reset(struct s2n_hash_state *state)
154 {
155 __VERIFIER_ASSUME_LEAKAGE(0);
156 state->currently_in_hash_block = 0;
157 return SUCCESS;
158 }
159
160 int s2n_hash_free(struct s2n_hash_state *state)
161 {
162 return SUCCESS;
163 }
164
165 int s2n_hash_get_currently_in_hash_total(struct s2n_hash_state *state, uint64_t *out)
166 {
167 *out = state->currently_in_hash_block;
168 return SUCCESS;
169 }