sha3.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 (c) 2013, Aleksey Kravchenko <[email protected]>
  7. *
  8. * Permission to use, copy, modify, and/or distribute this software for any
  9. * purpose with or without fee is hereby granted.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  12. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  13. * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  14. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  16. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  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. #define XORED_A(i) A[(i)] ^ A[(i) + 5] ^ A[(i) + 10] ^ A[(i) + 15] ^ A[(i) + 20]
  80. #define THETA_STEP(i) \
  81. A[(i)] ^= D[(i)]; \
  82. A[(i) + 5] ^= D[(i)]; \
  83. A[(i) + 10] ^= D[(i)]; \
  84. A[(i) + 15] ^= D[(i)]; \
  85. A[(i) + 20] ^= D[(i)] \
  86. /* Keccak theta() transformation */
  87. static void keccak_theta(uint64_t* A)
  88. {
  89. uint64_t D[5];
  90. D[0] = ROTL64(XORED_A(1), 1) ^ XORED_A(4);
  91. D[1] = ROTL64(XORED_A(2), 1) ^ XORED_A(0);
  92. D[2] = ROTL64(XORED_A(3), 1) ^ XORED_A(1);
  93. D[3] = ROTL64(XORED_A(4), 1) ^ XORED_A(2);
  94. D[4] = ROTL64(XORED_A(0), 1) ^ XORED_A(3);
  95. THETA_STEP(0);
  96. THETA_STEP(1);
  97. THETA_STEP(2);
  98. THETA_STEP(3);
  99. THETA_STEP(4);
  100. }
  101. /* Keccak pi() transformation */
  102. static void keccak_pi(uint64_t* A)
  103. {
  104. uint64_t A1;
  105. A1 = A[1];
  106. A[ 1] = A[ 6];
  107. A[ 6] = A[ 9];
  108. A[ 9] = A[22];
  109. A[22] = A[14];
  110. A[14] = A[20];
  111. A[20] = A[ 2];
  112. A[ 2] = A[12];
  113. A[12] = A[13];
  114. A[13] = A[19];
  115. A[19] = A[23];
  116. A[23] = A[15];
  117. A[15] = A[ 4];
  118. A[ 4] = A[24];
  119. A[24] = A[21];
  120. A[21] = A[ 8];
  121. A[ 8] = A[16];
  122. A[16] = A[ 5];
  123. A[ 5] = A[ 3];
  124. A[ 3] = A[18];
  125. A[18] = A[17];
  126. A[17] = A[11];
  127. A[11] = A[ 7];
  128. A[ 7] = A[10];
  129. A[10] = A1;
  130. /* note: A[ 0] is left as is */
  131. }
  132. #define CHI_STEP(i) \
  133. A0 = A[0 + (i)]; \
  134. A1 = A[1 + (i)]; \
  135. A[0 + (i)] ^= ~A1 & A[2 + (i)]; \
  136. A[1 + (i)] ^= ~A[2 + (i)] & A[3 + (i)]; \
  137. A[2 + (i)] ^= ~A[3 + (i)] & A[4 + (i)]; \
  138. A[3 + (i)] ^= ~A[4 + (i)] & A0; \
  139. A[4 + (i)] ^= ~A0 & A1 \
  140. /* Keccak chi() transformation */
  141. static void keccak_chi(uint64_t* A)
  142. {
  143. uint64_t A0, A1;
  144. CHI_STEP(0);
  145. CHI_STEP(5);
  146. CHI_STEP(10);
  147. CHI_STEP(15);
  148. CHI_STEP(20);
  149. }
  150. static void rhash_sha3_permutation(uint64_t* state)
  151. {
  152. int round;
  153. for (round = 0; round < NumberOfRounds; round++)
  154. {
  155. keccak_theta(state);
  156. /* apply Keccak rho() transformation */
  157. state[ 1] = ROTL64(state[ 1], 1);
  158. state[ 2] = ROTL64(state[ 2], 62);
  159. state[ 3] = ROTL64(state[ 3], 28);
  160. state[ 4] = ROTL64(state[ 4], 27);
  161. state[ 5] = ROTL64(state[ 5], 36);
  162. state[ 6] = ROTL64(state[ 6], 44);
  163. state[ 7] = ROTL64(state[ 7], 6);
  164. state[ 8] = ROTL64(state[ 8], 55);
  165. state[ 9] = ROTL64(state[ 9], 20);
  166. state[10] = ROTL64(state[10], 3);
  167. state[11] = ROTL64(state[11], 10);
  168. state[12] = ROTL64(state[12], 43);
  169. state[13] = ROTL64(state[13], 25);
  170. state[14] = ROTL64(state[14], 39);
  171. state[15] = ROTL64(state[15], 41);
  172. state[16] = ROTL64(state[16], 45);
  173. state[17] = ROTL64(state[17], 15);
  174. state[18] = ROTL64(state[18], 21);
  175. state[19] = ROTL64(state[19], 8);
  176. state[20] = ROTL64(state[20], 18);
  177. state[21] = ROTL64(state[21], 2);
  178. state[22] = ROTL64(state[22], 61);
  179. state[23] = ROTL64(state[23], 56);
  180. state[24] = ROTL64(state[24], 14);
  181. keccak_pi(state);
  182. keccak_chi(state);
  183. /* apply iota(state, round) */
  184. *state ^= keccak_round_constants[round];
  185. }
  186. }
  187. /**
  188. * The core transformation. Process the specified block of data.
  189. *
  190. * @param hash the algorithm state
  191. * @param block the message block to process
  192. * @param block_size the size of the processed block in bytes
  193. */
  194. static void rhash_sha3_process_block(uint64_t hash[25], const uint64_t* block, size_t block_size)
  195. {
  196. /* expanded loop */
  197. hash[ 0] ^= le2me_64(block[ 0]);
  198. hash[ 1] ^= le2me_64(block[ 1]);
  199. hash[ 2] ^= le2me_64(block[ 2]);
  200. hash[ 3] ^= le2me_64(block[ 3]);
  201. hash[ 4] ^= le2me_64(block[ 4]);
  202. hash[ 5] ^= le2me_64(block[ 5]);
  203. hash[ 6] ^= le2me_64(block[ 6]);
  204. hash[ 7] ^= le2me_64(block[ 7]);
  205. hash[ 8] ^= le2me_64(block[ 8]);
  206. /* if not sha3-512 */
  207. if (block_size > 72) {
  208. hash[ 9] ^= le2me_64(block[ 9]);
  209. hash[10] ^= le2me_64(block[10]);
  210. hash[11] ^= le2me_64(block[11]);
  211. hash[12] ^= le2me_64(block[12]);
  212. /* if not sha3-384 */
  213. if (block_size > 104) {
  214. hash[13] ^= le2me_64(block[13]);
  215. hash[14] ^= le2me_64(block[14]);
  216. hash[15] ^= le2me_64(block[15]);
  217. hash[16] ^= le2me_64(block[16]);
  218. /* if not sha3-256 */
  219. if (block_size > 136) {
  220. hash[17] ^= le2me_64(block[17]);
  221. #ifdef FULL_SHA3_FAMILY_SUPPORT
  222. /* if not sha3-224 */
  223. if (block_size > 144) {
  224. hash[18] ^= le2me_64(block[18]);
  225. hash[19] ^= le2me_64(block[19]);
  226. hash[20] ^= le2me_64(block[20]);
  227. hash[21] ^= le2me_64(block[21]);
  228. hash[22] ^= le2me_64(block[22]);
  229. hash[23] ^= le2me_64(block[23]);
  230. hash[24] ^= le2me_64(block[24]);
  231. }
  232. #endif
  233. }
  234. }
  235. }
  236. /* make a permutation of the hash */
  237. rhash_sha3_permutation(hash);
  238. }
  239. #define SHA3_FINALIZED 0x80000000
  240. /**
  241. * Calculate message hash.
  242. * Can be called repeatedly with chunks of the message to be hashed.
  243. *
  244. * @param ctx the algorithm context containing current hashing state
  245. * @param msg message chunk
  246. * @param size length of the message chunk
  247. */
  248. void rhash_sha3_update(sha3_ctx* ctx, const unsigned char* msg, size_t size)
  249. {
  250. size_t index = (size_t)ctx->rest;
  251. size_t block_size = (size_t)ctx->block_size;
  252. if (ctx->rest & SHA3_FINALIZED) return; /* too late for additional input */
  253. ctx->rest = (unsigned)((ctx->rest + size) % block_size);
  254. /* fill partial block */
  255. if (index) {
  256. size_t left = block_size - index;
  257. memcpy((char*)ctx->message + index, msg, (size < left ? size : left));
  258. if (size < left) return;
  259. /* process partial block */
  260. rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
  261. msg += left;
  262. size -= left;
  263. }
  264. while (size >= block_size) {
  265. uint64_t* aligned_message_block;
  266. if (IS_ALIGNED_64(msg)) {
  267. /* the most common case is processing of an already aligned message
  268. without copying it */
  269. aligned_message_block = (uint64_t*)msg;
  270. } else {
  271. memcpy(ctx->message, msg, block_size);
  272. aligned_message_block = ctx->message;
  273. }
  274. rhash_sha3_process_block(ctx->hash, aligned_message_block, block_size);
  275. msg += block_size;
  276. size -= block_size;
  277. }
  278. if (size) {
  279. memcpy(ctx->message, msg, size); /* save leftovers */
  280. }
  281. }
  282. /**
  283. * Store calculated hash into the given array.
  284. *
  285. * @param ctx the algorithm context containing current hashing state
  286. * @param result calculated hash in binary form
  287. */
  288. void rhash_sha3_final(sha3_ctx* ctx, unsigned char* result)
  289. {
  290. size_t digest_length = 100 - ctx->block_size / 2;
  291. const size_t block_size = ctx->block_size;
  292. if (!(ctx->rest & SHA3_FINALIZED))
  293. {
  294. /* clear the rest of the data queue */
  295. memset((char*)ctx->message + ctx->rest, 0, block_size - ctx->rest);
  296. ((char*)ctx->message)[ctx->rest] |= 0x06;
  297. ((char*)ctx->message)[block_size - 1] |= 0x80;
  298. /* process final block */
  299. rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
  300. ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
  301. }
  302. assert(block_size > digest_length);
  303. if (result) me64_to_le_str(result, ctx->hash, digest_length);
  304. }
  305. #ifdef USE_KECCAK
  306. /**
  307. * Store calculated hash into the given array.
  308. *
  309. * @param ctx the algorithm context containing current hashing state
  310. * @param result calculated hash in binary form
  311. */
  312. void rhash_keccak_final(sha3_ctx* ctx, unsigned char* result)
  313. {
  314. size_t digest_length = 100 - ctx->block_size / 2;
  315. const size_t block_size = ctx->block_size;
  316. if (!(ctx->rest & SHA3_FINALIZED))
  317. {
  318. /* clear the rest of the data queue */
  319. memset((char*)ctx->message + ctx->rest, 0, block_size - ctx->rest);
  320. ((char*)ctx->message)[ctx->rest] |= 0x01;
  321. ((char*)ctx->message)[block_size - 1] |= 0x80;
  322. /* process final block */
  323. rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
  324. ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
  325. }
  326. assert(block_size > digest_length);
  327. if (result) me64_to_le_str(result, ctx->hash, digest_length);
  328. }
  329. #endif /* USE_KECCAK */