sha3_generic.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Cryptographic API.
  3. *
  4. * SHA-3, as specified in
  5. * http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
  6. *
  7. * SHA-3 code by Jeff Garzik <jeff@garzik.org>
  8. * Ard Biesheuvel <ard.biesheuvel@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)•
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/hash.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <crypto/sha3.h>
  21. #include <asm/unaligned.h>
  22. /*
  23. * On some 32-bit architectures (h8300), GCC ends up using
  24. * over 1 KB of stack if we inline the round calculation into the loop
  25. * in keccakf(). On the other hand, on 64-bit architectures with plenty
  26. * of [64-bit wide] general purpose registers, not inlining it severely
  27. * hurts performance. So let's use 64-bitness as a heuristic to decide
  28. * whether to inline or not.
  29. */
  30. #ifdef CONFIG_64BIT
  31. #define SHA3_INLINE inline
  32. #else
  33. #define SHA3_INLINE noinline
  34. #endif
  35. #define KECCAK_ROUNDS 24
  36. static const u64 keccakf_rndc[24] = {
  37. 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
  38. 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
  39. 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
  40. 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
  41. 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
  42. 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
  43. 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
  44. 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
  45. };
  46. /* update the state with given number of rounds */
  47. static SHA3_INLINE void keccakf_round(u64 st[25])
  48. {
  49. u64 t[5], tt, bc[5];
  50. /* Theta */
  51. bc[0] = st[0] ^ st[5] ^ st[10] ^ st[15] ^ st[20];
  52. bc[1] = st[1] ^ st[6] ^ st[11] ^ st[16] ^ st[21];
  53. bc[2] = st[2] ^ st[7] ^ st[12] ^ st[17] ^ st[22];
  54. bc[3] = st[3] ^ st[8] ^ st[13] ^ st[18] ^ st[23];
  55. bc[4] = st[4] ^ st[9] ^ st[14] ^ st[19] ^ st[24];
  56. t[0] = bc[4] ^ rol64(bc[1], 1);
  57. t[1] = bc[0] ^ rol64(bc[2], 1);
  58. t[2] = bc[1] ^ rol64(bc[3], 1);
  59. t[3] = bc[2] ^ rol64(bc[4], 1);
  60. t[4] = bc[3] ^ rol64(bc[0], 1);
  61. st[0] ^= t[0];
  62. /* Rho Pi */
  63. tt = st[1];
  64. st[ 1] = rol64(st[ 6] ^ t[1], 44);
  65. st[ 6] = rol64(st[ 9] ^ t[4], 20);
  66. st[ 9] = rol64(st[22] ^ t[2], 61);
  67. st[22] = rol64(st[14] ^ t[4], 39);
  68. st[14] = rol64(st[20] ^ t[0], 18);
  69. st[20] = rol64(st[ 2] ^ t[2], 62);
  70. st[ 2] = rol64(st[12] ^ t[2], 43);
  71. st[12] = rol64(st[13] ^ t[3], 25);
  72. st[13] = rol64(st[19] ^ t[4], 8);
  73. st[19] = rol64(st[23] ^ t[3], 56);
  74. st[23] = rol64(st[15] ^ t[0], 41);
  75. st[15] = rol64(st[ 4] ^ t[4], 27);
  76. st[ 4] = rol64(st[24] ^ t[4], 14);
  77. st[24] = rol64(st[21] ^ t[1], 2);
  78. st[21] = rol64(st[ 8] ^ t[3], 55);
  79. st[ 8] = rol64(st[16] ^ t[1], 45);
  80. st[16] = rol64(st[ 5] ^ t[0], 36);
  81. st[ 5] = rol64(st[ 3] ^ t[3], 28);
  82. st[ 3] = rol64(st[18] ^ t[3], 21);
  83. st[18] = rol64(st[17] ^ t[2], 15);
  84. st[17] = rol64(st[11] ^ t[1], 10);
  85. st[11] = rol64(st[ 7] ^ t[2], 6);
  86. st[ 7] = rol64(st[10] ^ t[0], 3);
  87. st[10] = rol64( tt ^ t[1], 1);
  88. /* Chi */
  89. bc[ 0] = ~st[ 1] & st[ 2];
  90. bc[ 1] = ~st[ 2] & st[ 3];
  91. bc[ 2] = ~st[ 3] & st[ 4];
  92. bc[ 3] = ~st[ 4] & st[ 0];
  93. bc[ 4] = ~st[ 0] & st[ 1];
  94. st[ 0] ^= bc[ 0];
  95. st[ 1] ^= bc[ 1];
  96. st[ 2] ^= bc[ 2];
  97. st[ 3] ^= bc[ 3];
  98. st[ 4] ^= bc[ 4];
  99. bc[ 0] = ~st[ 6] & st[ 7];
  100. bc[ 1] = ~st[ 7] & st[ 8];
  101. bc[ 2] = ~st[ 8] & st[ 9];
  102. bc[ 3] = ~st[ 9] & st[ 5];
  103. bc[ 4] = ~st[ 5] & st[ 6];
  104. st[ 5] ^= bc[ 0];
  105. st[ 6] ^= bc[ 1];
  106. st[ 7] ^= bc[ 2];
  107. st[ 8] ^= bc[ 3];
  108. st[ 9] ^= bc[ 4];
  109. bc[ 0] = ~st[11] & st[12];
  110. bc[ 1] = ~st[12] & st[13];
  111. bc[ 2] = ~st[13] & st[14];
  112. bc[ 3] = ~st[14] & st[10];
  113. bc[ 4] = ~st[10] & st[11];
  114. st[10] ^= bc[ 0];
  115. st[11] ^= bc[ 1];
  116. st[12] ^= bc[ 2];
  117. st[13] ^= bc[ 3];
  118. st[14] ^= bc[ 4];
  119. bc[ 0] = ~st[16] & st[17];
  120. bc[ 1] = ~st[17] & st[18];
  121. bc[ 2] = ~st[18] & st[19];
  122. bc[ 3] = ~st[19] & st[15];
  123. bc[ 4] = ~st[15] & st[16];
  124. st[15] ^= bc[ 0];
  125. st[16] ^= bc[ 1];
  126. st[17] ^= bc[ 2];
  127. st[18] ^= bc[ 3];
  128. st[19] ^= bc[ 4];
  129. bc[ 0] = ~st[21] & st[22];
  130. bc[ 1] = ~st[22] & st[23];
  131. bc[ 2] = ~st[23] & st[24];
  132. bc[ 3] = ~st[24] & st[20];
  133. bc[ 4] = ~st[20] & st[21];
  134. st[20] ^= bc[ 0];
  135. st[21] ^= bc[ 1];
  136. st[22] ^= bc[ 2];
  137. st[23] ^= bc[ 3];
  138. st[24] ^= bc[ 4];
  139. }
  140. static void keccakf(u64 st[25])
  141. {
  142. int round;
  143. for (round = 0; round < KECCAK_ROUNDS; round++) {
  144. keccakf_round(st);
  145. /* Iota */
  146. st[0] ^= keccakf_rndc[round];
  147. }
  148. }
  149. int crypto_sha3_init(struct shash_desc *desc)
  150. {
  151. struct sha3_state *sctx = shash_desc_ctx(desc);
  152. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  153. sctx->rsiz = 200 - 2 * digest_size;
  154. sctx->rsizw = sctx->rsiz / 8;
  155. sctx->partial = 0;
  156. memset(sctx->st, 0, sizeof(sctx->st));
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(crypto_sha3_init);
  160. int crypto_sha3_update(struct shash_desc *desc, const u8 *data,
  161. unsigned int len)
  162. {
  163. struct sha3_state *sctx = shash_desc_ctx(desc);
  164. unsigned int done;
  165. const u8 *src;
  166. done = 0;
  167. src = data;
  168. if ((sctx->partial + len) > (sctx->rsiz - 1)) {
  169. if (sctx->partial) {
  170. done = -sctx->partial;
  171. memcpy(sctx->buf + sctx->partial, data,
  172. done + sctx->rsiz);
  173. src = sctx->buf;
  174. }
  175. do {
  176. unsigned int i;
  177. for (i = 0; i < sctx->rsizw; i++)
  178. sctx->st[i] ^= get_unaligned_le64(src + 8 * i);
  179. keccakf(sctx->st);
  180. done += sctx->rsiz;
  181. src = data + done;
  182. } while (done + (sctx->rsiz - 1) < len);
  183. sctx->partial = 0;
  184. }
  185. memcpy(sctx->buf + sctx->partial, src, len - done);
  186. sctx->partial += (len - done);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL(crypto_sha3_update);
  190. int crypto_sha3_final(struct shash_desc *desc, u8 *out)
  191. {
  192. struct sha3_state *sctx = shash_desc_ctx(desc);
  193. unsigned int i, inlen = sctx->partial;
  194. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  195. __le64 *digest = (__le64 *)out;
  196. sctx->buf[inlen++] = 0x06;
  197. memset(sctx->buf + inlen, 0, sctx->rsiz - inlen);
  198. sctx->buf[sctx->rsiz - 1] |= 0x80;
  199. for (i = 0; i < sctx->rsizw; i++)
  200. sctx->st[i] ^= get_unaligned_le64(sctx->buf + 8 * i);
  201. keccakf(sctx->st);
  202. for (i = 0; i < digest_size / 8; i++)
  203. put_unaligned_le64(sctx->st[i], digest++);
  204. if (digest_size & 4)
  205. put_unaligned_le32(sctx->st[i], (__le32 *)digest);
  206. memset(sctx, 0, sizeof(*sctx));
  207. return 0;
  208. }
  209. EXPORT_SYMBOL(crypto_sha3_final);
  210. static struct shash_alg algs[] = { {
  211. .digestsize = SHA3_224_DIGEST_SIZE,
  212. .init = crypto_sha3_init,
  213. .update = crypto_sha3_update,
  214. .final = crypto_sha3_final,
  215. .descsize = sizeof(struct sha3_state),
  216. .base.cra_name = "sha3-224",
  217. .base.cra_driver_name = "sha3-224-generic",
  218. .base.cra_blocksize = SHA3_224_BLOCK_SIZE,
  219. .base.cra_module = THIS_MODULE,
  220. }, {
  221. .digestsize = SHA3_256_DIGEST_SIZE,
  222. .init = crypto_sha3_init,
  223. .update = crypto_sha3_update,
  224. .final = crypto_sha3_final,
  225. .descsize = sizeof(struct sha3_state),
  226. .base.cra_name = "sha3-256",
  227. .base.cra_driver_name = "sha3-256-generic",
  228. .base.cra_blocksize = SHA3_256_BLOCK_SIZE,
  229. .base.cra_module = THIS_MODULE,
  230. }, {
  231. .digestsize = SHA3_384_DIGEST_SIZE,
  232. .init = crypto_sha3_init,
  233. .update = crypto_sha3_update,
  234. .final = crypto_sha3_final,
  235. .descsize = sizeof(struct sha3_state),
  236. .base.cra_name = "sha3-384",
  237. .base.cra_driver_name = "sha3-384-generic",
  238. .base.cra_blocksize = SHA3_384_BLOCK_SIZE,
  239. .base.cra_module = THIS_MODULE,
  240. }, {
  241. .digestsize = SHA3_512_DIGEST_SIZE,
  242. .init = crypto_sha3_init,
  243. .update = crypto_sha3_update,
  244. .final = crypto_sha3_final,
  245. .descsize = sizeof(struct sha3_state),
  246. .base.cra_name = "sha3-512",
  247. .base.cra_driver_name = "sha3-512-generic",
  248. .base.cra_blocksize = SHA3_512_BLOCK_SIZE,
  249. .base.cra_module = THIS_MODULE,
  250. } };
  251. static int __init sha3_generic_mod_init(void)
  252. {
  253. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  254. }
  255. static void __exit sha3_generic_mod_fini(void)
  256. {
  257. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  258. }
  259. module_init(sha3_generic_mod_init);
  260. module_exit(sha3_generic_mod_fini);
  261. MODULE_LICENSE("GPL");
  262. MODULE_DESCRIPTION("SHA-3 Secure Hash Algorithm");
  263. MODULE_ALIAS_CRYPTO("sha3-224");
  264. MODULE_ALIAS_CRYPTO("sha3-224-generic");
  265. MODULE_ALIAS_CRYPTO("sha3-256");
  266. MODULE_ALIAS_CRYPTO("sha3-256-generic");
  267. MODULE_ALIAS_CRYPTO("sha3-384");
  268. MODULE_ALIAS_CRYPTO("sha3-384-generic");
  269. MODULE_ALIAS_CRYPTO("sha3-512");
  270. MODULE_ALIAS_CRYPTO("sha3-512-generic");