/* MIT License1*2* Copyright (c) 2016-2022 INRIA, CMU and Microsoft Corporation3* Copyright (c) 2022-2023 HACL* Contributors4*5* Permission is hereby granted, free of charge, to any person obtaining a copy6* of this software and associated documentation files (the "Software"), to deal7* in the Software without restriction, including without limitation the rights8* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9* copies of the Software, and to permit persons to whom the Software is10* furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included in all13* copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/232425#ifndef __Hacl_Hash_SHA2_H26#define __Hacl_Hash_SHA2_H2728#if defined(__cplusplus)29extern "C" {30#endif3132#include <string.h>33#include "python_hacl_namespaces.h"34#include "krml/types.h"35#include "krml/lowstar_endianness.h"36#include "krml/internal/target.h"3738#include "Hacl_Streaming_Types.h"394041typedef Hacl_Streaming_MD_state_32 Hacl_Streaming_SHA2_state_sha2_224;4243typedef Hacl_Streaming_MD_state_32 Hacl_Streaming_SHA2_state_sha2_256;4445typedef Hacl_Streaming_MD_state_64 Hacl_Streaming_SHA2_state_sha2_384;4647typedef Hacl_Streaming_MD_state_64 Hacl_Streaming_SHA2_state_sha2_512;4849/**50Allocate initial state for the SHA2_256 hash. The state is to be freed by51calling `free_256`.52*/53Hacl_Streaming_MD_state_32 *Hacl_Streaming_SHA2_create_in_256(void);5455/**56Copies the state passed as argument into a newly allocated state (deep copy).57The state is to be freed by calling `free_256`. Cloning the state this way is58useful, for instance, if your control-flow diverges and you need to feed59more (different) data into the hash in each branch.60*/61Hacl_Streaming_MD_state_32 *Hacl_Streaming_SHA2_copy_256(Hacl_Streaming_MD_state_32 *s0);6263/**64Reset an existing state to the initial hash state with empty data.65*/66void Hacl_Streaming_SHA2_init_256(Hacl_Streaming_MD_state_32 *s);6768/**69Feed an arbitrary amount of data into the hash. This function returns 0 for70success, or 1 if the combined length of all of the data passed to `update_256`71(since the last call to `init_256`) exceeds 2^61-1 bytes.7273This function is identical to the update function for SHA2_224.74*/75Hacl_Streaming_Types_error_code76Hacl_Streaming_SHA2_update_256(77Hacl_Streaming_MD_state_32 *p,78uint8_t *input,79uint32_t input_len80);8182/**83Write the resulting hash into `dst`, an array of 32 bytes. The state remains84valid after a call to `finish_256`, meaning the user may feed more data into85the hash via `update_256`. (The finish_256 function operates on an internal copy of86the state and therefore does not invalidate the client-held state `p`.)87*/88void Hacl_Streaming_SHA2_finish_256(Hacl_Streaming_MD_state_32 *p, uint8_t *dst);8990/**91Free a state allocated with `create_in_256`.9293This function is identical to the free function for SHA2_224.94*/95void Hacl_Streaming_SHA2_free_256(Hacl_Streaming_MD_state_32 *s);9697/**98Hash `input`, of len `input_len`, into `dst`, an array of 32 bytes.99*/100void Hacl_Streaming_SHA2_hash_256(uint8_t *input, uint32_t input_len, uint8_t *dst);101102Hacl_Streaming_MD_state_32 *Hacl_Streaming_SHA2_create_in_224(void);103104void Hacl_Streaming_SHA2_init_224(Hacl_Streaming_MD_state_32 *s);105106Hacl_Streaming_Types_error_code107Hacl_Streaming_SHA2_update_224(108Hacl_Streaming_MD_state_32 *p,109uint8_t *input,110uint32_t input_len111);112113/**114Write the resulting hash into `dst`, an array of 28 bytes. The state remains115valid after a call to `finish_224`, meaning the user may feed more data into116the hash via `update_224`.117*/118void Hacl_Streaming_SHA2_finish_224(Hacl_Streaming_MD_state_32 *p, uint8_t *dst);119120void Hacl_Streaming_SHA2_free_224(Hacl_Streaming_MD_state_32 *p);121122/**123Hash `input`, of len `input_len`, into `dst`, an array of 28 bytes.124*/125void Hacl_Streaming_SHA2_hash_224(uint8_t *input, uint32_t input_len, uint8_t *dst);126127Hacl_Streaming_MD_state_64 *Hacl_Streaming_SHA2_create_in_512(void);128129/**130Copies the state passed as argument into a newly allocated state (deep copy).131The state is to be freed by calling `free_512`. Cloning the state this way is132useful, for instance, if your control-flow diverges and you need to feed133more (different) data into the hash in each branch.134*/135Hacl_Streaming_MD_state_64 *Hacl_Streaming_SHA2_copy_512(Hacl_Streaming_MD_state_64 *s0);136137void Hacl_Streaming_SHA2_init_512(Hacl_Streaming_MD_state_64 *s);138139/**140Feed an arbitrary amount of data into the hash. This function returns 0 for141success, or 1 if the combined length of all of the data passed to `update_512`142(since the last call to `init_512`) exceeds 2^125-1 bytes.143144This function is identical to the update function for SHA2_384.145*/146Hacl_Streaming_Types_error_code147Hacl_Streaming_SHA2_update_512(148Hacl_Streaming_MD_state_64 *p,149uint8_t *input,150uint32_t input_len151);152153/**154Write the resulting hash into `dst`, an array of 64 bytes. The state remains155valid after a call to `finish_512`, meaning the user may feed more data into156the hash via `update_512`. (The finish_512 function operates on an internal copy of157the state and therefore does not invalidate the client-held state `p`.)158*/159void Hacl_Streaming_SHA2_finish_512(Hacl_Streaming_MD_state_64 *p, uint8_t *dst);160161/**162Free a state allocated with `create_in_512`.163164This function is identical to the free function for SHA2_384.165*/166void Hacl_Streaming_SHA2_free_512(Hacl_Streaming_MD_state_64 *s);167168/**169Hash `input`, of len `input_len`, into `dst`, an array of 64 bytes.170*/171void Hacl_Streaming_SHA2_hash_512(uint8_t *input, uint32_t input_len, uint8_t *dst);172173Hacl_Streaming_MD_state_64 *Hacl_Streaming_SHA2_create_in_384(void);174175void Hacl_Streaming_SHA2_init_384(Hacl_Streaming_MD_state_64 *s);176177Hacl_Streaming_Types_error_code178Hacl_Streaming_SHA2_update_384(179Hacl_Streaming_MD_state_64 *p,180uint8_t *input,181uint32_t input_len182);183184/**185Write the resulting hash into `dst`, an array of 48 bytes. The state remains186valid after a call to `finish_384`, meaning the user may feed more data into187the hash via `update_384`.188*/189void Hacl_Streaming_SHA2_finish_384(Hacl_Streaming_MD_state_64 *p, uint8_t *dst);190191void Hacl_Streaming_SHA2_free_384(Hacl_Streaming_MD_state_64 *p);192193/**194Hash `input`, of len `input_len`, into `dst`, an array of 48 bytes.195*/196void Hacl_Streaming_SHA2_hash_384(uint8_t *input, uint32_t input_len, uint8_t *dst);197198#if defined(__cplusplus)199}200#endif201202#define __Hacl_Hash_SHA2_H_DEFINED203#endif204205206