sha224.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* sha224.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 sha224.c
  18. * \author Daniel Otte
  19. * \date 16.05.2006
  20. *
  21. * \par License:
  22. * GPL
  23. *
  24. * \brief SHA-224 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/sha224.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 sha224_init_vector[]={
  41. 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
  42. 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
  43. };
  44. /*************************************************************************/
  45. /**
  46. * \brief \c sh224_init initialises a sha224 context for hashing.
  47. * \c sh224_init c initialises the given sha224 context for hashing
  48. * @param state pointer to a sha224 context
  49. * @return none
  50. */
  51. void sha224_init(sha224_ctx_t *state){
  52. state->length=0;
  53. memcpy(state->h, sha224_init_vector, 8*4);
  54. }
  55. /*************************************************************************/
  56. void sha224_nextBlock (sha224_ctx_t *state, const void* block){
  57. sha2_small_common_nextBlock(state, block);
  58. }
  59. /*************************************************************************/
  60. void sha224_lastBlock (sha224_ctx_t *state, const void* block, uint16_t length_b){
  61. sha2_small_common_lastBlock(state, block, length_b);
  62. }
  63. /*************************************************************************/
  64. /**
  65. * \brief function to process the last block being hashed
  66. * @param state Pointer to the context in which this block should be processed.
  67. * @param block Pointer to the message wich should be hashed.
  68. * @param length is the length of only THIS block in BITS not in bytes!
  69. * bits are big endian, meaning high bits come first.
  70. * if you have a message with bits at the end, the byte must be padded with zeros
  71. */
  72. /*************************************************************************/
  73. /*
  74. * length in bits!
  75. */
  76. void sha224(void* dest, const void* msg, uint32_t length_b){ /* length could be choosen longer but this is for µC */
  77. sha224_ctx_t s;
  78. sha224_init(&s);
  79. while(length_b >= SHA224_BLOCK_BITS){
  80. sha224_nextBlock(&s, msg);
  81. msg = (uint8_t*)msg + SHA224_BLOCK_BITS/8;
  82. length_b -= SHA224_BLOCK_BITS;
  83. }
  84. sha224_lastBlock(&s, msg, length_b);
  85. sha224_ctx2hash(dest,&s);
  86. }
  87. /*************************************************************************/
  88. void sha224_ctx2hash(void* dest, const sha224_ctx_t *state){
  89. #if defined LITTLE_ENDIAN
  90. uint8_t i, j, *s=(uint8_t*)(state->h);
  91. i=7;
  92. do{
  93. j=3;
  94. do{
  95. *((uint8_t*)dest) = s[j];
  96. dest = (uint8_t*)dest + 1;
  97. }while(j--);
  98. s += 4;
  99. }while(--i);
  100. #elif BIG_ENDIAN
  101. memcpy(dest, state->h, 28);
  102. #else
  103. # error unsupported endian type!
  104. #endif
  105. }