chacha.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto API wrappers for the ChaCha20, XChaCha20, and XChaCha12 stream ciphers
  4. *
  5. * Copyright (C) 2015 Martin Willi
  6. * Copyright (C) 2018 Google LLC
  7. */
  8. #include <linux/unaligned.h>
  9. #include <crypto/algapi.h>
  10. #include <crypto/chacha.h>
  11. #include <crypto/internal/skcipher.h>
  12. #include <linux/module.h>
  13. struct chacha_ctx {
  14. u32 key[8];
  15. int nrounds;
  16. };
  17. static int chacha_setkey(struct crypto_skcipher *tfm,
  18. const u8 *key, unsigned int keysize, int nrounds)
  19. {
  20. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  21. int i;
  22. if (keysize != CHACHA_KEY_SIZE)
  23. return -EINVAL;
  24. for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
  25. ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
  26. ctx->nrounds = nrounds;
  27. return 0;
  28. }
  29. static int chacha20_setkey(struct crypto_skcipher *tfm,
  30. const u8 *key, unsigned int keysize)
  31. {
  32. return chacha_setkey(tfm, key, keysize, 20);
  33. }
  34. static int chacha12_setkey(struct crypto_skcipher *tfm,
  35. const u8 *key, unsigned int keysize)
  36. {
  37. return chacha_setkey(tfm, key, keysize, 12);
  38. }
  39. static int chacha_stream_xor(struct skcipher_request *req,
  40. const struct chacha_ctx *ctx,
  41. const u8 iv[CHACHA_IV_SIZE])
  42. {
  43. struct skcipher_walk walk;
  44. struct chacha_state state;
  45. int err;
  46. err = skcipher_walk_virt(&walk, req, false);
  47. chacha_init(&state, ctx->key, iv);
  48. while (walk.nbytes > 0) {
  49. unsigned int nbytes = walk.nbytes;
  50. if (nbytes < walk.total)
  51. nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE);
  52. chacha_crypt(&state, walk.dst.virt.addr, walk.src.virt.addr,
  53. nbytes, ctx->nrounds);
  54. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  55. }
  56. return err;
  57. }
  58. static int crypto_chacha_crypt(struct skcipher_request *req)
  59. {
  60. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  61. const struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  62. return chacha_stream_xor(req, ctx, req->iv);
  63. }
  64. static int crypto_xchacha_crypt(struct skcipher_request *req)
  65. {
  66. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  67. const struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  68. struct chacha_ctx subctx;
  69. struct chacha_state state;
  70. u8 real_iv[16];
  71. /* Compute the subkey given the original key and first 128 nonce bits */
  72. chacha_init(&state, ctx->key, req->iv);
  73. hchacha_block(&state, subctx.key, ctx->nrounds);
  74. subctx.nrounds = ctx->nrounds;
  75. /* Build the real IV */
  76. memcpy(&real_iv[0], req->iv + 24, 8); /* stream position */
  77. memcpy(&real_iv[8], req->iv + 16, 8); /* remaining 64 nonce bits */
  78. /* Generate the stream and XOR it with the data */
  79. return chacha_stream_xor(req, &subctx, real_iv);
  80. }
  81. static struct skcipher_alg algs[] = {
  82. {
  83. .base.cra_name = "chacha20",
  84. .base.cra_driver_name = "chacha20-lib",
  85. .base.cra_priority = 300,
  86. .base.cra_blocksize = 1,
  87. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  88. .base.cra_module = THIS_MODULE,
  89. .min_keysize = CHACHA_KEY_SIZE,
  90. .max_keysize = CHACHA_KEY_SIZE,
  91. .ivsize = CHACHA_IV_SIZE,
  92. .chunksize = CHACHA_BLOCK_SIZE,
  93. .setkey = chacha20_setkey,
  94. .encrypt = crypto_chacha_crypt,
  95. .decrypt = crypto_chacha_crypt,
  96. },
  97. {
  98. .base.cra_name = "xchacha20",
  99. .base.cra_driver_name = "xchacha20-lib",
  100. .base.cra_priority = 300,
  101. .base.cra_blocksize = 1,
  102. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  103. .base.cra_module = THIS_MODULE,
  104. .min_keysize = CHACHA_KEY_SIZE,
  105. .max_keysize = CHACHA_KEY_SIZE,
  106. .ivsize = XCHACHA_IV_SIZE,
  107. .chunksize = CHACHA_BLOCK_SIZE,
  108. .setkey = chacha20_setkey,
  109. .encrypt = crypto_xchacha_crypt,
  110. .decrypt = crypto_xchacha_crypt,
  111. },
  112. {
  113. .base.cra_name = "xchacha12",
  114. .base.cra_driver_name = "xchacha12-lib",
  115. .base.cra_priority = 300,
  116. .base.cra_blocksize = 1,
  117. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  118. .base.cra_module = THIS_MODULE,
  119. .min_keysize = CHACHA_KEY_SIZE,
  120. .max_keysize = CHACHA_KEY_SIZE,
  121. .ivsize = XCHACHA_IV_SIZE,
  122. .chunksize = CHACHA_BLOCK_SIZE,
  123. .setkey = chacha12_setkey,
  124. .encrypt = crypto_xchacha_crypt,
  125. .decrypt = crypto_xchacha_crypt,
  126. }
  127. };
  128. static int __init crypto_chacha_mod_init(void)
  129. {
  130. return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
  131. }
  132. static void __exit crypto_chacha_mod_fini(void)
  133. {
  134. crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
  135. }
  136. module_init(crypto_chacha_mod_init);
  137. module_exit(crypto_chacha_mod_fini);
  138. MODULE_LICENSE("GPL");
  139. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  140. MODULE_DESCRIPTION("Crypto API wrappers for the ChaCha20, XChaCha20, and XChaCha12 stream ciphers");
  141. MODULE_ALIAS_CRYPTO("chacha20");
  142. MODULE_ALIAS_CRYPTO("chacha20-lib");
  143. MODULE_ALIAS_CRYPTO("xchacha20");
  144. MODULE_ALIAS_CRYPTO("xchacha20-lib");
  145. MODULE_ALIAS_CRYPTO("xchacha12");
  146. MODULE_ALIAS_CRYPTO("xchacha12-lib");