salsa20_glue.c 3.6 KB

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