| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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); |
| 89 |
} |
| 90 |
|
| 91 |
int s2n_hash_update(struct s2n_hash_state *state, const void *data, uint32_t size) |
| 92 |
{ |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
__VERIFIER_assert(size >= 0); |
| 102 |
__VERIFIER_assert(size <= MAX_SIZE); |
| 103 |
__VERIFIER_assert(state->currently_in_hash_block < BLOCK_SIZE); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 127 |
|
| 128 |
const int MARKER_BYTE_LENGTH = 1; |
| 129 |
|
| 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 |
} |