sha256_base.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * sha256_base.h - core logic for SHA-256 implementations
  4. *
  5. * Copyright (C) 2015 Linaro Ltd <[email protected]>
  6. */
  7. #ifndef _CRYPTO_SHA256_BASE_H
  8. #define _CRYPTO_SHA256_BASE_H
  9. #include <crypto/internal/hash.h>
  10. #include <crypto/sha2.h>
  11. #include <linux/crypto.h>
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <asm/unaligned.h>
  15. typedef void (sha256_block_fn)(struct sha256_state *sst, u8 const *src,
  16. int blocks);
  17. static inline int sha224_base_init(struct shash_desc *desc)
  18. {
  19. struct sha256_state *sctx = shash_desc_ctx(desc);
  20. sha224_init(sctx);
  21. return 0;
  22. }
  23. static inline int sha256_base_init(struct shash_desc *desc)
  24. {
  25. struct sha256_state *sctx = shash_desc_ctx(desc);
  26. sha256_init(sctx);
  27. return 0;
  28. }
  29. static inline int sha256_base_do_update(struct shash_desc *desc,
  30. const u8 *data,
  31. unsigned int len,
  32. sha256_block_fn *block_fn)
  33. {
  34. struct sha256_state *sctx = shash_desc_ctx(desc);
  35. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  36. sctx->count += len;
  37. if (unlikely((partial + len) >= SHA256_BLOCK_SIZE)) {
  38. int blocks;
  39. if (partial) {
  40. int p = SHA256_BLOCK_SIZE - partial;
  41. memcpy(sctx->buf + partial, data, p);
  42. data += p;
  43. len -= p;
  44. block_fn(sctx, sctx->buf, 1);
  45. }
  46. blocks = len / SHA256_BLOCK_SIZE;
  47. len %= SHA256_BLOCK_SIZE;
  48. if (blocks) {
  49. block_fn(sctx, data, blocks);
  50. data += blocks * SHA256_BLOCK_SIZE;
  51. }
  52. partial = 0;
  53. }
  54. if (len)
  55. memcpy(sctx->buf + partial, data, len);
  56. return 0;
  57. }
  58. static inline int sha256_base_do_finalize(struct shash_desc *desc,
  59. sha256_block_fn *block_fn)
  60. {
  61. const int bit_offset = SHA256_BLOCK_SIZE - sizeof(__be64);
  62. struct sha256_state *sctx = shash_desc_ctx(desc);
  63. __be64 *bits = (__be64 *)(sctx->buf + bit_offset);
  64. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  65. sctx->buf[partial++] = 0x80;
  66. if (partial > bit_offset) {
  67. memset(sctx->buf + partial, 0x0, SHA256_BLOCK_SIZE - partial);
  68. partial = 0;
  69. block_fn(sctx, sctx->buf, 1);
  70. }
  71. memset(sctx->buf + partial, 0x0, bit_offset - partial);
  72. *bits = cpu_to_be64(sctx->count << 3);
  73. block_fn(sctx, sctx->buf, 1);
  74. return 0;
  75. }
  76. static inline int sha256_base_finish(struct shash_desc *desc, u8 *out)
  77. {
  78. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  79. struct sha256_state *sctx = shash_desc_ctx(desc);
  80. __be32 *digest = (__be32 *)out;
  81. int i;
  82. for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be32))
  83. put_unaligned_be32(sctx->state[i], digest++);
  84. memzero_explicit(sctx, sizeof(*sctx));
  85. return 0;
  86. }
  87. #endif /* _CRYPTO_SHA256_BASE_H */