salsa20_generic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. int i;
  134. if (dst != src)
  135. memcpy(dst, src, bytes);
  136. while (bytes) {
  137. salsa20_wordtobyte(buf, ctx->input);
  138. ctx->input[8] = PLUSONE(ctx->input[8]);
  139. if (!ctx->input[8])
  140. ctx->input[9] = PLUSONE(ctx->input[9]);
  141. if (bytes <= 64) {
  142. for (i = 0; i < bytes/4; ++i)
  143. ((u32*)dst)[i] ^= ((u32*)buf)[i];
  144. for (i = bytes - bytes % 4; i < bytes; ++i)
  145. dst[i] ^= buf[i];
  146. return;
  147. }
  148. for (i = 0; i < 64/4; ++i)
  149. ((u32*)dst)[i] ^= ((u32*)buf)[i];
  150. bytes -= 64;
  151. dst += 64;
  152. }
  153. }
  154. /*
  155. * End of code taken from D. J. Bernstein's reference implementation.
  156. */
  157. static int setkey(struct crypto_tfm *tfm, const u8 *key,
  158. unsigned int keysize)
  159. {
  160. struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
  161. salsa20_keysetup(ctx, key, keysize);
  162. return 0;
  163. }
  164. static int encrypt(struct blkcipher_desc *desc,
  165. struct scatterlist *dst, struct scatterlist *src,
  166. unsigned int nbytes)
  167. {
  168. struct blkcipher_walk walk;
  169. struct crypto_blkcipher *tfm = desc->tfm;
  170. struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
  171. int err;
  172. blkcipher_walk_init(&walk, dst, src, nbytes);
  173. err = blkcipher_walk_virt(desc, &walk);
  174. salsa20_ivsetup(ctx, walk.iv);
  175. salsa20_encrypt_bytes(ctx, walk.dst.virt.addr,
  176. walk.src.virt.addr, nbytes);
  177. err = blkcipher_walk_done(desc, &walk, 0);
  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_list = LIST_HEAD_INIT(alg.cra_list),
  191. .cra_u = {
  192. .blkcipher = {
  193. .setkey = setkey,
  194. .encrypt = encrypt,
  195. .decrypt = encrypt,
  196. .min_keysize = SALSA20_MIN_KEY_SIZE,
  197. .max_keysize = SALSA20_MAX_KEY_SIZE,
  198. .ivsize = SALSA20_IV_SIZE,
  199. }
  200. }
  201. };
  202. static int __init init(void)
  203. {
  204. return crypto_register_alg(&alg);
  205. }
  206. static void __exit fini(void)
  207. {
  208. crypto_unregister_alg(&alg);
  209. }
  210. module_init(init);
  211. module_exit(fini);
  212. MODULE_LICENSE("GPL");
  213. MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm");
  214. MODULE_ALIAS("salsa20");