sha1.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 workspace, *block;
  65. block = &workspace;
  66. memcpy(block,buffer,sizeof(workspace));
  67. /* Copy context->state[] to working vars */
  68. a = state[0];
  69. b = state[1];
  70. c = state[2];
  71. d = state[3];
  72. e = state[4];
  73. /* 4 rounds of 20 operations each. Loop unrolled. */
  74. R0(a,b,c,d,e,0);
  75. R0(e,a,b,c,d,1);
  76. R0(d,e,a,b,c,2);
  77. R0(c,d,e,a,b,3);
  78. R0(b,c,d,e,a,4);
  79. R0(a,b,c,d,e,5);
  80. R0(e,a,b,c,d,6);
  81. R0(d,e,a,b,c,7);
  82. R0(c,d,e,a,b,8);
  83. R0(b,c,d,e,a,9);
  84. R0(a,b,c,d,e,10);
  85. R0(e,a,b,c,d,11);
  86. R0(d,e,a,b,c,12);
  87. R0(c,d,e,a,b,13);
  88. R0(b,c,d,e,a,14);
  89. R0(a,b,c,d,e,15);
  90. R1(e,a,b,c,d,16);
  91. R1(d,e,a,b,c,17);
  92. R1(c,d,e,a,b,18);
  93. R1(b,c,d,e,a,19);
  94. R2(a,b,c,d,e,20);
  95. R2(e,a,b,c,d,21);
  96. R2(d,e,a,b,c,22);
  97. R2(c,d,e,a,b,23);
  98. R2(b,c,d,e,a,24);
  99. R2(a,b,c,d,e,25);
  100. R2(e,a,b,c,d,26);
  101. R2(d,e,a,b,c,27);
  102. R2(c,d,e,a,b,28);
  103. R2(b,c,d,e,a,29);
  104. R2(a,b,c,d,e,30);
  105. R2(e,a,b,c,d,31);
  106. R2(d,e,a,b,c,32);
  107. R2(c,d,e,a,b,33);
  108. R2(b,c,d,e,a,34);
  109. R2(a,b,c,d,e,35);
  110. R2(e,a,b,c,d,36);
  111. R2(d,e,a,b,c,37);
  112. R2(c,d,e,a,b,38);
  113. R2(b,c,d,e,a,39);
  114. R3(a,b,c,d,e,40);
  115. R3(e,a,b,c,d,41);
  116. R3(d,e,a,b,c,42);
  117. R3(c,d,e,a,b,43);
  118. R3(b,c,d,e,a,44);
  119. R3(a,b,c,d,e,45);
  120. R3(e,a,b,c,d,46);
  121. R3(d,e,a,b,c,47);
  122. R3(c,d,e,a,b,48);
  123. R3(b,c,d,e,a,49);
  124. R3(a,b,c,d,e,50);
  125. R3(e,a,b,c,d,51);
  126. R3(d,e,a,b,c,52);
  127. R3(c,d,e,a,b,53);
  128. R3(b,c,d,e,a,54);
  129. R3(a,b,c,d,e,55);
  130. R3(e,a,b,c,d,56);
  131. R3(d,e,a,b,c,57);
  132. R3(c,d,e,a,b,58);
  133. R3(b,c,d,e,a,59);
  134. R4(a,b,c,d,e,60);
  135. R4(e,a,b,c,d,61);
  136. R4(d,e,a,b,c,62);
  137. R4(c,d,e,a,b,63);
  138. R4(b,c,d,e,a,64);
  139. R4(a,b,c,d,e,65);
  140. R4(e,a,b,c,d,66);
  141. R4(d,e,a,b,c,67);
  142. R4(c,d,e,a,b,68);
  143. R4(b,c,d,e,a,69);
  144. R4(a,b,c,d,e,70);
  145. R4(e,a,b,c,d,71);
  146. R4(d,e,a,b,c,72);
  147. R4(c,d,e,a,b,73);
  148. R4(b,c,d,e,a,74);
  149. R4(a,b,c,d,e,75);
  150. R4(e,a,b,c,d,76);
  151. R4(d,e,a,b,c,77);
  152. R4(c,d,e,a,b,78);
  153. R4(b,c,d,e,a,79);
  154. /* Add the working vars back into context.state[] */
  155. state[0] += a;
  156. state[1] += b;
  157. state[2] += c;
  158. state[3] += d;
  159. state[4] += e;
  160. }
  161. /*}}}*/
  162. // SHA1SumValue::SHA1SumValue - Constructs the summation from a string /*{{{*/
  163. // ---------------------------------------------------------------------
  164. /* The string form of a SHA1 is a 40 character hex number */
  165. SHA1SumValue::SHA1SumValue(string Str)
  166. {
  167. memset(Sum,0,sizeof(Sum));
  168. Set(Str);
  169. }
  170. /*}}} */
  171. // SHA1SumValue::SHA1SumValue - Default constructor /*{{{*/
  172. // ---------------------------------------------------------------------
  173. /* Sets the value to 0 */
  174. SHA1SumValue::SHA1SumValue()
  175. {
  176. memset(Sum,0,sizeof(Sum));
  177. }
  178. /*}}} */
  179. // SHA1SumValue::Set - Set the sum from a string /*{{{*/
  180. // ---------------------------------------------------------------------
  181. /* Converts the hex string into a set of chars */
  182. bool SHA1SumValue::Set(string Str)
  183. {
  184. return Hex2Num(Str,Sum,sizeof(Sum));
  185. }
  186. /*}}} */
  187. // SHA1SumValue::Value - Convert the number into a string /*{{{*/
  188. // ---------------------------------------------------------------------
  189. /* Converts the set of chars into a hex string in lower case */
  190. string SHA1SumValue::Value() const
  191. {
  192. char Conv[16] =
  193. { '0','1','2','3','4','5','6','7','8','9','a','b',
  194. 'c','d','e','f'
  195. };
  196. char Result[41];
  197. Result[40] = 0;
  198. // Convert each char into two letters
  199. int J = 0;
  200. int I = 0;
  201. for (; I != 40; J++,I += 2)
  202. {
  203. Result[I] = Conv[Sum[J] >> 4];
  204. Result[I + 1] = Conv[Sum[J] & 0xF];
  205. }
  206. return string(Result);
  207. }
  208. /*}}} */
  209. // SHA1SumValue::operator == - Comparator /*{{{*/
  210. // ---------------------------------------------------------------------
  211. /* Call memcmp on the buffer */
  212. bool SHA1SumValue::operator == (const SHA1SumValue & rhs) const
  213. {
  214. return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
  215. }
  216. /*}}}*/
  217. // SHA1Summation::SHA1Summation - Constructor /*{{{*/
  218. // ---------------------------------------------------------------------
  219. /* */
  220. SHA1Summation::SHA1Summation()
  221. {
  222. uint32_t *state = (uint32_t *)State;
  223. uint32_t *count = (uint32_t *)Count;
  224. /* SHA1 initialization constants */
  225. state[0] = 0x67452301;
  226. state[1] = 0xEFCDAB89;
  227. state[2] = 0x98BADCFE;
  228. state[3] = 0x10325476;
  229. state[4] = 0xC3D2E1F0;
  230. count[0] = 0;
  231. count[1] = 0;
  232. Done = false;
  233. }
  234. /*}}}*/
  235. // SHA1Summation::Result - Return checksum value /*{{{*/
  236. // ---------------------------------------------------------------------
  237. /* Add() may not be called after this */
  238. SHA1SumValue SHA1Summation::Result()
  239. {
  240. uint32_t *state = (uint32_t *)State;
  241. uint32_t *count = (uint32_t *)Count;
  242. // Apply the padding
  243. if (Done == false)
  244. {
  245. unsigned char finalcount[8];
  246. for (unsigned i = 0; i < 8; i++)
  247. {
  248. // Endian independent
  249. finalcount[i] = (unsigned char) ((count[(i >= 4 ? 0 : 1)]
  250. >> ((3 - (i & 3)) * 8)) & 255);
  251. }
  252. Add((unsigned char *) "\200",1);
  253. while ((count[0] & 504) != 448)
  254. Add((unsigned char *) "\0",1);
  255. Add(finalcount,8); /* Should cause a SHA1Transform() */
  256. }
  257. Done = true;
  258. // Transfer over the result
  259. SHA1SumValue Value;
  260. for (unsigned i = 0; i < 20; i++)
  261. {
  262. Value.Sum[i] = (unsigned char)
  263. ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  264. }
  265. return Value;
  266. }
  267. /*}}}*/
  268. // SHA1Summation::Add - Adds content of buffer into the checksum /*{{{*/
  269. // ---------------------------------------------------------------------
  270. /* May not be called after Result() is called */
  271. bool SHA1Summation::Add(const unsigned char *data,unsigned long len)
  272. {
  273. if (Done)
  274. return false;
  275. uint32_t *state = (uint32_t *)State;
  276. uint32_t *count = (uint32_t *)Count;
  277. uint8_t *buffer = (uint8_t *)Buffer;
  278. uint32_t i,j;
  279. j = (count[0] >> 3) & 63;
  280. if ((count[0] += len << 3) < (len << 3))
  281. count[1]++;
  282. count[1] += (len >> 29);
  283. if ((j + len) > 63)
  284. {
  285. memcpy(&buffer[j],data,(i = 64 - j));
  286. SHA1Transform(state,buffer);
  287. for (; i + 63 < len; i += 64)
  288. {
  289. SHA1Transform(state,&data[i]);
  290. }
  291. j = 0;
  292. }
  293. else
  294. i = 0;
  295. memcpy(&buffer[j],&data[i],len - i);
  296. return true;
  297. }
  298. /*}}}*/
  299. // SHA1Summation::AddFD - Add content of file into the checksum /*{{{*/
  300. // ---------------------------------------------------------------------
  301. /* */
  302. bool SHA1Summation::AddFD(int Fd,unsigned long Size)
  303. {
  304. unsigned char Buf[64 * 64];
  305. int Res = 0;
  306. int ToEOF = (Size == 0);
  307. while (Size != 0 || ToEOF)
  308. {
  309. unsigned n = sizeof(Buf);
  310. if (!ToEOF) n = min(Size,(unsigned long)n);
  311. Res = read(Fd,Buf,n);
  312. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  313. return false;
  314. if (ToEOF && Res == 0) // EOF
  315. break;
  316. Size -= Res;
  317. Add(Buf,Res);
  318. }
  319. return true;
  320. }
  321. /*}}}*/