salsa20_glue.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Glue code for optimized assembly version of Salsa20.
  3. *
  4. * Copyright (c) 2007 Tan Swee Heng <thesweeheng@gmail.com>
  5. *
  6. * The assembly codes are public domain assembly codes written by Daniel. J.
  7. * Bernstein <djb@cr.yp.to>. The codes are modified to include indentation
  8. * and to remove extraneous comments and functions that are not needed.
  9. * - i586 version, renamed as salsa20-i586-asm_32.S
  10. * available from <http://cr.yp.to/snuffle/salsa20/x86-pm/salsa20.s>
  11. * - x86-64 version, renamed as salsa20-x86_64-asm_64.S
  12. * available from <http://cr.yp.to/snuffle/salsa20/amd64-3/salsa20.s>
  13. *
  14. * Also modified to set up the initial state using the generic C code rather
  15. * than in assembly.
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the Free
  19. * Software Foundation; either version 2 of the License, or (at your option)
  20. * any later version.
  21. *
  22. */
  23. #include <asm/unaligned.h>
  24. #include <crypto/internal/skcipher.h>
  25. #include <crypto/salsa20.h>
  26. #include <linux/module.h>
  27. asmlinkage void salsa20_encrypt_bytes(u32 state[16], const u8 *src, u8 *dst,
  28. u32 bytes);
  29. static int salsa20_asm_crypt(struct skcipher_request *req)
  30. {
  31. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  32. const struct salsa20_ctx *ctx = crypto_skcipher_ctx(tfm);
  33. struct skcipher_walk walk;
  34. u32 state[16];
  35. int err;
  36. err = skcipher_walk_virt(&walk, req, true);
  37. crypto_salsa20_init(state, ctx, walk.iv);
  38. while (walk.nbytes > 0) {
  39. unsigned int nbytes = walk.nbytes;
  40. if (nbytes < walk.total)
  41. nbytes = round_down(nbytes, walk.stride);
  42. salsa20_encrypt_bytes(state, walk.src.virt.addr,
  43. walk.dst.virt.addr, nbytes);
  44. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  45. }
  46. return err;
  47. }
  48. static struct skcipher_alg alg = {
  49. .base.cra_name = "salsa20",
  50. .base.cra_driver_name = "salsa20-asm",
  51. .base.cra_priority = 200,
  52. .base.cra_blocksize = 1,
  53. .base.cra_ctxsize = sizeof(struct salsa20_ctx),
  54. .base.cra_module = THIS_MODULE,
  55. .min_keysize = SALSA20_MIN_KEY_SIZE,
  56. .max_keysize = SALSA20_MAX_KEY_SIZE,
  57. .ivsize = SALSA20_IV_SIZE,
  58. .chunksize = SALSA20_BLOCK_SIZE,
  59. .setkey = crypto_salsa20_setkey,
  60. .encrypt = salsa20_asm_crypt,
  61. .decrypt = salsa20_asm_crypt,
  62. };
  63. static int __init init(void)
  64. {
  65. return crypto_register_skcipher(&alg);
  66. }
  67. static void __exit fini(void)
  68. {
  69. crypto_unregister_skcipher(&alg);
  70. }
  71. module_init(init);
  72. module_exit(fini);
  73. MODULE_LICENSE("GPL");
  74. MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm (optimized assembly version)");
  75. MODULE_ALIAS_CRYPTO("salsa20");
  76. MODULE_ALIAS_CRYPTO("salsa20-asm");