sha1_s390.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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
  41. sha1_init(void *ctx)
  42. {
  43. static const struct crypt_s390_sha1_ctx initstate = {
  44. .state = {
  45. 0x67452301,
  46. 0xEFCDAB89,
  47. 0x98BADCFE,
  48. 0x10325476,
  49. 0xC3D2E1F0
  50. },
  51. };
  52. memcpy(ctx, &initstate, sizeof(initstate));
  53. }
  54. static void
  55. sha1_update(void *ctx, const u8 *data, unsigned int len)
  56. {
  57. struct crypt_s390_sha1_ctx *sctx;
  58. long imd_len;
  59. sctx = ctx;
  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
  103. sha1_final(void* ctx, u8 *out)
  104. {
  105. struct crypt_s390_sha1_ctx *sctx = ctx;
  106. //must perform manual padding
  107. pad_message(sctx);
  108. crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buffer, sctx->buf_len);
  109. //copy digest to out
  110. memcpy(out, sctx->state, SHA1_DIGEST_SIZE);
  111. /* Wipe context */
  112. memset(sctx, 0, sizeof *sctx);
  113. }
  114. static struct crypto_alg alg = {
  115. .cra_name = "sha1",
  116. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  117. .cra_blocksize = SHA1_BLOCK_SIZE,
  118. .cra_ctxsize = sizeof(struct crypt_s390_sha1_ctx),
  119. .cra_module = THIS_MODULE,
  120. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  121. .cra_u = { .digest = {
  122. .dia_digestsize = SHA1_DIGEST_SIZE,
  123. .dia_init = sha1_init,
  124. .dia_update = sha1_update,
  125. .dia_final = sha1_final } }
  126. };
  127. static int
  128. init(void)
  129. {
  130. int ret = -ENOSYS;
  131. if (crypt_s390_func_available(KIMD_SHA_1)){
  132. ret = crypto_register_alg(&alg);
  133. if (ret == 0){
  134. printk(KERN_INFO "crypt_s390: sha1_s390 loaded.\n");
  135. }
  136. }
  137. return ret;
  138. }
  139. static void __exit
  140. fini(void)
  141. {
  142. crypto_unregister_alg(&alg);
  143. }
  144. module_init(init);
  145. module_exit(fini);
  146. MODULE_ALIAS("sha1");
  147. MODULE_LICENSE("GPL");
  148. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");