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 <stdbool.h>
20 
21 #include <openssl/md5.h>
22 #include <openssl/sha.h>
23 
24 #include "crypto/s2n_evp.h"
25 
26 #define S2N_MAX_DIGEST_LEN SHA512_DIGEST_LENGTH
27 
28 typedef enum {
29     S2N_HASH_NONE=0,
30     S2N_HASH_MD5,
31     S2N_HASH_SHA1,
32     S2N_HASH_SHA224,
33     S2N_HASH_SHA256,
34     S2N_HASH_SHA384,
35     S2N_HASH_SHA512,
36     S2N_HASH_MD5_SHA1,
37     /* Don't add any hash algorithms below S2N_HASH_SENTINEL */
38     S2N_HASH_SENTINEL
39 } s2n_hash_algorithm;
40 
41 /* The low_level_digest stores all OpenSSL structs that are alg-specific to be used with OpenSSL's low-level hash API's. */
42 union s2n_hash_low_level_digest {
43     MD5_CTX md5;
44     SHA_CTX sha1;
45     SHA256_CTX sha224;
46     SHA256_CTX sha256;
47     SHA512_CTX sha384;
48     SHA512_CTX sha512;
49     struct {
50         MD5_CTX md5;
51         SHA_CTX sha1;
52     } md5_sha1;
53 };
54 
55 /* The evp_digest stores all OpenSSL structs to be used with OpenSSL's EVP hash API's. */
56 struct s2n_hash_evp_digest {
57     struct s2n_evp_digest evp;
58     /* Always store a secondary evp_digest to allow resetting a hash_state to MD5_SHA1 from another alg. */
59     struct s2n_evp_digest evp_md5_secondary;
60 };
61 
62 /* s2n_hash_state stores the s2n_hash implementation being used (low-level or EVP),
63  * the hash algorithm being used at the time, and either low_level or high_level (EVP) OpenSSL digest structs.
64  */
65 struct s2n_hash_state {
66     const struct s2n_hash *hash_impl;
67     s2n_hash_algorithm alg;
68     uint8_t is_ready_for_input;
69     uint64_t currently_in_hash;
70     union {
71         union s2n_hash_low_level_digest low_level;
72         struct s2n_hash_evp_digest high_level;
73     } digest;
74 };
75 
76 /* The s2n hash implementation is abstracted to allow for separate implementations, using
77  * either OpenSSL's low-level algorithm-specific API's or OpenSSL's EVP API's.
78  */
79 struct s2n_hash {
80     int (*alloc) (struct s2n_hash_state *state);
81     int (*allow_md5_for_fips) (struct s2n_hash_state *state);
82     int (*init) (struct s2n_hash_state *state, s2n_hash_algorithm alg);
83     int (*update) (struct s2n_hash_state *state, const void *data, uint32_t size);
84     int (*digest) (struct s2n_hash_state *state, void *out, uint32_t size);
85     int (*copy) (struct s2n_hash_state *to, struct s2n_hash_state *from);
86     int (*reset) (struct s2n_hash_state *state);
87     int (*free) (struct s2n_hash_state *state);
88 };
89 
90 extern int s2n_hash_digest_size(s2n_hash_algorithm alg, uint8_t *out);
91 extern int s2n_hash_block_size(s2n_hash_algorithm alg, uint64_t *block_size);
92 extern bool s2n_hash_is_available(s2n_hash_algorithm alg);
93 extern int s2n_hash_is_ready_for_input(struct s2n_hash_state *state);
94 extern int s2n_hash_new(struct s2n_hash_state *state);
95 S2N_RESULT s2n_hash_state_validate(struct s2n_hash_state *state);
96 extern int s2n_hash_allow_md5_for_fips(struct s2n_hash_state *state);
97 extern int s2n_hash_init(struct s2n_hash_state *state, s2n_hash_algorithm alg);
98 extern int s2n_hash_update(struct s2n_hash_state *state, const void *data, uint32_t size);
99 extern int s2n_hash_digest(struct s2n_hash_state *state, void *out, uint32_t size);
100 extern int s2n_hash_copy(struct s2n_hash_state *to, struct s2n_hash_state *from);
101 extern int s2n_hash_reset(struct s2n_hash_state *state);
102 extern int s2n_hash_free(struct s2n_hash_state *state);
103 extern int s2n_hash_get_currently_in_hash_total(struct s2n_hash_state *state, uint64_t *out);
104 extern int s2n_hash_const_time_get_currently_in_hash_block(struct s2n_hash_state *state, uint64_t *out);
105