sha3_generic.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)•
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/internal/hash.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <crypto/sha3.h>
  20. #include <asm/byteorder.h>
  21. #define KECCAK_ROUNDS 24
  22. #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
  23. static const u64 keccakf_rndc[24] = {
  24. 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
  25. 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
  26. 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
  27. 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
  28. 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
  29. 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
  30. 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
  31. 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
  32. };
  33. static const int keccakf_rotc[24] = {
  34. 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
  35. 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
  36. };
  37. static const int keccakf_piln[24] = {
  38. 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
  39. 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
  40. };
  41. /* update the state with given number of rounds */
  42. static void keccakf(u64 st[25])
  43. {
  44. int i, j, round;
  45. u64 t, bc[5];
  46. for (round = 0; round < KECCAK_ROUNDS; round++) {
  47. /* Theta */
  48. for (i = 0; i < 5; i++)
  49. bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15]
  50. ^ st[i + 20];
  51. for (i = 0; i < 5; i++) {
  52. t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
  53. for (j = 0; j < 25; j += 5)
  54. st[j + i] ^= t;
  55. }
  56. /* Rho Pi */
  57. t = st[1];
  58. for (i = 0; i < 24; i++) {
  59. j = keccakf_piln[i];
  60. bc[0] = st[j];
  61. st[j] = ROTL64(t, keccakf_rotc[i]);
  62. t = bc[0];
  63. }
  64. /* Chi */
  65. for (j = 0; j < 25; j += 5) {
  66. for (i = 0; i < 5; i++)
  67. bc[i] = st[j + i];
  68. for (i = 0; i < 5; i++)
  69. st[j + i] ^= (~bc[(i + 1) % 5]) &
  70. bc[(i + 2) % 5];
  71. }
  72. /* Iota */
  73. st[0] ^= keccakf_rndc[round];
  74. }
  75. }
  76. static void sha3_init(struct sha3_state *sctx, unsigned int digest_sz)
  77. {
  78. memset(sctx, 0, sizeof(*sctx));
  79. sctx->md_len = digest_sz;
  80. sctx->rsiz = 200 - 2 * digest_sz;
  81. sctx->rsizw = sctx->rsiz / 8;
  82. }
  83. static int sha3_224_init(struct shash_desc *desc)
  84. {
  85. struct sha3_state *sctx = shash_desc_ctx(desc);
  86. sha3_init(sctx, SHA3_224_DIGEST_SIZE);
  87. return 0;
  88. }
  89. static int sha3_256_init(struct shash_desc *desc)
  90. {
  91. struct sha3_state *sctx = shash_desc_ctx(desc);
  92. sha3_init(sctx, SHA3_256_DIGEST_SIZE);
  93. return 0;
  94. }
  95. static int sha3_384_init(struct shash_desc *desc)
  96. {
  97. struct sha3_state *sctx = shash_desc_ctx(desc);
  98. sha3_init(sctx, SHA3_384_DIGEST_SIZE);
  99. return 0;
  100. }
  101. static int sha3_512_init(struct shash_desc *desc)
  102. {
  103. struct sha3_state *sctx = shash_desc_ctx(desc);
  104. sha3_init(sctx, SHA3_512_DIGEST_SIZE);
  105. return 0;
  106. }
  107. static int sha3_update(struct shash_desc *desc, const u8 *data,
  108. unsigned int len)
  109. {
  110. struct sha3_state *sctx = shash_desc_ctx(desc);
  111. unsigned int done;
  112. const u8 *src;
  113. done = 0;
  114. src = data;
  115. if ((sctx->partial + len) > (sctx->rsiz - 1)) {
  116. if (sctx->partial) {
  117. done = -sctx->partial;
  118. memcpy(sctx->buf + sctx->partial, data,
  119. done + sctx->rsiz);
  120. src = sctx->buf;
  121. }
  122. do {
  123. unsigned int i;
  124. for (i = 0; i < sctx->rsizw; i++)
  125. sctx->st[i] ^= ((u64 *) src)[i];
  126. keccakf(sctx->st);
  127. done += sctx->rsiz;
  128. src = data + done;
  129. } while (done + (sctx->rsiz - 1) < len);
  130. sctx->partial = 0;
  131. }
  132. memcpy(sctx->buf + sctx->partial, src, len - done);
  133. sctx->partial += (len - done);
  134. return 0;
  135. }
  136. static int sha3_final(struct shash_desc *desc, u8 *out)
  137. {
  138. struct sha3_state *sctx = shash_desc_ctx(desc);
  139. unsigned int i, inlen = sctx->partial;
  140. sctx->buf[inlen++] = 0x06;
  141. memset(sctx->buf + inlen, 0, sctx->rsiz - inlen);
  142. sctx->buf[sctx->rsiz - 1] |= 0x80;
  143. for (i = 0; i < sctx->rsizw; i++)
  144. sctx->st[i] ^= ((u64 *) sctx->buf)[i];
  145. keccakf(sctx->st);
  146. for (i = 0; i < sctx->rsizw; i++)
  147. sctx->st[i] = cpu_to_le64(sctx->st[i]);
  148. memcpy(out, sctx->st, sctx->md_len);
  149. memset(sctx, 0, sizeof(*sctx));
  150. return 0;
  151. }
  152. static struct shash_alg sha3_224 = {
  153. .digestsize = SHA3_224_DIGEST_SIZE,
  154. .init = sha3_224_init,
  155. .update = sha3_update,
  156. .final = sha3_final,
  157. .descsize = sizeof(struct sha3_state),
  158. .base = {
  159. .cra_name = "sha3-224",
  160. .cra_driver_name = "sha3-224-generic",
  161. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  162. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  163. .cra_module = THIS_MODULE,
  164. }
  165. };
  166. static struct shash_alg sha3_256 = {
  167. .digestsize = SHA3_256_DIGEST_SIZE,
  168. .init = sha3_256_init,
  169. .update = sha3_update,
  170. .final = sha3_final,
  171. .descsize = sizeof(struct sha3_state),
  172. .base = {
  173. .cra_name = "sha3-256",
  174. .cra_driver_name = "sha3-256-generic",
  175. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  176. .cra_blocksize = SHA3_256_BLOCK_SIZE,
  177. .cra_module = THIS_MODULE,
  178. }
  179. };
  180. static struct shash_alg sha3_384 = {
  181. .digestsize = SHA3_384_DIGEST_SIZE,
  182. .init = sha3_384_init,
  183. .update = sha3_update,
  184. .final = sha3_final,
  185. .descsize = sizeof(struct sha3_state),
  186. .base = {
  187. .cra_name = "sha3-384",
  188. .cra_driver_name = "sha3-384-generic",
  189. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  190. .cra_blocksize = SHA3_384_BLOCK_SIZE,
  191. .cra_module = THIS_MODULE,
  192. }
  193. };
  194. static struct shash_alg sha3_512 = {
  195. .digestsize = SHA3_512_DIGEST_SIZE,
  196. .init = sha3_512_init,
  197. .update = sha3_update,
  198. .final = sha3_final,
  199. .descsize = sizeof(struct sha3_state),
  200. .base = {
  201. .cra_name = "sha3-512",
  202. .cra_driver_name = "sha3-512-generic",
  203. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  204. .cra_blocksize = SHA3_512_BLOCK_SIZE,
  205. .cra_module = THIS_MODULE,
  206. }
  207. };
  208. static int __init sha3_generic_mod_init(void)
  209. {
  210. int ret;
  211. ret = crypto_register_shash(&sha3_224);
  212. if (ret < 0)
  213. goto err_out;
  214. ret = crypto_register_shash(&sha3_256);
  215. if (ret < 0)
  216. goto err_out_224;
  217. ret = crypto_register_shash(&sha3_384);
  218. if (ret < 0)
  219. goto err_out_256;
  220. ret = crypto_register_shash(&sha3_512);
  221. if (ret < 0)
  222. goto err_out_384;
  223. return 0;
  224. err_out_384:
  225. crypto_unregister_shash(&sha3_384);
  226. err_out_256:
  227. crypto_unregister_shash(&sha3_256);
  228. err_out_224:
  229. crypto_unregister_shash(&sha3_224);
  230. err_out:
  231. return ret;
  232. }
  233. static void __exit sha3_generic_mod_fini(void)
  234. {
  235. crypto_unregister_shash(&sha3_224);
  236. crypto_unregister_shash(&sha3_256);
  237. crypto_unregister_shash(&sha3_384);
  238. crypto_unregister_shash(&sha3_512);
  239. }
  240. module_init(sha3_generic_mod_init);
  241. module_exit(sha3_generic_mod_fini);
  242. MODULE_LICENSE("GPL");
  243. MODULE_DESCRIPTION("SHA-3 Secure Hash Algorithm");
  244. MODULE_ALIAS_CRYPTO("sha3-224");
  245. MODULE_ALIAS_CRYPTO("sha3-224-generic");
  246. MODULE_ALIAS_CRYPTO("sha3-256");
  247. MODULE_ALIAS_CRYPTO("sha3-256-generic");
  248. MODULE_ALIAS_CRYPTO("sha3-384");
  249. MODULE_ALIAS_CRYPTO("sha3-384-generic");
  250. MODULE_ALIAS_CRYPTO("sha3-512");
  251. MODULE_ALIAS_CRYPTO("sha3-512-generic");