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