sha1.cc 9.7 KB

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