sha256.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* sha256.h */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file sha256.h
  18. * \author Daniel Otte
  19. * \date 2006-05-16
  20. * \license GPLv3 or later
  21. *
  22. */
  23. #ifndef SHA256_H_
  24. #define SHA256_H_
  25. #include <stdint.h>
  26. #include <crypto/sha2_small_common.h>
  27. /** \def SHA256_HASH_BITS
  28. * defines the size of a SHA-256 hash value in bits
  29. */
  30. /** \def SHA256_HASH_BYTES
  31. * defines the size of a SHA-256 hash value in bytes
  32. */
  33. /** \def SHA256_BLOCK_BITS
  34. * defines the size of a SHA-256 input block in bits
  35. */
  36. /** \def SHA256_BLOCK_BYTES
  37. * defines the size of a SHA-256 input block in bytes
  38. */
  39. #define SHA256_HASH_BITS 256
  40. #define SHA256_HASH_BYTES (SHA256_HASH_BITS/8)
  41. #define SHA256_BLOCK_BITS 512
  42. #define SHA256_BLOCK_BYTES (SHA256_BLOCK_BITS/8)
  43. /** \typedef sha256_ctx_t
  44. * \brief SHA-256 context type
  45. *
  46. * A variable of this type may hold the state of a SHA-256 hashing process
  47. */
  48. typedef sha2_small_common_ctx_t sha256_ctx_t;
  49. /** \fn void sha256_init(sha256_ctx_t *state)
  50. * \brief initialize a SHA-256 context
  51. *
  52. * This function sets a ::sha256_ctx_t to the initial values for hashing.
  53. * \param state pointer to the SHA-256 hashing context
  54. */
  55. void sha256_init(sha256_ctx_t *state);
  56. /** \fn void sha256_nextBlock (sha256_ctx_t* state, const void* block)
  57. * \brief update the context with a given block
  58. *
  59. * This function updates the SHA-256 hash context by processing the given block
  60. * of fixed length.
  61. * \param state pointer to the SHA-256 hash context
  62. * \param block pointer to the block of fixed length (512 bit = 64 byte)
  63. */
  64. void sha256_nextBlock (sha256_ctx_t* state, const void* block);
  65. /** \fn void sha256_lastBlock(sha256_ctx_t* state, const void* block, uint16_t length_b)
  66. * \brief finalize the context with the given block
  67. *
  68. * This function finalizes the SHA-256 hash context by processing the given block
  69. * of variable length.
  70. * \param state pointer to the SHA-256 hash context
  71. * \param block pointer to the block of fixed length (512 bit = 64 byte)
  72. * \param length_b the length of the block in bits
  73. */
  74. void sha256_lastBlock(sha256_ctx_t* state, const void* block, uint16_t length_b);
  75. /** \fn void sha256_ctx2hash(sha256_hash_t* dest, const sha256_ctx_t* state)
  76. * \brief convert the hash state into the hash value
  77. * This function reads the context and writes the hash value to the destination
  78. * \param dest pointer to the location where the hash value should be written
  79. * \param state pointer to the SHA-256 hash context
  80. */
  81. void sha256_ctx2hash(void* dest, const sha256_ctx_t* state);
  82. /** \fn void sha256(sha256_hash_t* dest, const void* msg, uint32_t length_b)
  83. * \brief simple SHA-256 hashing function for direct hashing
  84. *
  85. * This function automatically hashes a given message of arbitary length with
  86. * the SHA-256 hashing algorithm.
  87. * \param dest pointer to the location where the hash value is going to be written to
  88. * \param msg pointer to the message thats going to be hashed
  89. * \param length_b length of the message in bits
  90. */
  91. void sha256(void* dest, const void* msg, uint32_t length_b);
  92. #endif /*SHA256_H_*/