sha1_base.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * sha1_base.h - core logic for SHA-1 implementations
  3. *
  4. * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/sha.h>
  12. #include <linux/crypto.h>
  13. #include <linux/module.h>
  14. #include <asm/unaligned.h>
  15. typedef void (sha1_block_fn)(struct sha1_state *sst, u8 const *src, int blocks);
  16. static inline int sha1_base_init(struct shash_desc *desc)
  17. {
  18. struct sha1_state *sctx = shash_desc_ctx(desc);
  19. sctx->state[0] = SHA1_H0;
  20. sctx->state[1] = SHA1_H1;
  21. sctx->state[2] = SHA1_H2;
  22. sctx->state[3] = SHA1_H3;
  23. sctx->state[4] = SHA1_H4;
  24. sctx->count = 0;
  25. return 0;
  26. }
  27. static inline int sha1_base_do_update(struct shash_desc *desc,
  28. const u8 *data,
  29. unsigned int len,
  30. sha1_block_fn *block_fn)
  31. {
  32. struct sha1_state *sctx = shash_desc_ctx(desc);
  33. unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
  34. sctx->count += len;
  35. if (unlikely((partial + len) >= SHA1_BLOCK_SIZE)) {
  36. int blocks;
  37. if (partial) {
  38. int p = SHA1_BLOCK_SIZE - partial;
  39. memcpy(sctx->buffer + partial, data, p);
  40. data += p;
  41. len -= p;
  42. block_fn(sctx, sctx->buffer, 1);
  43. }
  44. blocks = len / SHA1_BLOCK_SIZE;
  45. len %= SHA1_BLOCK_SIZE;
  46. if (blocks) {
  47. block_fn(sctx, data, blocks);
  48. data += blocks * SHA1_BLOCK_SIZE;
  49. }
  50. partial = 0;
  51. }
  52. if (len)
  53. memcpy(sctx->buffer + partial, data, len);
  54. return 0;
  55. }
  56. static inline int sha1_base_do_finalize(struct shash_desc *desc,
  57. sha1_block_fn *block_fn)
  58. {
  59. const int bit_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
  60. struct sha1_state *sctx = shash_desc_ctx(desc);
  61. __be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
  62. unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
  63. sctx->buffer[partial++] = 0x80;
  64. if (partial > bit_offset) {
  65. memset(sctx->buffer + partial, 0x0, SHA1_BLOCK_SIZE - partial);
  66. partial = 0;
  67. block_fn(sctx, sctx->buffer, 1);
  68. }
  69. memset(sctx->buffer + partial, 0x0, bit_offset - partial);
  70. *bits = cpu_to_be64(sctx->count << 3);
  71. block_fn(sctx, sctx->buffer, 1);
  72. return 0;
  73. }
  74. static inline int sha1_base_finish(struct shash_desc *desc, u8 *out)
  75. {
  76. struct sha1_state *sctx = shash_desc_ctx(desc);
  77. __be32 *digest = (__be32 *)out;
  78. int i;
  79. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
  80. put_unaligned_be32(sctx->state[i], digest++);
  81. *sctx = (struct sha1_state){};
  82. return 0;
  83. }