sha512_base.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * sha512_base.h - core logic for SHA-512 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 (sha512_block_fn)(struct sha512_state *sst, u8 const *src,
  16. int blocks);
  17. static inline int sha384_base_init(struct shash_desc *desc)
  18. {
  19. struct sha512_state *sctx = shash_desc_ctx(desc);
  20. sctx->state[0] = SHA384_H0;
  21. sctx->state[1] = SHA384_H1;
  22. sctx->state[2] = SHA384_H2;
  23. sctx->state[3] = SHA384_H3;
  24. sctx->state[4] = SHA384_H4;
  25. sctx->state[5] = SHA384_H5;
  26. sctx->state[6] = SHA384_H6;
  27. sctx->state[7] = SHA384_H7;
  28. sctx->count[0] = sctx->count[1] = 0;
  29. return 0;
  30. }
  31. static inline int sha512_base_init(struct shash_desc *desc)
  32. {
  33. struct sha512_state *sctx = shash_desc_ctx(desc);
  34. sctx->state[0] = SHA512_H0;
  35. sctx->state[1] = SHA512_H1;
  36. sctx->state[2] = SHA512_H2;
  37. sctx->state[3] = SHA512_H3;
  38. sctx->state[4] = SHA512_H4;
  39. sctx->state[5] = SHA512_H5;
  40. sctx->state[6] = SHA512_H6;
  41. sctx->state[7] = SHA512_H7;
  42. sctx->count[0] = sctx->count[1] = 0;
  43. return 0;
  44. }
  45. static inline int sha512_base_do_update(struct shash_desc *desc,
  46. const u8 *data,
  47. unsigned int len,
  48. sha512_block_fn *block_fn)
  49. {
  50. struct sha512_state *sctx = shash_desc_ctx(desc);
  51. unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
  52. sctx->count[0] += len;
  53. if (sctx->count[0] < len)
  54. sctx->count[1]++;
  55. if (unlikely((partial + len) >= SHA512_BLOCK_SIZE)) {
  56. int blocks;
  57. if (partial) {
  58. int p = SHA512_BLOCK_SIZE - partial;
  59. memcpy(sctx->buf + partial, data, p);
  60. data += p;
  61. len -= p;
  62. block_fn(sctx, sctx->buf, 1);
  63. }
  64. blocks = len / SHA512_BLOCK_SIZE;
  65. len %= SHA512_BLOCK_SIZE;
  66. if (blocks) {
  67. block_fn(sctx, data, blocks);
  68. data += blocks * SHA512_BLOCK_SIZE;
  69. }
  70. partial = 0;
  71. }
  72. if (len)
  73. memcpy(sctx->buf + partial, data, len);
  74. return 0;
  75. }
  76. static inline int sha512_base_do_finalize(struct shash_desc *desc,
  77. sha512_block_fn *block_fn)
  78. {
  79. const int bit_offset = SHA512_BLOCK_SIZE - sizeof(__be64[2]);
  80. struct sha512_state *sctx = shash_desc_ctx(desc);
  81. __be64 *bits = (__be64 *)(sctx->buf + bit_offset);
  82. unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
  83. sctx->buf[partial++] = 0x80;
  84. if (partial > bit_offset) {
  85. memset(sctx->buf + partial, 0x0, SHA512_BLOCK_SIZE - partial);
  86. partial = 0;
  87. block_fn(sctx, sctx->buf, 1);
  88. }
  89. memset(sctx->buf + partial, 0x0, bit_offset - partial);
  90. bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
  91. bits[1] = cpu_to_be64(sctx->count[0] << 3);
  92. block_fn(sctx, sctx->buf, 1);
  93. return 0;
  94. }
  95. static inline int sha512_base_finish(struct shash_desc *desc, u8 *out)
  96. {
  97. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  98. struct sha512_state *sctx = shash_desc_ctx(desc);
  99. __be64 *digest = (__be64 *)out;
  100. int i;
  101. for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64))
  102. put_unaligned_be64(sctx->state[i], digest++);
  103. *sctx = (struct sha512_state){};
  104. return 0;
  105. }