sha256.h 5.3 KB

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