sha256_s390.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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;
  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 / 8 & 0x3f;
  51. /* update message bit length */
  52. sctx->count += len * 8;
  53. if ((index + len) < SHA256_BLOCK_SIZE)
  54. goto store;
  55. /* process one stored block */
  56. if (index) {
  57. memcpy(sctx->buf + index, data, SHA256_BLOCK_SIZE - index);
  58. ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
  59. SHA256_BLOCK_SIZE);
  60. BUG_ON(ret != SHA256_BLOCK_SIZE);
  61. data += SHA256_BLOCK_SIZE - index;
  62. len -= SHA256_BLOCK_SIZE - index;
  63. }
  64. /* process as many blocks as possible */
  65. if (len >= SHA256_BLOCK_SIZE) {
  66. ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, data,
  67. len & ~(SHA256_BLOCK_SIZE - 1));
  68. BUG_ON(ret != (len & ~(SHA256_BLOCK_SIZE - 1)));
  69. data += ret;
  70. len -= ret;
  71. }
  72. store:
  73. /* anything left? */
  74. if (len)
  75. memcpy(sctx->buf + index , data, len);
  76. }
  77. static void pad_message(struct s390_sha256_ctx* sctx)
  78. {
  79. int index, end;
  80. index = sctx->count / 8 & 0x3f;
  81. end = index < 56 ? SHA256_BLOCK_SIZE : 2 * SHA256_BLOCK_SIZE;
  82. /* start pad with 1 */
  83. sctx->buf[index] = 0x80;
  84. /* pad with zeros */
  85. index++;
  86. memset(sctx->buf + index, 0x00, end - index - 8);
  87. /* append message length */
  88. memcpy(sctx->buf + end - 8, &sctx->count, sizeof sctx->count);
  89. sctx->count = end * 8;
  90. }
  91. /* Add padding and return the message digest */
  92. static void sha256_final(struct crypto_tfm *tfm, u8 *out)
  93. {
  94. struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
  95. /* must perform manual padding */
  96. pad_message(sctx);
  97. crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
  98. sctx->count / 8);
  99. /* copy digest to out */
  100. memcpy(out, sctx->state, SHA256_DIGEST_SIZE);
  101. /* wipe context */
  102. memset(sctx, 0, sizeof *sctx);
  103. }
  104. static struct crypto_alg alg = {
  105. .cra_name = "sha256",
  106. .cra_driver_name = "sha256-s390",
  107. .cra_priority = CRYPT_S390_PRIORITY,
  108. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  109. .cra_blocksize = SHA256_BLOCK_SIZE,
  110. .cra_ctxsize = sizeof(struct s390_sha256_ctx),
  111. .cra_module = THIS_MODULE,
  112. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  113. .cra_u = { .digest = {
  114. .dia_digestsize = SHA256_DIGEST_SIZE,
  115. .dia_init = sha256_init,
  116. .dia_update = sha256_update,
  117. .dia_final = sha256_final } }
  118. };
  119. static int init(void)
  120. {
  121. if (!crypt_s390_func_available(KIMD_SHA_256))
  122. return -EOPNOTSUPP;
  123. return crypto_register_alg(&alg);
  124. }
  125. static void __exit fini(void)
  126. {
  127. crypto_unregister_alg(&alg);
  128. }
  129. module_init(init);
  130. module_exit(fini);
  131. MODULE_ALIAS("sha256");
  132. MODULE_LICENSE("GPL");
  133. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");