sha256_neon_glue.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
  3. * using NEON instructions.
  4. *
  5. * Copyright © 2015 Google Inc.
  6. *
  7. * This file is based on sha512_neon_glue.c:
  8. * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/hash.h>
  17. #include <linux/cryptohash.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <crypto/sha.h>
  21. #include <crypto/sha256_base.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/simd.h>
  24. #include <asm/neon.h>
  25. #include "sha256_glue.h"
  26. asmlinkage void sha256_block_data_order_neon(u32 *digest, const void *data,
  27. unsigned int num_blks);
  28. static int sha256_update(struct shash_desc *desc, const u8 *data,
  29. unsigned int len)
  30. {
  31. struct sha256_state *sctx = shash_desc_ctx(desc);
  32. if (!may_use_simd() ||
  33. (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  34. return crypto_sha256_arm_update(desc, data, len);
  35. kernel_neon_begin();
  36. sha256_base_do_update(desc, data, len,
  37. (sha256_block_fn *)sha256_block_data_order_neon);
  38. kernel_neon_end();
  39. return 0;
  40. }
  41. static int sha256_finup(struct shash_desc *desc, const u8 *data,
  42. unsigned int len, u8 *out)
  43. {
  44. if (!may_use_simd())
  45. return crypto_sha256_arm_finup(desc, data, len, out);
  46. kernel_neon_begin();
  47. if (len)
  48. sha256_base_do_update(desc, data, len,
  49. (sha256_block_fn *)sha256_block_data_order_neon);
  50. sha256_base_do_finalize(desc,
  51. (sha256_block_fn *)sha256_block_data_order_neon);
  52. kernel_neon_end();
  53. return sha256_base_finish(desc, out);
  54. }
  55. static int sha256_final(struct shash_desc *desc, u8 *out)
  56. {
  57. return sha256_finup(desc, NULL, 0, out);
  58. }
  59. struct shash_alg sha256_neon_algs[] = { {
  60. .digestsize = SHA256_DIGEST_SIZE,
  61. .init = sha256_base_init,
  62. .update = sha256_update,
  63. .final = sha256_final,
  64. .finup = sha256_finup,
  65. .descsize = sizeof(struct sha256_state),
  66. .base = {
  67. .cra_name = "sha256",
  68. .cra_driver_name = "sha256-neon",
  69. .cra_priority = 250,
  70. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  71. .cra_blocksize = SHA256_BLOCK_SIZE,
  72. .cra_module = THIS_MODULE,
  73. }
  74. }, {
  75. .digestsize = SHA224_DIGEST_SIZE,
  76. .init = sha224_base_init,
  77. .update = sha256_update,
  78. .final = sha256_final,
  79. .finup = sha256_finup,
  80. .descsize = sizeof(struct sha256_state),
  81. .base = {
  82. .cra_name = "sha224",
  83. .cra_driver_name = "sha224-neon",
  84. .cra_priority = 250,
  85. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  86. .cra_blocksize = SHA224_BLOCK_SIZE,
  87. .cra_module = THIS_MODULE,
  88. }
  89. } };