sha1.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* sha1.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 sha1.c
  18. * \author Daniel Otte
  19. * \date 2006-10-08
  20. * \license GPLv3 or later
  21. * \brief SHA-1 implementation.
  22. *
  23. */
  24. #include <string.h> /* memcpy & co */
  25. #include <stdint.h>
  26. #include <crypto/sha1.h>
  27. #ifdef DEBUG
  28. # undef DEBUG
  29. #endif
  30. #define LITTLE_ENDIAN
  31. /********************************************************************************************************/
  32. /**
  33. * \brief initialises given SHA-1 context
  34. *
  35. */
  36. void sha1_init(sha1_ctx_t *state){
  37. state->h[0] = 0x67452301;
  38. state->h[1] = 0xefcdab89;
  39. state->h[2] = 0x98badcfe;
  40. state->h[3] = 0x10325476;
  41. state->h[4] = 0xc3d2e1f0;
  42. state->length = 0;
  43. }
  44. /********************************************************************************************************/
  45. /* some helping functions */
  46. static const
  47. uint32_t rotl32(uint32_t n, uint8_t bits){
  48. return ((n<<bits) | (n>>(32-bits)));
  49. }
  50. /*
  51. static const
  52. uint32_t change_endian32(uint32_t x){
  53. return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8));
  54. }
  55. */
  56. /* three SHA-1 inner functions */
  57. const
  58. uint32_t ch(uint32_t x, uint32_t y, uint32_t z){
  59. return ((x&y)^((~x)&z));
  60. }
  61. const
  62. uint32_t maj(uint32_t x, uint32_t y, uint32_t z){
  63. return ((x&y)^(x&z)^(y&z));
  64. }
  65. const
  66. uint32_t parity(uint32_t x, uint32_t y, uint32_t z){
  67. return ((x^y)^z);
  68. }
  69. /********************************************************************************************************/
  70. /**
  71. * \brief "add" a block to the hash
  72. * This is the core function of the hash algorithm. To understand how it's working
  73. * and what thoese variables do, take a look at FIPS-182. This is an "alternativ" implementation
  74. */
  75. #define MASK 0x0000000f
  76. typedef const uint32_t (*pf_t)(uint32_t x, uint32_t y, uint32_t z);
  77. static
  78. void load_endian32_changed(uint8_t* dest, uint8_t* src, uint16_t words){
  79. while(words--){
  80. *dest++ = src[3];
  81. *dest++ = src[2];
  82. *dest++ = src[1];
  83. *dest++ = src[0];
  84. src += 4;
  85. }
  86. }
  87. void sha1_nextBlock (sha1_ctx_t *state, const void* block){
  88. uint32_t a[5];
  89. uint32_t w[16];
  90. uint32_t temp;
  91. uint8_t t,s,fi, fib;
  92. pf_t f[] = {ch,parity,maj,parity};
  93. uint32_t k[4]={ 0x5a827999,
  94. 0x6ed9eba1,
  95. 0x8f1bbcdc,
  96. 0xca62c1d6};
  97. /* load the w array (changing the endian and so) */
  98. load_endian32_changed((uint8_t*)w, (uint8_t*)block, 16);
  99. #if DEBUG
  100. uint8_t dbgi;
  101. for(dbgi=0; dbgi<16; ++dbgi){
  102. cli_putstr("\r\nBlock:");
  103. cli_hexdump(&dbgi, 1);
  104. cli_putc(':');
  105. cli_hexdump(&(w[dbgi]) ,4);
  106. }
  107. #endif
  108. /* load the state */
  109. memcpy(a, state->h, 5*sizeof(uint32_t));
  110. /* the fun stuff */
  111. for(fi=0,fib=0,t=0; t<=79; ++t){
  112. s = t & MASK;
  113. if(t>=16){
  114. w[s] = rotl32( w[(s+13)&MASK] ^ w[(s+8)&MASK] ^
  115. w[(s+ 2)&MASK] ^ w[s] ,1);
  116. }
  117. uint32_t dtemp;
  118. temp = rotl32(a[0],5) + (dtemp=f[fi](a[1],a[2],a[3])) + a[4] + k[fi] + w[s];
  119. memmove(&(a[1]), &(a[0]), 4*sizeof(uint32_t)); /* e=d; d=c; c=b; b=a; */
  120. a[0] = temp;
  121. a[2] = rotl32(a[2],30); /* we might also do rotr32(c,2) */
  122. fib++;
  123. if(fib==20){
  124. fib=0;
  125. fi = (fi+1)%4;
  126. }
  127. }
  128. /* update the state */
  129. for(t=0; t<5; ++t){
  130. state->h[t] += a[t];
  131. }
  132. state->length += 512;
  133. }
  134. /********************************************************************************************************/
  135. void sha1_lastBlock(sha1_ctx_t *state, const void* block, uint16_t length){
  136. uint8_t lb[SHA1_BLOCK_BYTES]; /* local block */
  137. while(length>=SHA1_BLOCK_BITS){
  138. sha1_nextBlock(state, block);
  139. length -= SHA1_BLOCK_BITS;
  140. block = (uint8_t*)block + SHA1_BLOCK_BYTES;
  141. }
  142. state->length += length;
  143. memset(lb, 0, SHA1_BLOCK_BYTES);
  144. memcpy (lb, block, (length+7)>>3);
  145. /* set the final one bit */
  146. lb[length>>3] |= 0x80>>(length & 0x07);
  147. if (length>512-64-1){ /* not enouth space for 64bit length value */
  148. sha1_nextBlock(state, lb);
  149. state->length -= 512;
  150. memset(lb, 0, SHA1_BLOCK_BYTES);
  151. }
  152. /* store the 64bit length value */
  153. #if defined LITTLE_ENDIAN
  154. /* this is now rolled up */
  155. uint8_t i;
  156. for (i=0; i<8; ++i){
  157. lb[56+i] = ((uint8_t*)&(state->length))[7-i];
  158. }
  159. #elif defined BIG_ENDIAN
  160. *((uint64_t)&(lb[56])) = state->length;
  161. #endif
  162. sha1_nextBlock(state, lb);
  163. }
  164. /********************************************************************************************************/
  165. void sha1_ctx2hash (void *dest, sha1_ctx_t *state){
  166. #if defined LITTLE_ENDIAN
  167. load_endian32_changed((uint8_t*)dest, (uint8_t*)state->h, 5);
  168. #elif BIG_ENDIAN
  169. if (dest != state->h)
  170. memcpy(dest, state->h, SHA1_HASH_BITS/8);
  171. #else
  172. # error unsupported endian type!
  173. #endif
  174. }
  175. /********************************************************************************************************/
  176. /**
  177. *
  178. *
  179. */
  180. void sha1 (void *dest, const void* msg, uint32_t length){
  181. sha1_ctx_t s;
  182. sha1_init(&s);
  183. while(length & (~0x0001ff)){ /* length>=512 */
  184. sha1_nextBlock(&s, msg);
  185. msg = (uint8_t*)msg + SHA1_BLOCK_BITS/8; /* increment pointer to next block */
  186. length -= SHA1_BLOCK_BITS;
  187. }
  188. sha1_lastBlock(&s, msg, length);
  189. sha1_ctx2hash(dest, &s);
  190. }