sha3_512_s390.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Cryptographic API.
  4. *
  5. * s390 implementation of the SHA512 and SHA384 Secure Hash Algorithm.
  6. *
  7. * Copyright IBM Corp. 2019
  8. * Author(s): Joerg Schmidbauer ([email protected])
  9. */
  10. #include <crypto/internal/hash.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/cpufeature.h>
  14. #include <crypto/sha3.h>
  15. #include <asm/cpacf.h>
  16. #include "sha.h"
  17. static int sha3_512_init(struct shash_desc *desc)
  18. {
  19. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  20. memset(sctx->state, 0, sizeof(sctx->state));
  21. sctx->count = 0;
  22. sctx->func = CPACF_KIMD_SHA3_512;
  23. return 0;
  24. }
  25. static int sha3_512_export(struct shash_desc *desc, void *out)
  26. {
  27. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  28. struct sha3_state *octx = out;
  29. octx->rsiz = sctx->count;
  30. octx->rsizw = sctx->count >> 32;
  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_512_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. if (unlikely(ictx->rsizw))
  40. return -ERANGE;
  41. sctx->count = ictx->rsiz;
  42. memcpy(sctx->state, ictx->st, sizeof(ictx->st));
  43. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  44. sctx->func = CPACF_KIMD_SHA3_512;
  45. return 0;
  46. }
  47. static int sha3_384_import(struct shash_desc *desc, const void *in)
  48. {
  49. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  50. const struct sha3_state *ictx = in;
  51. if (unlikely(ictx->rsizw))
  52. return -ERANGE;
  53. sctx->count = ictx->rsiz;
  54. memcpy(sctx->state, ictx->st, sizeof(ictx->st));
  55. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  56. sctx->func = CPACF_KIMD_SHA3_384;
  57. return 0;
  58. }
  59. static struct shash_alg sha3_512_alg = {
  60. .digestsize = SHA3_512_DIGEST_SIZE,
  61. .init = sha3_512_init,
  62. .update = s390_sha_update,
  63. .final = s390_sha_final,
  64. .export = sha3_512_export,
  65. .import = sha3_512_import,
  66. .descsize = sizeof(struct s390_sha_ctx),
  67. .statesize = sizeof(struct sha3_state),
  68. .base = {
  69. .cra_name = "sha3-512",
  70. .cra_driver_name = "sha3-512-s390",
  71. .cra_priority = 300,
  72. .cra_blocksize = SHA3_512_BLOCK_SIZE,
  73. .cra_module = THIS_MODULE,
  74. }
  75. };
  76. MODULE_ALIAS_CRYPTO("sha3-512");
  77. static int sha3_384_init(struct shash_desc *desc)
  78. {
  79. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  80. memset(sctx->state, 0, sizeof(sctx->state));
  81. sctx->count = 0;
  82. sctx->func = CPACF_KIMD_SHA3_384;
  83. return 0;
  84. }
  85. static struct shash_alg sha3_384_alg = {
  86. .digestsize = SHA3_384_DIGEST_SIZE,
  87. .init = sha3_384_init,
  88. .update = s390_sha_update,
  89. .final = s390_sha_final,
  90. .export = sha3_512_export, /* same as for 512 */
  91. .import = sha3_384_import, /* function code different! */
  92. .descsize = sizeof(struct s390_sha_ctx),
  93. .statesize = sizeof(struct sha3_state),
  94. .base = {
  95. .cra_name = "sha3-384",
  96. .cra_driver_name = "sha3-384-s390",
  97. .cra_priority = 300,
  98. .cra_blocksize = SHA3_384_BLOCK_SIZE,
  99. .cra_ctxsize = sizeof(struct s390_sha_ctx),
  100. .cra_module = THIS_MODULE,
  101. }
  102. };
  103. MODULE_ALIAS_CRYPTO("sha3-384");
  104. static int __init init(void)
  105. {
  106. int ret;
  107. if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA3_512))
  108. return -ENODEV;
  109. ret = crypto_register_shash(&sha3_512_alg);
  110. if (ret < 0)
  111. goto out;
  112. ret = crypto_register_shash(&sha3_384_alg);
  113. if (ret < 0)
  114. crypto_unregister_shash(&sha3_512_alg);
  115. out:
  116. return ret;
  117. }
  118. static void __exit fini(void)
  119. {
  120. crypto_unregister_shash(&sha3_512_alg);
  121. crypto_unregister_shash(&sha3_384_alg);
  122. }
  123. module_cpu_feature_match(S390_CPU_FEATURE_MSA, init);
  124. module_exit(fini);
  125. MODULE_LICENSE("GPL");
  126. MODULE_DESCRIPTION("SHA3-512 and SHA3-384 Secure Hash Algorithm");