chacha20_generic.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * ChaCha20 256-bit cipher algorithm, RFC7539
  3. *
  4. * Copyright (C) 2015 Martin Willi
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <crypto/algapi.h>
  12. #include <crypto/chacha20.h>
  13. #include <crypto/internal/skcipher.h>
  14. #include <linux/module.h>
  15. static inline u32 le32_to_cpuvp(const void *p)
  16. {
  17. return le32_to_cpup(p);
  18. }
  19. static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
  20. unsigned int bytes)
  21. {
  22. u8 stream[CHACHA20_BLOCK_SIZE];
  23. if (dst != src)
  24. memcpy(dst, src, bytes);
  25. while (bytes >= CHACHA20_BLOCK_SIZE) {
  26. chacha20_block(state, stream);
  27. crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE);
  28. bytes -= CHACHA20_BLOCK_SIZE;
  29. dst += CHACHA20_BLOCK_SIZE;
  30. }
  31. if (bytes) {
  32. chacha20_block(state, stream);
  33. crypto_xor(dst, stream, bytes);
  34. }
  35. }
  36. void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
  37. {
  38. static const char constant[16] = "expand 32-byte k";
  39. state[0] = le32_to_cpuvp(constant + 0);
  40. state[1] = le32_to_cpuvp(constant + 4);
  41. state[2] = le32_to_cpuvp(constant + 8);
  42. state[3] = le32_to_cpuvp(constant + 12);
  43. state[4] = ctx->key[0];
  44. state[5] = ctx->key[1];
  45. state[6] = ctx->key[2];
  46. state[7] = ctx->key[3];
  47. state[8] = ctx->key[4];
  48. state[9] = ctx->key[5];
  49. state[10] = ctx->key[6];
  50. state[11] = ctx->key[7];
  51. state[12] = le32_to_cpuvp(iv + 0);
  52. state[13] = le32_to_cpuvp(iv + 4);
  53. state[14] = le32_to_cpuvp(iv + 8);
  54. state[15] = le32_to_cpuvp(iv + 12);
  55. }
  56. EXPORT_SYMBOL_GPL(crypto_chacha20_init);
  57. int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
  58. unsigned int keysize)
  59. {
  60. struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
  61. int i;
  62. if (keysize != CHACHA20_KEY_SIZE)
  63. return -EINVAL;
  64. for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
  65. ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32));
  66. return 0;
  67. }
  68. EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
  69. int crypto_chacha20_crypt(struct skcipher_request *req)
  70. {
  71. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  72. struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
  73. struct skcipher_walk walk;
  74. u32 state[16];
  75. int err;
  76. err = skcipher_walk_virt(&walk, req, true);
  77. crypto_chacha20_init(state, ctx, walk.iv);
  78. while (walk.nbytes > 0) {
  79. chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
  80. walk.nbytes);
  81. err = skcipher_walk_done(&walk, 0);
  82. }
  83. return err;
  84. }
  85. EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
  86. static struct skcipher_alg alg = {
  87. .base.cra_name = "chacha20",
  88. .base.cra_driver_name = "chacha20-generic",
  89. .base.cra_priority = 100,
  90. .base.cra_blocksize = 1,
  91. .base.cra_ctxsize = sizeof(struct chacha20_ctx),
  92. .base.cra_alignmask = sizeof(u32) - 1,
  93. .base.cra_module = THIS_MODULE,
  94. .min_keysize = CHACHA20_KEY_SIZE,
  95. .max_keysize = CHACHA20_KEY_SIZE,
  96. .ivsize = CHACHA20_IV_SIZE,
  97. .chunksize = CHACHA20_BLOCK_SIZE,
  98. .setkey = crypto_chacha20_setkey,
  99. .encrypt = crypto_chacha20_crypt,
  100. .decrypt = crypto_chacha20_crypt,
  101. };
  102. static int __init chacha20_generic_mod_init(void)
  103. {
  104. return crypto_register_skcipher(&alg);
  105. }
  106. static void __exit chacha20_generic_mod_fini(void)
  107. {
  108. crypto_unregister_skcipher(&alg);
  109. }
  110. module_init(chacha20_generic_mod_init);
  111. module_exit(chacha20_generic_mod_fini);
  112. MODULE_LICENSE("GPL");
  113. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  114. MODULE_DESCRIPTION("chacha20 cipher algorithm");
  115. MODULE_ALIAS_CRYPTO("chacha20");
  116. MODULE_ALIAS_CRYPTO("chacha20-generic");