sha_common.c 2.5 KB

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