sha3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 "sha3.h"
  22. #include "memzero.h"
  23. #include "byte_order.h"
  24. #define I64(x) x##LL
  25. #define ROTL64(qword, n) ((qword) << (n) ^ ((qword) >> (64 - (n))))
  26. #define le2me_64(x) (x)
  27. #define IS_ALIGNED_64(p) (0 == (((uintptr_t)(const void *)(p) & 0x7)))
  28. # define me64_to_le_str(to, from, length) memcpy((to), (from), (length))
  29. /* constants */
  30. #define NumberOfRounds 24
  31. /* SHA3 (Keccak) constants for 24 rounds */
  32. static uint64_t keccak_round_constants[NumberOfRounds] = {
  33. I64(0x0000000000000001), I64(0x0000000000008082), I64(0x800000000000808A), I64(0x8000000080008000),
  34. I64(0x000000000000808B), I64(0x0000000080000001), I64(0x8000000080008081), I64(0x8000000000008009),
  35. I64(0x000000000000008A), I64(0x0000000000000088), I64(0x0000000080008009), I64(0x000000008000000A),
  36. I64(0x000000008000808B), I64(0x800000000000008B), I64(0x8000000000008089), I64(0x8000000000008003),
  37. I64(0x8000000000008002), I64(0x8000000000000080), I64(0x000000000000800A), I64(0x800000008000000A),
  38. I64(0x8000000080008081), I64(0x8000000000008080), I64(0x0000000080000001), I64(0x8000000080008008)
  39. };
  40. /* Initializing a sha3 context for given number of output bits */
  41. static void keccak_Init(SHA3_CTX *ctx, unsigned bits)
  42. {
  43. /* NB: The Keccak capacity parameter = bits * 2 */
  44. unsigned rate = 1600 - bits * 2;
  45. memzero(ctx, sizeof(SHA3_CTX));
  46. ctx->block_size = rate / 8;
  47. assert(rate <= 1600 && (rate % 64) == 0);
  48. }
  49. /**
  50. * Initialize context before calculating hash.
  51. *
  52. * @param ctx context to initialize
  53. */
  54. void sha3_224_Init(SHA3_CTX *ctx)
  55. {
  56. keccak_Init(ctx, 224);
  57. }
  58. /**
  59. * Initialize context before calculating hash.
  60. *
  61. * @param ctx context to initialize
  62. */
  63. void sha3_256_Init(SHA3_CTX *ctx)
  64. {
  65. keccak_Init(ctx, 256);
  66. }
  67. /**
  68. * Initialize context before calculating hash.
  69. *
  70. * @param ctx context to initialize
  71. */
  72. void sha3_384_Init(SHA3_CTX *ctx)
  73. {
  74. keccak_Init(ctx, 384);
  75. }
  76. /**
  77. * Initialize context before calculating hash.
  78. *
  79. * @param ctx context to initialize
  80. */
  81. void sha3_512_Init(SHA3_CTX *ctx)
  82. {
  83. keccak_Init(ctx, 512);
  84. }
  85. /* Keccak theta() transformation */
  86. static void keccak_theta(uint64_t *A)
  87. {
  88. unsigned int x = 0;
  89. uint64_t C[5] = {0}, D[5] = {0};
  90. for (x = 0; x < 5; x++) {
  91. C[x] = A[x] ^ A[x + 5] ^ A[x + 10] ^ A[x + 15] ^ A[x + 20];
  92. }
  93. D[0] = ROTL64(C[1], 1) ^ C[4];
  94. D[1] = ROTL64(C[2], 1) ^ C[0];
  95. D[2] = ROTL64(C[3], 1) ^ C[1];
  96. D[3] = ROTL64(C[4], 1) ^ C[2];
  97. D[4] = ROTL64(C[0], 1) ^ C[3];
  98. for (x = 0; x < 5; x++) {
  99. A[x] ^= D[x];
  100. A[x + 5] ^= D[x];
  101. A[x + 10] ^= D[x];
  102. A[x + 15] ^= D[x];
  103. A[x + 20] ^= D[x];
  104. }
  105. }
  106. /* Keccak pi() transformation */
  107. static void keccak_pi(uint64_t *A)
  108. {
  109. uint64_t A1 = 0;
  110. A1 = A[1];
  111. A[ 1] = A[ 6];
  112. A[ 6] = A[ 9];
  113. A[ 9] = A[22];
  114. A[22] = A[14];
  115. A[14] = A[20];
  116. A[20] = A[ 2];
  117. A[ 2] = A[12];
  118. A[12] = A[13];
  119. A[13] = A[19];
  120. A[19] = A[23];
  121. A[23] = A[15];
  122. A[15] = A[ 4];
  123. A[ 4] = A[24];
  124. A[24] = A[21];
  125. A[21] = A[ 8];
  126. A[ 8] = A[16];
  127. A[16] = A[ 5];
  128. A[ 5] = A[ 3];
  129. A[ 3] = A[18];
  130. A[18] = A[17];
  131. A[17] = A[11];
  132. A[11] = A[ 7];
  133. A[ 7] = A[10];
  134. A[10] = A1;
  135. /* note: A[ 0] is left as is */
  136. }
  137. /* Keccak chi() transformation */
  138. static void keccak_chi(uint64_t *A)
  139. {
  140. int i = 0;
  141. for (i = 0; i < 25; i += 5) {
  142. uint64_t A0 = A[0 + i], A1 = A[1 + i];
  143. A[0 + i] ^= ~A1 & A[2 + i];
  144. A[1 + i] ^= ~A[2 + i] & A[3 + i];
  145. A[2 + i] ^= ~A[3 + i] & A[4 + i];
  146. A[3 + i] ^= ~A[4 + i] & A0;
  147. A[4 + i] ^= ~A0 & A1;
  148. }
  149. }
  150. static void sha3_permutation(uint64_t *state)
  151. {
  152. #if BYTE_ORDER == BIG_ENDIAN
  153. int i;
  154. for (i = 0; i < 25; i++)
  155. {
  156. REVERSE64(state[i], state[i]);
  157. }
  158. #endif
  159. int round = 0;
  160. for (round = 0; round < NumberOfRounds; round++)
  161. {
  162. keccak_theta(state);
  163. /* apply Keccak rho() transformation */
  164. state[ 1] = ROTL64(state[ 1], 1);
  165. state[ 2] = ROTL64(state[ 2], 62);
  166. state[ 3] = ROTL64(state[ 3], 28);
  167. state[ 4] = ROTL64(state[ 4], 27);
  168. state[ 5] = ROTL64(state[ 5], 36);
  169. state[ 6] = ROTL64(state[ 6], 44);
  170. state[ 7] = ROTL64(state[ 7], 6);
  171. state[ 8] = ROTL64(state[ 8], 55);
  172. state[ 9] = ROTL64(state[ 9], 20);
  173. state[10] = ROTL64(state[10], 3);
  174. state[11] = ROTL64(state[11], 10);
  175. state[12] = ROTL64(state[12], 43);
  176. state[13] = ROTL64(state[13], 25);
  177. state[14] = ROTL64(state[14], 39);
  178. state[15] = ROTL64(state[15], 41);
  179. state[16] = ROTL64(state[16], 45);
  180. state[17] = ROTL64(state[17], 15);
  181. state[18] = ROTL64(state[18], 21);
  182. state[19] = ROTL64(state[19], 8);
  183. state[20] = ROTL64(state[20], 18);
  184. state[21] = ROTL64(state[21], 2);
  185. state[22] = ROTL64(state[22], 61);
  186. state[23] = ROTL64(state[23], 56);
  187. state[24] = ROTL64(state[24], 14);
  188. keccak_pi(state);
  189. keccak_chi(state);
  190. /* apply iota(state, round) */
  191. *state ^= keccak_round_constants[round];
  192. }
  193. #if BYTE_ORDER == BIG_ENDIAN
  194. for (i = 0; i < 25; i++)
  195. {
  196. REVERSE64(state[i], state[i]);
  197. }
  198. #endif
  199. }
  200. /**
  201. * The core transformation. Process the specified block of data.
  202. *
  203. * @param hash the algorithm state
  204. * @param block the message block to process
  205. * @param block_size the size of the processed block in bytes
  206. */
  207. static void sha3_process_block(uint64_t hash[25], const uint64_t *block, size_t block_size)
  208. {
  209. /* expanded loop */
  210. hash[ 0] ^= le2me_64(block[ 0]);
  211. hash[ 1] ^= le2me_64(block[ 1]);
  212. hash[ 2] ^= le2me_64(block[ 2]);
  213. hash[ 3] ^= le2me_64(block[ 3]);
  214. hash[ 4] ^= le2me_64(block[ 4]);
  215. hash[ 5] ^= le2me_64(block[ 5]);
  216. hash[ 6] ^= le2me_64(block[ 6]);
  217. hash[ 7] ^= le2me_64(block[ 7]);
  218. hash[ 8] ^= le2me_64(block[ 8]);
  219. /* if not sha3-512 */
  220. if (block_size > 72) {
  221. hash[ 9] ^= le2me_64(block[ 9]);
  222. hash[10] ^= le2me_64(block[10]);
  223. hash[11] ^= le2me_64(block[11]);
  224. hash[12] ^= le2me_64(block[12]);
  225. /* if not sha3-384 */
  226. if (block_size > 104) {
  227. hash[13] ^= le2me_64(block[13]);
  228. hash[14] ^= le2me_64(block[14]);
  229. hash[15] ^= le2me_64(block[15]);
  230. hash[16] ^= le2me_64(block[16]);
  231. /* if not sha3-256 */
  232. if (block_size > 136) {
  233. hash[17] ^= le2me_64(block[17]);
  234. #ifdef FULL_SHA3_FAMILY_SUPPORT
  235. /* if not sha3-224 */
  236. if (block_size > 144) {
  237. hash[18] ^= le2me_64(block[18]);
  238. hash[19] ^= le2me_64(block[19]);
  239. hash[20] ^= le2me_64(block[20]);
  240. hash[21] ^= le2me_64(block[21]);
  241. hash[22] ^= le2me_64(block[22]);
  242. hash[23] ^= le2me_64(block[23]);
  243. hash[24] ^= le2me_64(block[24]);
  244. }
  245. #endif
  246. }
  247. }
  248. }
  249. /* make a permutation of the hash */
  250. sha3_permutation(hash);
  251. }
  252. #define SHA3_FINALIZED 0x80000000
  253. /**
  254. * Calculate message hash.
  255. * Can be called repeatedly with chunks of the message to be hashed.
  256. *
  257. * @param ctx the algorithm context containing current hashing state
  258. * @param msg message chunk
  259. * @param size length of the message chunk
  260. */
  261. void sha3_Update(SHA3_CTX *ctx, const unsigned char *msg, size_t size)
  262. {
  263. if (size == 0) return;
  264. size_t idx = (size_t)ctx->rest;
  265. size_t block_size = (size_t)ctx->block_size;
  266. if (ctx->rest & SHA3_FINALIZED) return; /* too late for additional input */
  267. ctx->rest = (unsigned)((ctx->rest + size) % block_size);
  268. /* fill partial block */
  269. if (idx) {
  270. size_t left = block_size - idx;
  271. memcpy((char*)ctx->message + idx, msg, (size < left ? size : left));
  272. if (size < left) return;
  273. /* process partial block */
  274. sha3_process_block(ctx->hash, ctx->message, block_size);
  275. msg += left;
  276. size -= left;
  277. }
  278. while (size >= block_size) {
  279. uint64_t *aligned_message_block = NULL;
  280. if (IS_ALIGNED_64(msg)) {
  281. /* the most common case is processing of an already aligned message
  282. without copying it */
  283. aligned_message_block = (uint64_t*)(void*)msg;
  284. } else {
  285. memcpy(ctx->message, msg, block_size);
  286. aligned_message_block = ctx->message;
  287. }
  288. sha3_process_block(ctx->hash, aligned_message_block, block_size);
  289. msg += block_size;
  290. size -= block_size;
  291. }
  292. if (size) {
  293. memcpy(ctx->message, msg, size); /* save leftovers */
  294. }
  295. }
  296. /**
  297. * Store calculated hash into the given array.
  298. *
  299. * @param ctx the algorithm context containing current hashing state
  300. * @param result calculated hash in binary form
  301. */
  302. void sha3_Final(SHA3_CTX *ctx, unsigned char* result)
  303. {
  304. size_t digest_length = 100 - ctx->block_size / 2;
  305. const size_t block_size = ctx->block_size;
  306. if (!(ctx->rest & SHA3_FINALIZED))
  307. {
  308. /* clear the rest of the data queue */
  309. memzero((char*)ctx->message + ctx->rest, block_size - ctx->rest);
  310. ((char*)ctx->message)[ctx->rest] |= 0x06;
  311. ((char*)ctx->message)[block_size - 1] |= 0x80;
  312. /* process final block */
  313. sha3_process_block(ctx->hash, ctx->message, block_size);
  314. ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
  315. }
  316. assert(block_size > digest_length);
  317. if (result) me64_to_le_str(result, ctx->hash, digest_length);
  318. memzero(ctx, sizeof(SHA3_CTX));
  319. }
  320. #if USE_KECCAK
  321. /**
  322. * Store calculated hash into the given array.
  323. *
  324. * @param ctx the algorithm context containing current hashing state
  325. * @param result calculated hash in binary form
  326. */
  327. void keccak_Final(SHA3_CTX *ctx, unsigned char* result)
  328. {
  329. size_t digest_length = 100 - ctx->block_size / 2;
  330. const size_t block_size = ctx->block_size;
  331. if (!(ctx->rest & SHA3_FINALIZED))
  332. {
  333. /* clear the rest of the data queue */
  334. memzero((char*)ctx->message + ctx->rest, block_size - ctx->rest);
  335. ((char*)ctx->message)[ctx->rest] |= 0x01;
  336. ((char*)ctx->message)[block_size - 1] |= 0x80;
  337. /* process final block */
  338. sha3_process_block(ctx->hash, ctx->message, block_size);
  339. ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
  340. }
  341. assert(block_size > digest_length);
  342. if (result) me64_to_le_str(result, ctx->hash, digest_length);
  343. memzero(ctx, sizeof(SHA3_CTX));
  344. }
  345. void keccak_256(const unsigned char* data, size_t len, unsigned char* digest)
  346. {
  347. SHA3_CTX ctx = {0};
  348. keccak_256_Init(&ctx);
  349. keccak_Update(&ctx, data, len);
  350. keccak_Final(&ctx, digest);
  351. }
  352. void keccak_512(const unsigned char* data, size_t len, unsigned char* digest)
  353. {
  354. SHA3_CTX ctx = {0};
  355. keccak_512_Init(&ctx);
  356. keccak_Update(&ctx, data, len);
  357. keccak_Final(&ctx, digest);
  358. }
  359. #endif /* USE_KECCAK */
  360. void sha3_256(const unsigned char* data, size_t len, unsigned char* digest)
  361. {
  362. SHA3_CTX ctx = {0};
  363. sha3_256_Init(&ctx);
  364. sha3_Update(&ctx, data, len);
  365. sha3_Final(&ctx, digest);
  366. }
  367. void sha3_512(const unsigned char* data, size_t len, unsigned char* digest)
  368. {
  369. SHA3_CTX ctx = {0};
  370. sha3_512_Init(&ctx);
  371. sha3_Update(&ctx, data, len);
  372. sha3_Final(&ctx, digest);
  373. }