sha1_s390.c 3.8 KB

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