sha1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  3. * based on:
  4. * FIPS-180-1 compliant SHA-1 implementation
  5. *
  6. * Copyright (C) 2003-2006 Christophe Devine
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License, version 2.1 as published by the Free Software Foundation.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. * MA 02110-1301 USA
  21. */
  22. /*
  23. * The SHA-1 standard was published by NIST in 1993.
  24. *
  25. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  26. */
  27. #ifndef _CRT_SECURE_NO_DEPRECATE
  28. #define _CRT_SECURE_NO_DEPRECATE 1
  29. #endif
  30. #ifndef USE_HOSTCC
  31. #include <common.h>
  32. #include <linux/string.h>
  33. #else
  34. #include <string.h>
  35. #endif /* USE_HOSTCC */
  36. #include <watchdog.h>
  37. #include "sha1.h"
  38. /*
  39. * 32-bit integer manipulation macros (big endian)
  40. */
  41. #ifndef GET_UINT32_BE
  42. #define GET_UINT32_BE(n,b,i) { \
  43. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  44. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  45. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  46. | ( (unsigned long) (b)[(i) + 3] ); \
  47. }
  48. #endif
  49. #ifndef PUT_UINT32_BE
  50. #define PUT_UINT32_BE(n,b,i) { \
  51. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  52. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  53. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  54. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  55. }
  56. #endif
  57. /*
  58. * SHA-1 context setup
  59. */
  60. void sha1_starts (sha1_context * ctx)
  61. {
  62. ctx->total[0] = 0;
  63. ctx->total[1] = 0;
  64. ctx->state[0] = 0x67452301;
  65. ctx->state[1] = 0xEFCDAB89;
  66. ctx->state[2] = 0x98BADCFE;
  67. ctx->state[3] = 0x10325476;
  68. ctx->state[4] = 0xC3D2E1F0;
  69. }
  70. static void sha1_process (sha1_context * ctx, unsigned char data[64])
  71. {
  72. unsigned long temp, W[16], A, B, C, D, E;
  73. GET_UINT32_BE (W[0], data, 0);
  74. GET_UINT32_BE (W[1], data, 4);
  75. GET_UINT32_BE (W[2], data, 8);
  76. GET_UINT32_BE (W[3], data, 12);
  77. GET_UINT32_BE (W[4], data, 16);
  78. GET_UINT32_BE (W[5], data, 20);
  79. GET_UINT32_BE (W[6], data, 24);
  80. GET_UINT32_BE (W[7], data, 28);
  81. GET_UINT32_BE (W[8], data, 32);
  82. GET_UINT32_BE (W[9], data, 36);
  83. GET_UINT32_BE (W[10], data, 40);
  84. GET_UINT32_BE (W[11], data, 44);
  85. GET_UINT32_BE (W[12], data, 48);
  86. GET_UINT32_BE (W[13], data, 52);
  87. GET_UINT32_BE (W[14], data, 56);
  88. GET_UINT32_BE (W[15], data, 60);
  89. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  90. #define R(t) ( \
  91. temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
  92. W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
  93. ( W[t & 0x0F] = S(temp,1) ) \
  94. )
  95. #define P(a,b,c,d,e,x) { \
  96. e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
  97. }
  98. A = ctx->state[0];
  99. B = ctx->state[1];
  100. C = ctx->state[2];
  101. D = ctx->state[3];
  102. E = ctx->state[4];
  103. #define F(x,y,z) (z ^ (x & (y ^ z)))
  104. #define K 0x5A827999
  105. P (A, B, C, D, E, W[0]);
  106. P (E, A, B, C, D, W[1]);
  107. P (D, E, A, B, C, W[2]);
  108. P (C, D, E, A, B, W[3]);
  109. P (B, C, D, E, A, W[4]);
  110. P (A, B, C, D, E, W[5]);
  111. P (E, A, B, C, D, W[6]);
  112. P (D, E, A, B, C, W[7]);
  113. P (C, D, E, A, B, W[8]);
  114. P (B, C, D, E, A, W[9]);
  115. P (A, B, C, D, E, W[10]);
  116. P (E, A, B, C, D, W[11]);
  117. P (D, E, A, B, C, W[12]);
  118. P (C, D, E, A, B, W[13]);
  119. P (B, C, D, E, A, W[14]);
  120. P (A, B, C, D, E, W[15]);
  121. P (E, A, B, C, D, R (16));
  122. P (D, E, A, B, C, R (17));
  123. P (C, D, E, A, B, R (18));
  124. P (B, C, D, E, A, R (19));
  125. #undef K
  126. #undef F
  127. #define F(x,y,z) (x ^ y ^ z)
  128. #define K 0x6ED9EBA1
  129. P (A, B, C, D, E, R (20));
  130. P (E, A, B, C, D, R (21));
  131. P (D, E, A, B, C, R (22));
  132. P (C, D, E, A, B, R (23));
  133. P (B, C, D, E, A, R (24));
  134. P (A, B, C, D, E, R (25));
  135. P (E, A, B, C, D, R (26));
  136. P (D, E, A, B, C, R (27));
  137. P (C, D, E, A, B, R (28));
  138. P (B, C, D, E, A, R (29));
  139. P (A, B, C, D, E, R (30));
  140. P (E, A, B, C, D, R (31));
  141. P (D, E, A, B, C, R (32));
  142. P (C, D, E, A, B, R (33));
  143. P (B, C, D, E, A, R (34));
  144. P (A, B, C, D, E, R (35));
  145. P (E, A, B, C, D, R (36));
  146. P (D, E, A, B, C, R (37));
  147. P (C, D, E, A, B, R (38));
  148. P (B, C, D, E, A, R (39));
  149. #undef K
  150. #undef F
  151. #define F(x,y,z) ((x & y) | (z & (x | y)))
  152. #define K 0x8F1BBCDC
  153. P (A, B, C, D, E, R (40));
  154. P (E, A, B, C, D, R (41));
  155. P (D, E, A, B, C, R (42));
  156. P (C, D, E, A, B, R (43));
  157. P (B, C, D, E, A, R (44));
  158. P (A, B, C, D, E, R (45));
  159. P (E, A, B, C, D, R (46));
  160. P (D, E, A, B, C, R (47));
  161. P (C, D, E, A, B, R (48));
  162. P (B, C, D, E, A, R (49));
  163. P (A, B, C, D, E, R (50));
  164. P (E, A, B, C, D, R (51));
  165. P (D, E, A, B, C, R (52));
  166. P (C, D, E, A, B, R (53));
  167. P (B, C, D, E, A, R (54));
  168. P (A, B, C, D, E, R (55));
  169. P (E, A, B, C, D, R (56));
  170. P (D, E, A, B, C, R (57));
  171. P (C, D, E, A, B, R (58));
  172. P (B, C, D, E, A, R (59));
  173. #undef K
  174. #undef F
  175. #define F(x,y,z) (x ^ y ^ z)
  176. #define K 0xCA62C1D6
  177. P (A, B, C, D, E, R (60));
  178. P (E, A, B, C, D, R (61));
  179. P (D, E, A, B, C, R (62));
  180. P (C, D, E, A, B, R (63));
  181. P (B, C, D, E, A, R (64));
  182. P (A, B, C, D, E, R (65));
  183. P (E, A, B, C, D, R (66));
  184. P (D, E, A, B, C, R (67));
  185. P (C, D, E, A, B, R (68));
  186. P (B, C, D, E, A, R (69));
  187. P (A, B, C, D, E, R (70));
  188. P (E, A, B, C, D, R (71));
  189. P (D, E, A, B, C, R (72));
  190. P (C, D, E, A, B, R (73));
  191. P (B, C, D, E, A, R (74));
  192. P (A, B, C, D, E, R (75));
  193. P (E, A, B, C, D, R (76));
  194. P (D, E, A, B, C, R (77));
  195. P (C, D, E, A, B, R (78));
  196. P (B, C, D, E, A, R (79));
  197. #undef K
  198. #undef F
  199. ctx->state[0] += A;
  200. ctx->state[1] += B;
  201. ctx->state[2] += C;
  202. ctx->state[3] += D;
  203. ctx->state[4] += E;
  204. }
  205. /*
  206. * SHA-1 process buffer
  207. */
  208. void sha1_update (sha1_context * ctx, unsigned char *input, int ilen)
  209. {
  210. int fill;
  211. unsigned long left;
  212. if (ilen <= 0)
  213. return;
  214. left = ctx->total[0] & 0x3F;
  215. fill = 64 - left;
  216. ctx->total[0] += ilen;
  217. ctx->total[0] &= 0xFFFFFFFF;
  218. if (ctx->total[0] < (unsigned long) ilen)
  219. ctx->total[1]++;
  220. if (left && ilen >= fill) {
  221. memcpy ((void *) (ctx->buffer + left), (void *) input, fill);
  222. sha1_process (ctx, ctx->buffer);
  223. input += fill;
  224. ilen -= fill;
  225. left = 0;
  226. }
  227. while (ilen >= 64) {
  228. sha1_process (ctx, input);
  229. input += 64;
  230. ilen -= 64;
  231. }
  232. if (ilen > 0) {
  233. memcpy ((void *) (ctx->buffer + left), (void *) input, ilen);
  234. }
  235. }
  236. static const unsigned char sha1_padding[64] = {
  237. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  238. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  239. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  240. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  241. };
  242. /*
  243. * SHA-1 final digest
  244. */
  245. void sha1_finish (sha1_context * ctx, unsigned char output[20])
  246. {
  247. unsigned long last, padn;
  248. unsigned long high, low;
  249. unsigned char msglen[8];
  250. high = (ctx->total[0] >> 29)
  251. | (ctx->total[1] << 3);
  252. low = (ctx->total[0] << 3);
  253. PUT_UINT32_BE (high, msglen, 0);
  254. PUT_UINT32_BE (low, msglen, 4);
  255. last = ctx->total[0] & 0x3F;
  256. padn = (last < 56) ? (56 - last) : (120 - last);
  257. sha1_update (ctx, (unsigned char *) sha1_padding, padn);
  258. sha1_update (ctx, msglen, 8);
  259. PUT_UINT32_BE (ctx->state[0], output, 0);
  260. PUT_UINT32_BE (ctx->state[1], output, 4);
  261. PUT_UINT32_BE (ctx->state[2], output, 8);
  262. PUT_UINT32_BE (ctx->state[3], output, 12);
  263. PUT_UINT32_BE (ctx->state[4], output, 16);
  264. }
  265. /*
  266. * Output = SHA-1( input buffer )
  267. */
  268. void sha1_csum (unsigned char *input, int ilen, unsigned char output[20])
  269. {
  270. sha1_context ctx;
  271. sha1_starts (&ctx);
  272. sha1_update (&ctx, input, ilen);
  273. sha1_finish (&ctx, output);
  274. }
  275. /*
  276. * Output = SHA-1( input buffer ). Trigger the watchdog every 'chunk_sz'
  277. * bytes of input processed.
  278. */
  279. void sha1_csum_wd (unsigned char *input, int ilen, unsigned char output[20],
  280. unsigned int chunk_sz)
  281. {
  282. sha1_context ctx;
  283. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  284. unsigned char *end, *curr;
  285. int chunk;
  286. #endif
  287. sha1_starts (&ctx);
  288. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  289. curr = input;
  290. end = input + ilen;
  291. while (curr < end) {
  292. chunk = end - curr;
  293. if (chunk > chunk_sz)
  294. chunk = chunk_sz;
  295. sha1_update (&ctx, curr, chunk);
  296. curr += chunk;
  297. WATCHDOG_RESET ();
  298. }
  299. #else
  300. sha1_update (&ctx, input, ilen);
  301. #endif
  302. sha1_finish (&ctx, output);
  303. }
  304. /*
  305. * Output = HMAC-SHA-1( input buffer, hmac key )
  306. */
  307. void sha1_hmac (unsigned char *key, int keylen,
  308. unsigned char *input, int ilen, unsigned char output[20])
  309. {
  310. int i;
  311. sha1_context ctx;
  312. unsigned char k_ipad[64];
  313. unsigned char k_opad[64];
  314. unsigned char tmpbuf[20];
  315. memset (k_ipad, 0x36, 64);
  316. memset (k_opad, 0x5C, 64);
  317. for (i = 0; i < keylen; i++) {
  318. if (i >= 64)
  319. break;
  320. k_ipad[i] ^= key[i];
  321. k_opad[i] ^= key[i];
  322. }
  323. sha1_starts (&ctx);
  324. sha1_update (&ctx, k_ipad, 64);
  325. sha1_update (&ctx, input, ilen);
  326. sha1_finish (&ctx, tmpbuf);
  327. sha1_starts (&ctx);
  328. sha1_update (&ctx, k_opad, 64);
  329. sha1_update (&ctx, tmpbuf, 20);
  330. sha1_finish (&ctx, output);
  331. memset (k_ipad, 0, 64);
  332. memset (k_opad, 0, 64);
  333. memset (tmpbuf, 0, 20);
  334. memset (&ctx, 0, sizeof (sha1_context));
  335. }
  336. static const char _sha1_src[] = "_sha1_src";
  337. #ifdef SELF_TEST
  338. /*
  339. * FIPS-180-1 test vectors
  340. */
  341. static const char sha1_test_str[3][57] = {
  342. {"abc"},
  343. {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
  344. {""}
  345. };
  346. static const unsigned char sha1_test_sum[3][20] = {
  347. {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  348. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D},
  349. {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  350. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1},
  351. {0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  352. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F}
  353. };
  354. /*
  355. * Checkup routine
  356. */
  357. int sha1_self_test (void)
  358. {
  359. int i, j;
  360. unsigned char buf[1000];
  361. unsigned char sha1sum[20];
  362. sha1_context ctx;
  363. for (i = 0; i < 3; i++) {
  364. printf (" SHA-1 test #%d: ", i + 1);
  365. sha1_starts (&ctx);
  366. if (i < 2)
  367. sha1_update (&ctx, (unsigned char *) sha1_test_str[i],
  368. strlen (sha1_test_str[i]));
  369. else {
  370. memset (buf, 'a', 1000);
  371. for (j = 0; j < 1000; j++)
  372. sha1_update (&ctx, buf, 1000);
  373. }
  374. sha1_finish (&ctx, sha1sum);
  375. if (memcmp (sha1sum, sha1_test_sum[i], 20) != 0) {
  376. printf ("failed\n");
  377. return (1);
  378. }
  379. printf ("passed\n");
  380. }
  381. printf ("\n");
  382. return (0);
  383. }
  384. #else
  385. int sha1_self_test (void)
  386. {
  387. return (0);
  388. }
  389. #endif