chacha20_generic.c 5.9 KB

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