chacha20_generic.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <asm/unaligned.h>
  12. #include <crypto/algapi.h>
  13. #include <crypto/chacha20.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <linux/module.h>
  16. static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
  17. unsigned int bytes)
  18. {
  19. u32 stream[CHACHA20_BLOCK_WORDS];
  20. if (dst != src)
  21. memcpy(dst, src, bytes);
  22. while (bytes >= CHACHA20_BLOCK_SIZE) {
  23. chacha20_block(state, stream);
  24. crypto_xor(dst, (const u8 *)stream, CHACHA20_BLOCK_SIZE);
  25. bytes -= CHACHA20_BLOCK_SIZE;
  26. dst += CHACHA20_BLOCK_SIZE;
  27. }
  28. if (bytes) {
  29. chacha20_block(state, stream);
  30. crypto_xor(dst, (const u8 *)stream, bytes);
  31. }
  32. }
  33. void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
  34. {
  35. state[0] = 0x61707865; /* "expa" */
  36. state[1] = 0x3320646e; /* "nd 3" */
  37. state[2] = 0x79622d32; /* "2-by" */
  38. state[3] = 0x6b206574; /* "te k" */
  39. state[4] = ctx->key[0];
  40. state[5] = ctx->key[1];
  41. state[6] = ctx->key[2];
  42. state[7] = ctx->key[3];
  43. state[8] = ctx->key[4];
  44. state[9] = ctx->key[5];
  45. state[10] = ctx->key[6];
  46. state[11] = ctx->key[7];
  47. state[12] = get_unaligned_le32(iv + 0);
  48. state[13] = get_unaligned_le32(iv + 4);
  49. state[14] = get_unaligned_le32(iv + 8);
  50. state[15] = get_unaligned_le32(iv + 12);
  51. }
  52. EXPORT_SYMBOL_GPL(crypto_chacha20_init);
  53. int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
  54. unsigned int keysize)
  55. {
  56. struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
  57. int i;
  58. if (keysize != CHACHA20_KEY_SIZE)
  59. return -EINVAL;
  60. for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
  61. ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
  65. int crypto_chacha20_crypt(struct skcipher_request *req)
  66. {
  67. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  68. struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
  69. struct skcipher_walk walk;
  70. u32 state[16];
  71. int err;
  72. err = skcipher_walk_virt(&walk, req, true);
  73. crypto_chacha20_init(state, ctx, walk.iv);
  74. while (walk.nbytes > 0) {
  75. unsigned int nbytes = walk.nbytes;
  76. if (nbytes < walk.total)
  77. nbytes = round_down(nbytes, walk.stride);
  78. chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
  79. nbytes);
  80. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  81. }
  82. return err;
  83. }
  84. EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
  85. static struct skcipher_alg alg = {
  86. .base.cra_name = "chacha20",
  87. .base.cra_driver_name = "chacha20-generic",
  88. .base.cra_priority = 100,
  89. .base.cra_blocksize = 1,
  90. .base.cra_ctxsize = sizeof(struct chacha20_ctx),
  91. .base.cra_module = THIS_MODULE,
  92. .min_keysize = CHACHA20_KEY_SIZE,
  93. .max_keysize = CHACHA20_KEY_SIZE,
  94. .ivsize = CHACHA20_IV_SIZE,
  95. .chunksize = CHACHA20_BLOCK_SIZE,
  96. .setkey = crypto_chacha20_setkey,
  97. .encrypt = crypto_chacha20_crypt,
  98. .decrypt = crypto_chacha20_crypt,
  99. };
  100. static int __init chacha20_generic_mod_init(void)
  101. {
  102. return crypto_register_skcipher(&alg);
  103. }
  104. static void __exit chacha20_generic_mod_fini(void)
  105. {
  106. crypto_unregister_skcipher(&alg);
  107. }
  108. module_init(chacha20_generic_mod_init);
  109. module_exit(chacha20_generic_mod_fini);
  110. MODULE_LICENSE("GPL");
  111. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  112. MODULE_DESCRIPTION("chacha20 cipher algorithm");
  113. MODULE_ALIAS_CRYPTO("chacha20");
  114. MODULE_ALIAS_CRYPTO("chacha20-generic");