sha1.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <apt-pkg/sha1.h>
  26. #include <apt-pkg/strutl.h>
  27. #include <apt-pkg/macros.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <inttypes.h>
  31. #include <config.h>
  32. /*}}}*/
  33. // SHA1Transform - Alters an existing SHA-1 hash /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* The core of the SHA-1 algorithm. This alters an existing SHA-1 hash to
  36. reflect the addition of 16 longwords of new data. Other routines convert
  37. incoming stream data into 16 long word chunks for this routine */
  38. #define rol(value,bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  39. /* blk0() and blk() perform the initial expand. */
  40. /* I got the idea of expanding during the round function from SSLeay */
  41. #ifndef WORDS_BIGENDIAN
  42. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  43. |(rol(block->l[i],8)&0x00FF00FF))
  44. #else
  45. #define blk0(i) block->l[i]
  46. #endif
  47. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  48. ^block->l[(i+2)&15]^block->l[i&15],1))
  49. /* (R0+R1),R2,R3,R4 are the different operations used in SHA1 */
  50. #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  51. #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  52. #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  53. #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  54. #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  55. static void SHA1Transform(uint32_t state[5],uint8_t const buffer[64])
  56. {
  57. uint32_t a,b,c,d,e;
  58. typedef union
  59. {
  60. uint8_t c[64];
  61. uint32_t l[16];
  62. }
  63. CHAR64LONG16;
  64. CHAR64LONG16 *block;
  65. uint8_t workspace[64];
  66. block = (CHAR64LONG16 *)workspace;
  67. memcpy(block,buffer,sizeof(workspace));
  68. /* Copy context->state[] to working vars */
  69. a = state[0];
  70. b = state[1];
  71. c = state[2];
  72. d = state[3];
  73. e = state[4];
  74. /* 4 rounds of 20 operations each. Loop unrolled. */
  75. R0(a,b,c,d,e,0);
  76. R0(e,a,b,c,d,1);
  77. R0(d,e,a,b,c,2);
  78. R0(c,d,e,a,b,3);
  79. R0(b,c,d,e,a,4);
  80. R0(a,b,c,d,e,5);
  81. R0(e,a,b,c,d,6);
  82. R0(d,e,a,b,c,7);
  83. R0(c,d,e,a,b,8);
  84. R0(b,c,d,e,a,9);
  85. R0(a,b,c,d,e,10);
  86. R0(e,a,b,c,d,11);
  87. R0(d,e,a,b,c,12);
  88. R0(c,d,e,a,b,13);
  89. R0(b,c,d,e,a,14);
  90. R0(a,b,c,d,e,15);
  91. R1(e,a,b,c,d,16);
  92. R1(d,e,a,b,c,17);
  93. R1(c,d,e,a,b,18);
  94. R1(b,c,d,e,a,19);
  95. R2(a,b,c,d,e,20);
  96. R2(e,a,b,c,d,21);
  97. R2(d,e,a,b,c,22);
  98. R2(c,d,e,a,b,23);
  99. R2(b,c,d,e,a,24);
  100. R2(a,b,c,d,e,25);
  101. R2(e,a,b,c,d,26);
  102. R2(d,e,a,b,c,27);
  103. R2(c,d,e,a,b,28);
  104. R2(b,c,d,e,a,29);
  105. R2(a,b,c,d,e,30);
  106. R2(e,a,b,c,d,31);
  107. R2(d,e,a,b,c,32);
  108. R2(c,d,e,a,b,33);
  109. R2(b,c,d,e,a,34);
  110. R2(a,b,c,d,e,35);
  111. R2(e,a,b,c,d,36);
  112. R2(d,e,a,b,c,37);
  113. R2(c,d,e,a,b,38);
  114. R2(b,c,d,e,a,39);
  115. R3(a,b,c,d,e,40);
  116. R3(e,a,b,c,d,41);
  117. R3(d,e,a,b,c,42);
  118. R3(c,d,e,a,b,43);
  119. R3(b,c,d,e,a,44);
  120. R3(a,b,c,d,e,45);
  121. R3(e,a,b,c,d,46);
  122. R3(d,e,a,b,c,47);
  123. R3(c,d,e,a,b,48);
  124. R3(b,c,d,e,a,49);
  125. R3(a,b,c,d,e,50);
  126. R3(e,a,b,c,d,51);
  127. R3(d,e,a,b,c,52);
  128. R3(c,d,e,a,b,53);
  129. R3(b,c,d,e,a,54);
  130. R3(a,b,c,d,e,55);
  131. R3(e,a,b,c,d,56);
  132. R3(d,e,a,b,c,57);
  133. R3(c,d,e,a,b,58);
  134. R3(b,c,d,e,a,59);
  135. R4(a,b,c,d,e,60);
  136. R4(e,a,b,c,d,61);
  137. R4(d,e,a,b,c,62);
  138. R4(c,d,e,a,b,63);
  139. R4(b,c,d,e,a,64);
  140. R4(a,b,c,d,e,65);
  141. R4(e,a,b,c,d,66);
  142. R4(d,e,a,b,c,67);
  143. R4(c,d,e,a,b,68);
  144. R4(b,c,d,e,a,69);
  145. R4(a,b,c,d,e,70);
  146. R4(e,a,b,c,d,71);
  147. R4(d,e,a,b,c,72);
  148. R4(c,d,e,a,b,73);
  149. R4(b,c,d,e,a,74);
  150. R4(a,b,c,d,e,75);
  151. R4(e,a,b,c,d,76);
  152. R4(d,e,a,b,c,77);
  153. R4(c,d,e,a,b,78);
  154. R4(b,c,d,e,a,79);
  155. /* Add the working vars back into context.state[] */
  156. state[0] += a;
  157. state[1] += b;
  158. state[2] += c;
  159. state[3] += d;
  160. state[4] += e;
  161. }
  162. /*}}}*/
  163. // SHA1Summation::SHA1Summation - Constructor /*{{{*/
  164. // ---------------------------------------------------------------------
  165. /* */
  166. SHA1Summation::SHA1Summation()
  167. {
  168. uint32_t *state = (uint32_t *)State;
  169. uint32_t *count = (uint32_t *)Count;
  170. /* SHA1 initialization constants */
  171. state[0] = 0x67452301;
  172. state[1] = 0xEFCDAB89;
  173. state[2] = 0x98BADCFE;
  174. state[3] = 0x10325476;
  175. state[4] = 0xC3D2E1F0;
  176. count[0] = 0;
  177. count[1] = 0;
  178. Done = false;
  179. }
  180. /*}}}*/
  181. // SHA1Summation::Result - Return checksum value /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* Add() may not be called after this */
  184. SHA1SumValue SHA1Summation::Result()
  185. {
  186. uint32_t *state = (uint32_t *)State;
  187. uint32_t *count = (uint32_t *)Count;
  188. // Apply the padding
  189. if (Done == false)
  190. {
  191. unsigned char finalcount[8];
  192. for (unsigned i = 0; i < 8; i++)
  193. {
  194. // Endian independent
  195. finalcount[i] = (unsigned char) ((count[(i >= 4 ? 0 : 1)]
  196. >> ((3 - (i & 3)) * 8)) & 255);
  197. }
  198. Add((unsigned char *) "\200",1);
  199. while ((count[0] & 504) != 448)
  200. Add((unsigned char *) "\0",1);
  201. Add(finalcount,8); /* Should cause a SHA1Transform() */
  202. }
  203. Done = true;
  204. // Transfer over the result
  205. SHA1SumValue Value;
  206. unsigned char res[20];
  207. for (unsigned i = 0; i < 20; i++)
  208. {
  209. res[i] = (unsigned char)
  210. ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  211. }
  212. Value.Set(res);
  213. return Value;
  214. }
  215. /*}}}*/
  216. // SHA1Summation::Add - Adds content of buffer into the checksum /*{{{*/
  217. // ---------------------------------------------------------------------
  218. /* May not be called after Result() is called */
  219. bool SHA1Summation::Add(const unsigned char *data,unsigned long len)
  220. {
  221. if (Done)
  222. return false;
  223. uint32_t *state = (uint32_t *)State;
  224. uint32_t *count = (uint32_t *)Count;
  225. uint8_t *buffer = (uint8_t *)Buffer;
  226. uint32_t i,j;
  227. j = (count[0] >> 3) & 63;
  228. if ((count[0] += len << 3) < (len << 3))
  229. count[1]++;
  230. count[1] += (len >> 29);
  231. if ((j + len) > 63)
  232. {
  233. memcpy(&buffer[j],data,(i = 64 - j));
  234. SHA1Transform(state,buffer);
  235. for (; i + 63 < len; i += 64)
  236. {
  237. SHA1Transform(state,&data[i]);
  238. }
  239. j = 0;
  240. }
  241. else
  242. i = 0;
  243. memcpy(&buffer[j],&data[i],len - i);
  244. return true;
  245. }
  246. /*}}}*/
  247. // SHA1Summation::AddFD - Add content of file into the checksum /*{{{*/
  248. // ---------------------------------------------------------------------
  249. /* */
  250. bool SHA1Summation::AddFD(int Fd,unsigned long Size)
  251. {
  252. unsigned char Buf[64 * 64];
  253. int Res = 0;
  254. int ToEOF = (Size == 0);
  255. while (Size != 0 || ToEOF)
  256. {
  257. unsigned n = sizeof(Buf);
  258. if (!ToEOF) n = min(Size,(unsigned long)n);
  259. Res = read(Fd,Buf,n);
  260. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  261. return false;
  262. if (ToEOF && Res == 0) // EOF
  263. break;
  264. Size -= Res;
  265. Add(Buf,Res);
  266. }
  267. return true;
  268. }
  269. /*}}}*/