sha256_s390.c 3.7 KB

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