sha1_s390.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 (C) 2003 IBM Deutschland GmbH, IBM Corporation
  12. * Author(s): Thomas Spatzier (tspat@de.ibm.com)
  13. *
  14. * Derived from "crypto/sha1.c"
  15. * Copyright (c) Alan Smithee.
  16. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  17. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the Free
  21. * Software Foundation; either version 2 of the License, or (at your option)
  22. * any later version.
  23. *
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/mm.h>
  28. #include <linux/crypto.h>
  29. #include <asm/scatterlist.h>
  30. #include <asm/byteorder.h>
  31. #include "crypt_s390.h"
  32. #define SHA1_DIGEST_SIZE 20
  33. #define SHA1_BLOCK_SIZE 64
  34. struct crypt_s390_sha1_ctx {
  35. u64 count;
  36. u32 state[5];
  37. u32 buf_len;
  38. u8 buffer[2 * SHA1_BLOCK_SIZE];
  39. };
  40. static void sha1_init(struct crypto_tfm *tfm)
  41. {
  42. struct crypt_s390_sha1_ctx *ctx = crypto_tfm_ctx(tfm);
  43. static const u32 initstate[5] = {
  44. 0x67452301,
  45. 0xEFCDAB89,
  46. 0x98BADCFE,
  47. 0x10325476,
  48. 0xC3D2E1F0
  49. };
  50. ctx->count = 0;
  51. memcpy(ctx->state, &initstate, sizeof(initstate));
  52. ctx->buf_len = 0;
  53. }
  54. static void sha1_update(struct crypto_tfm *tfm, const u8 *data,
  55. unsigned int len)
  56. {
  57. struct crypt_s390_sha1_ctx *sctx;
  58. long imd_len;
  59. sctx = crypto_tfm_ctx(tfm);
  60. sctx->count += len * 8; //message bit length
  61. //anything in buffer yet? -> must be completed
  62. if (sctx->buf_len && (sctx->buf_len + len) >= SHA1_BLOCK_SIZE) {
  63. //complete full block and hash
  64. memcpy(sctx->buffer + sctx->buf_len, data,
  65. SHA1_BLOCK_SIZE - sctx->buf_len);
  66. crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buffer,
  67. SHA1_BLOCK_SIZE);
  68. data += SHA1_BLOCK_SIZE - sctx->buf_len;
  69. len -= SHA1_BLOCK_SIZE - sctx->buf_len;
  70. sctx->buf_len = 0;
  71. }
  72. //rest of data contains full blocks?
  73. imd_len = len & ~0x3ful;
  74. if (imd_len){
  75. crypt_s390_kimd(KIMD_SHA_1, sctx->state, data, imd_len);
  76. data += imd_len;
  77. len -= imd_len;
  78. }
  79. //anything left? store in buffer
  80. if (len){
  81. memcpy(sctx->buffer + sctx->buf_len , data, len);
  82. sctx->buf_len += len;
  83. }
  84. }
  85. static void
  86. pad_message(struct crypt_s390_sha1_ctx* sctx)
  87. {
  88. int index;
  89. index = sctx->buf_len;
  90. sctx->buf_len = (sctx->buf_len < 56)?
  91. SHA1_BLOCK_SIZE:2 * SHA1_BLOCK_SIZE;
  92. //start pad with 1
  93. sctx->buffer[index] = 0x80;
  94. //pad with zeros
  95. index++;
  96. memset(sctx->buffer + index, 0x00, sctx->buf_len - index);
  97. //append length
  98. memcpy(sctx->buffer + sctx->buf_len - 8, &sctx->count,
  99. sizeof sctx->count);
  100. }
  101. /* Add padding and return the message digest. */
  102. static void sha1_final(struct crypto_tfm *tfm, u8 *out)
  103. {
  104. struct crypt_s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
  105. //must perform manual padding
  106. pad_message(sctx);
  107. crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buffer, sctx->buf_len);
  108. //copy digest to out
  109. memcpy(out, sctx->state, SHA1_DIGEST_SIZE);
  110. /* Wipe context */
  111. memset(sctx, 0, sizeof *sctx);
  112. }
  113. static struct crypto_alg alg = {
  114. .cra_name = "sha1",
  115. .cra_driver_name = "sha1-s390",
  116. .cra_priority = CRYPT_S390_PRIORITY,
  117. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  118. .cra_blocksize = SHA1_BLOCK_SIZE,
  119. .cra_ctxsize = sizeof(struct crypt_s390_sha1_ctx),
  120. .cra_module = THIS_MODULE,
  121. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  122. .cra_u = { .digest = {
  123. .dia_digestsize = SHA1_DIGEST_SIZE,
  124. .dia_init = sha1_init,
  125. .dia_update = sha1_update,
  126. .dia_final = sha1_final } }
  127. };
  128. static int
  129. init(void)
  130. {
  131. int ret = -ENOSYS;
  132. if (crypt_s390_func_available(KIMD_SHA_1)){
  133. ret = crypto_register_alg(&alg);
  134. if (ret == 0){
  135. printk(KERN_INFO "crypt_s390: sha1_s390 loaded.\n");
  136. }
  137. }
  138. return ret;
  139. }
  140. static void __exit
  141. fini(void)
  142. {
  143. crypto_unregister_alg(&alg);
  144. }
  145. module_init(init);
  146. module_exit(fini);
  147. MODULE_ALIAS("sha1");
  148. MODULE_LICENSE("GPL");
  149. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");