You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

241 lines
7.2 KiB

  1. /**
  2. * @cond internal
  3. * @file shake.c
  4. * @copyright
  5. * Uses public domain code by Mathias Panzenböck \n
  6. * Uses CC0 code by David Leon Gil, 2015 \n
  7. * Copyright (c) 2015 Cryptography Research, Inc. \n
  8. * Released under the MIT License. See LICENSE.txt for license information.
  9. * @author Mike Hamburg
  10. * @brief SHA-3-n and SHAKE-n instances.
  11. * @warning EXPERIMENTAL! The names, parameter orders etc are likely to change.
  12. */
  13. #define __STDC_WANT_LIB_EXT1__ 1 /* for memset_s */
  14. #define _BSD_SOURCE 1 /* for endian */
  15. #define _DEFAULT_SOURCE 1 /* for endian with glibc 2.20 */
  16. #include <assert.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include "portable_endian.h"
  20. #include "keccak_internal.h"
  21. #include <decaf/shake.h>
  22. #define FLAG_ABSORBING 'A'
  23. #define FLAG_SQUEEZING 'Z'
  24. /** Constants. **/
  25. static const uint8_t pi[24] = {
  26. 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
  27. 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
  28. };
  29. #define RC_B(x,n) ((((x##ull)>>n)&1)<<((1<<n)-1))
  30. #define RC_X(x) (RC_B(x,0)|RC_B(x,1)|RC_B(x,2)|RC_B(x,3)|RC_B(x,4)|RC_B(x,5)|RC_B(x,6))
  31. static const uint64_t RC[24] = {
  32. RC_X(0x01), RC_X(0x1a), RC_X(0x5e), RC_X(0x70), RC_X(0x1f), RC_X(0x21),
  33. RC_X(0x79), RC_X(0x55), RC_X(0x0e), RC_X(0x0c), RC_X(0x35), RC_X(0x26),
  34. RC_X(0x3f), RC_X(0x4f), RC_X(0x5d), RC_X(0x53), RC_X(0x52), RC_X(0x48),
  35. RC_X(0x16), RC_X(0x66), RC_X(0x79), RC_X(0x58), RC_X(0x21), RC_X(0x74)
  36. };
  37. static inline uint64_t rol(uint64_t x, int s) {
  38. return (x << s) | (x >> (64 - s));
  39. }
  40. /* Helper macros to unroll the permutation. */
  41. #define REPEAT5(e) e e e e e
  42. #define FOR51(v, e) v = 0; REPEAT5(e; v += 1;)
  43. #ifndef SHAKE_NO_UNROLL_LOOPS
  44. # define FOR55(v, e) v = 0; REPEAT5(e; v += 5;)
  45. # define REPEAT24(e) e e e e e e e e e e e e e e e e e e e e e e e e
  46. #else
  47. # define FOR55(v, e) for (v=0; v<25; v+= 5) { e; }
  48. # define REPEAT24(e) {int _j=0; for (_j=0; _j<24; _j++) { e }}
  49. #endif
  50. /*** The Keccak-f[1600] permutation ***/
  51. void keccakf(kdomain_t state, uint8_t start_round) {
  52. uint64_t* a = state->w;
  53. uint64_t b[5] = {0}, t, u;
  54. uint8_t x, y, i;
  55. for (i=0; i<25; i++) a[i] = le64toh(a[i]);
  56. for (i = start_round; i < 24; i++) {
  57. FOR51(x, b[x] = 0; )
  58. FOR55(y, FOR51(x, b[x] ^= a[x + y]; ))
  59. FOR55(y, FOR51(x,
  60. a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1);
  61. ))
  62. // Rho and pi
  63. t = a[1];
  64. x = y = 0;
  65. REPEAT24(u = a[pi[x]]; y += x+1; a[pi[x]] = rol(t, y % 64); t = u; x++; )
  66. // Chi
  67. FOR55(y,
  68. FOR51(x, b[x] = a[y + x];)
  69. FOR51(x, a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]);)
  70. )
  71. // Iota
  72. a[0] ^= RC[i];
  73. }
  74. for (i=0; i<25; i++) a[i] = htole64(a[i]);
  75. }
  76. decaf_error_t decaf_sha3_update (
  77. struct decaf_keccak_sponge_s * __restrict__ decaf_sponge,
  78. const uint8_t *in,
  79. size_t len
  80. ) {
  81. assert(decaf_sponge->params->position < decaf_sponge->params->rate);
  82. assert(decaf_sponge->params->rate < sizeof(decaf_sponge->state));
  83. assert(decaf_sponge->params->flags == FLAG_ABSORBING);
  84. while (len) {
  85. size_t cando = decaf_sponge->params->rate - decaf_sponge->params->position, i;
  86. uint8_t* state = &decaf_sponge->state->b[decaf_sponge->params->position];
  87. if (cando > len) {
  88. for (i = 0; i < len; i += 1) state[i] ^= in[i];
  89. decaf_sponge->params->position += len;
  90. break;
  91. } else {
  92. for (i = 0; i < cando; i += 1) state[i] ^= in[i];
  93. dokeccak(decaf_sponge);
  94. len -= cando;
  95. in += cando;
  96. }
  97. }
  98. return (decaf_sponge->params->flags == FLAG_ABSORBING) ? DECAF_SUCCESS : DECAF_FAILURE;
  99. }
  100. decaf_error_t decaf_sha3_output (
  101. decaf_keccak_sponge_t decaf_sponge,
  102. uint8_t * __restrict__ out,
  103. size_t len
  104. ) {
  105. decaf_error_t ret = DECAF_SUCCESS;
  106. assert(decaf_sponge->params->position < decaf_sponge->params->rate);
  107. assert(decaf_sponge->params->rate < sizeof(decaf_sponge->state));
  108. if (decaf_sponge->params->max_out != 0xFF) {
  109. if (decaf_sponge->params->remaining >= len) {
  110. decaf_sponge->params->remaining -= len;
  111. } else {
  112. decaf_sponge->params->remaining = 0;
  113. ret = DECAF_FAILURE;
  114. }
  115. }
  116. switch (decaf_sponge->params->flags) {
  117. case FLAG_SQUEEZING: break;
  118. case FLAG_ABSORBING:
  119. {
  120. uint8_t* state = decaf_sponge->state->b;
  121. state[decaf_sponge->params->position] ^= decaf_sponge->params->pad;
  122. state[decaf_sponge->params->rate - 1] ^= decaf_sponge->params->rate_pad;
  123. dokeccak(decaf_sponge);
  124. decaf_sponge->params->flags = FLAG_SQUEEZING;
  125. break;
  126. }
  127. default:
  128. assert(0);
  129. }
  130. while (len) {
  131. size_t cando = decaf_sponge->params->rate - decaf_sponge->params->position;
  132. uint8_t* state = &decaf_sponge->state->b[decaf_sponge->params->position];
  133. if (cando > len) {
  134. memcpy(out, state, len);
  135. decaf_sponge->params->position += len;
  136. return ret;
  137. } else {
  138. memcpy(out, state, cando);
  139. dokeccak(decaf_sponge);
  140. len -= cando;
  141. out += cando;
  142. }
  143. }
  144. return ret;
  145. }
  146. decaf_error_t decaf_sha3_final (
  147. decaf_keccak_sponge_t decaf_sponge,
  148. uint8_t * __restrict__ out,
  149. size_t len
  150. ) {
  151. decaf_error_t ret = decaf_sha3_output(decaf_sponge,out,len);
  152. decaf_sha3_reset(decaf_sponge);
  153. return ret;
  154. }
  155. void decaf_sha3_reset (
  156. decaf_keccak_sponge_t decaf_sponge
  157. ) {
  158. decaf_sha3_init(decaf_sponge, decaf_sponge->params);
  159. decaf_sponge->params->flags = FLAG_ABSORBING;
  160. decaf_sponge->params->remaining = decaf_sponge->params->max_out;
  161. }
  162. void decaf_sha3_destroy (decaf_keccak_sponge_t decaf_sponge) {
  163. decaf_bzero(decaf_sponge, sizeof(decaf_keccak_sponge_t));
  164. }
  165. void decaf_sha3_init (
  166. decaf_keccak_sponge_t decaf_sponge,
  167. const struct decaf_kparams_s *params
  168. ) {
  169. memset(decaf_sponge->state, 0, sizeof(decaf_sponge->state));
  170. decaf_sponge->params[0] = params[0];
  171. decaf_sponge->params->position = 0;
  172. }
  173. decaf_error_t decaf_sha3_hash (
  174. uint8_t *out,
  175. size_t outlen,
  176. const uint8_t *in,
  177. size_t inlen,
  178. const struct decaf_kparams_s *params
  179. ) {
  180. decaf_keccak_sponge_t decaf_sponge;
  181. decaf_sha3_init(decaf_sponge, params);
  182. decaf_sha3_update(decaf_sponge, in, inlen);
  183. decaf_error_t ret = decaf_sha3_output(decaf_sponge, out, outlen);
  184. decaf_sha3_destroy(decaf_sponge);
  185. return ret;
  186. }
  187. #define DEFSHAKE(n) \
  188. const struct decaf_kparams_s DECAF_SHAKE##n##_params_s = \
  189. { 0, FLAG_ABSORBING, 200-n/4, 0, 0x1f, 0x80, 0xFF, 0xFF };
  190. #define DEFSHA3(n) \
  191. const struct decaf_kparams_s DECAF_SHA3_##n##_params_s = \
  192. { 0, FLAG_ABSORBING, 200-n/4, 0, 0x06, 0x80, n/8, n/8 };
  193. size_t decaf_sha3_default_output_bytes (
  194. const decaf_keccak_sponge_t s
  195. ) {
  196. return (s->params->max_out == 0xFF)
  197. ? (200-s->params->rate)
  198. : ((200-s->params->rate)/2);
  199. }
  200. size_t decaf_sha3_max_output_bytes (
  201. const decaf_keccak_sponge_t s
  202. ) {
  203. return (s->params->max_out == 0xFF)
  204. ? SIZE_MAX
  205. : (size_t)((200-s->params->rate)/2);
  206. }
  207. DEFSHAKE(128)
  208. DEFSHAKE(256)
  209. DEFSHA3(224)
  210. DEFSHA3(256)
  211. DEFSHA3(384)
  212. DEFSHA3(512)
  213. /* FUTURE: Keyak instances, etc */