sha256_s390.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the SHA256 Secure Hash Algorithm.
  5. *
  6. * s390 Version:
  7. * Copyright IBM Corp. 2005,2007
  8. * Author(s): Jan Glauber (jang@de.ibm.com)
  9. *
  10. * Derived from "crypto/sha256_generic.c"
  11. * and "arch/s390/crypto/sha1_s390.c"
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. *
  18. */
  19. #include <crypto/internal/hash.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <crypto/sha.h>
  23. #include "crypt_s390.h"
  24. #include "sha.h"
  25. static int sha256_init(struct shash_desc *desc)
  26. {
  27. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  28. sctx->state[0] = SHA256_H0;
  29. sctx->state[1] = SHA256_H1;
  30. sctx->state[2] = SHA256_H2;
  31. sctx->state[3] = SHA256_H3;
  32. sctx->state[4] = SHA256_H4;
  33. sctx->state[5] = SHA256_H5;
  34. sctx->state[6] = SHA256_H6;
  35. sctx->state[7] = SHA256_H7;
  36. sctx->count = 0;
  37. sctx->func = KIMD_SHA_256;
  38. return 0;
  39. }
  40. static int sha256_export(struct shash_desc *desc, void *out)
  41. {
  42. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  43. struct sha256_state *octx = out;
  44. octx->count = sctx->count;
  45. memcpy(octx->state, sctx->state, sizeof(octx->state));
  46. memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
  47. return 0;
  48. }
  49. static int sha256_import(struct shash_desc *desc, const void *in)
  50. {
  51. struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
  52. const struct sha256_state *ictx = in;
  53. sctx->count = ictx->count;
  54. memcpy(sctx->state, ictx->state, sizeof(ictx->state));
  55. memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
  56. sctx->func = KIMD_SHA_256;
  57. return 0;
  58. }
  59. static struct shash_alg alg = {
  60. .digestsize = SHA256_DIGEST_SIZE,
  61. .init = sha256_init,
  62. .update = s390_sha_update,
  63. .final = s390_sha_final,
  64. .export = sha256_export,
  65. .import = sha256_import,
  66. .descsize = sizeof(struct s390_sha_ctx),
  67. .statesize = sizeof(struct sha256_state),
  68. .base = {
  69. .cra_name = "sha256",
  70. .cra_driver_name= "sha256-s390",
  71. .cra_priority = CRYPT_S390_PRIORITY,
  72. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  73. .cra_blocksize = SHA256_BLOCK_SIZE,
  74. .cra_module = THIS_MODULE,
  75. }
  76. };
  77. static int sha256_s390_init(void)
  78. {
  79. if (!crypt_s390_func_available(KIMD_SHA_256, CRYPT_S390_MSA))
  80. return -EOPNOTSUPP;
  81. return crypto_register_shash(&alg);
  82. }
  83. static void __exit sha256_s390_fini(void)
  84. {
  85. crypto_unregister_shash(&alg);
  86. }
  87. module_init(sha256_s390_init);
  88. module_exit(sha256_s390_fini);
  89. MODULE_ALIAS("sha256");
  90. MODULE_LICENSE("GPL");
  91. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");