sha256.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* sha256.h - TinyCrypt interface to a SHA-256 implementation */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * @file
  33. * @brief Interface to a SHA-256 implementation.
  34. *
  35. * Overview: SHA-256 is a NIST approved cryptographic hashing algorithm
  36. * specified in FIPS 180. A hash algorithm maps data of arbitrary
  37. * size to data of fixed length.
  38. *
  39. * Security: SHA-256 provides 128 bits of security against collision attacks
  40. * and 256 bits of security against pre-image attacks. SHA-256 does
  41. * NOT behave like a random oracle, but it can be used as one if
  42. * the string being hashed is prefix-free encoded before hashing.
  43. *
  44. * Usage: 1) call tc_sha256_init to initialize a struct
  45. * tc_sha256_state_struct before hashing a new string.
  46. *
  47. * 2) call tc_sha256_update to hash the next string segment;
  48. * tc_sha256_update can be called as many times as needed to hash
  49. * all of the segments of a string; the order is important.
  50. *
  51. * 3) call tc_sha256_final to out put the digest from a hashing
  52. * operation.
  53. */
  54. #ifndef __TC_SHA256_H__
  55. #define __TC_SHA256_H__
  56. #include <stddef.h>
  57. #include <stdint.h>
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. #define TC_SHA256_BLOCK_SIZE (64)
  62. #define TC_SHA256_DIGEST_SIZE (32)
  63. #define TC_SHA256_STATE_BLOCKS (TC_SHA256_DIGEST_SIZE/4)
  64. struct tc_sha256_state_struct {
  65. unsigned int iv[TC_SHA256_STATE_BLOCKS];
  66. uint64_t bits_hashed;
  67. uint8_t leftover[TC_SHA256_BLOCK_SIZE];
  68. size_t leftover_offset;
  69. };
  70. typedef struct tc_sha256_state_struct *TCSha256State_t;
  71. /**
  72. * @brief SHA256 initialization procedure
  73. * Initializes s
  74. * @return returns TC_CRYPTO_SUCCESS (1)
  75. * returns TC_CRYPTO_FAIL (0) if s == NULL
  76. * @param s Sha256 state struct
  77. */
  78. int tc_sha256_init(TCSha256State_t s);
  79. /**
  80. * @brief SHA256 update procedure
  81. * Hashes data_length bytes addressed by data into state s
  82. * @return returns TC_CRYPTO_SUCCESS (1)
  83. * returns TC_CRYPTO_FAIL (0) if:
  84. * s == NULL,
  85. * s->iv == NULL,
  86. * data == NULL
  87. * @note Assumes s has been initialized by tc_sha256_init
  88. * @warning The state buffer 'leftover' is left in memory after processing
  89. * If your application intends to have sensitive data in this
  90. * buffer, remind to erase it after the data has been processed
  91. * @param s Sha256 state struct
  92. * @param data message to hash
  93. * @param datalen length of message to hash
  94. */
  95. int tc_sha256_update (TCSha256State_t s, const uint8_t *data, size_t datalen);
  96. /**
  97. * @brief SHA256 final procedure
  98. * Inserts the completed hash computation into digest
  99. * @return returns TC_CRYPTO_SUCCESS (1)
  100. * returns TC_CRYPTO_FAIL (0) if:
  101. * s == NULL,
  102. * s->iv == NULL,
  103. * digest == NULL
  104. * @note Assumes: s has been initialized by tc_sha256_init
  105. * digest points to at least TC_SHA256_DIGEST_SIZE bytes
  106. * @warning The state buffer 'leftover' is left in memory after processing
  107. * If your application intends to have sensitive data in this
  108. * buffer, remind to erase it after the data has been processed
  109. * @param digest unsigned eight bit integer
  110. * @param Sha256 state struct
  111. */
  112. int tc_sha256_final(uint8_t *digest, TCSha256State_t s);
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif /* __TC_SHA256_H__ */