sha1_s390.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Cryptographic API.
  4. *
  5. * s390 implementation of the SHA1 Secure Hash Algorithm.
  6. *
  7. * Derived from cryptoapi implementation, adapted for in-place
  8. * scatterlist interface. Originally based on the public domain
  9. * implementation written by Steve Reid.
  10. *
  11. * s390 Version:
  12. * Copyright IBM Corp. 2003, 2007
  13. * Author(s): Thomas Spatzier
  14. * Jan Glauber ([email protected])
  15. *
  16. * Derived from "crypto/sha1_generic.c"
  17. * Copyright (c) Alan Smithee.
  18. * Copyright (c) Andrew McDonald <[email protected]>
  19. * Copyright (c) Jean-Francois Dive <[email protected]>
  20. */
  21. #include <crypto/internal/hash.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/cpufeature.h>
  25. #include <crypto/sha1.h>
  26. #include <asm/cpacf.h>
  27. #include "sha.h"
  28. static int s390_sha1_init(struct shash_desc *desc)
  29. {
  30. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  31. sctx->state[0] = SHA1_H0;
  32. sctx->state[1] = SHA1_H1;
  33. sctx->state[2] = SHA1_H2;
  34. sctx->state[3] = SHA1_H3;
  35. sctx->state[4] = SHA1_H4;
  36. sctx->count = 0;
  37. sctx->func = CPACF_KIMD_SHA_1;
  38. return 0;
  39. }
  40. static int s390_sha1_export(struct shash_desc *desc, void *out)
  41. {
  42. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  43. struct sha1_state *octx = out;
  44. octx->count = sctx->count;
  45. memcpy(octx->state, sctx->state, sizeof(octx->state));
  46. memcpy(octx->buffer, sctx->buf, sizeof(octx->buffer));
  47. return 0;
  48. }
  49. static int s390_sha1_import(struct shash_desc *desc, const void *in)
  50. {
  51. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  52. const struct sha1_state *ictx = in;
  53. sctx->count = ictx->count;
  54. memcpy(sctx->state, ictx->state, sizeof(ictx->state));
  55. memcpy(sctx->buf, ictx->buffer, sizeof(ictx->buffer));
  56. sctx->func = CPACF_KIMD_SHA_1;
  57. return 0;
  58. }
  59. static struct shash_alg alg = {
  60. .digestsize = SHA1_DIGEST_SIZE,
  61. .init = s390_sha1_init,
  62. .update = s390_sha_update,
  63. .final = s390_sha_final,
  64. .export = s390_sha1_export,
  65. .import = s390_sha1_import,
  66. .descsize = sizeof(struct s390_sha_ctx),
  67. .statesize = sizeof(struct sha1_state),
  68. .base = {
  69. .cra_name = "sha1",
  70. .cra_driver_name= "sha1-s390",
  71. .cra_priority = 300,
  72. .cra_blocksize = SHA1_BLOCK_SIZE,
  73. .cra_module = THIS_MODULE,
  74. }
  75. };
  76. static int __init sha1_s390_init(void)
  77. {
  78. if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_1))
  79. return -ENODEV;
  80. return crypto_register_shash(&alg);
  81. }
  82. static void __exit sha1_s390_fini(void)
  83. {
  84. crypto_unregister_shash(&alg);
  85. }
  86. module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha1_s390_init);
  87. module_exit(sha1_s390_fini);
  88. MODULE_ALIAS_CRYPTO("sha1");
  89. MODULE_LICENSE("GPL");
  90. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");