keccak-tiny-unrolled.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /** libkeccak-tiny
  2. *
  3. * A single-file implementation of SHA-3 and SHAKE.
  4. *
  5. * Implementor: David Leon Gil
  6. * License: CC0, attribution kindly requested. Blame taken too,
  7. * but not liability.
  8. */
  9. #include "keccak-tiny.h"
  10. #include <string.h>
  11. #include "crypto.h"
  12. #include "byteorder.h"
  13. /******** Endianness conversion helpers ********/
  14. static inline uint64_t
  15. loadu64le(const unsigned char *x) {
  16. uint64_t r;
  17. memcpy(&r, x, sizeof(r));
  18. return _le64toh(r);
  19. }
  20. static inline void
  21. storeu64le(uint8_t *x, uint64_t u) {
  22. uint64_t val = _le64toh(u);
  23. memcpy(x, &val, sizeof(u));
  24. }
  25. /******** The Keccak-f[1600] permutation ********/
  26. /*** Constants. ***/
  27. static const uint8_t rho[24] = \
  28. { 1, 3, 6, 10, 15, 21,
  29. 28, 36, 45, 55, 2, 14,
  30. 27, 41, 56, 8, 25, 43,
  31. 62, 18, 39, 61, 20, 44};
  32. static const uint8_t pi[24] = \
  33. {10, 7, 11, 17, 18, 3,
  34. 5, 16, 8, 21, 24, 4,
  35. 15, 23, 19, 13, 12, 2,
  36. 20, 14, 22, 9, 6, 1};
  37. static const uint64_t RC[24] = \
  38. {1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
  39. 0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
  40. 0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
  41. 0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
  42. 0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
  43. 0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
  44. /*** Helper macros to unroll the permutation. ***/
  45. #define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
  46. #define REPEAT6(e) e e e e e e
  47. #define REPEAT24(e) REPEAT6(e e e e)
  48. #define REPEAT5(e) e e e e e
  49. #define FOR5(v, s, e) \
  50. v = 0; \
  51. REPEAT5(e; v += s;)
  52. /*** Keccak-f[1600] ***/
  53. static inline void keccakf(void* state) {
  54. uint64_t* a = (uint64_t*)state;
  55. uint64_t b[5] = {0};
  56. uint64_t t = 0;
  57. uint8_t x, y, i = 0;
  58. REPEAT24(
  59. // Theta
  60. FOR5(x, 1,
  61. b[x] = 0;
  62. FOR5(y, 5,
  63. b[x] ^= a[x + y]; ))
  64. FOR5(x, 1,
  65. FOR5(y, 5,
  66. a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1); ))
  67. // Rho and pi
  68. t = a[1];
  69. x = 0;
  70. REPEAT24(b[0] = a[pi[x]];
  71. a[pi[x]] = rol(t, rho[x]);
  72. t = b[0];
  73. x++; )
  74. // Chi
  75. FOR5(y,
  76. 5,
  77. FOR5(x, 1,
  78. b[x] = a[y + x];)
  79. FOR5(x, 1,
  80. a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]); ))
  81. // Iota
  82. a[0] ^= RC[i];
  83. i++; )
  84. }
  85. /******** The FIPS202-defined functions. ********/
  86. /*** Some helper macros. ***/
  87. // `xorin` modified to handle Big Endian systems, `buf` being unaligned on
  88. // systems that care about such things. Assumes that len is a multiple of 8,
  89. // which is always true for the rates we use, and the modified finalize.
  90. static inline void
  91. xorin8(uint8_t *dst, const uint8_t *src, size_t len) {
  92. uint64_t* a = (uint64_t*)dst; // Always aligned.
  93. for (size_t i = 0; i < len; i += 8) {
  94. a[i/8] ^= loadu64le(src + i);
  95. }
  96. }
  97. // `setout` likewise modified to handle Big Endian systems. Assumes that len
  98. // is a multiple of 8, which is true for every rate we use.
  99. static inline void
  100. setout8(const uint8_t *src, uint8_t *dst, size_t len) {
  101. const uint64_t *si = (const uint64_t*)src; // Always aligned.
  102. for (size_t i = 0; i < len; i+= 8) {
  103. storeu64le(dst+i, si[i/8]);
  104. }
  105. }
  106. #define P keccakf
  107. #define Plen KECCAK_MAX_RATE
  108. #define KECCAK_DELIM_DIGEST 0x06
  109. #define KECCAK_DELIM_XOF 0x1f
  110. // Fold P*F over the full blocks of an input.
  111. #define foldP(I, L, F) \
  112. while (L >= s->rate) { \
  113. F(s->a, I, s->rate); \
  114. P(s->a); \
  115. I += s->rate; \
  116. L -= s->rate; \
  117. }
  118. static inline void
  119. keccak_absorb_blocks(keccak_state *s, const uint8_t *buf, size_t nr_blocks)
  120. {
  121. size_t blen = nr_blocks * s->rate;
  122. foldP(buf, blen, xorin8);
  123. }
  124. static int
  125. keccak_update(keccak_state *s, const uint8_t *buf, size_t len)
  126. {
  127. if (s->finalized)
  128. return -1;
  129. if ((buf == NULL) && len != 0)
  130. return -1;
  131. size_t remaining = len;
  132. while (remaining > 0) {
  133. if (s->offset == 0) {
  134. const size_t blocks = remaining / s->rate;
  135. size_t direct_bytes = blocks * s->rate;
  136. if (direct_bytes > 0) {
  137. keccak_absorb_blocks(s, buf, blocks);
  138. remaining -= direct_bytes;
  139. buf += direct_bytes;
  140. }
  141. }
  142. const size_t buf_avail = s->rate - s->offset;
  143. const size_t buf_bytes = (buf_avail > remaining) ? remaining : buf_avail;
  144. if (buf_bytes > 0) {
  145. memcpy(&s->block[s->offset], buf, buf_bytes);
  146. s->offset += buf_bytes;
  147. remaining -= buf_bytes;
  148. buf += buf_bytes;
  149. }
  150. if (s->offset == s->rate) {
  151. keccak_absorb_blocks(s, s->block, 1);
  152. s->offset = 0;
  153. }
  154. }
  155. return 0;
  156. }
  157. static void
  158. keccak_finalize(keccak_state *s)
  159. {
  160. // Xor in the DS and pad frame.
  161. s->block[s->offset++] = s->delim; // DS.
  162. for (size_t i = s->offset; i < s->rate; i++) {
  163. s->block[i] = 0;
  164. }
  165. s->block[s->rate - 1] |= 0x80; // Pad frame.
  166. // Xor in the last block.
  167. xorin8(s->a, s->block, s->rate);
  168. memwipe(s->block, 0, sizeof(s->block));
  169. s->finalized = 1;
  170. s->offset = s->rate;
  171. }
  172. static inline void
  173. keccak_squeeze_blocks(keccak_state *s, uint8_t *out, size_t nr_blocks)
  174. {
  175. for (size_t n = 0; n < nr_blocks; n++) {
  176. keccakf(s->a);
  177. setout8(s->a, out, s->rate);
  178. out += s->rate;
  179. }
  180. }
  181. static int
  182. keccak_squeeze(keccak_state *s, uint8_t *out, size_t outlen)
  183. {
  184. if (!s->finalized)
  185. return -1;
  186. size_t remaining = outlen;
  187. while (remaining > 0) {
  188. if (s->offset == s->rate) {
  189. const size_t blocks = remaining / s->rate;
  190. const size_t direct_bytes = blocks * s->rate;
  191. if (blocks > 0) {
  192. keccak_squeeze_blocks(s, out, blocks);
  193. out += direct_bytes;
  194. remaining -= direct_bytes;
  195. }
  196. if (remaining > 0) {
  197. keccak_squeeze_blocks(s, s->block, 1);
  198. s->offset = 0;
  199. }
  200. }
  201. const size_t buf_bytes = s->rate - s->offset;
  202. const size_t indirect_bytes = (buf_bytes > remaining) ? remaining : buf_bytes;
  203. if (indirect_bytes > 0) {
  204. memcpy(out, &s->block[s->offset], indirect_bytes);
  205. out += indirect_bytes;
  206. s->offset += indirect_bytes;
  207. remaining -= indirect_bytes;
  208. }
  209. }
  210. return 0;
  211. }
  212. int
  213. keccak_digest_init(keccak_state *s, size_t bits)
  214. {
  215. if (s == NULL)
  216. return -1;
  217. if (bits != 224 && bits != 256 && bits != 384 && bits != 512)
  218. return -1;
  219. keccak_cleanse(s);
  220. s->rate = KECCAK_RATE(bits);
  221. s->delim = KECCAK_DELIM_DIGEST;
  222. return 0;
  223. }
  224. int
  225. keccak_digest_update(keccak_state *s, const uint8_t *buf, size_t len)
  226. {
  227. if (s == NULL)
  228. return -1;
  229. if (s->delim != KECCAK_DELIM_DIGEST)
  230. return -1;
  231. return keccak_update(s, buf, len);
  232. }
  233. int
  234. keccak_digest_sum(const keccak_state *s, uint8_t *out, size_t outlen)
  235. {
  236. if (s == NULL)
  237. return -1;
  238. if (s->delim != KECCAK_DELIM_DIGEST)
  239. return -1;
  240. if (out == NULL || outlen > 4 * (KECCAK_MAX_RATE - s->rate) / 8)
  241. return -1;
  242. // Work in a copy so that incremental/rolling hashes are easy.
  243. keccak_state s_tmp;
  244. keccak_clone(&s_tmp, s);
  245. keccak_finalize(&s_tmp);
  246. int ret = keccak_squeeze(&s_tmp, out, outlen);
  247. keccak_cleanse(&s_tmp);
  248. return ret;
  249. }
  250. int
  251. keccak_xof_init(keccak_state *s, size_t bits)
  252. {
  253. if (s == NULL)
  254. return -1;
  255. if (bits != 128 && bits != 256)
  256. return -1;
  257. keccak_cleanse(s);
  258. s->rate = KECCAK_RATE(bits);
  259. s->delim = KECCAK_DELIM_XOF;
  260. return 0;
  261. }
  262. int
  263. keccak_xof_absorb(keccak_state *s, const uint8_t *buf, size_t len)
  264. {
  265. if (s == NULL)
  266. return -1;
  267. if (s->delim != KECCAK_DELIM_XOF)
  268. return -1;
  269. return keccak_update(s, buf, len);
  270. }
  271. int
  272. keccak_xof_squeeze(keccak_state *s, uint8_t *out, size_t outlen)
  273. {
  274. if (s == NULL)
  275. return -1;
  276. if (s->delim != KECCAK_DELIM_XOF)
  277. return -1;
  278. if (!s->finalized)
  279. keccak_finalize(s);
  280. return keccak_squeeze(s, out, outlen);
  281. }
  282. void
  283. keccak_clone(keccak_state *out, const keccak_state *in)
  284. {
  285. memcpy(out, in, sizeof(keccak_state));
  286. }
  287. void
  288. keccak_cleanse(keccak_state *s)
  289. {
  290. memwipe(s, 0, sizeof(keccak_state));
  291. }
  292. /** The sponge-based hash construction. **/
  293. static inline int hash(uint8_t* out, size_t outlen,
  294. const uint8_t* in, size_t inlen,
  295. size_t bits, uint8_t delim) {
  296. if ((out == NULL) || ((in == NULL) && inlen != 0)) {
  297. return -1;
  298. }
  299. int ret = 0;
  300. keccak_state s;
  301. keccak_cleanse(&s);
  302. switch (delim) {
  303. case KECCAK_DELIM_DIGEST:
  304. ret |= keccak_digest_init(&s, bits);
  305. ret |= keccak_digest_update(&s, in, inlen);
  306. // Use the internal API instead of sum to avoid the memcpy.
  307. keccak_finalize(&s);
  308. ret |= keccak_squeeze(&s, out, outlen);
  309. break;
  310. case KECCAK_DELIM_XOF:
  311. ret |= keccak_xof_init(&s, bits);
  312. ret |= keccak_xof_absorb(&s, in, inlen);
  313. ret |= keccak_xof_squeeze(&s, out, outlen);
  314. break;
  315. default:
  316. return -1;
  317. }
  318. keccak_cleanse(&s);
  319. return ret;
  320. }
  321. /*** Helper macros to define SHA3 and SHAKE instances. ***/
  322. #define defshake(bits) \
  323. int shake##bits(uint8_t* out, size_t outlen, \
  324. const uint8_t* in, size_t inlen) { \
  325. return hash(out, outlen, in, inlen, bits, KECCAK_DELIM_XOF); \
  326. }
  327. #define defsha3(bits) \
  328. int sha3_##bits(uint8_t* out, size_t outlen, \
  329. const uint8_t* in, size_t inlen) { \
  330. if (outlen > (bits/8)) { \
  331. return -1; \
  332. } \
  333. return hash(out, outlen, in, inlen, bits, KECCAK_DELIM_DIGEST); \
  334. }
  335. /*** FIPS202 SHAKE VOFs ***/
  336. defshake(128)
  337. defshake(256)
  338. /*** FIPS202 SHA3 FOFs ***/
  339. defsha3(224)
  340. defsha3(256)
  341. defsha3(384)
  342. defsha3(512)