sha_common.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 generic implementation of the SHA Secure Hash Algorithms.
  5. *
  6. * Copyright IBM Corp. 2007
  7. * Author(s): Jan Glauber (jang@de.ibm.com)
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/internal/hash.h>
  16. #include <linux/module.h>
  17. #include "sha.h"
  18. #include "crypt_s390.h"
  19. int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
  20. {
  21. struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
  22. unsigned int bsize = crypto_shash_blocksize(desc->tfm);
  23. unsigned int index;
  24. int ret;
  25. /* how much is already in the buffer? */
  26. index = ctx->count & (bsize - 1);
  27. ctx->count += len;
  28. if ((index + len) < bsize)
  29. goto store;
  30. /* process one stored block */
  31. if (index) {
  32. memcpy(ctx->buf + index, data, bsize - index);
  33. ret = crypt_s390_kimd(ctx->func, ctx->state, ctx->buf, bsize);
  34. if (ret != bsize)
  35. return -EIO;
  36. data += bsize - index;
  37. len -= bsize - index;
  38. index = 0;
  39. }
  40. /* process as many blocks as possible */
  41. if (len >= bsize) {
  42. ret = crypt_s390_kimd(ctx->func, ctx->state, data,
  43. len & ~(bsize - 1));
  44. if (ret != (len & ~(bsize - 1)))
  45. return -EIO;
  46. data += ret;
  47. len -= ret;
  48. }
  49. store:
  50. if (len)
  51. memcpy(ctx->buf + index , data, len);
  52. return 0;
  53. }
  54. EXPORT_SYMBOL_GPL(s390_sha_update);
  55. int s390_sha_final(struct shash_desc *desc, u8 *out)
  56. {
  57. struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
  58. unsigned int bsize = crypto_shash_blocksize(desc->tfm);
  59. u64 bits;
  60. unsigned int index, end, plen;
  61. int ret;
  62. /* SHA-512 uses 128 bit padding length */
  63. plen = (bsize > SHA256_BLOCK_SIZE) ? 16 : 8;
  64. /* must perform manual padding */
  65. index = ctx->count & (bsize - 1);
  66. end = (index < bsize - plen) ? bsize : (2 * bsize);
  67. /* start pad with 1 */
  68. ctx->buf[index] = 0x80;
  69. index++;
  70. /* pad with zeros */
  71. memset(ctx->buf + index, 0x00, end - index - 8);
  72. /*
  73. * Append message length. Well, SHA-512 wants a 128 bit length value,
  74. * nevertheless we use u64, should be enough for now...
  75. */
  76. bits = ctx->count * 8;
  77. memcpy(ctx->buf + end - 8, &bits, sizeof(bits));
  78. ret = crypt_s390_kimd(ctx->func, ctx->state, ctx->buf, end);
  79. if (ret != end)
  80. return -EIO;
  81. /* copy digest to out */
  82. memcpy(out, ctx->state, crypto_shash_digestsize(desc->tfm));
  83. /* wipe context */
  84. memset(ctx, 0, sizeof *ctx);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(s390_sha_final);
  88. MODULE_LICENSE("GPL");
  89. MODULE_DESCRIPTION("s390 SHA cipher common functions");