sha256.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* sha256.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 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.c
  18. * \author Daniel Otte
  19. * \date 16.05.2006
  20. *
  21. * \par License:
  22. * GPL
  23. *
  24. * \brief SHA-256 implementation.
  25. *
  26. *
  27. */
  28. #include <stdint.h>
  29. #include <string.h> /* for memcpy, memmove, memset */
  30. #include <crypto/sha2_small_common.h>
  31. #include <crypto/sha256.h>
  32. #define LITTLE_ENDIAN
  33. #if defined LITTLE_ENDIAN
  34. #elif defined BIG_ENDIAN
  35. #else
  36. #error specify endianess!!!
  37. #endif
  38. /*************************************************************************/
  39. const
  40. uint32_t sha256_init_vector[]={
  41. 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  42. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 };
  43. /*************************************************************************/
  44. /**
  45. * \brief \c sh256_init initialises a sha256 context for hashing.
  46. * \c sh256_init c initialises the given sha256 context for hashing
  47. * @param state pointer to a sha256 context
  48. * @return none
  49. */
  50. void sha256_init(sha256_ctx_t *state){
  51. state->length=0;
  52. memcpy(state->h, sha256_init_vector, 8*4);
  53. }
  54. /*************************************************************************/
  55. void sha256_nextBlock (sha256_ctx_t *state, const void* block){
  56. sha2_small_common_nextBlock(state, block);
  57. }
  58. /*************************************************************************/
  59. void sha256_lastBlock (sha256_ctx_t *state, const void* block, uint16_t length_b){
  60. sha2_small_common_lastBlock(state, block, length_b);
  61. }
  62. /*************************************************************************/
  63. /**
  64. * \brief function to process the last block being hashed
  65. * @param state Pointer to the context in which this block should be processed.
  66. * @param block Pointer to the message wich should be hashed.
  67. * @param length is the length of only THIS block in BITS not in bytes!
  68. * bits are big endian, meaning high bits come first.
  69. * if you have a message with bits at the end, the byte must be padded with zeros
  70. */
  71. /*************************************************************************/
  72. /*
  73. * length in bits!
  74. */
  75. void sha256(void* dest, const void* msg, uint32_t length_b){ /* length could be choosen longer but this is for µC */
  76. sha256_ctx_t s;
  77. sha256_init(&s);
  78. while(length_b >= SHA256_BLOCK_BITS){
  79. sha256_nextBlock(&s, msg);
  80. msg = (uint8_t*)msg + SHA256_BLOCK_BITS/8;
  81. length_b -= SHA256_BLOCK_BITS;
  82. }
  83. sha256_lastBlock(&s, msg, length_b);
  84. sha256_ctx2hash(dest,&s);
  85. }
  86. /*************************************************************************/
  87. void sha256_ctx2hash(void* dest, const sha256_ctx_t *state){
  88. #if defined LITTLE_ENDIAN
  89. uint8_t i, j, *s=(uint8_t*)(state->h);
  90. i=8;
  91. do{
  92. j=3;
  93. do{
  94. *((uint8_t*)dest) = s[j];
  95. dest = (uint8_t*)dest + 1;
  96. }while(j--);
  97. s += 4;
  98. }while(--i);
  99. #elif BIG_ENDIAN
  100. memcpy(dest, state->h, 32);
  101. #else
  102. # error unsupported endian type!
  103. #endif
  104. }