sha3.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * SHA-3, as defined in FIPS PUB 202.
  3. */
  4. #include <assert.h>
  5. #include <string.h>
  6. #include "ssh.h"
  7. static inline uint64_t rol(uint64_t x, unsigned shift)
  8. {
  9. unsigned L = (+shift) & 63;
  10. unsigned R = (-shift) & 63;
  11. return (x << L) | (x >> R);
  12. }
  13. /*
  14. * General Keccak is defined such that its state is a 5x5 array of
  15. * words which can be any power-of-2 size from 1 up to 64. SHA-3 fixes
  16. * on 64, and so do we.
  17. *
  18. * The number of rounds is defined as 12 + 2k if the word size is 2^k.
  19. * Here we have 64-bit words only, so k=6, so 24 rounds always.
  20. */
  21. typedef uint64_t keccak_core_state[5][5];
  22. #define NROUNDS 24 /* would differ for other word sizes */
  23. static const uint64_t round_constants[NROUNDS];
  24. static const unsigned rotation_counts[5][5];
  25. /*
  26. * Core Keccak transform: just squodge the state around internally,
  27. * without adding or extracting any data from it.
  28. */
  29. static void keccak_transform(keccak_core_state A)
  30. {
  31. union {
  32. uint64_t C[5];
  33. uint64_t B[5][5];
  34. } u;
  35. for (unsigned round = 0; round < NROUNDS; round++) {
  36. /* theta step */
  37. for (unsigned x = 0; x < 5; x++)
  38. u.C[x] = A[x][0] ^ A[x][1] ^ A[x][2] ^ A[x][3] ^ A[x][4];
  39. for (unsigned x = 0; x < 5; x++) {
  40. uint64_t D = rol(u.C[(x+1) % 5], 1) ^ u.C[(x+4) % 5];
  41. for (unsigned y = 0; y < 5; y++)
  42. A[x][y] ^= D;
  43. }
  44. /* rho and pi steps */
  45. for (unsigned x = 0; x < 5; x++)
  46. for (unsigned y = 0; y < 5; y++)
  47. u.B[y][(2*x+3*y) % 5] = rol(A[x][y], rotation_counts[x][y]);
  48. /* chi step */
  49. for (unsigned x = 0; x < 5; x++)
  50. for (unsigned y = 0; y < 5; y++)
  51. A[x][y] = u.B[x][y] ^ (u.B[(x+2)%5][y] & ~u.B[(x+1)%5][y]);
  52. /* iota step */
  53. A[0][0] ^= round_constants[round];
  54. }
  55. smemclr(&u, sizeof(u));
  56. }
  57. typedef struct {
  58. keccak_core_state A;
  59. unsigned char bytes[25*8];
  60. unsigned char first_pad_byte;
  61. size_t bytes_got, bytes_wanted, hash_bytes;
  62. } keccak_state;
  63. /*
  64. * Keccak accumulation function: given a piece of message, add it to
  65. * the hash.
  66. */
  67. static void keccak_accumulate(keccak_state *s, const void *vdata, size_t len)
  68. {
  69. const unsigned char *data = (const unsigned char *)vdata;
  70. while (len >= s->bytes_wanted - s->bytes_got) {
  71. size_t b = s->bytes_wanted - s->bytes_got;
  72. memcpy(s->bytes + s->bytes_got, data, b);
  73. len -= b;
  74. data += b;
  75. size_t n = 0;
  76. for (unsigned y = 0; y < 5; y++) {
  77. for (unsigned x = 0; x < 5; x++) {
  78. if (n >= s->bytes_wanted)
  79. break;
  80. s->A[x][y] ^= GET_64BIT_LSB_FIRST(s->bytes + n);
  81. n += 8;
  82. }
  83. }
  84. keccak_transform(s->A);
  85. s->bytes_got = 0;
  86. }
  87. memcpy(s->bytes + s->bytes_got, data, len);
  88. s->bytes_got += len;
  89. }
  90. /*
  91. * Keccak output function.
  92. */
  93. static void keccak_output(keccak_state *s, void *voutput)
  94. {
  95. unsigned char *output = (unsigned char *)voutput;
  96. /*
  97. * Add message padding.
  98. */
  99. {
  100. unsigned char padding[25*8];
  101. size_t len = s->bytes_wanted - s->bytes_got;
  102. if (len == 0)
  103. len = s->bytes_wanted;
  104. memset(padding, 0, len);
  105. padding[0] |= s->first_pad_byte;
  106. padding[len-1] |= 0x80;
  107. keccak_accumulate(s, padding, len);
  108. }
  109. size_t n = 0;
  110. for (unsigned y = 0; y < 5; y++) {
  111. for (unsigned x = 0; x < 5; x++) {
  112. size_t to_copy = s->hash_bytes - n;
  113. if (to_copy == 0)
  114. break;
  115. if (to_copy > 8)
  116. to_copy = 8;
  117. unsigned char outbytes[8];
  118. PUT_64BIT_LSB_FIRST(outbytes, s->A[x][y]);
  119. memcpy(output + n, outbytes, to_copy);
  120. n += to_copy;
  121. }
  122. }
  123. }
  124. static void keccak_init(keccak_state *s, unsigned hashbits, unsigned ratebits,
  125. unsigned char first_pad_byte)
  126. {
  127. int x, y;
  128. assert(hashbits % 8 == 0);
  129. assert(ratebits % 8 == 0);
  130. s->hash_bytes = hashbits / 8;
  131. s->bytes_wanted = (25 * 64 - ratebits) / 8;
  132. s->bytes_got = 0;
  133. s->first_pad_byte = first_pad_byte;
  134. assert(s->bytes_wanted % 8 == 0);
  135. for (y = 0; y < 5; y++)
  136. for (x = 0; x < 5; x++)
  137. s->A[x][y] = 0;
  138. }
  139. static void keccak_sha3_init(keccak_state *s, int hashbits)
  140. {
  141. keccak_init(s, hashbits, hashbits * 2, 0x06);
  142. }
  143. static void keccak_shake_init(keccak_state *s, int parambits, int hashbits)
  144. {
  145. keccak_init(s, hashbits, parambits * 2, 0x1f);
  146. }
  147. /*
  148. * Keccak round constants, generated via the LFSR specified in the
  149. * Keccak reference by the following piece of Python:
  150. import textwrap
  151. from functools import reduce
  152. rbytes = [1]
  153. while len(rbytes) < 7*24:
  154. k = rbytes[-1] * 2
  155. rbytes.append(k ^ (0x171 * (k >> 8)))
  156. rbits = [byte & 1 for byte in rbytes]
  157. rwords = [sum(rbits[i+j] << ((1 << j) - 1) for j in range(7))
  158. for i in range(0, len(rbits), 7)]
  159. print(textwrap.indent("\n".join(textwrap.wrap(", ".join(
  160. map("0x{:016x}".format, rwords)))), " "*4))
  161. */
  162. static const uint64_t round_constants[24] = {
  163. 0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
  164. 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
  165. 0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
  166. 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
  167. 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
  168. 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
  169. 0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
  170. 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
  171. };
  172. /*
  173. * Keccak per-element rotation counts, generated from the matrix
  174. * formula in the Keccak reference by the following piece of Python:
  175. coords = [1, 0]
  176. while len(coords) < 26:
  177. coords.append((2*coords[-2] + 3*coords[-1]) % 5)
  178. matrix = { (coords[i], coords[i+1]) : i for i in range(24) }
  179. matrix[0,0] = -1
  180. f = lambda t: (t+1) * (t+2) // 2 % 64
  181. for y in range(5):
  182. print(" {{{}}},".format(", ".join("{:2d}".format(f(matrix[y,x]))
  183. for x in range(5))))
  184. */
  185. static const unsigned rotation_counts[5][5] = {
  186. { 0, 36, 3, 41, 18},
  187. { 1, 44, 10, 45, 2},
  188. {62, 6, 43, 15, 61},
  189. {28, 55, 25, 21, 56},
  190. {27, 20, 39, 8, 14},
  191. };
  192. /*
  193. * The PuTTY ssh_hashalg abstraction.
  194. */
  195. struct keccak_hash {
  196. keccak_state state;
  197. ssh_hash hash;
  198. BinarySink_IMPLEMENTATION;
  199. };
  200. static void keccak_BinarySink_write(BinarySink *bs, const void *p, size_t len)
  201. {
  202. struct keccak_hash *kh = BinarySink_DOWNCAST(bs, struct keccak_hash);
  203. keccak_accumulate(&kh->state, p, len);
  204. }
  205. static ssh_hash *keccak_new(const ssh_hashalg *alg)
  206. {
  207. struct keccak_hash *kh = snew(struct keccak_hash);
  208. kh->hash.vt = alg;
  209. BinarySink_INIT(kh, keccak_BinarySink_write);
  210. BinarySink_DELEGATE_INIT(&kh->hash, kh);
  211. return ssh_hash_reset(&kh->hash);
  212. }
  213. static void keccak_free(ssh_hash *hash)
  214. {
  215. struct keccak_hash *kh = container_of(hash, struct keccak_hash, hash);
  216. smemclr(kh, sizeof(*kh));
  217. sfree(kh);
  218. }
  219. static void keccak_copyfrom(ssh_hash *hnew, ssh_hash *hold)
  220. {
  221. struct keccak_hash *khold = container_of(hold, struct keccak_hash, hash);
  222. struct keccak_hash *khnew = container_of(hnew, struct keccak_hash, hash);
  223. khnew->state = khold->state;
  224. }
  225. static void keccak_digest(ssh_hash *hash, unsigned char *output)
  226. {
  227. struct keccak_hash *kh = container_of(hash, struct keccak_hash, hash);
  228. keccak_output(&kh->state, output);
  229. }
  230. static void sha3_reset(ssh_hash *hash)
  231. {
  232. struct keccak_hash *kh = container_of(hash, struct keccak_hash, hash);
  233. keccak_sha3_init(&kh->state, hash->vt->hlen * 8);
  234. }
  235. #define DEFINE_SHA3(bits) \
  236. const ssh_hashalg ssh_sha3_##bits = { \
  237. .new = keccak_new, \
  238. .reset = sha3_reset, \
  239. .copyfrom = keccak_copyfrom, \
  240. .digest = keccak_digest, \
  241. .free = keccak_free, \
  242. .hlen = bits/8, \
  243. .blocklen = 200 - 2*(bits/8), \
  244. HASHALG_NAMES_BARE("SHA3-" #bits), \
  245. }
  246. DEFINE_SHA3(224);
  247. DEFINE_SHA3(256);
  248. DEFINE_SHA3(384);
  249. DEFINE_SHA3(512);
  250. static void shake256_reset(ssh_hash *hash)
  251. {
  252. struct keccak_hash *kh = container_of(hash, struct keccak_hash, hash);
  253. keccak_shake_init(&kh->state, 256, hash->vt->hlen * 8);
  254. }
  255. /*
  256. * There is some confusion over the output length parameter for the
  257. * SHAKE functions. By my reading, FIPS PUB 202 defines SHAKE256(M,d)
  258. * to generate d _bits_ of output. But RFC 8032 (defining Ed448) talks
  259. * about "SHAKE256(x,114)" in a context where it definitely means
  260. * generating 114 _bytes_ of output.
  261. *
  262. * Our internal ID therefore suffixes the output length with "bytes",
  263. * to be clear which we're talking about
  264. */
  265. #define DEFINE_SHAKE(param, hashbytes) \
  266. const ssh_hashalg ssh_shake##param##_##hashbytes##bytes = { \
  267. .new = keccak_new, \
  268. .reset = shake##param##_reset, \
  269. .copyfrom = keccak_copyfrom, \
  270. .digest = keccak_digest, \
  271. .free = keccak_free, \
  272. .hlen = hashbytes, \
  273. .blocklen = 0, \
  274. HASHALG_NAMES_BARE("SHAKE" #param), \
  275. }
  276. DEFINE_SHAKE(256, 114);