Debian Codegraph @

crt/s2n/crypto/s2n_hash.h

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 #pragma once
17
18 #include <openssl/md5.h>
19 #include <openssl/sha.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22
23 #include "crypto/s2n_evp.h"
24
25 #define S2N_MAX_DIGEST_LEN SHA512_DIGEST_LENGTH
26
27 typedef enum {
28 S2N_HASH_NONE = 0,
29 S2N_HASH_MD5,
30 S2N_HASH_SHA1,
31 S2N_HASH_SHA224,
32 S2N_HASH_SHA256,
33 S2N_HASH_SHA384,
34 S2N_HASH_SHA512,
35 S2N_HASH_MD5_SHA1,
36 S2N_HASH_SHAKE256_64,
37 /* Don't add any hash algorithms below S2N_HASH_ALGS_COUNT */
38 S2N_HASH_ALGS_COUNT
39 } s2n_hash_algorithm;
40
41 /* The evp_digest stores all OpenSSL structs to be used with OpenSSL's EVP hash API's. */
42 struct s2n_hash_evp_digest {
43 struct s2n_evp_digest evp;
44 /* Always store a secondary evp_digest to allow resetting a hash_state to MD5_SHA1 from another alg. */
45 struct s2n_evp_digest evp_md5_secondary;
46 };
47
48 /* s2n_hash_state stores the state and a reference to the implementation being used.
49 * Currently only EVP hashing is supported, so the only state are EVP_MD contexts.
50 */
51 struct s2n_hash_state {
52 const struct s2n_hash *hash_impl;
53 s2n_hash_algorithm alg;
54 uint8_t is_ready_for_input;
55 uint64_t currently_in_hash;
56 union {
57 struct s2n_hash_evp_digest high_level;
58 } digest;
59 };
60
61 /* The s2n hash implementation is abstracted to allow for separate implementations.
62 * Currently the only implementation uses the EVP APIs.
63 */
64 struct s2n_hash {
65 int (*alloc)(struct s2n_hash_state *state);
66 int (*init)(struct s2n_hash_state *state, s2n_hash_algorithm alg);
67 int (*update)(struct s2n_hash_state *state, const void *data, uint32_t size);
68 int (*digest)(struct s2n_hash_state *state, void *out, uint32_t size);
69 int (*copy)(struct s2n_hash_state *to, struct s2n_hash_state *from);
70 int (*reset)(struct s2n_hash_state *state);
71 int (*free)(struct s2n_hash_state *state);
72 };
73
74 S2N_RESULT s2n_hash_algorithms_init();
75 S2N_RESULT s2n_hash_algorithms_cleanup();
76 bool s2n_hash_use_custom_md5_sha1();
77 bool s2n_hash_supports_shake();
78 const EVP_MD *s2n_hash_alg_to_evp_md(s2n_hash_algorithm alg);
79 int s2n_hash_digest_size(s2n_hash_algorithm alg, uint8_t *out);
80 bool s2n_hash_is_available(s2n_hash_algorithm alg);
81 int s2n_hash_is_ready_for_input(struct s2n_hash_state *state);
82 int s2n_hash_new(struct s2n_hash_state *state);
83 S2N_RESULT s2n_hash_state_validate(struct s2n_hash_state *state);
84 int s2n_hash_init(struct s2n_hash_state *state, s2n_hash_algorithm alg);
85 int s2n_hash_update(struct s2n_hash_state *state, const void *data, uint32_t size);
86 int s2n_hash_digest(struct s2n_hash_state *state, void *out, uint32_t size);
87 int s2n_hash_copy(struct s2n_hash_state *to, struct s2n_hash_state *from);
88 int s2n_hash_reset(struct s2n_hash_state *state);
89 int s2n_hash_free(struct s2n_hash_state *state);
90 int s2n_hash_get_currently_in_hash_total(struct s2n_hash_state *state, uint64_t *out);