sha_common.c 2.4 KB

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