sha3.c 12 KB

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