sha256_s390.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Cryptographic API.
  4. *
  5. * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
  6. *
  7. * s390 Version:
  8. * Copyright IBM Corp. 2005, 2011
  9. * Author(s): Jan Glauber (jang@de.ibm.com)
  10. */
  11. #include <crypto/internal/hash.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/cpufeature.h>
  15. #include <crypto/sha.h>
  16. #include <asm/cpacf.h>
  17. #include "sha.h"
  18. static int sha256_init(struct shash_desc *desc)
  19. {
  20. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  21. sctx->state[0] = SHA256_H0;
  22. sctx->state[1] = SHA256_H1;
  23. sctx->state[2] = SHA256_H2;
  24. sctx->state[3] = SHA256_H3;
  25. sctx->state[4] = SHA256_H4;
  26. sctx->state[5] = SHA256_H5;
  27. sctx->state[6] = SHA256_H6;
  28. sctx->state[7] = SHA256_H7;
  29. sctx->count = 0;
  30. sctx->func = CPACF_KIMD_SHA_256;
  31. return 0;
  32. }
  33. static int sha256_export(struct shash_desc *desc, void *out)
  34. {
  35. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  36. struct sha256_state *octx = out;
  37. octx->count = sctx->count;
  38. memcpy(octx->state, sctx->state, sizeof(octx->state));
  39. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  40. return 0;
  41. }
  42. static int sha256_import(struct shash_desc *desc, const void *in)
  43. {
  44. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  45. const struct sha256_state *ictx = in;
  46. sctx->count = ictx->count;
  47. memcpy(sctx->state, ictx->state, sizeof(ictx->state));
  48. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  49. sctx->func = CPACF_KIMD_SHA_256;
  50. return 0;
  51. }
  52. static struct shash_alg sha256_alg = {
  53. .digestsize = SHA256_DIGEST_SIZE,
  54. .init = sha256_init,
  55. .update = s390_sha_update,
  56. .final = s390_sha_final,
  57. .export = sha256_export,
  58. .import = sha256_import,
  59. .descsize = sizeof(struct s390_sha_ctx),
  60. .statesize = sizeof(struct sha256_state),
  61. .base = {
  62. .cra_name = "sha256",
  63. .cra_driver_name= "sha256-s390",
  64. .cra_priority = 300,
  65. .cra_blocksize = SHA256_BLOCK_SIZE,
  66. .cra_module = THIS_MODULE,
  67. }
  68. };
  69. static int sha224_init(struct shash_desc *desc)
  70. {
  71. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  72. sctx->state[0] = SHA224_H0;
  73. sctx->state[1] = SHA224_H1;
  74. sctx->state[2] = SHA224_H2;
  75. sctx->state[3] = SHA224_H3;
  76. sctx->state[4] = SHA224_H4;
  77. sctx->state[5] = SHA224_H5;
  78. sctx->state[6] = SHA224_H6;
  79. sctx->state[7] = SHA224_H7;
  80. sctx->count = 0;
  81. sctx->func = CPACF_KIMD_SHA_256;
  82. return 0;
  83. }
  84. static struct shash_alg sha224_alg = {
  85. .digestsize = SHA224_DIGEST_SIZE,
  86. .init = sha224_init,
  87. .update = s390_sha_update,
  88. .final = s390_sha_final,
  89. .export = sha256_export,
  90. .import = sha256_import,
  91. .descsize = sizeof(struct s390_sha_ctx),
  92. .statesize = sizeof(struct sha256_state),
  93. .base = {
  94. .cra_name = "sha224",
  95. .cra_driver_name= "sha224-s390",
  96. .cra_priority = 300,
  97. .cra_blocksize = SHA224_BLOCK_SIZE,
  98. .cra_module = THIS_MODULE,
  99. }
  100. };
  101. static int __init sha256_s390_init(void)
  102. {
  103. int ret;
  104. if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256))
  105. return -EOPNOTSUPP;
  106. ret = crypto_register_shash(&sha256_alg);
  107. if (ret < 0)
  108. goto out;
  109. ret = crypto_register_shash(&sha224_alg);
  110. if (ret < 0)
  111. crypto_unregister_shash(&sha256_alg);
  112. out:
  113. return ret;
  114. }
  115. static void __exit sha256_s390_fini(void)
  116. {
  117. crypto_unregister_shash(&sha224_alg);
  118. crypto_unregister_shash(&sha256_alg);
  119. }
  120. module_cpu_feature_match(MSA, sha256_s390_init);
  121. module_exit(sha256_s390_fini);
  122. MODULE_ALIAS_CRYPTO("sha256");
  123. MODULE_ALIAS_CRYPTO("sha224");
  124. MODULE_LICENSE("GPL");
  125. MODULE_DESCRIPTION("SHA256 and SHA224 Secure Hash Algorithm");