sha1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* sha1.c - SHA1 hash function
  2. * Copyright (C) 1998, 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* Test vectors:
  20. *
  21. * "abc"
  22. * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
  23. *
  24. * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  25. * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
  26. */
  27. #include <config.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #ifdef HAVE_STDINT_H
  32. # include <stdint.h>
  33. #endif
  34. #include "g10lib.h"
  35. #include "bithelp.h"
  36. #include "cipher.h"
  37. #include "hash-common.h"
  38. /* A macro to test whether P is properly aligned for an u32 type.
  39. Note that config.h provides a suitable replacement for uintptr_t if
  40. it does not exist in stdint.h. */
  41. /* #if __GNUC__ >= 2 */
  42. /* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % __alignof__ (u32))) */
  43. /* #else */
  44. /* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % sizeof (u32))) */
  45. /* #endif */
  46. #define TRANSFORM(x,d,n) transform ((x), (d), (n))
  47. typedef struct
  48. {
  49. u32 h0,h1,h2,h3,h4;
  50. u32 nblocks;
  51. unsigned char buf[64];
  52. int count;
  53. } SHA1_CONTEXT;
  54. static void
  55. sha1_init (void *context)
  56. {
  57. SHA1_CONTEXT *hd = context;
  58. hd->h0 = 0x67452301;
  59. hd->h1 = 0xefcdab89;
  60. hd->h2 = 0x98badcfe;
  61. hd->h3 = 0x10325476;
  62. hd->h4 = 0xc3d2e1f0;
  63. hd->nblocks = 0;
  64. hd->count = 0;
  65. }
  66. /* Round function macros. */
  67. #define K1 0x5A827999L
  68. #define K2 0x6ED9EBA1L
  69. #define K3 0x8F1BBCDCL
  70. #define K4 0xCA62C1D6L
  71. #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
  72. #define F2(x,y,z) ( x ^ y ^ z )
  73. #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
  74. #define F4(x,y,z) ( x ^ y ^ z )
  75. #define M(i) ( tm = x[ i &0x0f] \
  76. ^ x[(i-14)&0x0f] \
  77. ^ x[(i-8) &0x0f] \
  78. ^ x[(i-3) &0x0f], \
  79. (x[i&0x0f] = rol(tm, 1)))
  80. #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
  81. + f( b, c, d ) \
  82. + k \
  83. + m; \
  84. b = rol( b, 30 ); \
  85. } while(0)
  86. /*
  87. * Transform NBLOCKS of each 64 bytes (16 32-bit words) at DATA.
  88. */
  89. static void
  90. transform (SHA1_CONTEXT *hd, const unsigned char *data, size_t nblocks)
  91. {
  92. register u32 a, b, c, d, e; /* Local copies of the chaining variables. */
  93. register u32 tm; /* Helper. */
  94. u32 x[16]; /* The array we work on. */
  95. /* Loop over all blocks. */
  96. for ( ;nblocks; nblocks--)
  97. {
  98. #ifdef WORDS_BIGENDIAN
  99. memcpy (x, data, 64);
  100. data += 64;
  101. #else
  102. {
  103. int i;
  104. unsigned char *p;
  105. for(i=0, p=(unsigned char*)x; i < 16; i++, p += 4 )
  106. {
  107. p[3] = *data++;
  108. p[2] = *data++;
  109. p[1] = *data++;
  110. p[0] = *data++;
  111. }
  112. }
  113. #endif
  114. /* Get the values of the chaining variables. */
  115. a = hd->h0;
  116. b = hd->h1;
  117. c = hd->h2;
  118. d = hd->h3;
  119. e = hd->h4;
  120. /* Transform. */
  121. R( a, b, c, d, e, F1, K1, x[ 0] );
  122. R( e, a, b, c, d, F1, K1, x[ 1] );
  123. R( d, e, a, b, c, F1, K1, x[ 2] );
  124. R( c, d, e, a, b, F1, K1, x[ 3] );
  125. R( b, c, d, e, a, F1, K1, x[ 4] );
  126. R( a, b, c, d, e, F1, K1, x[ 5] );
  127. R( e, a, b, c, d, F1, K1, x[ 6] );
  128. R( d, e, a, b, c, F1, K1, x[ 7] );
  129. R( c, d, e, a, b, F1, K1, x[ 8] );
  130. R( b, c, d, e, a, F1, K1, x[ 9] );
  131. R( a, b, c, d, e, F1, K1, x[10] );
  132. R( e, a, b, c, d, F1, K1, x[11] );
  133. R( d, e, a, b, c, F1, K1, x[12] );
  134. R( c, d, e, a, b, F1, K1, x[13] );
  135. R( b, c, d, e, a, F1, K1, x[14] );
  136. R( a, b, c, d, e, F1, K1, x[15] );
  137. R( e, a, b, c, d, F1, K1, M(16) );
  138. R( d, e, a, b, c, F1, K1, M(17) );
  139. R( c, d, e, a, b, F1, K1, M(18) );
  140. R( b, c, d, e, a, F1, K1, M(19) );
  141. R( a, b, c, d, e, F2, K2, M(20) );
  142. R( e, a, b, c, d, F2, K2, M(21) );
  143. R( d, e, a, b, c, F2, K2, M(22) );
  144. R( c, d, e, a, b, F2, K2, M(23) );
  145. R( b, c, d, e, a, F2, K2, M(24) );
  146. R( a, b, c, d, e, F2, K2, M(25) );
  147. R( e, a, b, c, d, F2, K2, M(26) );
  148. R( d, e, a, b, c, F2, K2, M(27) );
  149. R( c, d, e, a, b, F2, K2, M(28) );
  150. R( b, c, d, e, a, F2, K2, M(29) );
  151. R( a, b, c, d, e, F2, K2, M(30) );
  152. R( e, a, b, c, d, F2, K2, M(31) );
  153. R( d, e, a, b, c, F2, K2, M(32) );
  154. R( c, d, e, a, b, F2, K2, M(33) );
  155. R( b, c, d, e, a, F2, K2, M(34) );
  156. R( a, b, c, d, e, F2, K2, M(35) );
  157. R( e, a, b, c, d, F2, K2, M(36) );
  158. R( d, e, a, b, c, F2, K2, M(37) );
  159. R( c, d, e, a, b, F2, K2, M(38) );
  160. R( b, c, d, e, a, F2, K2, M(39) );
  161. R( a, b, c, d, e, F3, K3, M(40) );
  162. R( e, a, b, c, d, F3, K3, M(41) );
  163. R( d, e, a, b, c, F3, K3, M(42) );
  164. R( c, d, e, a, b, F3, K3, M(43) );
  165. R( b, c, d, e, a, F3, K3, M(44) );
  166. R( a, b, c, d, e, F3, K3, M(45) );
  167. R( e, a, b, c, d, F3, K3, M(46) );
  168. R( d, e, a, b, c, F3, K3, M(47) );
  169. R( c, d, e, a, b, F3, K3, M(48) );
  170. R( b, c, d, e, a, F3, K3, M(49) );
  171. R( a, b, c, d, e, F3, K3, M(50) );
  172. R( e, a, b, c, d, F3, K3, M(51) );
  173. R( d, e, a, b, c, F3, K3, M(52) );
  174. R( c, d, e, a, b, F3, K3, M(53) );
  175. R( b, c, d, e, a, F3, K3, M(54) );
  176. R( a, b, c, d, e, F3, K3, M(55) );
  177. R( e, a, b, c, d, F3, K3, M(56) );
  178. R( d, e, a, b, c, F3, K3, M(57) );
  179. R( c, d, e, a, b, F3, K3, M(58) );
  180. R( b, c, d, e, a, F3, K3, M(59) );
  181. R( a, b, c, d, e, F4, K4, M(60) );
  182. R( e, a, b, c, d, F4, K4, M(61) );
  183. R( d, e, a, b, c, F4, K4, M(62) );
  184. R( c, d, e, a, b, F4, K4, M(63) );
  185. R( b, c, d, e, a, F4, K4, M(64) );
  186. R( a, b, c, d, e, F4, K4, M(65) );
  187. R( e, a, b, c, d, F4, K4, M(66) );
  188. R( d, e, a, b, c, F4, K4, M(67) );
  189. R( c, d, e, a, b, F4, K4, M(68) );
  190. R( b, c, d, e, a, F4, K4, M(69) );
  191. R( a, b, c, d, e, F4, K4, M(70) );
  192. R( e, a, b, c, d, F4, K4, M(71) );
  193. R( d, e, a, b, c, F4, K4, M(72) );
  194. R( c, d, e, a, b, F4, K4, M(73) );
  195. R( b, c, d, e, a, F4, K4, M(74) );
  196. R( a, b, c, d, e, F4, K4, M(75) );
  197. R( e, a, b, c, d, F4, K4, M(76) );
  198. R( d, e, a, b, c, F4, K4, M(77) );
  199. R( c, d, e, a, b, F4, K4, M(78) );
  200. R( b, c, d, e, a, F4, K4, M(79) );
  201. /* Update the chaining variables. */
  202. hd->h0 += a;
  203. hd->h1 += b;
  204. hd->h2 += c;
  205. hd->h3 += d;
  206. hd->h4 += e;
  207. }
  208. }
  209. /* Update the message digest with the contents
  210. * of INBUF with length INLEN.
  211. */
  212. static void
  213. sha1_write( void *context, const void *inbuf_arg, size_t inlen)
  214. {
  215. const unsigned char *inbuf = inbuf_arg;
  216. SHA1_CONTEXT *hd = context;
  217. size_t nblocks;
  218. if (hd->count == 64) /* Flush the buffer. */
  219. {
  220. TRANSFORM( hd, hd->buf, 1 );
  221. _gcry_burn_stack (88+4*sizeof(void*));
  222. hd->count = 0;
  223. hd->nblocks++;
  224. }
  225. if (!inbuf)
  226. return;
  227. if (hd->count)
  228. {
  229. for (; inlen && hd->count < 64; inlen--)
  230. hd->buf[hd->count++] = *inbuf++;
  231. sha1_write (hd, NULL, 0);
  232. if (!inlen)
  233. return;
  234. }
  235. nblocks = inlen / 64;
  236. if (nblocks)
  237. {
  238. TRANSFORM (hd, inbuf, nblocks);
  239. hd->count = 0;
  240. hd->nblocks += nblocks;
  241. inlen -= nblocks * 64;
  242. inbuf += nblocks * 64;
  243. }
  244. _gcry_burn_stack (88+4*sizeof(void*));
  245. /* Save remaining bytes. */
  246. for (; inlen && hd->count < 64; inlen--)
  247. hd->buf[hd->count++] = *inbuf++;
  248. }
  249. /* The routine final terminates the computation and
  250. * returns the digest.
  251. * The handle is prepared for a new cycle, but adding bytes to the
  252. * handle will the destroy the returned buffer.
  253. * Returns: 20 bytes representing the digest.
  254. */
  255. static void
  256. sha1_final(void *context)
  257. {
  258. SHA1_CONTEXT *hd = context;
  259. u32 t, msb, lsb;
  260. unsigned char *p;
  261. sha1_write(hd, NULL, 0); /* flush */;
  262. t = hd->nblocks;
  263. /* multiply by 64 to make a byte count */
  264. lsb = t << 6;
  265. msb = t >> 26;
  266. /* add the count */
  267. t = lsb;
  268. if( (lsb += hd->count) < t )
  269. msb++;
  270. /* multiply by 8 to make a bit count */
  271. t = lsb;
  272. lsb <<= 3;
  273. msb <<= 3;
  274. msb |= t >> 29;
  275. if( hd->count < 56 ) /* enough room */
  276. {
  277. hd->buf[hd->count++] = 0x80; /* pad */
  278. while( hd->count < 56 )
  279. hd->buf[hd->count++] = 0; /* pad */
  280. }
  281. else /* need one extra block */
  282. {
  283. hd->buf[hd->count++] = 0x80; /* pad character */
  284. while( hd->count < 64 )
  285. hd->buf[hd->count++] = 0;
  286. sha1_write(hd, NULL, 0); /* flush */;
  287. memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
  288. }
  289. /* append the 64 bit count */
  290. hd->buf[56] = msb >> 24;
  291. hd->buf[57] = msb >> 16;
  292. hd->buf[58] = msb >> 8;
  293. hd->buf[59] = msb ;
  294. hd->buf[60] = lsb >> 24;
  295. hd->buf[61] = lsb >> 16;
  296. hd->buf[62] = lsb >> 8;
  297. hd->buf[63] = lsb ;
  298. TRANSFORM( hd, hd->buf, 1 );
  299. _gcry_burn_stack (88+4*sizeof(void*));
  300. p = hd->buf;
  301. #ifdef WORDS_BIGENDIAN
  302. #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
  303. #else /* little endian */
  304. #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
  305. *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
  306. #endif
  307. X(0);
  308. X(1);
  309. X(2);
  310. X(3);
  311. X(4);
  312. #undef X
  313. }
  314. static unsigned char *
  315. sha1_read( void *context )
  316. {
  317. SHA1_CONTEXT *hd = context;
  318. return hd->buf;
  319. }
  320. /****************
  321. * Shortcut functions which puts the hash value of the supplied buffer
  322. * into outbuf which must have a size of 20 bytes.
  323. */
  324. void
  325. _gcry_sha1_hash_buffer (void *outbuf, const void *buffer, size_t length)
  326. {
  327. SHA1_CONTEXT hd;
  328. sha1_init (&hd);
  329. sha1_write (&hd, buffer, length);
  330. sha1_final (&hd);
  331. memcpy (outbuf, hd.buf, 20);
  332. }
  333. /*
  334. Self-test section.
  335. */
  336. static gpg_err_code_t
  337. selftests_sha1 (int extended, selftest_report_func_t report)
  338. {
  339. const char *what;
  340. const char *errtxt;
  341. what = "short string";
  342. errtxt = _gcry_hash_selftest_check_one
  343. (GCRY_MD_SHA1, 0,
  344. "abc", 3,
  345. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  346. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  347. if (errtxt)
  348. goto failed;
  349. if (extended)
  350. {
  351. what = "long string";
  352. errtxt = _gcry_hash_selftest_check_one
  353. (GCRY_MD_SHA1, 0,
  354. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
  355. "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE"
  356. "\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1", 20);
  357. if (errtxt)
  358. goto failed;
  359. what = "one million \"a\"";
  360. errtxt = _gcry_hash_selftest_check_one
  361. (GCRY_MD_SHA1, 1,
  362. NULL, 0,
  363. "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E"
  364. "\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F", 20);
  365. if (errtxt)
  366. goto failed;
  367. }
  368. return 0; /* Succeeded. */
  369. failed:
  370. if (report)
  371. report ("digest", GCRY_MD_SHA1, what, errtxt);
  372. return GPG_ERR_SELFTEST_FAILED;
  373. }
  374. /* Run a full self-test for ALGO and return 0 on success. */
  375. static gpg_err_code_t
  376. run_selftests (int algo, int extended, selftest_report_func_t report)
  377. {
  378. gpg_err_code_t ec;
  379. switch (algo)
  380. {
  381. case GCRY_MD_SHA1:
  382. ec = selftests_sha1 (extended, report);
  383. break;
  384. default:
  385. ec = GPG_ERR_DIGEST_ALGO;
  386. break;
  387. }
  388. return ec;
  389. }
  390. static unsigned char asn[15] = /* Object ID is 1.3.14.3.2.26 */
  391. { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
  392. 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
  393. static gcry_md_oid_spec_t oid_spec_sha1[] =
  394. {
  395. /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
  396. { "1.2.840.113549.1.1.5" },
  397. /* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
  398. { "1.2.840.10040.4.3" },
  399. /* from NIST's OIW (sha1) */
  400. { "1.3.14.3.2.26" },
  401. /* from NIST OIW (sha-1WithRSAEncryption) */
  402. { "1.3.14.3.2.29" },
  403. /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha1 */
  404. { "1.2.840.10045.4.1" },
  405. { NULL },
  406. };
  407. gcry_md_spec_t _gcry_digest_spec_sha1 =
  408. {
  409. "SHA1", asn, DIM (asn), oid_spec_sha1, 20,
  410. sha1_init, sha1_write, sha1_final, sha1_read,
  411. sizeof (SHA1_CONTEXT)
  412. };
  413. md_extra_spec_t _gcry_digest_extraspec_sha1 =
  414. {
  415. run_selftests
  416. };