sha_common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "sha.h"
  17. #include "crypt_s390.h"
  18. int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
  19. {
  20. struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
  21. unsigned int bsize = crypto_shash_blocksize(desc->tfm);
  22. unsigned int index;
  23. int ret;
  24. /* how much is already in the buffer? */
  25. index = ctx->count & (bsize - 1);
  26. ctx->count += len;
  27. if ((index + len) < bsize)
  28. goto store;
  29. /* process one stored block */
  30. if (index) {
  31. memcpy(ctx->buf + index, data, bsize - index);
  32. ret = crypt_s390_kimd(ctx->func, ctx->state, ctx->buf, bsize);
  33. BUG_ON(ret != bsize);
  34. data += bsize - index;
  35. len -= bsize - index;
  36. index = 0;
  37. }
  38. /* process as many blocks as possible */
  39. if (len >= bsize) {
  40. ret = crypt_s390_kimd(ctx->func, ctx->state, data,
  41. len & ~(bsize - 1));
  42. BUG_ON(ret != (len & ~(bsize - 1)));
  43. data += ret;
  44. len -= ret;
  45. }
  46. store:
  47. if (len)
  48. memcpy(ctx->buf + index , data, len);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL_GPL(s390_sha_update);
  52. int s390_sha_final(struct shash_desc *desc, u8 *out)
  53. {
  54. struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
  55. unsigned int bsize = crypto_shash_blocksize(desc->tfm);
  56. u64 bits;
  57. unsigned int index, end, plen;
  58. int ret;
  59. /* SHA-512 uses 128 bit padding length */
  60. plen = (bsize > SHA256_BLOCK_SIZE) ? 16 : 8;
  61. /* must perform manual padding */
  62. index = ctx->count & (bsize - 1);
  63. end = (index < bsize - plen) ? bsize : (2 * bsize);
  64. /* start pad with 1 */
  65. ctx->buf[index] = 0x80;
  66. index++;
  67. /* pad with zeros */
  68. memset(ctx->buf + index, 0x00, end - index - 8);
  69. /*
  70. * Append message length. Well, SHA-512 wants a 128 bit length value,
  71. * nevertheless we use u64, should be enough for now...
  72. */
  73. bits = ctx->count * 8;
  74. memcpy(ctx->buf + end - 8, &bits, sizeof(bits));
  75. ret = crypt_s390_kimd(ctx->func, ctx->state, ctx->buf, end);
  76. BUG_ON(ret != end);
  77. /* copy digest to out */
  78. memcpy(out, ctx->state, crypto_shash_digestsize(desc->tfm));
  79. /* wipe context */
  80. memset(ctx, 0, sizeof *ctx);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(s390_sha_final);
  84. MODULE_LICENSE("GPL");
  85. MODULE_DESCRIPTION("s390 SHA cipher common functions");