chacha20_generic.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <linux/crypto.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <crypto/chacha20.h>
  16. static inline u32 rotl32(u32 v, u8 n)
  17. {
  18. return (v << n) | (v >> (sizeof(v) * 8 - n));
  19. }
  20. static inline u32 le32_to_cpuvp(const void *p)
  21. {
  22. return le32_to_cpup(p);
  23. }
  24. static void chacha20_block(u32 *state, void *stream)
  25. {
  26. u32 x[16], *out = stream;
  27. int i;
  28. for (i = 0; i < ARRAY_SIZE(x); i++)
  29. x[i] = state[i];
  30. for (i = 0; i < 20; i += 2) {
  31. x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 16);
  32. x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 16);
  33. x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 16);
  34. x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 16);
  35. x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 12);
  36. x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 12);
  37. x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 12);
  38. x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 12);
  39. x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 8);
  40. x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 8);
  41. x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 8);
  42. x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 8);
  43. x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 7);
  44. x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 7);
  45. x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 7);
  46. x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 7);
  47. x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 16);
  48. x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 16);
  49. x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 16);
  50. x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 16);
  51. x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 12);
  52. x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 12);
  53. x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 12);
  54. x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 12);
  55. x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 8);
  56. x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 8);
  57. x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 8);
  58. x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 8);
  59. x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 7);
  60. x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 7);
  61. x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 7);
  62. x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 7);
  63. }
  64. for (i = 0; i < ARRAY_SIZE(x); i++)
  65. out[i] = cpu_to_le32(x[i] + state[i]);
  66. state[12]++;
  67. }
  68. static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
  69. unsigned int bytes)
  70. {
  71. u8 stream[CHACHA20_BLOCK_SIZE];
  72. if (dst != src)
  73. memcpy(dst, src, bytes);
  74. while (bytes >= CHACHA20_BLOCK_SIZE) {
  75. chacha20_block(state, stream);
  76. crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE);
  77. bytes -= CHACHA20_BLOCK_SIZE;
  78. dst += CHACHA20_BLOCK_SIZE;
  79. }
  80. if (bytes) {
  81. chacha20_block(state, stream);
  82. crypto_xor(dst, stream, bytes);
  83. }
  84. }
  85. void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
  86. {
  87. static const char constant[16] = "expand 32-byte k";
  88. state[0] = le32_to_cpuvp(constant + 0);
  89. state[1] = le32_to_cpuvp(constant + 4);
  90. state[2] = le32_to_cpuvp(constant + 8);
  91. state[3] = le32_to_cpuvp(constant + 12);
  92. state[4] = ctx->key[0];
  93. state[5] = ctx->key[1];
  94. state[6] = ctx->key[2];
  95. state[7] = ctx->key[3];
  96. state[8] = ctx->key[4];
  97. state[9] = ctx->key[5];
  98. state[10] = ctx->key[6];
  99. state[11] = ctx->key[7];
  100. state[12] = le32_to_cpuvp(iv + 0);
  101. state[13] = le32_to_cpuvp(iv + 4);
  102. state[14] = le32_to_cpuvp(iv + 8);
  103. state[15] = le32_to_cpuvp(iv + 12);
  104. }
  105. EXPORT_SYMBOL_GPL(crypto_chacha20_init);
  106. int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
  107. unsigned int keysize)
  108. {
  109. struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
  110. int i;
  111. if (keysize != CHACHA20_KEY_SIZE)
  112. return -EINVAL;
  113. for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
  114. ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32));
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
  118. int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  119. struct scatterlist *src, unsigned int nbytes)
  120. {
  121. struct blkcipher_walk walk;
  122. u32 state[16];
  123. int err;
  124. blkcipher_walk_init(&walk, dst, src, nbytes);
  125. err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
  126. crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
  127. while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
  128. chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
  129. rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
  130. err = blkcipher_walk_done(desc, &walk,
  131. walk.nbytes % CHACHA20_BLOCK_SIZE);
  132. }
  133. if (walk.nbytes) {
  134. chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
  135. walk.nbytes);
  136. err = blkcipher_walk_done(desc, &walk, 0);
  137. }
  138. return err;
  139. }
  140. EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
  141. static struct crypto_alg alg = {
  142. .cra_name = "chacha20",
  143. .cra_driver_name = "chacha20-generic",
  144. .cra_priority = 100,
  145. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  146. .cra_blocksize = 1,
  147. .cra_type = &crypto_blkcipher_type,
  148. .cra_ctxsize = sizeof(struct chacha20_ctx),
  149. .cra_alignmask = sizeof(u32) - 1,
  150. .cra_module = THIS_MODULE,
  151. .cra_u = {
  152. .blkcipher = {
  153. .min_keysize = CHACHA20_KEY_SIZE,
  154. .max_keysize = CHACHA20_KEY_SIZE,
  155. .ivsize = CHACHA20_IV_SIZE,
  156. .geniv = "seqiv",
  157. .setkey = crypto_chacha20_setkey,
  158. .encrypt = crypto_chacha20_crypt,
  159. .decrypt = crypto_chacha20_crypt,
  160. },
  161. },
  162. };
  163. static int __init chacha20_generic_mod_init(void)
  164. {
  165. return crypto_register_alg(&alg);
  166. }
  167. static void __exit chacha20_generic_mod_fini(void)
  168. {
  169. crypto_unregister_alg(&alg);
  170. }
  171. module_init(chacha20_generic_mod_init);
  172. module_exit(chacha20_generic_mod_fini);
  173. MODULE_LICENSE("GPL");
  174. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  175. MODULE_DESCRIPTION("chacha20 cipher algorithm");
  176. MODULE_ALIAS_CRYPTO("chacha20");
  177. MODULE_ALIAS_CRYPTO("chacha20-generic");