sha1_neon_glue.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Glue code for the SHA1 Secure Hash Algorithm assembler implementation using
  3. * ARM NEON instructions.
  4. *
  5. * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  6. *
  7. * This file is based on sha1_generic.c and sha1_ssse3_glue.c:
  8. * Copyright (c) Alan Smithee.
  9. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  10. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  11. * Copyright (c) Mathias Krause <minipli@googlemail.com>
  12. * Copyright (c) Chandramouli Narayanan <mouli@linux.intel.com>
  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/internal/hash.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm.h>
  24. #include <linux/cryptohash.h>
  25. #include <linux/types.h>
  26. #include <crypto/sha.h>
  27. #include <crypto/sha1_base.h>
  28. #include <asm/neon.h>
  29. #include <asm/simd.h>
  30. #include "sha1.h"
  31. asmlinkage void sha1_transform_neon(void *state_h, const char *data,
  32. unsigned int rounds);
  33. static int sha1_neon_update(struct shash_desc *desc, const u8 *data,
  34. unsigned int len)
  35. {
  36. struct sha1_state *sctx = shash_desc_ctx(desc);
  37. if (!may_use_simd() ||
  38. (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
  39. return sha1_update_arm(desc, data, len);
  40. kernel_neon_begin();
  41. sha1_base_do_update(desc, data, len,
  42. (sha1_block_fn *)sha1_transform_neon);
  43. kernel_neon_end();
  44. return 0;
  45. }
  46. static int sha1_neon_finup(struct shash_desc *desc, const u8 *data,
  47. unsigned int len, u8 *out)
  48. {
  49. if (!may_use_simd())
  50. return sha1_finup_arm(desc, data, len, out);
  51. kernel_neon_begin();
  52. if (len)
  53. sha1_base_do_update(desc, data, len,
  54. (sha1_block_fn *)sha1_transform_neon);
  55. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_transform_neon);
  56. kernel_neon_end();
  57. return sha1_base_finish(desc, out);
  58. }
  59. static int sha1_neon_final(struct shash_desc *desc, u8 *out)
  60. {
  61. return sha1_neon_finup(desc, NULL, 0, out);
  62. }
  63. static struct shash_alg alg = {
  64. .digestsize = SHA1_DIGEST_SIZE,
  65. .init = sha1_base_init,
  66. .update = sha1_neon_update,
  67. .final = sha1_neon_final,
  68. .finup = sha1_neon_finup,
  69. .descsize = sizeof(struct sha1_state),
  70. .base = {
  71. .cra_name = "sha1",
  72. .cra_driver_name = "sha1-neon",
  73. .cra_priority = 250,
  74. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  75. .cra_blocksize = SHA1_BLOCK_SIZE,
  76. .cra_module = THIS_MODULE,
  77. }
  78. };
  79. static int __init sha1_neon_mod_init(void)
  80. {
  81. if (!cpu_has_neon())
  82. return -ENODEV;
  83. return crypto_register_shash(&alg);
  84. }
  85. static void __exit sha1_neon_mod_fini(void)
  86. {
  87. crypto_unregister_shash(&alg);
  88. }
  89. module_init(sha1_neon_mod_init);
  90. module_exit(sha1_neon_mod_fini);
  91. MODULE_LICENSE("GPL");
  92. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, NEON accelerated");
  93. MODULE_ALIAS_CRYPTO("sha1");