sha3_generic.c 7.1 KB

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