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.

404 lines
11 KiB

  1. /* Jitter RNG: SHA-3 Implementation
  2. *
  3. * Copyright (C) 2021 - 2022, Stephan Mueller <smueller@chronox.de>
  4. *
  5. * License: see LICENSE file in root directory
  6. *
  7. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  8. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  9. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  10. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  11. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  12. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  13. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  14. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  15. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  17. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  18. * DAMAGE.
  19. */
  20. #include "jitterentropy-sha3.h"
  21. #include "jitterentropy.h"
  22. /***************************************************************************
  23. * Message Digest Implementation
  24. ***************************************************************************/
  25. /*
  26. * Conversion of Little-Endian representations in byte streams - the data
  27. * representation in the integer values is the host representation.
  28. */
  29. static inline uint32_t ptr_to_le32(const uint8_t *p)
  30. {
  31. return (uint32_t)p[0] | (uint32_t)p[1] << 8 |
  32. (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
  33. }
  34. static inline uint64_t ptr_to_le64(const uint8_t *p)
  35. {
  36. return (uint64_t)ptr_to_le32(p) | (uint64_t)ptr_to_le32(p + 4) << 32;
  37. }
  38. static inline void le32_to_ptr(uint8_t *p, const uint32_t value)
  39. {
  40. p[0] = (uint8_t)(value);
  41. p[1] = (uint8_t)(value >> 8);
  42. p[2] = (uint8_t)(value >> 16);
  43. p[3] = (uint8_t)(value >> 24);
  44. }
  45. static inline void le64_to_ptr(uint8_t *p, const uint64_t value)
  46. {
  47. le32_to_ptr(p + 4, (uint32_t)(value >> 32));
  48. le32_to_ptr(p, (uint32_t)(value));
  49. }
  50. /*********************************** Keccak ***********************************/
  51. /* state[x + y*5] */
  52. #define A(x, y) (x + 5 * y)
  53. static inline void keccakp_theta(uint64_t s[25])
  54. {
  55. uint64_t C[5], D[5];
  56. /* Step 1 */
  57. C[0] = s[A(0, 0)] ^ s[A(0, 1)] ^ s[A(0, 2)] ^ s[A(0, 3)] ^ s[A(0, 4)];
  58. C[1] = s[A(1, 0)] ^ s[A(1, 1)] ^ s[A(1, 2)] ^ s[A(1, 3)] ^ s[A(1, 4)];
  59. C[2] = s[A(2, 0)] ^ s[A(2, 1)] ^ s[A(2, 2)] ^ s[A(2, 3)] ^ s[A(2, 4)];
  60. C[3] = s[A(3, 0)] ^ s[A(3, 1)] ^ s[A(3, 2)] ^ s[A(3, 3)] ^ s[A(3, 4)];
  61. C[4] = s[A(4, 0)] ^ s[A(4, 1)] ^ s[A(4, 2)] ^ s[A(4, 3)] ^ s[A(4, 4)];
  62. /* Step 2 */
  63. D[0] = C[4] ^ rol64(C[1], 1);
  64. D[1] = C[0] ^ rol64(C[2], 1);
  65. D[2] = C[1] ^ rol64(C[3], 1);
  66. D[3] = C[2] ^ rol64(C[4], 1);
  67. D[4] = C[3] ^ rol64(C[0], 1);
  68. /* Step 3 */
  69. s[A(0, 0)] ^= D[0];
  70. s[A(1, 0)] ^= D[1];
  71. s[A(2, 0)] ^= D[2];
  72. s[A(3, 0)] ^= D[3];
  73. s[A(4, 0)] ^= D[4];
  74. s[A(0, 1)] ^= D[0];
  75. s[A(1, 1)] ^= D[1];
  76. s[A(2, 1)] ^= D[2];
  77. s[A(3, 1)] ^= D[3];
  78. s[A(4, 1)] ^= D[4];
  79. s[A(0, 2)] ^= D[0];
  80. s[A(1, 2)] ^= D[1];
  81. s[A(2, 2)] ^= D[2];
  82. s[A(3, 2)] ^= D[3];
  83. s[A(4, 2)] ^= D[4];
  84. s[A(0, 3)] ^= D[0];
  85. s[A(1, 3)] ^= D[1];
  86. s[A(2, 3)] ^= D[2];
  87. s[A(3, 3)] ^= D[3];
  88. s[A(4, 3)] ^= D[4];
  89. s[A(0, 4)] ^= D[0];
  90. s[A(1, 4)] ^= D[1];
  91. s[A(2, 4)] ^= D[2];
  92. s[A(3, 4)] ^= D[3];
  93. s[A(4, 4)] ^= D[4];
  94. }
  95. static inline void keccakp_rho(uint64_t s[25])
  96. {
  97. /* Step 1 */
  98. /* s[A(0, 0)] = s[A(0, 0)]; */
  99. #define RHO_ROL(t) (((t + 1) * (t + 2) / 2) % 64)
  100. /* Step 3 */
  101. s[A(1, 0)] = rol64(s[A(1, 0)], RHO_ROL(0));
  102. s[A(0, 2)] = rol64(s[A(0, 2)], RHO_ROL(1));
  103. s[A(2, 1)] = rol64(s[A(2, 1)], RHO_ROL(2));
  104. s[A(1, 2)] = rol64(s[A(1, 2)], RHO_ROL(3));
  105. s[A(2, 3)] = rol64(s[A(2, 3)], RHO_ROL(4));
  106. s[A(3, 3)] = rol64(s[A(3, 3)], RHO_ROL(5));
  107. s[A(3, 0)] = rol64(s[A(3, 0)], RHO_ROL(6));
  108. s[A(0, 1)] = rol64(s[A(0, 1)], RHO_ROL(7));
  109. s[A(1, 3)] = rol64(s[A(1, 3)], RHO_ROL(8));
  110. s[A(3, 1)] = rol64(s[A(3, 1)], RHO_ROL(9));
  111. s[A(1, 4)] = rol64(s[A(1, 4)], RHO_ROL(10));
  112. s[A(4, 4)] = rol64(s[A(4, 4)], RHO_ROL(11));
  113. s[A(4, 0)] = rol64(s[A(4, 0)], RHO_ROL(12));
  114. s[A(0, 3)] = rol64(s[A(0, 3)], RHO_ROL(13));
  115. s[A(3, 4)] = rol64(s[A(3, 4)], RHO_ROL(14));
  116. s[A(4, 3)] = rol64(s[A(4, 3)], RHO_ROL(15));
  117. s[A(3, 2)] = rol64(s[A(3, 2)], RHO_ROL(16));
  118. s[A(2, 2)] = rol64(s[A(2, 2)], RHO_ROL(17));
  119. s[A(2, 0)] = rol64(s[A(2, 0)], RHO_ROL(18));
  120. s[A(0, 4)] = rol64(s[A(0, 4)], RHO_ROL(19));
  121. s[A(4, 2)] = rol64(s[A(4, 2)], RHO_ROL(20));
  122. s[A(2, 4)] = rol64(s[A(2, 4)], RHO_ROL(21));
  123. s[A(4, 1)] = rol64(s[A(4, 1)], RHO_ROL(22));
  124. s[A(1, 1)] = rol64(s[A(1, 1)], RHO_ROL(23));
  125. }
  126. static inline void keccakp_pi(uint64_t s[25])
  127. {
  128. uint64_t t = s[A(4, 4)];
  129. /* Step 1 */
  130. /* s[A(0, 0)] = s[A(0, 0)]; */
  131. s[A(4, 4)] = s[A(1, 4)];
  132. s[A(1, 4)] = s[A(3, 1)];
  133. s[A(3, 1)] = s[A(1, 3)];
  134. s[A(1, 3)] = s[A(0, 1)];
  135. s[A(0, 1)] = s[A(3, 0)];
  136. s[A(3, 0)] = s[A(3, 3)];
  137. s[A(3, 3)] = s[A(2, 3)];
  138. s[A(2, 3)] = s[A(1, 2)];
  139. s[A(1, 2)] = s[A(2, 1)];
  140. s[A(2, 1)] = s[A(0, 2)];
  141. s[A(0, 2)] = s[A(1, 0)];
  142. s[A(1, 0)] = s[A(1, 1)];
  143. s[A(1, 1)] = s[A(4, 1)];
  144. s[A(4, 1)] = s[A(2, 4)];
  145. s[A(2, 4)] = s[A(4, 2)];
  146. s[A(4, 2)] = s[A(0, 4)];
  147. s[A(0, 4)] = s[A(2, 0)];
  148. s[A(2, 0)] = s[A(2, 2)];
  149. s[A(2, 2)] = s[A(3, 2)];
  150. s[A(3, 2)] = s[A(4, 3)];
  151. s[A(4, 3)] = s[A(3, 4)];
  152. s[A(3, 4)] = s[A(0, 3)];
  153. s[A(0, 3)] = s[A(4, 0)];
  154. s[A(4, 0)] = t;
  155. }
  156. static inline void keccakp_chi(uint64_t s[25])
  157. {
  158. uint64_t t0[5], t1[5];
  159. t0[0] = s[A(0, 0)];
  160. t0[1] = s[A(0, 1)];
  161. t0[2] = s[A(0, 2)];
  162. t0[3] = s[A(0, 3)];
  163. t0[4] = s[A(0, 4)];
  164. t1[0] = s[A(1, 0)];
  165. t1[1] = s[A(1, 1)];
  166. t1[2] = s[A(1, 2)];
  167. t1[3] = s[A(1, 3)];
  168. t1[4] = s[A(1, 4)];
  169. s[A(0, 0)] ^= ~s[A(1, 0)] & s[A(2, 0)];
  170. s[A(0, 1)] ^= ~s[A(1, 1)] & s[A(2, 1)];
  171. s[A(0, 2)] ^= ~s[A(1, 2)] & s[A(2, 2)];
  172. s[A(0, 3)] ^= ~s[A(1, 3)] & s[A(2, 3)];
  173. s[A(0, 4)] ^= ~s[A(1, 4)] & s[A(2, 4)];
  174. s[A(1, 0)] ^= ~s[A(2, 0)] & s[A(3, 0)];
  175. s[A(1, 1)] ^= ~s[A(2, 1)] & s[A(3, 1)];
  176. s[A(1, 2)] ^= ~s[A(2, 2)] & s[A(3, 2)];
  177. s[A(1, 3)] ^= ~s[A(2, 3)] & s[A(3, 3)];
  178. s[A(1, 4)] ^= ~s[A(2, 4)] & s[A(3, 4)];
  179. s[A(2, 0)] ^= ~s[A(3, 0)] & s[A(4, 0)];
  180. s[A(2, 1)] ^= ~s[A(3, 1)] & s[A(4, 1)];
  181. s[A(2, 2)] ^= ~s[A(3, 2)] & s[A(4, 2)];
  182. s[A(2, 3)] ^= ~s[A(3, 3)] & s[A(4, 3)];
  183. s[A(2, 4)] ^= ~s[A(3, 4)] & s[A(4, 4)];
  184. s[A(3, 0)] ^= ~s[A(4, 0)] & t0[0];
  185. s[A(3, 1)] ^= ~s[A(4, 1)] & t0[1];
  186. s[A(3, 2)] ^= ~s[A(4, 2)] & t0[2];
  187. s[A(3, 3)] ^= ~s[A(4, 3)] & t0[3];
  188. s[A(3, 4)] ^= ~s[A(4, 4)] & t0[4];
  189. s[A(4, 0)] ^= ~t0[0] & t1[0];
  190. s[A(4, 1)] ^= ~t0[1] & t1[1];
  191. s[A(4, 2)] ^= ~t0[2] & t1[2];
  192. s[A(4, 3)] ^= ~t0[3] & t1[3];
  193. s[A(4, 4)] ^= ~t0[4] & t1[4];
  194. }
  195. static const uint64_t keccakp_iota_vals[] = {
  196. 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
  197. 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
  198. 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
  199. 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
  200. 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
  201. 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
  202. 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
  203. 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
  204. };
  205. static inline void keccakp_iota(uint64_t s[25], unsigned int round)
  206. {
  207. s[0] ^= keccakp_iota_vals[round];
  208. }
  209. static inline void keccakp_1600(uint64_t s[25])
  210. {
  211. unsigned int round;
  212. for (round = 0; round < 24; round++) {
  213. keccakp_theta(s);
  214. keccakp_rho(s);
  215. keccakp_pi(s);
  216. keccakp_chi(s);
  217. keccakp_iota(s, round);
  218. }
  219. }
  220. /*********************************** SHA-3 ************************************/
  221. static inline void sha3_init(struct sha_ctx *ctx)
  222. {
  223. unsigned int i;
  224. for (i = 0; i < 25; i++)
  225. ctx->state[i] = 0;
  226. ctx->msg_len = 0;
  227. }
  228. void sha3_256_init(struct sha_ctx *ctx)
  229. {
  230. sha3_init(ctx);
  231. ctx->r = SHA3_256_SIZE_BLOCK;
  232. ctx->rword = SHA3_256_SIZE_BLOCK / sizeof(uint64_t);
  233. ctx->digestsize = SHA3_256_SIZE_DIGEST;
  234. }
  235. static inline void sha3_fill_state(struct sha_ctx *ctx, const uint8_t *in)
  236. {
  237. unsigned int i;
  238. for (i = 0; i < ctx->rword; i++) {
  239. ctx->state[i] ^= ptr_to_le64(in);
  240. in += 8;
  241. }
  242. }
  243. void sha3_update(struct sha_ctx *ctx, const uint8_t *in, size_t inlen)
  244. {
  245. size_t partial = ctx->msg_len % ctx->r;
  246. ctx->msg_len += inlen;
  247. /* Sponge absorbing phase */
  248. /* Check if we have a partial block stored */
  249. if (partial) {
  250. size_t todo = ctx->r - partial;
  251. /*
  252. * If the provided data is small enough to fit in the partial
  253. * buffer, copy it and leave it unprocessed.
  254. */
  255. if (inlen < todo) {
  256. memcpy(ctx->partial + partial, in, inlen);
  257. return;
  258. }
  259. /*
  260. * The input data is large enough to fill the entire partial
  261. * block buffer. Thus, we fill it and transform it.
  262. */
  263. memcpy(ctx->partial + partial, in, todo);
  264. inlen -= todo;
  265. in += todo;
  266. sha3_fill_state(ctx, ctx->partial);
  267. keccakp_1600(ctx->state);
  268. }
  269. /* Perform a transformation of full block-size messages */
  270. for (; inlen >= ctx->r; inlen -= ctx->r, in += ctx->r) {
  271. sha3_fill_state(ctx, in);
  272. keccakp_1600(ctx->state);
  273. }
  274. /* If we have data left, copy it into the partial block buffer */
  275. memcpy(ctx->partial, in, inlen);
  276. }
  277. void sha3_final(struct sha_ctx *ctx, uint8_t *digest)
  278. {
  279. size_t partial = ctx->msg_len % ctx->r;
  280. unsigned int i;
  281. /* Final round in sponge absorbing phase */
  282. /* Fill the unused part of the partial buffer with zeros */
  283. memset(ctx->partial + partial, 0, ctx->r - partial);
  284. /*
  285. * Add the leading and trailing bit as well as the 01 bits for the
  286. * SHA-3 suffix.
  287. */
  288. ctx->partial[partial] = 0x06;
  289. ctx->partial[ctx->r - 1] |= 0x80;
  290. /* Final transformation */
  291. sha3_fill_state(ctx, ctx->partial);
  292. keccakp_1600(ctx->state);
  293. /*
  294. * Sponge squeeze phase - the digest size is always smaller as the
  295. * state size r which implies we only have one squeeze round.
  296. */
  297. for (i = 0; i < ctx->digestsize / 8; i++, digest += 8)
  298. le64_to_ptr(digest, ctx->state[i]);
  299. /* Add remaining 4 bytes if we use SHA3-224 */
  300. if (ctx->digestsize % 8)
  301. le32_to_ptr(digest, (uint32_t)(ctx->state[i]));
  302. memset(ctx->partial, 0, ctx->r);
  303. sha3_init(ctx);
  304. }
  305. int sha3_tester(void)
  306. {
  307. HASH_CTX_ON_STACK(ctx);
  308. static const uint8_t msg_256[] = { 0x5E, 0x5E, 0xD6 };
  309. static const uint8_t exp_256[] = { 0xF1, 0x6E, 0x66, 0xC0, 0x43, 0x72,
  310. 0xB4, 0xA3, 0xE1, 0xE3, 0x2E, 0x07,
  311. 0xC4, 0x1C, 0x03, 0x40, 0x8A, 0xD5,
  312. 0x43, 0x86, 0x8C, 0xC4, 0x0E, 0xC5,
  313. 0x5E, 0x00, 0xBB, 0xBB, 0xBD, 0xF5,
  314. 0x91, 0x1E };
  315. uint8_t act[SHA3_256_SIZE_DIGEST] = { 0 };
  316. unsigned int i;
  317. sha3_256_init(&ctx);
  318. sha3_update(&ctx, msg_256, 3);
  319. sha3_final(&ctx, act);
  320. for (i = 0; i < SHA3_256_SIZE_DIGEST; i++) {
  321. if (exp_256[i] != act[i])
  322. return 1;
  323. }
  324. return 0;
  325. }
  326. int sha3_alloc(void **hash_state)
  327. {
  328. struct sha_ctx *tmp;
  329. tmp = jent_zalloc(SHA_MAX_CTX_SIZE);
  330. if (!tmp)
  331. return 1;
  332. *hash_state = tmp;
  333. return 0;
  334. }
  335. void sha3_dealloc(void *hash_state)
  336. {
  337. struct sha_ctx *ctx = (struct sha_ctx *)hash_state;
  338. jent_zfree(ctx, SHA_MAX_CTX_SIZE);
  339. }