sha_common.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <linux/crypto.h>
  16. #include "sha.h"
  17. #include "crypt_s390.h"
  18. void s390_sha_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
  19. {
  20. struct s390_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  21. unsigned int bsize = crypto_tfm_alg_blocksize(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. }
  49. EXPORT_SYMBOL_GPL(s390_sha_update);
  50. void s390_sha_final(struct crypto_tfm *tfm, u8 *out)
  51. {
  52. struct s390_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  53. unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
  54. u64 bits;
  55. unsigned int index, end, plen;
  56. int ret;
  57. /* SHA-512 uses 128 bit padding length */
  58. plen = (bsize > SHA256_BLOCK_SIZE) ? 16 : 8;
  59. /* must perform manual padding */
  60. index = ctx->count & (bsize - 1);
  61. end = (index < bsize - plen) ? bsize : (2 * bsize);
  62. /* start pad with 1 */
  63. ctx->buf[index] = 0x80;
  64. index++;
  65. /* pad with zeros */
  66. memset(ctx->buf + index, 0x00, end - index - 8);
  67. /*
  68. * Append message length. Well, SHA-512 wants a 128 bit lenght value,
  69. * nevertheless we use u64, should be enough for now...
  70. */
  71. bits = ctx->count * 8;
  72. memcpy(ctx->buf + end - 8, &bits, sizeof(bits));
  73. ret = crypt_s390_kimd(ctx->func, ctx->state, ctx->buf, end);
  74. BUG_ON(ret != end);
  75. /* copy digest to out */
  76. memcpy(out, ctx->state, crypto_hash_digestsize(crypto_hash_cast(tfm)));
  77. /* wipe context */
  78. memset(ctx, 0, sizeof *ctx);
  79. }
  80. EXPORT_SYMBOL_GPL(s390_sha_final);
  81. MODULE_LICENSE("GPL");
  82. MODULE_DESCRIPTION("s390 SHA cipher common functions");