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 #pragma once 17 18 #include <stdint.h> 19 #include <openssl/sha.h> 20 #include <openssl/md5.h> 21 22 #include "crypto/s2n_evp.h" 23 24 #define S2N_MAX_DIGEST_LEN SHA512_DIGEST_LENGTH 25 26 typedef enum { 27 S2N_HASH_NONE, 28 S2N_HASH_MD5, 29 S2N_HASH_SHA1, 30 S2N_HASH_SHA224, 31 S2N_HASH_SHA256, 32 S2N_HASH_SHA384, 33 S2N_HASH_SHA512, 34 S2N_HASH_MD5_SHA1, 35 /* Don't add any hash algorithms below S2N_HASH_SENTINEL */ 36 S2N_HASH_SENTINEL 37 } s2n_hash_algorithm; 38 39 struct s2n_hash_state { 40 s2n_hash_algorithm alg; 41 int currently_in_hash_block; 42 }; 43 44 /* SHA1 45 * These fields were determined from the SHA specification, augmented by 46 * analyzing SHA implementations. 47 * PER_BLOCK_COST is the cost of a compression round. Pessimistically assume 48 * it is 1000 cycles/block, which is worse than real implementations (larger 49 * numbers here make lucky13 leakages look worse), and hence large is safer. 50 * PER_BYTE_COST is the cost of memcopy one byte that is already in cache, 51 * to a location already in cache. 52 */ 53 enum { 54 PER_BLOCK_COST = 1000, 55 PER_BYTE_COST = 1, 56 BLOCK_SIZE = 64, 57 LENGTH_FIELD_SIZE = 8, 58 DIGEST_SIZE = 20 59 }; 60 61 #define MAX_SIZE 1024 62 63 enum { 64 SUCCESS = 0, 65 FAILURE = -1 66 }; 67 68 extern int s2n_hash_digest_size(s2n_hash_algorithm alg, uint8_t *out); 69 extern int s2n_hash_new(struct s2n_hash_state *state); 70 S2N_RESULT s2n_hash_state_validate(struct s2n_hash_state *state); 71 extern int s2n_hash_init(struct s2n_hash_state *state, s2n_hash_algorithm alg); 72 extern int s2n_hash_update(struct s2n_hash_state *state, const void *data, uint32_t size); 73 extern int s2n_hash_digest(struct s2n_hash_state *state, void *out, uint32_t size); 74 extern int s2n_hash_copy(struct s2n_hash_state *to, struct s2n_hash_state *from); 75 extern int s2n_hash_reset(struct s2n_hash_state *state); 76 extern int s2n_hash_free(struct s2n_hash_state *state); 77 extern int s2n_hash_get_currently_in_hash_total(struct s2n_hash_state *state, uint64_t *out); 78