sha256_generic.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <jlcooke@certainkey.com>
  6. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  7. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  8. * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
  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/sha.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. static int crypto_sha256_init(struct shash_desc *desc)
  34. {
  35. sha256_init(shash_desc_ctx(desc));
  36. return 0;
  37. }
  38. static int crypto_sha224_init(struct shash_desc *desc)
  39. {
  40. sha224_init(shash_desc_ctx(desc));
  41. return 0;
  42. }
  43. int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
  44. unsigned int len)
  45. {
  46. sha256_update(shash_desc_ctx(desc), data, len);
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(crypto_sha256_update);
  50. static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
  51. {
  52. if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
  53. sha224_final(shash_desc_ctx(desc), out);
  54. else
  55. sha256_final(shash_desc_ctx(desc), out);
  56. return 0;
  57. }
  58. int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
  59. unsigned int len, u8 *hash)
  60. {
  61. sha256_update(shash_desc_ctx(desc), data, len);
  62. return crypto_sha256_final(desc, hash);
  63. }
  64. EXPORT_SYMBOL(crypto_sha256_finup);
  65. static struct shash_alg sha256_algs[2] = { {
  66. .digestsize = SHA256_DIGEST_SIZE,
  67. .init = crypto_sha256_init,
  68. .update = crypto_sha256_update,
  69. .final = crypto_sha256_final,
  70. .finup = crypto_sha256_finup,
  71. .descsize = sizeof(struct sha256_state),
  72. .base = {
  73. .cra_name = "sha256",
  74. .cra_driver_name= "sha256-generic",
  75. .cra_priority = 100,
  76. .cra_blocksize = SHA256_BLOCK_SIZE,
  77. .cra_module = THIS_MODULE,
  78. }
  79. }, {
  80. .digestsize = SHA224_DIGEST_SIZE,
  81. .init = crypto_sha224_init,
  82. .update = crypto_sha256_update,
  83. .final = crypto_sha256_final,
  84. .finup = crypto_sha256_finup,
  85. .descsize = sizeof(struct sha256_state),
  86. .base = {
  87. .cra_name = "sha224",
  88. .cra_driver_name= "sha224-generic",
  89. .cra_priority = 100,
  90. .cra_blocksize = SHA224_BLOCK_SIZE,
  91. .cra_module = THIS_MODULE,
  92. }
  93. } };
  94. static int __init sha256_generic_mod_init(void)
  95. {
  96. return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
  97. }
  98. static void __exit sha256_generic_mod_fini(void)
  99. {
  100. crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
  101. }
  102. subsys_initcall(sha256_generic_mod_init);
  103. module_exit(sha256_generic_mod_fini);
  104. MODULE_LICENSE("GPL");
  105. MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
  106. MODULE_ALIAS_CRYPTO("sha224");
  107. MODULE_ALIAS_CRYPTO("sha224-generic");
  108. MODULE_ALIAS_CRYPTO("sha256");
  109. MODULE_ALIAS_CRYPTO("sha256-generic");