sha3_256_s390.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. 2019
  9. * Author(s): Joerg Schmidbauer ([email protected])
  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/sha3.h>
  16. #include <asm/cpacf.h>
  17. #include "sha.h"
  18. static int sha3_256_init(struct shash_desc *desc)
  19. {
  20. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  21. memset(sctx->state, 0, sizeof(sctx->state));
  22. sctx->count = 0;
  23. sctx->func = CPACF_KIMD_SHA3_256;
  24. return 0;
  25. }
  26. static int sha3_256_export(struct shash_desc *desc, void *out)
  27. {
  28. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  29. struct sha3_state *octx = out;
  30. octx->rsiz = sctx->count;
  31. memcpy(octx->st, sctx->state, sizeof(octx->st));
  32. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  33. return 0;
  34. }
  35. static int sha3_256_import(struct shash_desc *desc, const void *in)
  36. {
  37. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  38. const struct sha3_state *ictx = in;
  39. sctx->count = ictx->rsiz;
  40. memcpy(sctx->state, ictx->st, sizeof(ictx->st));
  41. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  42. sctx->func = CPACF_KIMD_SHA3_256;
  43. return 0;
  44. }
  45. static int sha3_224_import(struct shash_desc *desc, const void *in)
  46. {
  47. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  48. const struct sha3_state *ictx = in;
  49. sctx->count = ictx->rsiz;
  50. memcpy(sctx->state, ictx->st, sizeof(ictx->st));
  51. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  52. sctx->func = CPACF_KIMD_SHA3_224;
  53. return 0;
  54. }
  55. static struct shash_alg sha3_256_alg = {
  56. .digestsize = SHA3_256_DIGEST_SIZE, /* = 32 */
  57. .init = sha3_256_init,
  58. .update = s390_sha_update,
  59. .final = s390_sha_final,
  60. .export = sha3_256_export,
  61. .import = sha3_256_import,
  62. .descsize = sizeof(struct s390_sha_ctx),
  63. .statesize = sizeof(struct sha3_state),
  64. .base = {
  65. .cra_name = "sha3-256",
  66. .cra_driver_name = "sha3-256-s390",
  67. .cra_priority = 300,
  68. .cra_blocksize = SHA3_256_BLOCK_SIZE,
  69. .cra_module = THIS_MODULE,
  70. }
  71. };
  72. static int sha3_224_init(struct shash_desc *desc)
  73. {
  74. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  75. memset(sctx->state, 0, sizeof(sctx->state));
  76. sctx->count = 0;
  77. sctx->func = CPACF_KIMD_SHA3_224;
  78. return 0;
  79. }
  80. static struct shash_alg sha3_224_alg = {
  81. .digestsize = SHA3_224_DIGEST_SIZE,
  82. .init = sha3_224_init,
  83. .update = s390_sha_update,
  84. .final = s390_sha_final,
  85. .export = sha3_256_export, /* same as for 256 */
  86. .import = sha3_224_import, /* function code different! */
  87. .descsize = sizeof(struct s390_sha_ctx),
  88. .statesize = sizeof(struct sha3_state),
  89. .base = {
  90. .cra_name = "sha3-224",
  91. .cra_driver_name = "sha3-224-s390",
  92. .cra_priority = 300,
  93. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  94. .cra_module = THIS_MODULE,
  95. }
  96. };
  97. static int __init sha3_256_s390_init(void)
  98. {
  99. int ret;
  100. if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA3_256))
  101. return -ENODEV;
  102. ret = crypto_register_shash(&sha3_256_alg);
  103. if (ret < 0)
  104. goto out;
  105. ret = crypto_register_shash(&sha3_224_alg);
  106. if (ret < 0)
  107. crypto_unregister_shash(&sha3_256_alg);
  108. out:
  109. return ret;
  110. }
  111. static void __exit sha3_256_s390_fini(void)
  112. {
  113. crypto_unregister_shash(&sha3_224_alg);
  114. crypto_unregister_shash(&sha3_256_alg);
  115. }
  116. module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha3_256_s390_init);
  117. module_exit(sha3_256_s390_fini);
  118. MODULE_ALIAS_CRYPTO("sha3-256");
  119. MODULE_ALIAS_CRYPTO("sha3-224");
  120. MODULE_LICENSE("GPL");
  121. MODULE_DESCRIPTION("SHA3-256 and SHA3-224 Secure Hash Algorithm");