sm3_generic.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
  4. * described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
  5. *
  6. * Copyright (C) 2017 ARM Limited or its affiliates.
  7. * Written by Gilad Ben-Yossef <[email protected]>
  8. * Copyright (C) 2021 Tianjia Zhang <[email protected]>
  9. */
  10. #include <crypto/internal/hash.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/types.h>
  15. #include <crypto/sm3.h>
  16. #include <crypto/sm3_base.h>
  17. #include <linux/bitops.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/unaligned.h>
  20. const u8 sm3_zero_message_hash[SM3_DIGEST_SIZE] = {
  21. 0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F,
  22. 0x8e, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
  23. 0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74,
  24. 0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B
  25. };
  26. EXPORT_SYMBOL_GPL(sm3_zero_message_hash);
  27. static int crypto_sm3_update(struct shash_desc *desc, const u8 *data,
  28. unsigned int len)
  29. {
  30. sm3_update(shash_desc_ctx(desc), data, len);
  31. return 0;
  32. }
  33. static int crypto_sm3_final(struct shash_desc *desc, u8 *out)
  34. {
  35. sm3_final(shash_desc_ctx(desc), out);
  36. return 0;
  37. }
  38. static int crypto_sm3_finup(struct shash_desc *desc, const u8 *data,
  39. unsigned int len, u8 *hash)
  40. {
  41. struct sm3_state *sctx = shash_desc_ctx(desc);
  42. if (len)
  43. sm3_update(sctx, data, len);
  44. sm3_final(sctx, hash);
  45. return 0;
  46. }
  47. static struct shash_alg sm3_alg = {
  48. .digestsize = SM3_DIGEST_SIZE,
  49. .init = sm3_base_init,
  50. .update = crypto_sm3_update,
  51. .final = crypto_sm3_final,
  52. .finup = crypto_sm3_finup,
  53. .descsize = sizeof(struct sm3_state),
  54. .base = {
  55. .cra_name = "sm3",
  56. .cra_driver_name = "sm3-generic",
  57. .cra_priority = 100,
  58. .cra_blocksize = SM3_BLOCK_SIZE,
  59. .cra_module = THIS_MODULE,
  60. }
  61. };
  62. static int __init sm3_generic_mod_init(void)
  63. {
  64. return crypto_register_shash(&sm3_alg);
  65. }
  66. static void __exit sm3_generic_mod_fini(void)
  67. {
  68. crypto_unregister_shash(&sm3_alg);
  69. }
  70. subsys_initcall(sm3_generic_mod_init);
  71. module_exit(sm3_generic_mod_fini);
  72. MODULE_LICENSE("GPL v2");
  73. MODULE_DESCRIPTION("SM3 Secure Hash Algorithm");
  74. MODULE_ALIAS_CRYPTO("sm3");
  75. MODULE_ALIAS_CRYPTO("sm3-generic");