sm3_generic.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <gilad@benyossef.com>
  8. * Copyright (C) 2021 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
  9. */
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/sm3.h>
  12. #include <crypto/sm3_base.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. const u8 sm3_zero_message_hash[SM3_DIGEST_SIZE] = {
  16. 0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F,
  17. 0x8e, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
  18. 0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74,
  19. 0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B
  20. };
  21. EXPORT_SYMBOL_GPL(sm3_zero_message_hash);
  22. static int crypto_sm3_update(struct shash_desc *desc, const u8 *data,
  23. unsigned int len)
  24. {
  25. return sm3_base_do_update_blocks(desc, data, len, sm3_block_generic);
  26. }
  27. static int crypto_sm3_finup(struct shash_desc *desc, const u8 *data,
  28. unsigned int len, u8 *hash)
  29. {
  30. sm3_base_do_finup(desc, data, len, sm3_block_generic);
  31. return sm3_base_finish(desc, hash);
  32. }
  33. static struct shash_alg sm3_alg = {
  34. .digestsize = SM3_DIGEST_SIZE,
  35. .init = sm3_base_init,
  36. .update = crypto_sm3_update,
  37. .finup = crypto_sm3_finup,
  38. .descsize = SM3_STATE_SIZE,
  39. .base = {
  40. .cra_name = "sm3",
  41. .cra_driver_name = "sm3-generic",
  42. .cra_priority = 100,
  43. .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY |
  44. CRYPTO_AHASH_ALG_FINUP_MAX,
  45. .cra_blocksize = SM3_BLOCK_SIZE,
  46. .cra_module = THIS_MODULE,
  47. }
  48. };
  49. static int __init sm3_generic_mod_init(void)
  50. {
  51. return crypto_register_shash(&sm3_alg);
  52. }
  53. static void __exit sm3_generic_mod_fini(void)
  54. {
  55. crypto_unregister_shash(&sm3_alg);
  56. }
  57. module_init(sm3_generic_mod_init);
  58. module_exit(sm3_generic_mod_fini);
  59. MODULE_LICENSE("GPL v2");
  60. MODULE_DESCRIPTION("SM3 Secure Hash Algorithm");
  61. MODULE_ALIAS_CRYPTO("sm3");
  62. MODULE_ALIAS_CRYPTO("sm3-generic");