salsa20_generic.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Salsa20: Salsa20 stream cipher algorithm
  3. *
  4. * Copyright (c) 2007 Tan Swee Heng <thesweeheng@gmail.com>
  5. *
  6. * Derived from:
  7. * - salsa20.c: Public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
  8. *
  9. * Salsa20 is a stream cipher candidate in eSTREAM, the ECRYPT Stream
  10. * Cipher Project. It is designed by Daniel J. Bernstein <djb@cr.yp.to>.
  11. * More information about eSTREAM and Salsa20 can be found here:
  12. * http://www.ecrypt.eu.org/stream/
  13. * http://cr.yp.to/snuffle.html
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 of the License, or (at your option)
  18. * any later version.
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/crypto.h>
  25. #include <linux/types.h>
  26. #include <linux/bitops.h>
  27. #include <crypto/algapi.h>
  28. #include <asm/byteorder.h>
  29. #define SALSA20_IV_SIZE 8U
  30. #define SALSA20_MIN_KEY_SIZE 16U
  31. #define SALSA20_MAX_KEY_SIZE 32U
  32. /*
  33. * Start of code taken from D. J. Bernstein's reference implementation.
  34. * With some modifications and optimizations made to suit our needs.
  35. */
  36. /*
  37. salsa20-ref.c version 20051118
  38. D. J. Bernstein
  39. Public domain.
  40. */
  41. #define U32TO8_LITTLE(p, v) \
  42. { (p)[0] = (v >> 0) & 0xff; (p)[1] = (v >> 8) & 0xff; \
  43. (p)[2] = (v >> 16) & 0xff; (p)[3] = (v >> 24) & 0xff; }
  44. #define U8TO32_LITTLE(p) \
  45. (((u32)((p)[0]) ) | ((u32)((p)[1]) << 8) | \
  46. ((u32)((p)[2]) << 16) | ((u32)((p)[3]) << 24) )
  47. struct salsa20_ctx
  48. {
  49. u32 input[16];
  50. };
  51. static void salsa20_wordtobyte(u8 output[64], const u32 input[16])
  52. {
  53. u32 x[16];
  54. int i;
  55. memcpy(x, input, sizeof(x));
  56. for (i = 20; i > 0; i -= 2) {
  57. x[ 4] ^= rol32((x[ 0] + x[12]), 7);
  58. x[ 8] ^= rol32((x[ 4] + x[ 0]), 9);
  59. x[12] ^= rol32((x[ 8] + x[ 4]), 13);
  60. x[ 0] ^= rol32((x[12] + x[ 8]), 18);
  61. x[ 9] ^= rol32((x[ 5] + x[ 1]), 7);
  62. x[13] ^= rol32((x[ 9] + x[ 5]), 9);
  63. x[ 1] ^= rol32((x[13] + x[ 9]), 13);
  64. x[ 5] ^= rol32((x[ 1] + x[13]), 18);
  65. x[14] ^= rol32((x[10] + x[ 6]), 7);
  66. x[ 2] ^= rol32((x[14] + x[10]), 9);
  67. x[ 6] ^= rol32((x[ 2] + x[14]), 13);
  68. x[10] ^= rol32((x[ 6] + x[ 2]), 18);
  69. x[ 3] ^= rol32((x[15] + x[11]), 7);
  70. x[ 7] ^= rol32((x[ 3] + x[15]), 9);
  71. x[11] ^= rol32((x[ 7] + x[ 3]), 13);
  72. x[15] ^= rol32((x[11] + x[ 7]), 18);
  73. x[ 1] ^= rol32((x[ 0] + x[ 3]), 7);
  74. x[ 2] ^= rol32((x[ 1] + x[ 0]), 9);
  75. x[ 3] ^= rol32((x[ 2] + x[ 1]), 13);
  76. x[ 0] ^= rol32((x[ 3] + x[ 2]), 18);
  77. x[ 6] ^= rol32((x[ 5] + x[ 4]), 7);
  78. x[ 7] ^= rol32((x[ 6] + x[ 5]), 9);
  79. x[ 4] ^= rol32((x[ 7] + x[ 6]), 13);
  80. x[ 5] ^= rol32((x[ 4] + x[ 7]), 18);
  81. x[11] ^= rol32((x[10] + x[ 9]), 7);
  82. x[ 8] ^= rol32((x[11] + x[10]), 9);
  83. x[ 9] ^= rol32((x[ 8] + x[11]), 13);
  84. x[10] ^= rol32((x[ 9] + x[ 8]), 18);
  85. x[12] ^= rol32((x[15] + x[14]), 7);
  86. x[13] ^= rol32((x[12] + x[15]), 9);
  87. x[14] ^= rol32((x[13] + x[12]), 13);
  88. x[15] ^= rol32((x[14] + x[13]), 18);
  89. }
  90. for (i = 0; i < 16; ++i)
  91. x[i] += input[i];
  92. for (i = 0; i < 16; ++i)
  93. U32TO8_LITTLE(output + 4 * i,x[i]);
  94. }
  95. static const char sigma[16] = "expand 32-byte k";
  96. static const char tau[16] = "expand 16-byte k";
  97. static void salsa20_keysetup(struct salsa20_ctx *ctx, const u8 *k, u32 kbytes)
  98. {
  99. const char *constants;
  100. ctx->input[1] = U8TO32_LITTLE(k + 0);
  101. ctx->input[2] = U8TO32_LITTLE(k + 4);
  102. ctx->input[3] = U8TO32_LITTLE(k + 8);
  103. ctx->input[4] = U8TO32_LITTLE(k + 12);
  104. if (kbytes == 32) { /* recommended */
  105. k += 16;
  106. constants = sigma;
  107. } else { /* kbytes == 16 */
  108. constants = tau;
  109. }
  110. ctx->input[11] = U8TO32_LITTLE(k + 0);
  111. ctx->input[12] = U8TO32_LITTLE(k + 4);
  112. ctx->input[13] = U8TO32_LITTLE(k + 8);
  113. ctx->input[14] = U8TO32_LITTLE(k + 12);
  114. ctx->input[0] = U8TO32_LITTLE(constants + 0);
  115. ctx->input[5] = U8TO32_LITTLE(constants + 4);
  116. ctx->input[10] = U8TO32_LITTLE(constants + 8);
  117. ctx->input[15] = U8TO32_LITTLE(constants + 12);
  118. }
  119. static void salsa20_ivsetup(struct salsa20_ctx *ctx, const u8 *iv)
  120. {
  121. ctx->input[6] = U8TO32_LITTLE(iv + 0);
  122. ctx->input[7] = U8TO32_LITTLE(iv + 4);
  123. ctx->input[8] = 0;
  124. ctx->input[9] = 0;
  125. }
  126. static void salsa20_encrypt_bytes(struct salsa20_ctx *ctx, u8 *dst,
  127. const u8 *src, unsigned int bytes)
  128. {
  129. u8 buf[64];
  130. if (dst != src)
  131. memcpy(dst, src, bytes);
  132. while (bytes) {
  133. salsa20_wordtobyte(buf, ctx->input);
  134. ctx->input[8]++;
  135. if (!ctx->input[8])
  136. ctx->input[9]++;
  137. if (bytes <= 64) {
  138. crypto_xor(dst, buf, bytes);
  139. return;
  140. }
  141. crypto_xor(dst, buf, 64);
  142. bytes -= 64;
  143. dst += 64;
  144. }
  145. }
  146. /*
  147. * End of code taken from D. J. Bernstein's reference implementation.
  148. */
  149. static int setkey(struct crypto_tfm *tfm, const u8 *key,
  150. unsigned int keysize)
  151. {
  152. struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
  153. salsa20_keysetup(ctx, key, keysize);
  154. return 0;
  155. }
  156. static int encrypt(struct blkcipher_desc *desc,
  157. struct scatterlist *dst, struct scatterlist *src,
  158. unsigned int nbytes)
  159. {
  160. struct blkcipher_walk walk;
  161. struct crypto_blkcipher *tfm = desc->tfm;
  162. struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
  163. int err;
  164. blkcipher_walk_init(&walk, dst, src, nbytes);
  165. err = blkcipher_walk_virt_block(desc, &walk, 64);
  166. salsa20_ivsetup(ctx, walk.iv);
  167. while (walk.nbytes >= 64) {
  168. salsa20_encrypt_bytes(ctx, walk.dst.virt.addr,
  169. walk.src.virt.addr,
  170. walk.nbytes - (walk.nbytes % 64));
  171. err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
  172. }
  173. if (walk.nbytes) {
  174. salsa20_encrypt_bytes(ctx, walk.dst.virt.addr,
  175. walk.src.virt.addr, walk.nbytes);
  176. err = blkcipher_walk_done(desc, &walk, 0);
  177. }
  178. return err;
  179. }
  180. static struct crypto_alg alg = {
  181. .cra_name = "salsa20",
  182. .cra_driver_name = "salsa20-generic",
  183. .cra_priority = 100,
  184. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  185. .cra_type = &crypto_blkcipher_type,
  186. .cra_blocksize = 1,
  187. .cra_ctxsize = sizeof(struct salsa20_ctx),
  188. .cra_alignmask = 3,
  189. .cra_module = THIS_MODULE,
  190. .cra_u = {
  191. .blkcipher = {
  192. .setkey = setkey,
  193. .encrypt = encrypt,
  194. .decrypt = encrypt,
  195. .min_keysize = SALSA20_MIN_KEY_SIZE,
  196. .max_keysize = SALSA20_MAX_KEY_SIZE,
  197. .ivsize = SALSA20_IV_SIZE,
  198. }
  199. }
  200. };
  201. static int __init salsa20_generic_mod_init(void)
  202. {
  203. return crypto_register_alg(&alg);
  204. }
  205. static void __exit salsa20_generic_mod_fini(void)
  206. {
  207. crypto_unregister_alg(&alg);
  208. }
  209. module_init(salsa20_generic_mod_init);
  210. module_exit(salsa20_generic_mod_fini);
  211. MODULE_LICENSE("GPL");
  212. MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm");
  213. MODULE_ALIAS_CRYPTO("salsa20");
  214. MODULE_ALIAS_CRYPTO("salsa20-generic");