sha1-internal.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * SHA1 hash implementation and interface functions
  3. * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "sha1.h"
  17. #include "sha1_i.h"
  18. #include "md5.h"
  19. #include "crypto.h"
  20. typedef struct SHA1Context SHA1_CTX;
  21. void SHA1Transform(u32 state[5], const unsigned char buffer[64]);
  22. /**
  23. * sha1_vector - SHA-1 hash for data vector
  24. * @num_elem: Number of elements in the data vector
  25. * @addr: Pointers to the data areas
  26. * @len: Lengths of the data blocks
  27. * @mac: Buffer for the hash
  28. * Returns: 0 on success, -1 of failure
  29. */
  30. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  31. {
  32. SHA1_CTX ctx;
  33. size_t i;
  34. SHA1Init(&ctx);
  35. for (i = 0; i < num_elem; i++)
  36. SHA1Update(&ctx, addr[i], len[i]);
  37. SHA1Final(mac, &ctx);
  38. return 0;
  39. }
  40. /* ===== start - public domain SHA1 implementation ===== */
  41. /*
  42. SHA-1 in C
  43. By Steve Reid <sreid@sea-to-sky.net>
  44. 100% Public Domain
  45. -----------------
  46. Modified 7/98
  47. By James H. Brown <jbrown@burgoyne.com>
  48. Still 100% Public Domain
  49. Corrected a problem which generated improper hash values on 16 bit machines
  50. Routine SHA1Update changed from
  51. void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int
  52. len)
  53. to
  54. void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned
  55. long len)
  56. The 'len' parameter was declared an int which works fine on 32 bit machines.
  57. However, on 16 bit machines an int is too small for the shifts being done
  58. against
  59. it. This caused the hash function to generate incorrect values if len was
  60. greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
  61. Since the file IO in main() reads 16K at a time, any file 8K or larger would
  62. be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
  63. "a"s).
  64. I also changed the declaration of variables i & j in SHA1Update to
  65. unsigned long from unsigned int for the same reason.
  66. These changes should make no difference to any 32 bit implementations since
  67. an
  68. int and a long are the same size in those environments.
  69. --
  70. I also corrected a few compiler warnings generated by Borland C.
  71. 1. Added #include <process.h> for exit() prototype
  72. 2. Removed unused variable 'j' in SHA1Final
  73. 3. Changed exit(0) to return(0) at end of main.
  74. ALL changes I made can be located by searching for comments containing 'JHB'
  75. -----------------
  76. Modified 8/98
  77. By Steve Reid <sreid@sea-to-sky.net>
  78. Still 100% public domain
  79. 1- Removed #include <process.h> and used return() instead of exit()
  80. 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
  81. 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
  82. -----------------
  83. Modified 4/01
  84. By Saul Kravitz <Saul.Kravitz@celera.com>
  85. Still 100% PD
  86. Modified to run on Compaq Alpha hardware.
  87. -----------------
  88. Modified 4/01
  89. By Jouni Malinen <j@w1.fi>
  90. Minor changes to match the coding style used in Dynamics.
  91. Modified September 24, 2004
  92. By Jouni Malinen <j@w1.fi>
  93. Fixed alignment issue in SHA1Transform when SHA1HANDSOFF is defined.
  94. */
  95. /*
  96. Test Vectors (from FIPS PUB 180-1)
  97. "abc"
  98. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  99. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  100. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  101. A million repetitions of "a"
  102. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  103. */
  104. #define SHA1HANDSOFF
  105. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  106. /* blk0() and blk() perform the initial expand. */
  107. /* I got the idea of expanding during the round function from SSLeay */
  108. #ifndef WORDS_BIGENDIAN
  109. #define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \
  110. (rol(block->l[i], 8) & 0x00FF00FF))
  111. #else
  112. #define blk0(i) block->l[i]
  113. #endif
  114. #define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ \
  115. block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
  116. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  117. #define R0(v,w,x,y,z,i) \
  118. z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
  119. w = rol(w, 30);
  120. #define R1(v,w,x,y,z,i) \
  121. z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
  122. w = rol(w, 30);
  123. #define R2(v,w,x,y,z,i) \
  124. z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30);
  125. #define R3(v,w,x,y,z,i) \
  126. z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
  127. w = rol(w, 30);
  128. #define R4(v,w,x,y,z,i) \
  129. z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
  130. w=rol(w, 30);
  131. #ifdef VERBOSE /* SAK */
  132. void SHAPrintContext(SHA1_CTX *context, char *msg)
  133. {
  134. printf("%s (%d,%d) %x %x %x %x %x\n",
  135. msg,
  136. context->count[0], context->count[1],
  137. context->state[0],
  138. context->state[1],
  139. context->state[2],
  140. context->state[3],
  141. context->state[4]);
  142. }
  143. #endif
  144. /* Hash a single 512-bit block. This is the core of the algorithm. */
  145. void SHA1Transform(u32 state[5], const unsigned char buffer[64])
  146. {
  147. u32 a, b, c, d, e;
  148. typedef union {
  149. unsigned char c[64];
  150. u32 l[16];
  151. } CHAR64LONG16;
  152. CHAR64LONG16* block;
  153. #ifdef SHA1HANDSOFF
  154. CHAR64LONG16 workspace;
  155. block = &workspace;
  156. os_memcpy(block, buffer, 64);
  157. #else
  158. block = (CHAR64LONG16 *) buffer;
  159. #endif
  160. /* Copy context->state[] to working vars */
  161. a = state[0];
  162. b = state[1];
  163. c = state[2];
  164. d = state[3];
  165. e = state[4];
  166. /* 4 rounds of 20 operations each. Loop unrolled. */
  167. R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  168. R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  169. R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  170. R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  171. R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  172. R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  173. R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  174. R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  175. R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  176. R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  177. R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  178. R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  179. R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  180. R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  181. R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  182. R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  183. R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  184. R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  185. R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  186. R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  187. /* Add the working vars back into context.state[] */
  188. state[0] += a;
  189. state[1] += b;
  190. state[2] += c;
  191. state[3] += d;
  192. state[4] += e;
  193. /* Wipe variables */
  194. a = b = c = d = e = 0;
  195. #ifdef SHA1HANDSOFF
  196. os_memset(block, 0, 64);
  197. #endif
  198. }
  199. /* SHA1Init - Initialize new context */
  200. void SHA1Init(SHA1_CTX* context)
  201. {
  202. /* SHA1 initialization constants */
  203. context->state[0] = 0x67452301;
  204. context->state[1] = 0xEFCDAB89;
  205. context->state[2] = 0x98BADCFE;
  206. context->state[3] = 0x10325476;
  207. context->state[4] = 0xC3D2E1F0;
  208. context->count[0] = context->count[1] = 0;
  209. }
  210. /* Run your data through this. */
  211. void SHA1Update(SHA1_CTX* context, const void *_data, u32 len)
  212. {
  213. u32 i, j;
  214. const unsigned char *data = _data;
  215. #ifdef VERBOSE
  216. SHAPrintContext(context, "before");
  217. #endif
  218. j = (context->count[0] >> 3) & 63;
  219. if ((context->count[0] += len << 3) < (len << 3))
  220. context->count[1]++;
  221. context->count[1] += (len >> 29);
  222. if ((j + len) > 63) {
  223. os_memcpy(&context->buffer[j], data, (i = 64-j));
  224. SHA1Transform(context->state, context->buffer);
  225. for ( ; i + 63 < len; i += 64) {
  226. SHA1Transform(context->state, &data[i]);
  227. }
  228. j = 0;
  229. }
  230. else i = 0;
  231. os_memcpy(&context->buffer[j], &data[i], len - i);
  232. #ifdef VERBOSE
  233. SHAPrintContext(context, "after ");
  234. #endif
  235. }
  236. /* Add padding and return the message digest. */
  237. void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
  238. {
  239. u32 i;
  240. unsigned char finalcount[8];
  241. for (i = 0; i < 8; i++) {
  242. finalcount[i] = (unsigned char)
  243. ((context->count[(i >= 4 ? 0 : 1)] >>
  244. ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
  245. }
  246. SHA1Update(context, (unsigned char *) "\200", 1);
  247. while ((context->count[0] & 504) != 448) {
  248. SHA1Update(context, (unsigned char *) "\0", 1);
  249. }
  250. SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform()
  251. */
  252. for (i = 0; i < 20; i++) {
  253. digest[i] = (unsigned char)
  254. ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) &
  255. 255);
  256. }
  257. /* Wipe variables */
  258. i = 0;
  259. os_memset(context->buffer, 0, 64);
  260. os_memset(context->state, 0, 20);
  261. os_memset(context->count, 0, 8);
  262. os_memset(finalcount, 0, 8);
  263. }
  264. /* ===== end - public domain SHA1 implementation ===== */