sha3.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* sha3.c - an implementation of Secure Hash Algorithm 3 (Keccak).
  2. * based on the
  3. * The Keccak SHA-3 submission. Submission to NIST (Round 3), 2011
  4. * by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche
  5. *
  6. * Copyright: 2013 Aleksey Kravchenko <rhash.admin@gmail.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
  18. */
  19. #include <assert.h>
  20. #include <string.h>
  21. #include "byte_order.h"
  22. #include "sha3.h"
  23. /* constants */
  24. #define NumberOfRounds 24
  25. /* SHA3 (Keccak) constants for 24 rounds */
  26. static uint64_t keccak_round_constants[NumberOfRounds] = {
  27. I64(0x0000000000000001), I64(0x0000000000008082), I64(0x800000000000808A), I64(0x8000000080008000),
  28. I64(0x000000000000808B), I64(0x0000000080000001), I64(0x8000000080008081), I64(0x8000000000008009),
  29. I64(0x000000000000008A), I64(0x0000000000000088), I64(0x0000000080008009), I64(0x000000008000000A),
  30. I64(0x000000008000808B), I64(0x800000000000008B), I64(0x8000000000008089), I64(0x8000000000008003),
  31. I64(0x8000000000008002), I64(0x8000000000000080), I64(0x000000000000800A), I64(0x800000008000000A),
  32. I64(0x8000000080008081), I64(0x8000000000008080), I64(0x0000000080000001), I64(0x8000000080008008)
  33. };
  34. /* Initializing a sha3 context for given number of output bits */
  35. static void rhash_keccak_init(sha3_ctx *ctx, unsigned bits)
  36. {
  37. /* NB: The Keccak capacity parameter = bits * 2 */
  38. unsigned rate = 1600 - bits * 2;
  39. memset(ctx, 0, sizeof(sha3_ctx));
  40. ctx->block_size = rate / 8;
  41. assert(rate <= 1600 && (rate % 64) == 0);
  42. }
  43. /**
  44. * Initialize context before calculating hash.
  45. *
  46. * @param ctx context to initialize
  47. */
  48. void rhash_sha3_224_init(sha3_ctx *ctx)
  49. {
  50. rhash_keccak_init(ctx, 224);
  51. }
  52. /**
  53. * Initialize context before calculating hash.
  54. *
  55. * @param ctx context to initialize
  56. */
  57. void rhash_sha3_256_init(sha3_ctx *ctx)
  58. {
  59. rhash_keccak_init(ctx, 256);
  60. }
  61. /**
  62. * Initialize context before calculating hash.
  63. *
  64. * @param ctx context to initialize
  65. */
  66. void rhash_sha3_384_init(sha3_ctx *ctx)
  67. {
  68. rhash_keccak_init(ctx, 384);
  69. }
  70. /**
  71. * Initialize context before calculating hash.
  72. *
  73. * @param ctx context to initialize
  74. */
  75. void rhash_sha3_512_init(sha3_ctx *ctx)
  76. {
  77. rhash_keccak_init(ctx, 512);
  78. }
  79. /* Keccak theta() transformation */
  80. static void keccak_theta(uint64_t *A)
  81. {
  82. unsigned int x;
  83. uint64_t C[5], D[5];
  84. for (x = 0; x < 5; x++) {
  85. C[x] = A[x] ^ A[x + 5] ^ A[x + 10] ^ A[x + 15] ^ A[x + 20];
  86. }
  87. D[0] = ROTL64(C[1], 1) ^ C[4];
  88. D[1] = ROTL64(C[2], 1) ^ C[0];
  89. D[2] = ROTL64(C[3], 1) ^ C[1];
  90. D[3] = ROTL64(C[4], 1) ^ C[2];
  91. D[4] = ROTL64(C[0], 1) ^ C[3];
  92. for (x = 0; x < 5; x++) {
  93. A[x] ^= D[x];
  94. A[x + 5] ^= D[x];
  95. A[x + 10] ^= D[x];
  96. A[x + 15] ^= D[x];
  97. A[x + 20] ^= D[x];
  98. }
  99. }
  100. /* Keccak pi() transformation */
  101. static void keccak_pi(uint64_t *A)
  102. {
  103. uint64_t A1;
  104. A1 = A[1];
  105. A[ 1] = A[ 6];
  106. A[ 6] = A[ 9];
  107. A[ 9] = A[22];
  108. A[22] = A[14];
  109. A[14] = A[20];
  110. A[20] = A[ 2];
  111. A[ 2] = A[12];
  112. A[12] = A[13];
  113. A[13] = A[19];
  114. A[19] = A[23];
  115. A[23] = A[15];
  116. A[15] = A[ 4];
  117. A[ 4] = A[24];
  118. A[24] = A[21];
  119. A[21] = A[ 8];
  120. A[ 8] = A[16];
  121. A[16] = A[ 5];
  122. A[ 5] = A[ 3];
  123. A[ 3] = A[18];
  124. A[18] = A[17];
  125. A[17] = A[11];
  126. A[11] = A[ 7];
  127. A[ 7] = A[10];
  128. A[10] = A1;
  129. /* note: A[ 0] is left as is */
  130. }
  131. /* Keccak chi() transformation */
  132. static void keccak_chi(uint64_t *A)
  133. {
  134. int i;
  135. for (i = 0; i < 25; i += 5) {
  136. uint64_t A0 = A[0 + i], A1 = A[1 + i];
  137. A[0 + i] ^= ~A1 & A[2 + i];
  138. A[1 + i] ^= ~A[2 + i] & A[3 + i];
  139. A[2 + i] ^= ~A[3 + i] & A[4 + i];
  140. A[3 + i] ^= ~A[4 + i] & A0;
  141. A[4 + i] ^= ~A0 & A1;
  142. }
  143. }
  144. static void rhash_sha3_permutation(uint64_t *state)
  145. {
  146. int round;
  147. for (round = 0; round < NumberOfRounds; round++)
  148. {
  149. keccak_theta(state);
  150. /* apply Keccak rho() transformation */
  151. state[ 1] = ROTL64(state[ 1], 1);
  152. state[ 2] = ROTL64(state[ 2], 62);
  153. state[ 3] = ROTL64(state[ 3], 28);
  154. state[ 4] = ROTL64(state[ 4], 27);
  155. state[ 5] = ROTL64(state[ 5], 36);
  156. state[ 6] = ROTL64(state[ 6], 44);
  157. state[ 7] = ROTL64(state[ 7], 6);
  158. state[ 8] = ROTL64(state[ 8], 55);
  159. state[ 9] = ROTL64(state[ 9], 20);
  160. state[10] = ROTL64(state[10], 3);
  161. state[11] = ROTL64(state[11], 10);
  162. state[12] = ROTL64(state[12], 43);
  163. state[13] = ROTL64(state[13], 25);
  164. state[14] = ROTL64(state[14], 39);
  165. state[15] = ROTL64(state[15], 41);
  166. state[16] = ROTL64(state[16], 45);
  167. state[17] = ROTL64(state[17], 15);
  168. state[18] = ROTL64(state[18], 21);
  169. state[19] = ROTL64(state[19], 8);
  170. state[20] = ROTL64(state[20], 18);
  171. state[21] = ROTL64(state[21], 2);
  172. state[22] = ROTL64(state[22], 61);
  173. state[23] = ROTL64(state[23], 56);
  174. state[24] = ROTL64(state[24], 14);
  175. keccak_pi(state);
  176. keccak_chi(state);
  177. /* apply iota(state, round) */
  178. *state ^= keccak_round_constants[round];
  179. }
  180. }
  181. /**
  182. * The core transformation. Process the specified block of data.
  183. *
  184. * @param hash the algorithm state
  185. * @param block the message block to process
  186. * @param block_size the size of the processed block in bytes
  187. */
  188. static void rhash_sha3_process_block(uint64_t hash[25], const uint64_t *block, size_t block_size)
  189. {
  190. /* expanded loop */
  191. hash[ 0] ^= le2me_64(block[ 0]);
  192. hash[ 1] ^= le2me_64(block[ 1]);
  193. hash[ 2] ^= le2me_64(block[ 2]);
  194. hash[ 3] ^= le2me_64(block[ 3]);
  195. hash[ 4] ^= le2me_64(block[ 4]);
  196. hash[ 5] ^= le2me_64(block[ 5]);
  197. hash[ 6] ^= le2me_64(block[ 6]);
  198. hash[ 7] ^= le2me_64(block[ 7]);
  199. hash[ 8] ^= le2me_64(block[ 8]);
  200. /* if not sha3-512 */
  201. if (block_size > 72) {
  202. hash[ 9] ^= le2me_64(block[ 9]);
  203. hash[10] ^= le2me_64(block[10]);
  204. hash[11] ^= le2me_64(block[11]);
  205. hash[12] ^= le2me_64(block[12]);
  206. /* if not sha3-384 */
  207. if (block_size > 104) {
  208. hash[13] ^= le2me_64(block[13]);
  209. hash[14] ^= le2me_64(block[14]);
  210. hash[15] ^= le2me_64(block[15]);
  211. hash[16] ^= le2me_64(block[16]);
  212. /* if not sha3-256 */
  213. if (block_size > 136) {
  214. hash[17] ^= le2me_64(block[17]);
  215. #ifdef FULL_SHA3_FAMILY_SUPPORT
  216. /* if not sha3-224 */
  217. if (block_size > 144) {
  218. hash[18] ^= le2me_64(block[18]);
  219. hash[19] ^= le2me_64(block[19]);
  220. hash[20] ^= le2me_64(block[20]);
  221. hash[21] ^= le2me_64(block[21]);
  222. hash[22] ^= le2me_64(block[22]);
  223. hash[23] ^= le2me_64(block[23]);
  224. hash[24] ^= le2me_64(block[24]);
  225. }
  226. #endif
  227. }
  228. }
  229. }
  230. /* make a permutation of the hash */
  231. rhash_sha3_permutation(hash);
  232. }
  233. #define SHA3_FINALIZED 0x80000000
  234. /**
  235. * Calculate message hash.
  236. * Can be called repeatedly with chunks of the message to be hashed.
  237. *
  238. * @param ctx the algorithm context containing current hashing state
  239. * @param msg message chunk
  240. * @param size length of the message chunk
  241. */
  242. void rhash_sha3_update(sha3_ctx *ctx, const unsigned char *msg, size_t size)
  243. {
  244. size_t index = (size_t)ctx->rest;
  245. size_t block_size = (size_t)ctx->block_size;
  246. if (ctx->rest & SHA3_FINALIZED) return; /* too late for additional input */
  247. ctx->rest = (unsigned)((ctx->rest + size) % block_size);
  248. /* fill partial block */
  249. if (index) {
  250. size_t left = block_size - index;
  251. memcpy((char*)ctx->message + index, msg, (size < left ? size : left));
  252. if (size < left) return;
  253. /* process partial block */
  254. rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
  255. msg += left;
  256. size -= left;
  257. }
  258. while (size >= block_size) {
  259. uint64_t* aligned_message_block;
  260. if (IS_ALIGNED_64(msg)) {
  261. /* the most common case is processing of an already aligned message
  262. without copying it */
  263. aligned_message_block = (uint64_t*)msg;
  264. } else {
  265. memcpy(ctx->message, msg, block_size);
  266. aligned_message_block = ctx->message;
  267. }
  268. rhash_sha3_process_block(ctx->hash, aligned_message_block, block_size);
  269. msg += block_size;
  270. size -= block_size;
  271. }
  272. if (size) {
  273. memcpy(ctx->message, msg, size); /* save leftovers */
  274. }
  275. }
  276. /**
  277. * Store calculated hash into the given array.
  278. *
  279. * @param ctx the algorithm context containing current hashing state
  280. * @param result calculated hash in binary form
  281. */
  282. void rhash_sha3_final(sha3_ctx *ctx, unsigned char* result)
  283. {
  284. size_t digest_length = 100 - ctx->block_size / 2;
  285. const size_t block_size = ctx->block_size;
  286. if (!(ctx->rest & SHA3_FINALIZED))
  287. {
  288. /* clear the rest of the data queue */
  289. memset((char*)ctx->message + ctx->rest, 0, block_size - ctx->rest);
  290. ((char*)ctx->message)[ctx->rest] |= 0x06;
  291. ((char*)ctx->message)[block_size - 1] |= 0x80;
  292. /* process final block */
  293. rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
  294. ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
  295. }
  296. assert(block_size > digest_length);
  297. if (result) me64_to_le_str(result, ctx->hash, digest_length);
  298. }
  299. #ifdef USE_KECCAK
  300. /**
  301. * Store calculated hash into the given array.
  302. *
  303. * @param ctx the algorithm context containing current hashing state
  304. * @param result calculated hash in binary form
  305. */
  306. void rhash_keccak_final(sha3_ctx *ctx, unsigned char* result)
  307. {
  308. size_t digest_length = 100 - ctx->block_size / 2;
  309. const size_t block_size = ctx->block_size;
  310. if (!(ctx->rest & SHA3_FINALIZED))
  311. {
  312. /* clear the rest of the data queue */
  313. memset((char*)ctx->message + ctx->rest, 0, block_size - ctx->rest);
  314. ((char*)ctx->message)[ctx->rest] |= 0x01;
  315. ((char*)ctx->message)[block_size - 1] |= 0x80;
  316. /* process final block */
  317. rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
  318. ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
  319. }
  320. assert(block_size > digest_length);
  321. if (result) me64_to_le_str(result, ctx->hash, digest_length);
  322. }
  323. #endif /* USE_KECCAK */