salsa20_generic.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <crypto/algapi.h>
  27. #include <asm/byteorder.h>
  28. #define SALSA20_IV_SIZE 8U
  29. #define SALSA20_MIN_KEY_SIZE 16U
  30. #define SALSA20_MAX_KEY_SIZE 32U
  31. /*
  32. * Start of code taken from D. J. Bernstein's reference implementation.
  33. * With some modifications and optimizations made to suit our needs.
  34. */
  35. /*
  36. salsa20-ref.c version 20051118
  37. D. J. Bernstein
  38. Public domain.
  39. */
  40. #define ROTATE(v,n) (((v) << (n)) | ((v) >> (32 - (n))))
  41. #define XOR(v,w) ((v) ^ (w))
  42. #define PLUS(v,w) (((v) + (w)))
  43. #define PLUSONE(v) (PLUS((v),1))
  44. #define U32TO8_LITTLE(p, v) \
  45. { (p)[0] = (v >> 0) & 0xff; (p)[1] = (v >> 8) & 0xff; \
  46. (p)[2] = (v >> 16) & 0xff; (p)[3] = (v >> 24) & 0xff; }
  47. #define U8TO32_LITTLE(p) \
  48. (((u32)((p)[0]) ) | ((u32)((p)[1]) << 8) | \
  49. ((u32)((p)[2]) << 16) | ((u32)((p)[3]) << 24) )
  50. struct salsa20_ctx
  51. {
  52. u32 input[16];
  53. };
  54. static void salsa20_wordtobyte(u8 output[64], const u32 input[16])
  55. {
  56. u32 x[16];
  57. int i;
  58. memcpy(x, input, sizeof(x));
  59. for (i = 20; i > 0; i -= 2) {
  60. x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 0],x[12]), 7));
  61. x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[ 4],x[ 0]), 9));
  62. x[12] = XOR(x[12],ROTATE(PLUS(x[ 8],x[ 4]),13));
  63. x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[12],x[ 8]),18));
  64. x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 5],x[ 1]), 7));
  65. x[13] = XOR(x[13],ROTATE(PLUS(x[ 9],x[ 5]), 9));
  66. x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[13],x[ 9]),13));
  67. x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 1],x[13]),18));
  68. x[14] = XOR(x[14],ROTATE(PLUS(x[10],x[ 6]), 7));
  69. x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[14],x[10]), 9));
  70. x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 2],x[14]),13));
  71. x[10] = XOR(x[10],ROTATE(PLUS(x[ 6],x[ 2]),18));
  72. x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[15],x[11]), 7));
  73. x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 3],x[15]), 9));
  74. x[11] = XOR(x[11],ROTATE(PLUS(x[ 7],x[ 3]),13));
  75. x[15] = XOR(x[15],ROTATE(PLUS(x[11],x[ 7]),18));
  76. x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[ 0],x[ 3]), 7));
  77. x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[ 1],x[ 0]), 9));
  78. x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[ 2],x[ 1]),13));
  79. x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[ 3],x[ 2]),18));
  80. x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 5],x[ 4]), 7));
  81. x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 6],x[ 5]), 9));
  82. x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 7],x[ 6]),13));
  83. x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 4],x[ 7]),18));
  84. x[11] = XOR(x[11],ROTATE(PLUS(x[10],x[ 9]), 7));
  85. x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[11],x[10]), 9));
  86. x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 8],x[11]),13));
  87. x[10] = XOR(x[10],ROTATE(PLUS(x[ 9],x[ 8]),18));
  88. x[12] = XOR(x[12],ROTATE(PLUS(x[15],x[14]), 7));
  89. x[13] = XOR(x[13],ROTATE(PLUS(x[12],x[15]), 9));
  90. x[14] = XOR(x[14],ROTATE(PLUS(x[13],x[12]),13));
  91. x[15] = XOR(x[15],ROTATE(PLUS(x[14],x[13]),18));
  92. }
  93. for (i = 0; i < 16; ++i)
  94. x[i] = PLUS(x[i],input[i]);
  95. for (i = 0; i < 16; ++i)
  96. U32TO8_LITTLE(output + 4 * i,x[i]);
  97. }
  98. static const char sigma[16] = "expand 32-byte k";
  99. static const char tau[16] = "expand 16-byte k";
  100. static void salsa20_keysetup(struct salsa20_ctx *ctx, const u8 *k, u32 kbytes)
  101. {
  102. const char *constants;
  103. ctx->input[1] = U8TO32_LITTLE(k + 0);
  104. ctx->input[2] = U8TO32_LITTLE(k + 4);
  105. ctx->input[3] = U8TO32_LITTLE(k + 8);
  106. ctx->input[4] = U8TO32_LITTLE(k + 12);
  107. if (kbytes == 32) { /* recommended */
  108. k += 16;
  109. constants = sigma;
  110. } else { /* kbytes == 16 */
  111. constants = tau;
  112. }
  113. ctx->input[11] = U8TO32_LITTLE(k + 0);
  114. ctx->input[12] = U8TO32_LITTLE(k + 4);
  115. ctx->input[13] = U8TO32_LITTLE(k + 8);
  116. ctx->input[14] = U8TO32_LITTLE(k + 12);
  117. ctx->input[0] = U8TO32_LITTLE(constants + 0);
  118. ctx->input[5] = U8TO32_LITTLE(constants + 4);
  119. ctx->input[10] = U8TO32_LITTLE(constants + 8);
  120. ctx->input[15] = U8TO32_LITTLE(constants + 12);
  121. }
  122. static void salsa20_ivsetup(struct salsa20_ctx *ctx, const u8 *iv)
  123. {
  124. ctx->input[6] = U8TO32_LITTLE(iv + 0);
  125. ctx->input[7] = U8TO32_LITTLE(iv + 4);
  126. ctx->input[8] = 0;
  127. ctx->input[9] = 0;
  128. }
  129. static void salsa20_encrypt_bytes(struct salsa20_ctx *ctx, u8 *dst,
  130. const u8 *src, unsigned int bytes)
  131. {
  132. u8 buf[64];
  133. if (dst != src)
  134. memcpy(dst, src, bytes);
  135. while (bytes) {
  136. salsa20_wordtobyte(buf, ctx->input);
  137. ctx->input[8] = PLUSONE(ctx->input[8]);
  138. if (!ctx->input[8])
  139. ctx->input[9] = PLUSONE(ctx->input[9]);
  140. if (bytes <= 64) {
  141. crypto_xor(dst, buf, bytes);
  142. return;
  143. }
  144. crypto_xor(dst, buf, 64);
  145. bytes -= 64;
  146. dst += 64;
  147. }
  148. }
  149. /*
  150. * End of code taken from D. J. Bernstein's reference implementation.
  151. */
  152. static int setkey(struct crypto_tfm *tfm, const u8 *key,
  153. unsigned int keysize)
  154. {
  155. struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
  156. salsa20_keysetup(ctx, key, keysize);
  157. return 0;
  158. }
  159. static int encrypt(struct blkcipher_desc *desc,
  160. struct scatterlist *dst, struct scatterlist *src,
  161. unsigned int nbytes)
  162. {
  163. struct blkcipher_walk walk;
  164. struct crypto_blkcipher *tfm = desc->tfm;
  165. struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
  166. int err;
  167. blkcipher_walk_init(&walk, dst, src, nbytes);
  168. err = blkcipher_walk_virt_block(desc, &walk, 64);
  169. salsa20_ivsetup(ctx, walk.iv);
  170. if (likely(walk.nbytes == nbytes))
  171. {
  172. salsa20_encrypt_bytes(ctx, walk.dst.virt.addr,
  173. walk.src.virt.addr, nbytes);
  174. return blkcipher_walk_done(desc, &walk, 0);
  175. }
  176. while (walk.nbytes >= 64) {
  177. salsa20_encrypt_bytes(ctx, walk.dst.virt.addr,
  178. walk.src.virt.addr,
  179. walk.nbytes - (walk.nbytes % 64));
  180. err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
  181. }
  182. if (walk.nbytes) {
  183. salsa20_encrypt_bytes(ctx, walk.dst.virt.addr,
  184. walk.src.virt.addr, walk.nbytes);
  185. err = blkcipher_walk_done(desc, &walk, 0);
  186. }
  187. return err;
  188. }
  189. static struct crypto_alg alg = {
  190. .cra_name = "salsa20",
  191. .cra_driver_name = "salsa20-generic",
  192. .cra_priority = 100,
  193. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  194. .cra_type = &crypto_blkcipher_type,
  195. .cra_blocksize = 1,
  196. .cra_ctxsize = sizeof(struct salsa20_ctx),
  197. .cra_alignmask = 3,
  198. .cra_module = THIS_MODULE,
  199. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  200. .cra_u = {
  201. .blkcipher = {
  202. .setkey = setkey,
  203. .encrypt = encrypt,
  204. .decrypt = encrypt,
  205. .min_keysize = SALSA20_MIN_KEY_SIZE,
  206. .max_keysize = SALSA20_MAX_KEY_SIZE,
  207. .ivsize = SALSA20_IV_SIZE,
  208. }
  209. }
  210. };
  211. static int __init salsa20_generic_mod_init(void)
  212. {
  213. return crypto_register_alg(&alg);
  214. }
  215. static void __exit salsa20_generic_mod_fini(void)
  216. {
  217. crypto_unregister_alg(&alg);
  218. }
  219. module_init(salsa20_generic_mod_init);
  220. module_exit(salsa20_generic_mod_fini);
  221. MODULE_LICENSE("GPL");
  222. MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm");
  223. MODULE_ALIAS("salsa20");