sha1_s390.c 4.0 KB

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