sha512.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  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. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /// Interface to a SHA-512 implementation.
  17. /*! \file */
  18. #ifndef EPID_MEMBER_TINY_MATH_SHA512_H_
  19. #define EPID_MEMBER_TINY_MATH_SHA512_H_
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. /// block size
  23. #define SHA512_BLOCK_SIZE (128)
  24. /// digest size
  25. #define SHA512_DIGEST_SIZE (64)
  26. /// number of words in SHA state
  27. #define SHA512_DIGEST_WORDS (8)
  28. /// The SHA state
  29. /// \cond
  30. typedef struct sha512_state {
  31. uint64_t iv[SHA512_DIGEST_WORDS];
  32. uint64_t bits_hashed_low;
  33. uint64_t bits_hashed_high;
  34. unsigned char leftover[SHA512_BLOCK_SIZE];
  35. unsigned int leftover_offset;
  36. } sha512_state;
  37. /// \endcond
  38. /// Initializes the hash state
  39. /*!
  40. \param[in,out] s
  41. The hash state to initialize.
  42. */
  43. void tinysha512_init(sha512_state* s);
  44. /// Hashes data into state using SHA-512
  45. /*!
  46. \warning
  47. The state buffer 'leftover' is left in memory after processing. If
  48. your application intends to have sensitive data in this buffer,
  49. remember to erase it after the data has been processed
  50. \param[in,out] s
  51. The hash state. Must be non-null or behavior is undefined.
  52. \param[in] data
  53. The data to hash into s.
  54. \param[in] data_length
  55. The size of data in bytes.
  56. */
  57. void tinysha512_update(sha512_state* s, void const* data, size_t data_length);
  58. /// Computes the SHA-512 hash in the digest buffer
  59. /*!
  60. \note Assumes SHA512_DIGEST_SIZE bytes are available to accept the
  61. digest.
  62. \warning
  63. The state buffer 'leftover' is left in memory after processing. If
  64. your application intends to have sensitive data in this buffer,
  65. remember to erase it after the data has been processed
  66. \param[out] digest
  67. The computed digest. Must be non-null or behavior is undefined.
  68. \param[in] s
  69. The hash state. Must be non-null or behavior is undefined.
  70. */
  71. void tinysha512_final(unsigned char* digest, sha512_state* s);
  72. #endif // EPID_MEMBER_TINY_MATH_SHA512_H_