sm3_base.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * sm3_base.h - core logic for SM3 implementations
  3. *
  4. * Copyright (C) 2017 ARM Limited or its affiliates.
  5. * Written by Gilad Ben-Yossef <gilad@benyossef.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <crypto/internal/hash.h>
  20. #include <crypto/sm3.h>
  21. #include <linux/crypto.h>
  22. #include <linux/module.h>
  23. #include <asm/unaligned.h>
  24. typedef void (sm3_block_fn)(struct sm3_state *sst, u8 const *src, int blocks);
  25. static inline int sm3_base_init(struct shash_desc *desc)
  26. {
  27. struct sm3_state *sctx = shash_desc_ctx(desc);
  28. sctx->state[0] = SM3_IVA;
  29. sctx->state[1] = SM3_IVB;
  30. sctx->state[2] = SM3_IVC;
  31. sctx->state[3] = SM3_IVD;
  32. sctx->state[4] = SM3_IVE;
  33. sctx->state[5] = SM3_IVF;
  34. sctx->state[6] = SM3_IVG;
  35. sctx->state[7] = SM3_IVH;
  36. sctx->count = 0;
  37. return 0;
  38. }
  39. static inline int sm3_base_do_update(struct shash_desc *desc,
  40. const u8 *data,
  41. unsigned int len,
  42. sm3_block_fn *block_fn)
  43. {
  44. struct sm3_state *sctx = shash_desc_ctx(desc);
  45. unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
  46. sctx->count += len;
  47. if (unlikely((partial + len) >= SM3_BLOCK_SIZE)) {
  48. int blocks;
  49. if (partial) {
  50. int p = SM3_BLOCK_SIZE - partial;
  51. memcpy(sctx->buffer + partial, data, p);
  52. data += p;
  53. len -= p;
  54. block_fn(sctx, sctx->buffer, 1);
  55. }
  56. blocks = len / SM3_BLOCK_SIZE;
  57. len %= SM3_BLOCK_SIZE;
  58. if (blocks) {
  59. block_fn(sctx, data, blocks);
  60. data += blocks * SM3_BLOCK_SIZE;
  61. }
  62. partial = 0;
  63. }
  64. if (len)
  65. memcpy(sctx->buffer + partial, data, len);
  66. return 0;
  67. }
  68. static inline int sm3_base_do_finalize(struct shash_desc *desc,
  69. sm3_block_fn *block_fn)
  70. {
  71. const int bit_offset = SM3_BLOCK_SIZE - sizeof(__be64);
  72. struct sm3_state *sctx = shash_desc_ctx(desc);
  73. __be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
  74. unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
  75. sctx->buffer[partial++] = 0x80;
  76. if (partial > bit_offset) {
  77. memset(sctx->buffer + partial, 0x0, SM3_BLOCK_SIZE - partial);
  78. partial = 0;
  79. block_fn(sctx, sctx->buffer, 1);
  80. }
  81. memset(sctx->buffer + partial, 0x0, bit_offset - partial);
  82. *bits = cpu_to_be64(sctx->count << 3);
  83. block_fn(sctx, sctx->buffer, 1);
  84. return 0;
  85. }
  86. static inline int sm3_base_finish(struct shash_desc *desc, u8 *out)
  87. {
  88. struct sm3_state *sctx = shash_desc_ctx(desc);
  89. __be32 *digest = (__be32 *)out;
  90. int i;
  91. for (i = 0; i < SM3_DIGEST_SIZE / sizeof(__be32); i++)
  92. put_unaligned_be32(sctx->state[i], digest++);
  93. *sctx = (struct sm3_state){};
  94. return 0;
  95. }