sha256_generic.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
  4. *
  5. * Copyright (c) Jean-Luc Cooke <[email protected]>
  6. * Copyright (c) Andrew McDonald <[email protected]>
  7. * Copyright (c) 2002 James Morris <[email protected]>
  8. * SHA224 Support Copyright 2007 Intel Corporation <[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/sha2.h>
  16. #include <crypto/sha256_base.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/unaligned.h>
  19. const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
  20. 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
  21. 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
  22. 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
  23. 0x2f
  24. };
  25. EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
  26. const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
  27. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
  28. 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
  29. 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
  30. 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
  31. };
  32. EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
  33. int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
  34. unsigned int len)
  35. {
  36. sha256_update(shash_desc_ctx(desc), data, len);
  37. return 0;
  38. }
  39. EXPORT_SYMBOL(crypto_sha256_update);
  40. static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
  41. {
  42. if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
  43. sha224_final(shash_desc_ctx(desc), out);
  44. else
  45. sha256_final(shash_desc_ctx(desc), out);
  46. return 0;
  47. }
  48. int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
  49. unsigned int len, u8 *hash)
  50. {
  51. sha256_update(shash_desc_ctx(desc), data, len);
  52. return crypto_sha256_final(desc, hash);
  53. }
  54. EXPORT_SYMBOL(crypto_sha256_finup);
  55. static struct shash_alg sha256_algs[2] = { {
  56. .digestsize = SHA256_DIGEST_SIZE,
  57. .init = sha256_base_init,
  58. .update = crypto_sha256_update,
  59. .final = crypto_sha256_final,
  60. .finup = crypto_sha256_finup,
  61. .descsize = sizeof(struct sha256_state),
  62. .base = {
  63. .cra_name = "sha256",
  64. .cra_driver_name= "sha256-generic",
  65. .cra_priority = 100,
  66. .cra_blocksize = SHA256_BLOCK_SIZE,
  67. .cra_module = THIS_MODULE,
  68. }
  69. }, {
  70. .digestsize = SHA224_DIGEST_SIZE,
  71. .init = sha224_base_init,
  72. .update = crypto_sha256_update,
  73. .final = crypto_sha256_final,
  74. .finup = crypto_sha256_finup,
  75. .descsize = sizeof(struct sha256_state),
  76. .base = {
  77. .cra_name = "sha224",
  78. .cra_driver_name= "sha224-generic",
  79. .cra_priority = 100,
  80. .cra_blocksize = SHA224_BLOCK_SIZE,
  81. .cra_module = THIS_MODULE,
  82. }
  83. } };
  84. static int __init sha256_generic_mod_init(void)
  85. {
  86. return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
  87. }
  88. static void __exit sha256_generic_mod_fini(void)
  89. {
  90. crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
  91. }
  92. subsys_initcall(sha256_generic_mod_init);
  93. module_exit(sha256_generic_mod_fini);
  94. MODULE_LICENSE("GPL");
  95. MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
  96. MODULE_ALIAS_CRYPTO("sha224");
  97. MODULE_ALIAS_CRYPTO("sha224-generic");
  98. MODULE_ALIAS_CRYPTO("sha256");
  99. MODULE_ALIAS_CRYPTO("sha256-generic");