chacha_generic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * ChaCha and XChaCha stream ciphers, including ChaCha20 (RFC7539)
  3. *
  4. * Copyright (C) 2015 Martin Willi
  5. * Copyright (C) 2018 Google LLC
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <asm/unaligned.h>
  13. #include <crypto/algapi.h>
  14. #include <linux/crypto.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <crypto/chacha.h>
  18. static void chacha_docrypt(u32 *state, u8 *dst, const u8 *src,
  19. unsigned int bytes, int nrounds)
  20. {
  21. /* aligned to potentially speed up crypto_xor() */
  22. u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
  23. if (dst != src)
  24. memcpy(dst, src, bytes);
  25. while (bytes >= CHACHA_BLOCK_SIZE) {
  26. chacha_block(state, stream, nrounds);
  27. crypto_xor(dst, stream, CHACHA_BLOCK_SIZE);
  28. bytes -= CHACHA_BLOCK_SIZE;
  29. dst += CHACHA_BLOCK_SIZE;
  30. }
  31. if (bytes) {
  32. chacha_block(state, stream, nrounds);
  33. crypto_xor(dst, stream, bytes);
  34. }
  35. }
  36. static int chacha_stream_xor(struct blkcipher_desc *desc, struct scatterlist *dst,
  37. struct scatterlist *src, unsigned int nbytes,
  38. struct chacha_ctx *ctx, u8 *iv)
  39. {
  40. struct blkcipher_walk walk;
  41. u32 state[16];
  42. int err;
  43. blkcipher_walk_init(&walk, dst, src, nbytes);
  44. err = blkcipher_walk_virt_block(desc, &walk, CHACHA_BLOCK_SIZE);
  45. crypto_chacha_init(state, ctx, iv);
  46. while (walk.nbytes >= CHACHA_BLOCK_SIZE) {
  47. chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
  48. rounddown(walk.nbytes, CHACHA_BLOCK_SIZE),
  49. ctx->nrounds);
  50. err = blkcipher_walk_done(desc, &walk,
  51. walk.nbytes % CHACHA_BLOCK_SIZE);
  52. }
  53. if (walk.nbytes) {
  54. chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
  55. walk.nbytes, ctx->nrounds);
  56. err = blkcipher_walk_done(desc, &walk, 0);
  57. }
  58. return err;
  59. }
  60. void crypto_chacha_init(u32 *state, struct chacha_ctx *ctx, u8 *iv)
  61. {
  62. state[0] = 0x61707865; /* "expa" */
  63. state[1] = 0x3320646e; /* "nd 3" */
  64. state[2] = 0x79622d32; /* "2-by" */
  65. state[3] = 0x6b206574; /* "te k" */
  66. state[4] = ctx->key[0];
  67. state[5] = ctx->key[1];
  68. state[6] = ctx->key[2];
  69. state[7] = ctx->key[3];
  70. state[8] = ctx->key[4];
  71. state[9] = ctx->key[5];
  72. state[10] = ctx->key[6];
  73. state[11] = ctx->key[7];
  74. state[12] = get_unaligned_le32(iv + 0);
  75. state[13] = get_unaligned_le32(iv + 4);
  76. state[14] = get_unaligned_le32(iv + 8);
  77. state[15] = get_unaligned_le32(iv + 12);
  78. }
  79. EXPORT_SYMBOL_GPL(crypto_chacha_init);
  80. static int chacha_setkey(struct crypto_tfm *tfm, const u8 *key,
  81. unsigned int keysize, int nrounds)
  82. {
  83. struct chacha_ctx *ctx = crypto_tfm_ctx(tfm);
  84. int i;
  85. if (keysize != CHACHA_KEY_SIZE)
  86. return -EINVAL;
  87. for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
  88. ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
  89. ctx->nrounds = nrounds;
  90. return 0;
  91. }
  92. int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
  93. unsigned int keysize)
  94. {
  95. return chacha_setkey(tfm, key, keysize, 20);
  96. }
  97. EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
  98. int crypto_chacha12_setkey(struct crypto_tfm *tfm, const u8 *key,
  99. unsigned int keysize)
  100. {
  101. return chacha_setkey(tfm, key, keysize, 12);
  102. }
  103. EXPORT_SYMBOL_GPL(crypto_chacha12_setkey);
  104. int crypto_chacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  105. struct scatterlist *src, unsigned int nbytes)
  106. {
  107. struct chacha_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  108. u8 *iv = desc->info;
  109. return chacha_stream_xor(desc, dst, src, nbytes, ctx, iv);
  110. }
  111. EXPORT_SYMBOL_GPL(crypto_chacha_crypt);
  112. int crypto_xchacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  113. struct scatterlist *src, unsigned int nbytes)
  114. {
  115. struct chacha_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  116. u8 *iv = desc->info;
  117. struct chacha_ctx subctx;
  118. u32 state[16];
  119. u8 real_iv[16];
  120. /* Compute the subkey given the original key and first 128 nonce bits */
  121. crypto_chacha_init(state, ctx, iv);
  122. hchacha_block(state, subctx.key, ctx->nrounds);
  123. subctx.nrounds = ctx->nrounds;
  124. /* Build the real IV */
  125. memcpy(&real_iv[0], iv + 24, 8); /* stream position */
  126. memcpy(&real_iv[8], iv + 16, 8); /* remaining 64 nonce bits */
  127. /* Generate the stream and XOR it with the data */
  128. return chacha_stream_xor(desc, dst, src, nbytes, &subctx, real_iv);
  129. }
  130. EXPORT_SYMBOL_GPL(crypto_xchacha_crypt);
  131. static struct crypto_alg algs[] = {
  132. {
  133. .cra_name = "chacha20",
  134. .cra_driver_name = "chacha20-generic",
  135. .cra_priority = 100,
  136. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  137. .cra_blocksize = 1,
  138. .cra_type = &crypto_blkcipher_type,
  139. .cra_ctxsize = sizeof(struct chacha_ctx),
  140. .cra_alignmask = sizeof(u32) - 1,
  141. .cra_module = THIS_MODULE,
  142. .cra_u = {
  143. .blkcipher = {
  144. .min_keysize = CHACHA_KEY_SIZE,
  145. .max_keysize = CHACHA_KEY_SIZE,
  146. .ivsize = CHACHA_IV_SIZE,
  147. .geniv = "seqiv",
  148. .setkey = crypto_chacha20_setkey,
  149. .encrypt = crypto_chacha_crypt,
  150. .decrypt = crypto_chacha_crypt,
  151. },
  152. },
  153. }, {
  154. .cra_name = "xchacha20",
  155. .cra_driver_name = "xchacha20-generic",
  156. .cra_priority = 100,
  157. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  158. .cra_blocksize = 1,
  159. .cra_type = &crypto_blkcipher_type,
  160. .cra_ctxsize = sizeof(struct chacha_ctx),
  161. .cra_alignmask = sizeof(u32) - 1,
  162. .cra_module = THIS_MODULE,
  163. .cra_u = {
  164. .blkcipher = {
  165. .min_keysize = CHACHA_KEY_SIZE,
  166. .max_keysize = CHACHA_KEY_SIZE,
  167. .ivsize = XCHACHA_IV_SIZE,
  168. .geniv = "seqiv",
  169. .setkey = crypto_chacha20_setkey,
  170. .encrypt = crypto_xchacha_crypt,
  171. .decrypt = crypto_xchacha_crypt,
  172. },
  173. },
  174. }, {
  175. .cra_name = "xchacha12",
  176. .cra_driver_name = "xchacha12-generic",
  177. .cra_priority = 100,
  178. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  179. .cra_blocksize = 1,
  180. .cra_type = &crypto_blkcipher_type,
  181. .cra_ctxsize = sizeof(struct chacha_ctx),
  182. .cra_alignmask = sizeof(u32) - 1,
  183. .cra_module = THIS_MODULE,
  184. .cra_u = {
  185. .blkcipher = {
  186. .min_keysize = CHACHA_KEY_SIZE,
  187. .max_keysize = CHACHA_KEY_SIZE,
  188. .ivsize = XCHACHA_IV_SIZE,
  189. .geniv = "seqiv",
  190. .setkey = crypto_chacha12_setkey,
  191. .encrypt = crypto_xchacha_crypt,
  192. .decrypt = crypto_xchacha_crypt,
  193. },
  194. },
  195. },
  196. };
  197. static int __init chacha_generic_mod_init(void)
  198. {
  199. return crypto_register_algs(algs, ARRAY_SIZE(algs));
  200. }
  201. static void __exit chacha_generic_mod_fini(void)
  202. {
  203. crypto_unregister_algs(algs, ARRAY_SIZE(algs));
  204. }
  205. module_init(chacha_generic_mod_init);
  206. module_exit(chacha_generic_mod_fini);
  207. MODULE_LICENSE("GPL");
  208. MODULE_AUTHOR("Martin Willi <[email protected]>");
  209. MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)");
  210. MODULE_ALIAS_CRYPTO("chacha20");
  211. MODULE_ALIAS_CRYPTO("chacha20-generic");
  212. MODULE_ALIAS_CRYPTO("xchacha20");
  213. MODULE_ALIAS_CRYPTO("xchacha20-generic");
  214. MODULE_ALIAS_CRYPTO("xchacha12");
  215. MODULE_ALIAS_CRYPTO("xchacha12-generic");