Hacl_Hash_SHA2.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifndef __Hacl_Hash_SHA2_H
  25. #define __Hacl_Hash_SHA2_H
  26. #if defined(__cplusplus)
  27. extern "C" {
  28. #endif
  29. #include <string.h>
  30. #include "python_hacl_namespaces.h"
  31. #include "krml/types.h"
  32. #include "krml/lowstar_endianness.h"
  33. #include "krml/internal/target.h"
  34. #include "Hacl_Streaming_Types.h"
  35. typedef Hacl_Streaming_MD_state_32 Hacl_Hash_SHA2_state_t_224;
  36. typedef Hacl_Streaming_MD_state_32 Hacl_Hash_SHA2_state_t_256;
  37. typedef Hacl_Streaming_MD_state_64 Hacl_Hash_SHA2_state_t_384;
  38. typedef Hacl_Streaming_MD_state_64 Hacl_Hash_SHA2_state_t_512;
  39. /**
  40. Allocate initial state for the SHA2_256 hash. The state is to be freed by
  41. calling `free_256`.
  42. */
  43. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_256(void);
  44. /**
  45. Copies the state passed as argument into a newly allocated state (deep copy).
  46. The state is to be freed by calling `free_256`. Cloning the state this way is
  47. useful, for instance, if your control-flow diverges and you need to feed
  48. more (different) data into the hash in each branch.
  49. */
  50. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_copy_256(Hacl_Streaming_MD_state_32 *state);
  51. /**
  52. Reset an existing state to the initial hash state with empty data.
  53. */
  54. void Hacl_Hash_SHA2_reset_256(Hacl_Streaming_MD_state_32 *state);
  55. /**
  56. Feed an arbitrary amount of data into the hash. This function returns 0 for
  57. success, or 1 if the combined length of all of the data passed to `update_256`
  58. (since the last call to `reset_256`) exceeds 2^61-1 bytes.
  59. This function is identical to the update function for SHA2_224.
  60. */
  61. Hacl_Streaming_Types_error_code
  62. Hacl_Hash_SHA2_update_256(
  63. Hacl_Streaming_MD_state_32 *state,
  64. uint8_t *input,
  65. uint32_t input_len
  66. );
  67. /**
  68. Write the resulting hash into `output`, an array of 32 bytes. The state remains
  69. valid after a call to `digest_256`, meaning the user may feed more data into
  70. the hash via `update_256`. (The digest_256 function operates on an internal copy of
  71. the state and therefore does not invalidate the client-held state `p`.)
  72. */
  73. void Hacl_Hash_SHA2_digest_256(Hacl_Streaming_MD_state_32 *state, uint8_t *output);
  74. /**
  75. Free a state allocated with `malloc_256`.
  76. This function is identical to the free function for SHA2_224.
  77. */
  78. void Hacl_Hash_SHA2_free_256(Hacl_Streaming_MD_state_32 *state);
  79. /**
  80. Hash `input`, of len `input_len`, into `output`, an array of 32 bytes.
  81. */
  82. void Hacl_Hash_SHA2_hash_256(uint8_t *output, uint8_t *input, uint32_t input_len);
  83. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_224(void);
  84. void Hacl_Hash_SHA2_reset_224(Hacl_Streaming_MD_state_32 *state);
  85. Hacl_Streaming_Types_error_code
  86. Hacl_Hash_SHA2_update_224(
  87. Hacl_Streaming_MD_state_32 *state,
  88. uint8_t *input,
  89. uint32_t input_len
  90. );
  91. /**
  92. Write the resulting hash into `output`, an array of 28 bytes. The state remains
  93. valid after a call to `digest_224`, meaning the user may feed more data into
  94. the hash via `update_224`.
  95. */
  96. void Hacl_Hash_SHA2_digest_224(Hacl_Streaming_MD_state_32 *state, uint8_t *output);
  97. void Hacl_Hash_SHA2_free_224(Hacl_Streaming_MD_state_32 *state);
  98. /**
  99. Hash `input`, of len `input_len`, into `output`, an array of 28 bytes.
  100. */
  101. void Hacl_Hash_SHA2_hash_224(uint8_t *output, uint8_t *input, uint32_t input_len);
  102. Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_512(void);
  103. /**
  104. Copies the state passed as argument into a newly allocated state (deep copy).
  105. The state is to be freed by calling `free_512`. Cloning the state this way is
  106. useful, for instance, if your control-flow diverges and you need to feed
  107. more (different) data into the hash in each branch.
  108. */
  109. Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_copy_512(Hacl_Streaming_MD_state_64 *state);
  110. void Hacl_Hash_SHA2_reset_512(Hacl_Streaming_MD_state_64 *state);
  111. /**
  112. Feed an arbitrary amount of data into the hash. This function returns 0 for
  113. success, or 1 if the combined length of all of the data passed to `update_512`
  114. (since the last call to `reset_512`) exceeds 2^125-1 bytes.
  115. This function is identical to the update function for SHA2_384.
  116. */
  117. Hacl_Streaming_Types_error_code
  118. Hacl_Hash_SHA2_update_512(
  119. Hacl_Streaming_MD_state_64 *state,
  120. uint8_t *input,
  121. uint32_t input_len
  122. );
  123. /**
  124. Write the resulting hash into `output`, an array of 64 bytes. The state remains
  125. valid after a call to `digest_512`, meaning the user may feed more data into
  126. the hash via `update_512`. (The digest_512 function operates on an internal copy of
  127. the state and therefore does not invalidate the client-held state `p`.)
  128. */
  129. void Hacl_Hash_SHA2_digest_512(Hacl_Streaming_MD_state_64 *state, uint8_t *output);
  130. /**
  131. Free a state allocated with `malloc_512`.
  132. This function is identical to the free function for SHA2_384.
  133. */
  134. void Hacl_Hash_SHA2_free_512(Hacl_Streaming_MD_state_64 *state);
  135. /**
  136. Hash `input`, of len `input_len`, into `output`, an array of 64 bytes.
  137. */
  138. void Hacl_Hash_SHA2_hash_512(uint8_t *output, uint8_t *input, uint32_t input_len);
  139. Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_384(void);
  140. void Hacl_Hash_SHA2_reset_384(Hacl_Streaming_MD_state_64 *state);
  141. Hacl_Streaming_Types_error_code
  142. Hacl_Hash_SHA2_update_384(
  143. Hacl_Streaming_MD_state_64 *state,
  144. uint8_t *input,
  145. uint32_t input_len
  146. );
  147. /**
  148. Write the resulting hash into `output`, an array of 48 bytes. The state remains
  149. valid after a call to `digest_384`, meaning the user may feed more data into
  150. the hash via `update_384`.
  151. */
  152. void Hacl_Hash_SHA2_digest_384(Hacl_Streaming_MD_state_64 *state, uint8_t *output);
  153. void Hacl_Hash_SHA2_free_384(Hacl_Streaming_MD_state_64 *state);
  154. /**
  155. Hash `input`, of len `input_len`, into `output`, an array of 48 bytes.
  156. */
  157. void Hacl_Hash_SHA2_hash_384(uint8_t *output, uint8_t *input, uint32_t input_len);
  158. #if defined(__cplusplus)
  159. }
  160. #endif
  161. #define __Hacl_Hash_SHA2_H_DEFINED
  162. #endif