| 1 |
/* MIT License |
| 2 |
* |
| 3 |
* Copyright (c) 2016-2022 INRIA, CMU and Microsoft Corporation |
| 4 |
* Copyright (c) 2022-2023 HACL* Contributors |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in all |
| 14 |
* copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef Hacl_Hash_SHA2_H |
| 27 |
#define Hacl_Hash_SHA2_H |
| 28 |
|
| 29 |
#if defined(__cplusplus) |
| 30 |
extern "C" { |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <string.h> |
| 34 |
#include "python_hacl_namespaces.h" |
| 35 |
#include "krml/internal/types.h" |
| 36 |
#include "krml/lowstar_endianness.h" |
| 37 |
#include "krml/internal/target.h" |
| 38 |
|
| 39 |
#include "Hacl_Streaming_Types.h" |
| 40 |
|
| 41 |
typedef Hacl_Streaming_MD_state_32 Hacl_Hash_SHA2_state_t_224; |
| 42 |
|
| 43 |
typedef Hacl_Streaming_MD_state_32 Hacl_Hash_SHA2_state_t_256; |
| 44 |
|
| 45 |
typedef Hacl_Streaming_MD_state_64 Hacl_Hash_SHA2_state_t_384; |
| 46 |
|
| 47 |
typedef Hacl_Streaming_MD_state_64 Hacl_Hash_SHA2_state_t_512; |
| 48 |
|
| 49 |
/** |
| 50 |
Allocate initial state for the SHA2_256 hash. The state is to be freed by |
| 51 |
calling `free_256`. |
| 52 |
*/ |
| 53 |
Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_256(void); |
| 54 |
|
| 55 |
/** |
| 56 |
Copies the state passed as argument into a newly allocated state (deep copy). |
| 57 |
The state is to be freed by calling `free_256`. Cloning the state this way is |
| 58 |
useful, for instance, if your control-flow diverges and you need to feed |
| 59 |
more (different) data into the hash in each branch. |
| 60 |
*/ |
| 61 |
Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_copy_256(Hacl_Streaming_MD_state_32 *state); |
| 62 |
|
| 63 |
/** |
| 64 |
Reset an existing state to the initial hash state with empty data. |
| 65 |
*/ |
| 66 |
void Hacl_Hash_SHA2_reset_256(Hacl_Streaming_MD_state_32 *state); |
| 67 |
|
| 68 |
/** |
| 69 |
Feed an arbitrary amount of data into the hash. This function returns 0 for |
| 70 |
success, or 1 if the combined length of all of the data passed to `update_256` |
| 71 |
(since the last call to `reset_256`) exceeds 2^61-1 bytes. |
| 72 |
|
| 73 |
This function is identical to the update function for SHA2_224. |
| 74 |
*/ |
| 75 |
Hacl_Streaming_Types_error_code |
| 76 |
Hacl_Hash_SHA2_update_256( |
| 77 |
Hacl_Streaming_MD_state_32 *state, |
| 78 |
uint8_t *input, |
| 79 |
uint32_t input_len |
| 80 |
); |
| 81 |
|
| 82 |
/** |
| 83 |
Write the resulting hash into `output`, an array of 32 bytes. The state remains |
| 84 |
valid after a call to `digest_256`, meaning the user may feed more data into |
| 85 |
the hash via `update_256`. (The digest_256 function operates on an internal copy of |
| 86 |
the state and therefore does not invalidate the client-held state `p`.) |
| 87 |
*/ |
| 88 |
void Hacl_Hash_SHA2_digest_256(Hacl_Streaming_MD_state_32 *state, uint8_t *output); |
| 89 |
|
| 90 |
/** |
| 91 |
Free a state allocated with `malloc_256`. |
| 92 |
|
| 93 |
This function is identical to the free function for SHA2_224. |
| 94 |
*/ |
| 95 |
void Hacl_Hash_SHA2_free_256(Hacl_Streaming_MD_state_32 *state); |
| 96 |
|
| 97 |
/** |
| 98 |
Hash `input`, of len `input_len`, into `output`, an array of 32 bytes. |
| 99 |
*/ |
| 100 |
void Hacl_Hash_SHA2_hash_256(uint8_t *output, uint8_t *input, uint32_t input_len); |
| 101 |
|
| 102 |
Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_224(void); |
| 103 |
|
| 104 |
void Hacl_Hash_SHA2_reset_224(Hacl_Streaming_MD_state_32 *state); |
| 105 |
|
| 106 |
Hacl_Streaming_Types_error_code |
| 107 |
Hacl_Hash_SHA2_update_224( |
| 108 |
Hacl_Streaming_MD_state_32 *state, |
| 109 |
uint8_t *input, |
| 110 |
uint32_t input_len |
| 111 |
); |
| 112 |
|
| 113 |
/** |
| 114 |
Write the resulting hash into `output`, an array of 28 bytes. The state remains |
| 115 |
valid after a call to `digest_224`, meaning the user may feed more data into |
| 116 |
the hash via `update_224`. |
| 117 |
*/ |
| 118 |
void Hacl_Hash_SHA2_digest_224(Hacl_Streaming_MD_state_32 *state, uint8_t *output); |
| 119 |
|
| 120 |
void Hacl_Hash_SHA2_free_224(Hacl_Streaming_MD_state_32 *state); |
| 121 |
|
| 122 |
/** |
| 123 |
Hash `input`, of len `input_len`, into `output`, an array of 28 bytes. |
| 124 |
*/ |
| 125 |
void Hacl_Hash_SHA2_hash_224(uint8_t *output, uint8_t *input, uint32_t input_len); |
| 126 |
|
| 127 |
Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_512(void); |
| 128 |
|
| 129 |
/** |
| 130 |
Copies the state passed as argument into a newly allocated state (deep copy). |
| 131 |
The state is to be freed by calling `free_512`. Cloning the state this way is |
| 132 |
useful, for instance, if your control-flow diverges and you need to feed |
| 133 |
more (different) data into the hash in each branch. |
| 134 |
*/ |
| 135 |
Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_copy_512(Hacl_Streaming_MD_state_64 *state); |
| 136 |
|
| 137 |
void Hacl_Hash_SHA2_reset_512(Hacl_Streaming_MD_state_64 *state); |
| 138 |
|
| 139 |
/** |
| 140 |
Feed an arbitrary amount of data into the hash. This function returns 0 for |
| 141 |
success, or 1 if the combined length of all of the data passed to `update_512` |
| 142 |
(since the last call to `reset_512`) exceeds 2^125-1 bytes. |
| 143 |
|
| 144 |
This function is identical to the update function for SHA2_384. |
| 145 |
*/ |
| 146 |
Hacl_Streaming_Types_error_code |
| 147 |
Hacl_Hash_SHA2_update_512( |
| 148 |
Hacl_Streaming_MD_state_64 *state, |
| 149 |
uint8_t *input, |
| 150 |
uint32_t input_len |
| 151 |
); |
| 152 |
|
| 153 |
/** |
| 154 |
Write the resulting hash into `output`, an array of 64 bytes. The state remains |
| 155 |
valid after a call to `digest_512`, meaning the user may feed more data into |
| 156 |
the hash via `update_512`. (The digest_512 function operates on an internal copy of |
| 157 |
the state and therefore does not invalidate the client-held state `p`.) |
| 158 |
*/ |
| 159 |
void Hacl_Hash_SHA2_digest_512(Hacl_Streaming_MD_state_64 *state, uint8_t *output); |
| 160 |
|
| 161 |
/** |
| 162 |
Free a state allocated with `malloc_512`. |
| 163 |
|
| 164 |
This function is identical to the free function for SHA2_384. |
| 165 |
*/ |
| 166 |
void Hacl_Hash_SHA2_free_512(Hacl_Streaming_MD_state_64 *state); |
| 167 |
|
| 168 |
/** |
| 169 |
Hash `input`, of len `input_len`, into `output`, an array of 64 bytes. |
| 170 |
*/ |
| 171 |
void Hacl_Hash_SHA2_hash_512(uint8_t *output, uint8_t *input, uint32_t input_len); |
| 172 |
|
| 173 |
Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_384(void); |
| 174 |
|
| 175 |
void Hacl_Hash_SHA2_reset_384(Hacl_Streaming_MD_state_64 *state); |
| 176 |
|
| 177 |
Hacl_Streaming_Types_error_code |
| 178 |
Hacl_Hash_SHA2_update_384( |
| 179 |
Hacl_Streaming_MD_state_64 *state, |
| 180 |
uint8_t *input, |
| 181 |
uint32_t input_len |
| 182 |
); |
| 183 |
|
| 184 |
/** |
| 185 |
Write the resulting hash into `output`, an array of 48 bytes. The state remains |
| 186 |
valid after a call to `digest_384`, meaning the user may feed more data into |
| 187 |
the hash via `update_384`. |
| 188 |
*/ |
| 189 |
void Hacl_Hash_SHA2_digest_384(Hacl_Streaming_MD_state_64 *state, uint8_t *output); |
| 190 |
|
| 191 |
void Hacl_Hash_SHA2_free_384(Hacl_Streaming_MD_state_64 *state); |
| 192 |
|
| 193 |
/** |
| 194 |
Hash `input`, of len `input_len`, into `output`, an array of 48 bytes. |
| 195 |
*/ |
| 196 |
void Hacl_Hash_SHA2_hash_384(uint8_t *output, uint8_t *input, uint32_t input_len); |
| 197 |
|
| 198 |
#if defined(__cplusplus) |
| 199 |
} |
| 200 |
#endif |
| 201 |
|
| 202 |
#define Hacl_Hash_SHA2_H_DEFINED |
| 203 |
#endif /* Hacl_Hash_SHA2_H */ |