sha256_s390.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/crypto.h>
  22. #include <crypto/sha.h>
  23. #include "crypt_s390.h"
  24. struct s390_sha256_ctx {
  25. u64 count; /* message length */
  26. u32 state[8];
  27. u8 buf[2 * SHA256_BLOCK_SIZE];
  28. };
  29. static void sha256_init(struct crypto_tfm *tfm)
  30. {
  31. struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
  32. sctx->state[0] = SHA256_H0;
  33. sctx->state[1] = SHA256_H1;
  34. sctx->state[2] = SHA256_H2;
  35. sctx->state[3] = SHA256_H3;
  36. sctx->state[4] = SHA256_H4;
  37. sctx->state[5] = SHA256_H5;
  38. sctx->state[6] = SHA256_H6;
  39. sctx->state[7] = SHA256_H7;
  40. sctx->count = 0;
  41. }
  42. static void sha256_update(struct crypto_tfm *tfm, const u8 *data,
  43. unsigned int len)
  44. {
  45. struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
  46. unsigned int index;
  47. int ret;
  48. /* how much is already in the buffer? */
  49. index = sctx->count & 0x3f;
  50. sctx->count += len;
  51. if ((index + len) < SHA256_BLOCK_SIZE)
  52. goto store;
  53. /* process one stored block */
  54. if (index) {
  55. memcpy(sctx->buf + index, data, SHA256_BLOCK_SIZE - index);
  56. ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
  57. SHA256_BLOCK_SIZE);
  58. BUG_ON(ret != SHA256_BLOCK_SIZE);
  59. data += SHA256_BLOCK_SIZE - index;
  60. len -= SHA256_BLOCK_SIZE - index;
  61. }
  62. /* process as many blocks as possible */
  63. if (len >= SHA256_BLOCK_SIZE) {
  64. ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, data,
  65. len & ~(SHA256_BLOCK_SIZE - 1));
  66. BUG_ON(ret != (len & ~(SHA256_BLOCK_SIZE - 1)));
  67. data += ret;
  68. len -= ret;
  69. }
  70. store:
  71. /* anything left? */
  72. if (len)
  73. memcpy(sctx->buf + index , data, len);
  74. }
  75. /* Add padding and return the message digest */
  76. static void sha256_final(struct crypto_tfm *tfm, u8 *out)
  77. {
  78. struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
  79. u64 bits;
  80. unsigned int index, end;
  81. int ret;
  82. /* must perform manual padding */
  83. index = sctx->count & 0x3f;
  84. end = (index < 56) ? SHA256_BLOCK_SIZE : (2 * SHA256_BLOCK_SIZE);
  85. /* start pad with 1 */
  86. sctx->buf[index] = 0x80;
  87. /* pad with zeros */
  88. index++;
  89. memset(sctx->buf + index, 0x00, end - index - 8);
  90. /* append message length */
  91. bits = sctx->count * 8;
  92. memcpy(sctx->buf + end - 8, &bits, sizeof(bits));
  93. ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf, end);
  94. BUG_ON(ret != end);
  95. /* copy digest to out */
  96. memcpy(out, sctx->state, SHA256_DIGEST_SIZE);
  97. /* wipe context */
  98. memset(sctx, 0, sizeof *sctx);
  99. }
  100. static struct crypto_alg alg = {
  101. .cra_name = "sha256",
  102. .cra_driver_name = "sha256-s390",
  103. .cra_priority = CRYPT_S390_PRIORITY,
  104. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  105. .cra_blocksize = SHA256_BLOCK_SIZE,
  106. .cra_ctxsize = sizeof(struct s390_sha256_ctx),
  107. .cra_module = THIS_MODULE,
  108. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  109. .cra_u = { .digest = {
  110. .dia_digestsize = SHA256_DIGEST_SIZE,
  111. .dia_init = sha256_init,
  112. .dia_update = sha256_update,
  113. .dia_final = sha256_final } }
  114. };
  115. static int init(void)
  116. {
  117. if (!crypt_s390_func_available(KIMD_SHA_256))
  118. return -EOPNOTSUPP;
  119. return crypto_register_alg(&alg);
  120. }
  121. static void __exit fini(void)
  122. {
  123. crypto_unregister_alg(&alg);
  124. }
  125. module_init(init);
  126. module_exit(fini);
  127. MODULE_ALIAS("sha256");
  128. MODULE_LICENSE("GPL");
  129. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");