sha1.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sha1.cc,v 1.3 2001/05/13 05:15:03 jgg Exp $
  4. /* ######################################################################
  5. SHA1 - SHA-1 Secure Hash Algorithm.
  6. This file is a Public Domain wrapper for the Public Domain SHA1
  7. calculation code that is at it's end.
  8. The algorithm was originally implemented by
  9. Steve Reid <sreid@sea-to-sky.net> and later modified by
  10. James H. Brown <jbrown@burgoyne.com>.
  11. Modifications for APT were done by Alfredo K. Kojima and Jason
  12. Gunthorpe.
  13. Still in the public domain.
  14. Test Vectors (from FIPS PUB 180-1)
  15. "abc"
  16. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  17. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  18. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  19. A million repetitions of "a"
  20. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  21. #####################################################################
  22. */
  23. /*}}} */
  24. // Include Files /*{{{*/
  25. #include <config.h>
  26. #include <apt-pkg/sha1.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. /*}}}*/
  30. // SHA1Transform - Alters an existing SHA-1 hash /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* The core of the SHA-1 algorithm. This alters an existing SHA-1 hash to
  33. reflect the addition of 16 longwords of new data. Other routines convert
  34. incoming stream data into 16 long word chunks for this routine */
  35. #define rol(value,bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  36. /* blk0() and blk() perform the initial expand. */
  37. /* I got the idea of expanding during the round function from SSLeay */
  38. #ifndef WORDS_BIGENDIAN
  39. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  40. |(rol(block->l[i],8)&0x00FF00FF))
  41. #else
  42. #define blk0(i) block->l[i]
  43. #endif
  44. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  45. ^block->l[(i+2)&15]^block->l[i&15],1))
  46. /* (R0+R1),R2,R3,R4 are the different operations used in SHA1 */
  47. #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  48. #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  49. #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  50. #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  51. #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  52. static void SHA1Transform(uint32_t state[5],uint8_t const buffer[64])
  53. {
  54. uint32_t a,b,c,d,e;
  55. typedef union
  56. {
  57. uint8_t c[64];
  58. uint32_t l[16];
  59. }
  60. CHAR64LONG16;
  61. CHAR64LONG16 workspace, *block;
  62. block = &workspace;
  63. memcpy(block,buffer,sizeof(workspace));
  64. /* Copy context->state[] to working vars */
  65. a = state[0];
  66. b = state[1];
  67. c = state[2];
  68. d = state[3];
  69. e = state[4];
  70. /* 4 rounds of 20 operations each. Loop unrolled. */
  71. R0(a,b,c,d,e,0);
  72. R0(e,a,b,c,d,1);
  73. R0(d,e,a,b,c,2);
  74. R0(c,d,e,a,b,3);
  75. R0(b,c,d,e,a,4);
  76. R0(a,b,c,d,e,5);
  77. R0(e,a,b,c,d,6);
  78. R0(d,e,a,b,c,7);
  79. R0(c,d,e,a,b,8);
  80. R0(b,c,d,e,a,9);
  81. R0(a,b,c,d,e,10);
  82. R0(e,a,b,c,d,11);
  83. R0(d,e,a,b,c,12);
  84. R0(c,d,e,a,b,13);
  85. R0(b,c,d,e,a,14);
  86. R0(a,b,c,d,e,15);
  87. R1(e,a,b,c,d,16);
  88. R1(d,e,a,b,c,17);
  89. R1(c,d,e,a,b,18);
  90. R1(b,c,d,e,a,19);
  91. R2(a,b,c,d,e,20);
  92. R2(e,a,b,c,d,21);
  93. R2(d,e,a,b,c,22);
  94. R2(c,d,e,a,b,23);
  95. R2(b,c,d,e,a,24);
  96. R2(a,b,c,d,e,25);
  97. R2(e,a,b,c,d,26);
  98. R2(d,e,a,b,c,27);
  99. R2(c,d,e,a,b,28);
  100. R2(b,c,d,e,a,29);
  101. R2(a,b,c,d,e,30);
  102. R2(e,a,b,c,d,31);
  103. R2(d,e,a,b,c,32);
  104. R2(c,d,e,a,b,33);
  105. R2(b,c,d,e,a,34);
  106. R2(a,b,c,d,e,35);
  107. R2(e,a,b,c,d,36);
  108. R2(d,e,a,b,c,37);
  109. R2(c,d,e,a,b,38);
  110. R2(b,c,d,e,a,39);
  111. R3(a,b,c,d,e,40);
  112. R3(e,a,b,c,d,41);
  113. R3(d,e,a,b,c,42);
  114. R3(c,d,e,a,b,43);
  115. R3(b,c,d,e,a,44);
  116. R3(a,b,c,d,e,45);
  117. R3(e,a,b,c,d,46);
  118. R3(d,e,a,b,c,47);
  119. R3(c,d,e,a,b,48);
  120. R3(b,c,d,e,a,49);
  121. R3(a,b,c,d,e,50);
  122. R3(e,a,b,c,d,51);
  123. R3(d,e,a,b,c,52);
  124. R3(c,d,e,a,b,53);
  125. R3(b,c,d,e,a,54);
  126. R3(a,b,c,d,e,55);
  127. R3(e,a,b,c,d,56);
  128. R3(d,e,a,b,c,57);
  129. R3(c,d,e,a,b,58);
  130. R3(b,c,d,e,a,59);
  131. R4(a,b,c,d,e,60);
  132. R4(e,a,b,c,d,61);
  133. R4(d,e,a,b,c,62);
  134. R4(c,d,e,a,b,63);
  135. R4(b,c,d,e,a,64);
  136. R4(a,b,c,d,e,65);
  137. R4(e,a,b,c,d,66);
  138. R4(d,e,a,b,c,67);
  139. R4(c,d,e,a,b,68);
  140. R4(b,c,d,e,a,69);
  141. R4(a,b,c,d,e,70);
  142. R4(e,a,b,c,d,71);
  143. R4(d,e,a,b,c,72);
  144. R4(c,d,e,a,b,73);
  145. R4(b,c,d,e,a,74);
  146. R4(a,b,c,d,e,75);
  147. R4(e,a,b,c,d,76);
  148. R4(d,e,a,b,c,77);
  149. R4(c,d,e,a,b,78);
  150. R4(b,c,d,e,a,79);
  151. /* Add the working vars back into context.state[] */
  152. state[0] += a;
  153. state[1] += b;
  154. state[2] += c;
  155. state[3] += d;
  156. state[4] += e;
  157. }
  158. /*}}}*/
  159. // SHA1Summation::SHA1Summation - Constructor /*{{{*/
  160. // ---------------------------------------------------------------------
  161. /* */
  162. SHA1Summation::SHA1Summation()
  163. {
  164. uint32_t *state = (uint32_t *)State;
  165. uint32_t *count = (uint32_t *)Count;
  166. /* SHA1 initialization constants */
  167. state[0] = 0x67452301;
  168. state[1] = 0xEFCDAB89;
  169. state[2] = 0x98BADCFE;
  170. state[3] = 0x10325476;
  171. state[4] = 0xC3D2E1F0;
  172. count[0] = 0;
  173. count[1] = 0;
  174. Done = false;
  175. }
  176. /*}}}*/
  177. // SHA1Summation::Result - Return checksum value /*{{{*/
  178. // ---------------------------------------------------------------------
  179. /* Add() may not be called after this */
  180. SHA1SumValue SHA1Summation::Result()
  181. {
  182. uint32_t *state = (uint32_t *)State;
  183. uint32_t *count = (uint32_t *)Count;
  184. // Apply the padding
  185. if (Done == false)
  186. {
  187. unsigned char finalcount[8];
  188. for (unsigned i = 0; i < 8; i++)
  189. {
  190. // Endian independent
  191. finalcount[i] = (unsigned char) ((count[(i >= 4 ? 0 : 1)]
  192. >> ((3 - (i & 3)) * 8)) & 255);
  193. }
  194. Add((unsigned char *) "\200",1);
  195. while ((count[0] & 504) != 448)
  196. Add((unsigned char *) "\0",1);
  197. Add(finalcount,8); /* Should cause a SHA1Transform() */
  198. }
  199. Done = true;
  200. // Transfer over the result
  201. SHA1SumValue Value;
  202. unsigned char res[20];
  203. for (unsigned i = 0; i < 20; i++)
  204. {
  205. res[i] = (unsigned char)
  206. ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  207. }
  208. Value.Set(res);
  209. return Value;
  210. }
  211. /*}}}*/
  212. // SHA1Summation::Add - Adds content of buffer into the checksum /*{{{*/
  213. // ---------------------------------------------------------------------
  214. /* May not be called after Result() is called */
  215. bool SHA1Summation::Add(const unsigned char *data,unsigned long long len)
  216. {
  217. if (Done)
  218. return false;
  219. if (len == 0)
  220. return true;
  221. uint32_t *state = (uint32_t *)State;
  222. uint32_t *count = (uint32_t *)Count;
  223. uint8_t *buffer = (uint8_t *)Buffer;
  224. uint32_t i,j;
  225. j = (count[0] >> 3) & 63;
  226. if ((count[0] += len << 3) < (len << 3))
  227. count[1]++;
  228. count[1] += (len >> 29);
  229. if ((j + len) > 63)
  230. {
  231. memcpy(&buffer[j],data,(i = 64 - j));
  232. SHA1Transform(state,buffer);
  233. for (; i + 63 < len; i += 64)
  234. {
  235. SHA1Transform(state,&data[i]);
  236. }
  237. j = 0;
  238. }
  239. else
  240. i = 0;
  241. memcpy(&buffer[j],&data[i],len - i);
  242. return true;
  243. }
  244. /*}}}*/