sha1-ce-glue.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2014 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <asm/unaligned.h>
  12. #include <crypto/internal/hash.h>
  13. #include <crypto/sha.h>
  14. #include <crypto/sha1_base.h>
  15. #include <linux/cpufeature.h>
  16. #include <linux/crypto.h>
  17. #include <linux/module.h>
  18. #define ASM_EXPORT(sym, val) \
  19. asm(".globl " #sym "; .set " #sym ", %0" :: "I"(val));
  20. MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
  21. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  22. MODULE_LICENSE("GPL v2");
  23. struct sha1_ce_state {
  24. struct sha1_state sst;
  25. u32 finalize;
  26. };
  27. asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
  28. int blocks);
  29. static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
  30. unsigned int len)
  31. {
  32. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  33. sctx->finalize = 0;
  34. kernel_neon_begin_partial(16);
  35. sha1_base_do_update(desc, data, len,
  36. (sha1_block_fn *)sha1_ce_transform);
  37. kernel_neon_end();
  38. return 0;
  39. }
  40. static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
  41. unsigned int len, u8 *out)
  42. {
  43. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  44. bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
  45. ASM_EXPORT(sha1_ce_offsetof_count,
  46. offsetof(struct sha1_ce_state, sst.count));
  47. ASM_EXPORT(sha1_ce_offsetof_finalize,
  48. offsetof(struct sha1_ce_state, finalize));
  49. /*
  50. * Allow the asm code to perform the finalization if there is no
  51. * partial data and the input is a round multiple of the block size.
  52. */
  53. sctx->finalize = finalize;
  54. kernel_neon_begin_partial(16);
  55. sha1_base_do_update(desc, data, len,
  56. (sha1_block_fn *)sha1_ce_transform);
  57. if (!finalize)
  58. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
  59. kernel_neon_end();
  60. return sha1_base_finish(desc, out);
  61. }
  62. static int sha1_ce_final(struct shash_desc *desc, u8 *out)
  63. {
  64. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  65. sctx->finalize = 0;
  66. kernel_neon_begin_partial(16);
  67. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
  68. kernel_neon_end();
  69. return sha1_base_finish(desc, out);
  70. }
  71. static struct shash_alg alg = {
  72. .init = sha1_base_init,
  73. .update = sha1_ce_update,
  74. .final = sha1_ce_final,
  75. .finup = sha1_ce_finup,
  76. .descsize = sizeof(struct sha1_ce_state),
  77. .digestsize = SHA1_DIGEST_SIZE,
  78. .base = {
  79. .cra_name = "sha1",
  80. .cra_driver_name = "sha1-ce",
  81. .cra_priority = 200,
  82. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  83. .cra_blocksize = SHA1_BLOCK_SIZE,
  84. .cra_module = THIS_MODULE,
  85. }
  86. };
  87. static int __init sha1_ce_mod_init(void)
  88. {
  89. return crypto_register_shash(&alg);
  90. }
  91. static void __exit sha1_ce_mod_fini(void)
  92. {
  93. crypto_unregister_shash(&alg);
  94. }
  95. module_cpu_feature_match(SHA1, sha1_ce_mod_init);
  96. module_exit(sha1_ce_mod_fini);