sha1_s390.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the SHA1 Secure Hash Algorithm.
  5. *
  6. * Derived from cryptoapi implementation, adapted for in-place
  7. * scatterlist interface. Originally based on the public domain
  8. * implementation written by Steve Reid.
  9. *
  10. * s390 Version:
  11. * Copyright IBM Corp. 2003,2007
  12. * Author(s): Thomas Spatzier
  13. * Jan Glauber (jan.glauber@de.ibm.com)
  14. *
  15. * Derived from "crypto/sha1_generic.c"
  16. * Copyright (c) Alan Smithee.
  17. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  18. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  19. *
  20. * This program is free software; you can redistribute it and/or modify it
  21. * under the terms of the GNU General Public License as published by the Free
  22. * Software Foundation; either version 2 of the License, or (at your option)
  23. * any later version.
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/crypto.h>
  29. #include <crypto/sha.h>
  30. #include "crypt_s390.h"
  31. struct s390_sha1_ctx {
  32. u64 count; /* message length */
  33. u32 state[5];
  34. u8 buf[2 * SHA1_BLOCK_SIZE];
  35. };
  36. static void sha1_init(struct crypto_tfm *tfm)
  37. {
  38. struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
  39. sctx->state[0] = SHA1_H0;
  40. sctx->state[1] = SHA1_H1;
  41. sctx->state[2] = SHA1_H2;
  42. sctx->state[3] = SHA1_H3;
  43. sctx->state[4] = SHA1_H4;
  44. sctx->count = 0;
  45. }
  46. static void sha1_update(struct crypto_tfm *tfm, const u8 *data,
  47. unsigned int len)
  48. {
  49. struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
  50. unsigned int index;
  51. int ret;
  52. /* how much is already in the buffer? */
  53. index = sctx->count & 0x3f;
  54. sctx->count += len;
  55. if (index + len < SHA1_BLOCK_SIZE)
  56. goto store;
  57. /* process one stored block */
  58. if (index) {
  59. memcpy(sctx->buf + index, data, SHA1_BLOCK_SIZE - index);
  60. ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf,
  61. SHA1_BLOCK_SIZE);
  62. BUG_ON(ret != SHA1_BLOCK_SIZE);
  63. data += SHA1_BLOCK_SIZE - index;
  64. len -= SHA1_BLOCK_SIZE - index;
  65. }
  66. /* process as many blocks as possible */
  67. if (len >= SHA1_BLOCK_SIZE) {
  68. ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, data,
  69. len & ~(SHA1_BLOCK_SIZE - 1));
  70. BUG_ON(ret != (len & ~(SHA1_BLOCK_SIZE - 1)));
  71. data += ret;
  72. len -= ret;
  73. }
  74. store:
  75. /* anything left? */
  76. if (len)
  77. memcpy(sctx->buf + index , data, len);
  78. }
  79. /* Add padding and return the message digest. */
  80. static void sha1_final(struct crypto_tfm *tfm, u8 *out)
  81. {
  82. struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
  83. u64 bits;
  84. unsigned int index, end;
  85. int ret;
  86. /* must perform manual padding */
  87. index = sctx->count & 0x3f;
  88. end = (index < 56) ? SHA1_BLOCK_SIZE : (2 * SHA1_BLOCK_SIZE);
  89. /* start pad with 1 */
  90. sctx->buf[index] = 0x80;
  91. /* pad with zeros */
  92. index++;
  93. memset(sctx->buf + index, 0x00, end - index - 8);
  94. /* append message length */
  95. bits = sctx->count * 8;
  96. memcpy(sctx->buf + end - 8, &bits, sizeof(bits));
  97. ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf, end);
  98. BUG_ON(ret != end);
  99. /* copy digest to out */
  100. memcpy(out, sctx->state, SHA1_DIGEST_SIZE);
  101. /* wipe context */
  102. memset(sctx, 0, sizeof *sctx);
  103. }
  104. static struct crypto_alg alg = {
  105. .cra_name = "sha1",
  106. .cra_driver_name= "sha1-s390",
  107. .cra_priority = CRYPT_S390_PRIORITY,
  108. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  109. .cra_blocksize = SHA1_BLOCK_SIZE,
  110. .cra_ctxsize = sizeof(struct s390_sha1_ctx),
  111. .cra_module = THIS_MODULE,
  112. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  113. .cra_u = { .digest = {
  114. .dia_digestsize = SHA1_DIGEST_SIZE,
  115. .dia_init = sha1_init,
  116. .dia_update = sha1_update,
  117. .dia_final = sha1_final } }
  118. };
  119. static int __init init(void)
  120. {
  121. if (!crypt_s390_func_available(KIMD_SHA_1))
  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("sha1");
  132. MODULE_LICENSE("GPL");
  133. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");