sha256_s390.c 3.7 KB

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