sha256_base.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * sha256_base.h - core logic for SHA-256 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 (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. sctx->state[0] = SHA224_H0;
  21. sctx->state[1] = SHA224_H1;
  22. sctx->state[2] = SHA224_H2;
  23. sctx->state[3] = SHA224_H3;
  24. sctx->state[4] = SHA224_H4;
  25. sctx->state[5] = SHA224_H5;
  26. sctx->state[6] = SHA224_H6;
  27. sctx->state[7] = SHA224_H7;
  28. sctx->count = 0;
  29. return 0;
  30. }
  31. static inline int sha256_base_init(struct shash_desc *desc)
  32. {
  33. struct sha256_state *sctx = shash_desc_ctx(desc);
  34. sctx->state[0] = SHA256_H0;
  35. sctx->state[1] = SHA256_H1;
  36. sctx->state[2] = SHA256_H2;
  37. sctx->state[3] = SHA256_H3;
  38. sctx->state[4] = SHA256_H4;
  39. sctx->state[5] = SHA256_H5;
  40. sctx->state[6] = SHA256_H6;
  41. sctx->state[7] = SHA256_H7;
  42. sctx->count = 0;
  43. return 0;
  44. }
  45. static inline int sha256_base_do_update(struct shash_desc *desc,
  46. const u8 *data,
  47. unsigned int len,
  48. sha256_block_fn *block_fn)
  49. {
  50. struct sha256_state *sctx = shash_desc_ctx(desc);
  51. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  52. sctx->count += len;
  53. if (unlikely((partial + len) >= SHA256_BLOCK_SIZE)) {
  54. int blocks;
  55. if (partial) {
  56. int p = SHA256_BLOCK_SIZE - partial;
  57. memcpy(sctx->buf + partial, data, p);
  58. data += p;
  59. len -= p;
  60. block_fn(sctx, sctx->buf, 1);
  61. }
  62. blocks = len / SHA256_BLOCK_SIZE;
  63. len %= SHA256_BLOCK_SIZE;
  64. if (blocks) {
  65. block_fn(sctx, data, blocks);
  66. data += blocks * SHA256_BLOCK_SIZE;
  67. }
  68. partial = 0;
  69. }
  70. if (len)
  71. memcpy(sctx->buf + partial, data, len);
  72. return 0;
  73. }
  74. static inline int sha256_base_do_finalize(struct shash_desc *desc,
  75. sha256_block_fn *block_fn)
  76. {
  77. const int bit_offset = SHA256_BLOCK_SIZE - sizeof(__be64);
  78. struct sha256_state *sctx = shash_desc_ctx(desc);
  79. __be64 *bits = (__be64 *)(sctx->buf + bit_offset);
  80. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  81. sctx->buf[partial++] = 0x80;
  82. if (partial > bit_offset) {
  83. memset(sctx->buf + partial, 0x0, SHA256_BLOCK_SIZE - partial);
  84. partial = 0;
  85. block_fn(sctx, sctx->buf, 1);
  86. }
  87. memset(sctx->buf + partial, 0x0, bit_offset - partial);
  88. *bits = cpu_to_be64(sctx->count << 3);
  89. block_fn(sctx, sctx->buf, 1);
  90. return 0;
  91. }
  92. static inline int sha256_base_finish(struct shash_desc *desc, u8 *out)
  93. {
  94. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  95. struct sha256_state *sctx = shash_desc_ctx(desc);
  96. __be32 *digest = (__be32 *)out;
  97. int i;
  98. for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be32))
  99. put_unaligned_be32(sctx->state[i], digest++);
  100. *sctx = (struct sha256_state){};
  101. return 0;
  102. }