SHA1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* ===================================================================
  2. *
  3. * Copyright (c) 2018, Helder Eijs <helderijs@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. * ===================================================================
  30. */
  31. #include <stdio.h>
  32. #include "common.h"
  33. #include "endianess.h"
  34. FAKE_INIT(SHA1)
  35. /**
  36. * SHA-1 as defined in FIPS 180-4 http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
  37. */
  38. #define CH(x,y,z) ((x & y) ^ (~x & z)) /** 0 <= t <= 19 **/
  39. #define PARITY(x,y,z) (x ^ y ^ z) /** 20 <= t <= 39 and 60 <= t <= 79 **/
  40. #define MAJ(x,y,z) ((x & y) ^ (x & z) ^ (y & z)) /** 40 <= t <= 59 **/
  41. #define ROTL1(x) (((x)<<1) | ((x)>>(32-1)))
  42. #define ROTL5(x) (((x)<<5) | ((x)>>(32-5)))
  43. #define ROTL30(x) (((x)<<30) | ((x)>>(32-30)))
  44. #define Kx 0x5a827999 /** 0 <= t <= 19 **/
  45. #define Ky 0x6ed9eba1 /** 20 <= t <= 39 **/
  46. #define Kz 0x8f1bbcdc /** 40 <= t <= 59 **/
  47. #define Kw 0xca62c1d6 /** 60 <= t <= 79 **/
  48. /** Compute and update W[t] for t>=16 **/
  49. #define SCHED(t) (W[t&15]=ROTL1(W[(t-3)&15] ^ W[(t-8)&15] ^ W[(t-14)&15] ^ W[t&15]))
  50. #define ROUND_0_15(t) { \
  51. uint32_t T; \
  52. T = ROTL5(a) + CH(b,c,d) + e + Kx + W[t]; \
  53. e = d; \
  54. d = c; \
  55. c = ROTL30(b); \
  56. b = a; \
  57. a = T; }
  58. #define ROUND_16_19(t) { \
  59. uint32_t T; \
  60. T = ROTL5(a) + CH(b,c,d) + e + Kx + SCHED(t); \
  61. e = d; \
  62. d = c; \
  63. c = ROTL30(b); \
  64. b = a; \
  65. a = T; }
  66. #define ROUND_20_39(t) { \
  67. uint32_t T; \
  68. T = ROTL5(a) + PARITY(b,c,d) + e + Ky + SCHED(t); \
  69. e = d; \
  70. d = c; \
  71. c = ROTL30(b); \
  72. b = a; \
  73. a = T; }
  74. #define ROUND_40_59(t) { \
  75. uint32_t T; \
  76. T = ROTL5(a) + MAJ(b,c,d) + e + Kz + SCHED(t); \
  77. e = d; \
  78. d = c; \
  79. c = ROTL30(b); \
  80. b = a; \
  81. a = T; }
  82. #define ROUND_60_79(t) { \
  83. uint32_t T; \
  84. T = ROTL5(a) + PARITY(b,c,d) + e + Kw + SCHED(t); \
  85. e = d; \
  86. d = c; \
  87. c = ROTL30(b); \
  88. b = a; \
  89. a = T; }
  90. #define BLOCK_SIZE 64
  91. #define DIGEST_SIZE (160/8)
  92. typedef struct t_hash_state {
  93. uint32_t h[5];
  94. uint8_t buf[BLOCK_SIZE]; /** 64 bytes == 512 bits == sixteen 32-bit words **/
  95. unsigned curlen; /** Useful message bytes in buf[] (leftmost) **/
  96. uint64_t totbits; /** Total message length in bits **/
  97. } hash_state;
  98. static int add_bits(hash_state *hs, unsigned bits)
  99. {
  100. /** Maximum message length for SHA-1 is 2**64 bits **/
  101. hs->totbits += bits;
  102. return (hs->totbits < bits) ? ERR_MAX_DATA : 0;
  103. }
  104. static void sha_compress(hash_state * hs)
  105. {
  106. uint32_t a, b, c, d, e;
  107. uint32_t W[16];
  108. int i;
  109. /** Words flow in in big-endian mode **/
  110. for (i=0; i<16; i++) {
  111. W[i] = LOAD_U32_BIG(&hs->buf[i*4]);
  112. }
  113. a = hs->h[0];
  114. b = hs->h[1];
  115. c = hs->h[2];
  116. d = hs->h[3];
  117. e = hs->h[4];
  118. /** 0 <= t <= 15 **/
  119. ROUND_0_15(0);
  120. ROUND_0_15(1);
  121. ROUND_0_15(2);
  122. ROUND_0_15(3);
  123. ROUND_0_15(4);
  124. ROUND_0_15(5);
  125. ROUND_0_15(6);
  126. ROUND_0_15(7);
  127. ROUND_0_15(8);
  128. ROUND_0_15(9);
  129. ROUND_0_15(10);
  130. ROUND_0_15(11);
  131. ROUND_0_15(12);
  132. ROUND_0_15(13);
  133. ROUND_0_15(14);
  134. ROUND_0_15(15);
  135. /** 16 <= t <= 19 **/
  136. ROUND_16_19(16);
  137. ROUND_16_19(17);
  138. ROUND_16_19(18);
  139. ROUND_16_19(19);
  140. /** 20 <= t <= 39 **/
  141. ROUND_20_39(20);
  142. ROUND_20_39(21);
  143. ROUND_20_39(22);
  144. ROUND_20_39(23);
  145. ROUND_20_39(24);
  146. ROUND_20_39(25);
  147. ROUND_20_39(26);
  148. ROUND_20_39(27);
  149. ROUND_20_39(28);
  150. ROUND_20_39(29);
  151. ROUND_20_39(30);
  152. ROUND_20_39(31);
  153. ROUND_20_39(32);
  154. ROUND_20_39(33);
  155. ROUND_20_39(34);
  156. ROUND_20_39(35);
  157. ROUND_20_39(36);
  158. ROUND_20_39(37);
  159. ROUND_20_39(38);
  160. ROUND_20_39(39);
  161. /** 40 <= t <= 59 **/
  162. ROUND_40_59(40);
  163. ROUND_40_59(41);
  164. ROUND_40_59(42);
  165. ROUND_40_59(43);
  166. ROUND_40_59(44);
  167. ROUND_40_59(45);
  168. ROUND_40_59(46);
  169. ROUND_40_59(47);
  170. ROUND_40_59(48);
  171. ROUND_40_59(49);
  172. ROUND_40_59(50);
  173. ROUND_40_59(51);
  174. ROUND_40_59(52);
  175. ROUND_40_59(53);
  176. ROUND_40_59(54);
  177. ROUND_40_59(55);
  178. ROUND_40_59(56);
  179. ROUND_40_59(57);
  180. ROUND_40_59(58);
  181. ROUND_40_59(59);
  182. /** 60 <= t <= 79 **/
  183. ROUND_60_79(60);
  184. ROUND_60_79(61);
  185. ROUND_60_79(62);
  186. ROUND_60_79(63);
  187. ROUND_60_79(64);
  188. ROUND_60_79(65);
  189. ROUND_60_79(66);
  190. ROUND_60_79(67);
  191. ROUND_60_79(68);
  192. ROUND_60_79(69);
  193. ROUND_60_79(70);
  194. ROUND_60_79(71);
  195. ROUND_60_79(72);
  196. ROUND_60_79(73);
  197. ROUND_60_79(74);
  198. ROUND_60_79(75);
  199. ROUND_60_79(76);
  200. ROUND_60_79(77);
  201. ROUND_60_79(78);
  202. ROUND_60_79(79);
  203. /** compute new intermediate hash **/
  204. hs->h[0] += a;
  205. hs->h[1] += b;
  206. hs->h[2] += c;
  207. hs->h[3] += d;
  208. hs->h[4] += e;
  209. }
  210. EXPORT_SYM int SHA1_init(hash_state **shaState)
  211. {
  212. hash_state *hs;
  213. if (NULL == shaState) {
  214. return ERR_NULL;
  215. }
  216. *shaState = hs = (hash_state*) calloc(1, sizeof(hash_state));
  217. if (NULL == hs)
  218. return ERR_MEMORY;
  219. hs->curlen = 0;
  220. hs->totbits = 0;
  221. /** Initial intermediate hash value **/
  222. hs->h[0] = 0x67452301;
  223. hs->h[1] = 0xefcdab89;
  224. hs->h[2] = 0x98badcfe;
  225. hs->h[3] = 0x10325476;
  226. hs->h[4] = 0xc3d2e1f0;
  227. return 0;
  228. }
  229. EXPORT_SYM int SHA1_destroy (hash_state *shaState)
  230. {
  231. free(shaState);
  232. return 0;
  233. }
  234. EXPORT_SYM int SHA1_update(hash_state *hs, const uint8_t *buf, size_t len)
  235. {
  236. if (NULL == hs || NULL == buf) {
  237. return ERR_NULL;
  238. }
  239. assert(hs->curlen < BLOCK_SIZE);
  240. while (len>0) {
  241. unsigned btc, left;
  242. left = BLOCK_SIZE - hs->curlen;
  243. btc = (unsigned)MIN(left, len);
  244. memcpy(&hs->buf[hs->curlen], buf, btc);
  245. buf += btc;
  246. hs->curlen += btc;
  247. len -= btc;
  248. if (hs->curlen == BLOCK_SIZE) {
  249. sha_compress(hs);
  250. hs->curlen = 0;
  251. if (add_bits(hs, BLOCK_SIZE*8)) {
  252. return ERR_MAX_DATA;
  253. }
  254. }
  255. }
  256. return 0;
  257. }
  258. static int sha_finalize(hash_state *hs, uint8_t *hash /** [DIGEST_SIZE] **/)
  259. {
  260. unsigned left, i;
  261. uint32_t lo, high;
  262. assert(hs->curlen < BLOCK_SIZE);
  263. /* remaining length of the message */
  264. if (add_bits(hs, hs->curlen*8)) {
  265. return ERR_MAX_DATA;
  266. }
  267. /* append the '1' bit */
  268. /* buf[] is guaranteed to have at least 1 byte free */
  269. hs->buf[hs->curlen++] = 0x80;
  270. /** if there are less then 64 bits lef, just pad with zeroes and compress **/
  271. left = BLOCK_SIZE - hs->curlen;
  272. if (left < 8) {
  273. memset(&hs->buf[hs->curlen], 0, left);
  274. sha_compress(hs);
  275. hs->curlen = 0;
  276. }
  277. /**
  278. * pad with zeroes and close the block with the bit length
  279. * encoded as 64-bit integer big endian.
  280. **/
  281. left = BLOCK_SIZE - hs->curlen;
  282. memset(&hs->buf[hs->curlen], 0, left);
  283. lo = (uint32_t)(hs->totbits >> 32);
  284. high = (uint32_t)hs->totbits;
  285. STORE_U32_BIG(&hs->buf[BLOCK_SIZE-8], lo);
  286. STORE_U32_BIG(&hs->buf[BLOCK_SIZE-4], high);
  287. /** compress one last time **/
  288. sha_compress(hs);
  289. /** create final hash **/
  290. for (i=0; i<5; i++) {
  291. STORE_U32_BIG(hash, hs->h[i]);
  292. hash += 4;
  293. }
  294. return 0;
  295. }
  296. EXPORT_SYM int SHA1_digest(const hash_state *shaState, uint8_t digest[DIGEST_SIZE])
  297. {
  298. hash_state temp;
  299. if (NULL == shaState) {
  300. return ERR_NULL;
  301. }
  302. temp = *shaState;
  303. sha_finalize(&temp, digest);
  304. return 0;
  305. }
  306. EXPORT_SYM int SHA1_copy(const hash_state *src, hash_state *dst)
  307. {
  308. if (NULL == src || NULL == dst) {
  309. return ERR_NULL;
  310. }
  311. *dst = *src;
  312. return 0;
  313. }
  314. /**
  315. * This is a specialized function to efficiently perform the inner loop of PBKDF2-HMAC.
  316. *
  317. * - inner, the hash after the inner padded secret has been absorbed
  318. * - outer, the hash after the outer padded secret has been absorbed
  319. * - first_hmac, the output of the first HMAC iteration (with salt and counter)
  320. * - result, the XOR of the HMACs from all iterations
  321. * - iterations, the total number of PBKDF2 iterations (>0)
  322. *
  323. * This function does not change the state of either hash.
  324. */
  325. EXPORT_SYM int SHA1_pbkdf2_hmac_assist(const hash_state *inner, const hash_state *outer,
  326. const uint8_t first_hmac[DIGEST_SIZE],
  327. uint8_t result[DIGEST_SIZE],
  328. size_t iterations)
  329. {
  330. hash_state inner_temp, outer_temp;
  331. size_t i;
  332. uint8_t last_hmac[DIGEST_SIZE];
  333. if (NULL == inner || NULL == outer || NULL == first_hmac || NULL == result) {
  334. return ERR_NULL;
  335. }
  336. if (iterations == 0) {
  337. return ERR_NR_ROUNDS;
  338. }
  339. memcpy(result, first_hmac, DIGEST_SIZE);
  340. memcpy(last_hmac, first_hmac, DIGEST_SIZE);
  341. for (i=1; i<iterations; i++) {
  342. int j;
  343. inner_temp = *inner;
  344. outer_temp = *outer;
  345. SHA1_update(&inner_temp, last_hmac, DIGEST_SIZE);
  346. sha_finalize(&inner_temp, last_hmac);
  347. /** last_hmac is now the intermediate digest **/
  348. SHA1_update(&outer_temp, last_hmac, DIGEST_SIZE);
  349. sha_finalize(&outer_temp, last_hmac);
  350. for (j=0; j<DIGEST_SIZE; j++) {
  351. result[j] ^= last_hmac[j];
  352. }
  353. }
  354. return 0;
  355. }
  356. #ifdef MAIN
  357. int main(void)
  358. {
  359. hash_state *hs;
  360. const uint8_t tv[] = "The quick brown fox jumps over the lazy dog";
  361. uint8_t result[DIGEST_SIZE];
  362. int i;
  363. SHA1_init(&hs);
  364. SHA1_update(hs, tv, sizeof tv - 1);
  365. SHA1_digest(hs, result);
  366. SHA1_destroy(hs);
  367. for (i=0; i<sizeof result; i++) {
  368. printf("%02X", result[i]);
  369. }
  370. printf("\n");
  371. SHA1_init(&hs);
  372. SHA1_digest(hs, result);
  373. SHA1_destroy(hs);
  374. for (i=0; i<sizeof result; i++) {
  375. printf("%02X", result[i]);
  376. }
  377. printf("\n");
  378. SHA1_init(&hs);
  379. for (i=0; i<10000000; i++) {
  380. SHA1_update(hs, tv, sizeof tv - 1);
  381. }
  382. SHA1_destroy(hs);
  383. printf("\n");
  384. }
  385. #endif