hashwrap.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef EPID_MEMBER_TINY_MATH_HASHWRAP_H_
  17. #define EPID_MEMBER_TINY_MATH_HASHWRAP_H_
  18. /// Decleration of hash wrap function
  19. /*! \file */
  20. #include <stddef.h>
  21. #include "epid/common/types.h"
  22. #include "epid/member/tiny/math/mathtypes.h"
  23. #define SHA512_SUPPORT
  24. #define SHA256_SUPPORT
  25. #ifdef SHA512_SUPPORT
  26. #include "epid/member/tiny/math/sha512.h"
  27. #endif
  28. #ifdef SHA256_SUPPORT
  29. #include "epid/member/tiny/math/sha256.h"
  30. #endif
  31. /// Sha Digest Element
  32. typedef union sha_digest {
  33. #ifdef SHA512_SUPPORT
  34. uint8_t sha512_digest[SHA512_DIGEST_SIZE]; ///< Support digest for sha512
  35. #endif
  36. #ifdef SHA256_SUPPORT
  37. uint8_t sha256_digest[SHA256_DIGEST_SIZE]; ///< Support digest for sha256
  38. #endif
  39. uint8_t digest[1]; ///< Pointer to digest
  40. } sha_digest;
  41. /// Tiny Sha wrapper Element
  42. typedef struct tiny_sha {
  43. union {
  44. #ifdef SHA512_SUPPORT
  45. sha512_state sha512s; ///< The state of sha512 if supported
  46. #endif
  47. #ifdef SHA256_SUPPORT
  48. sha256_state sha256s; ///< The state of sha256 if supported
  49. #endif
  50. } sha_state_t; ///< Store state of hash algorithm
  51. HashAlg hash_alg; ///< The hash algorithem which selected
  52. } tiny_sha;
  53. /// Initializes the hash state
  54. /*!
  55. \param[in] sha_type
  56. Type of hash algorithm
  57. \param[in,out] s
  58. The hash state to initialize.
  59. */
  60. void tinysha_init(HashAlg sha_type, tiny_sha* s);
  61. /// Hashes data into state using a chosen hash algorithm
  62. /*!
  63. \param[in,out] s
  64. The hash state. Must be non-null or behavior is undefined.
  65. \param[in] data
  66. The data to hash into s.
  67. \param[in] data_length
  68. The size of data in bytes.
  69. */
  70. void tinysha_update(tiny_sha* s, void const* data, size_t data_length);
  71. /// Computes the hash algorithm in the digest buffer
  72. /*!
  73. \param[out] digest
  74. The computed digest. Must be non-null or behavior is undefined.
  75. \param[in] s
  76. The hash state. Must be non-null or behavior is undefined.
  77. */
  78. void tinysha_final(unsigned char* digest, tiny_sha* s);
  79. /// Returns size of digest depending on hash algorithm
  80. /*!
  81. \param[in] s
  82. The hash state. Must be non-null or behavior is undefined.
  83. \returns
  84. Size of digest
  85. */
  86. size_t tinysha_digest_size(tiny_sha* s);
  87. #endif // EPID_MEMBER_TINY_MATH_HASHWRAP_H_