salsa20_glue.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. */
  20. #include <crypto/algapi.h>
  21. #include <linux/module.h>
  22. #include <linux/crypto.h>
  23. #define SALSA20_IV_SIZE 8U
  24. #define SALSA20_MIN_KEY_SIZE 16U
  25. #define SALSA20_MAX_KEY_SIZE 32U
  26. // use the ECRYPT_* function names
  27. #define salsa20_keysetup ECRYPT_keysetup
  28. #define salsa20_ivsetup ECRYPT_ivsetup
  29. #define salsa20_encrypt_bytes ECRYPT_encrypt_bytes
  30. struct salsa20_ctx
  31. {
  32. u32 input[16];
  33. };
  34. asmlinkage void salsa20_keysetup(struct salsa20_ctx *ctx, const u8 *k,
  35. u32 keysize, u32 ivsize);
  36. asmlinkage void salsa20_ivsetup(struct salsa20_ctx *ctx, const u8 *iv);
  37. asmlinkage void salsa20_encrypt_bytes(struct salsa20_ctx *ctx,
  38. const u8 *src, u8 *dst, u32 bytes);
  39. static int setkey(struct crypto_tfm *tfm, const u8 *key,
  40. unsigned int keysize)
  41. {
  42. struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
  43. salsa20_keysetup(ctx, key, keysize*8, SALSA20_IV_SIZE*8);
  44. return 0;
  45. }
  46. static int encrypt(struct blkcipher_desc *desc,
  47. struct scatterlist *dst, struct scatterlist *src,
  48. unsigned int nbytes)
  49. {
  50. struct blkcipher_walk walk;
  51. struct crypto_blkcipher *tfm = desc->tfm;
  52. struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
  53. int err;
  54. blkcipher_walk_init(&walk, dst, src, nbytes);
  55. err = blkcipher_walk_virt_block(desc, &walk, 64);
  56. salsa20_ivsetup(ctx, walk.iv);
  57. if (likely(walk.nbytes == nbytes))
  58. {
  59. salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
  60. walk.dst.virt.addr, nbytes);
  61. return blkcipher_walk_done(desc, &walk, 0);
  62. }
  63. while (walk.nbytes >= 64) {
  64. salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
  65. walk.dst.virt.addr,
  66. walk.nbytes - (walk.nbytes % 64));
  67. err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
  68. }
  69. if (walk.nbytes) {
  70. salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
  71. walk.dst.virt.addr, walk.nbytes);
  72. err = blkcipher_walk_done(desc, &walk, 0);
  73. }
  74. return err;
  75. }
  76. static struct crypto_alg alg = {
  77. .cra_name = "salsa20",
  78. .cra_driver_name = "salsa20-asm",
  79. .cra_priority = 200,
  80. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  81. .cra_type = &crypto_blkcipher_type,
  82. .cra_blocksize = 1,
  83. .cra_ctxsize = sizeof(struct salsa20_ctx),
  84. .cra_alignmask = 3,
  85. .cra_module = THIS_MODULE,
  86. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  87. .cra_u = {
  88. .blkcipher = {
  89. .setkey = setkey,
  90. .encrypt = encrypt,
  91. .decrypt = encrypt,
  92. .min_keysize = SALSA20_MIN_KEY_SIZE,
  93. .max_keysize = SALSA20_MAX_KEY_SIZE,
  94. .ivsize = SALSA20_IV_SIZE,
  95. }
  96. }
  97. };
  98. static int __init init(void)
  99. {
  100. return crypto_register_alg(&alg);
  101. }
  102. static void __exit fini(void)
  103. {
  104. crypto_unregister_alg(&alg);
  105. }
  106. module_init(init);
  107. module_exit(fini);
  108. MODULE_LICENSE("GPL");
  109. MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm (optimized assembly version)");
  110. MODULE_ALIAS("salsa20");
  111. MODULE_ALIAS("salsa20-asm");