hasher.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2017 Saleem Rashid
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "hasher.h"
  23. #include "ripemd160.h"
  24. const uint32_t sha256_initial_tapsighash_state[8] = {
  25. 0xf504a425UL, 0xd7f8783bUL, 0x1363868aUL, 0xe3e55658UL,
  26. 0x6eee945dUL, 0xbc7888ddUL, 0x02a6e2c3UL, 0x1873fe9fUL,
  27. };
  28. void hasher_InitParam(Hasher *hasher, HasherType type, const void *param,
  29. uint32_t param_size) {
  30. hasher->type = type;
  31. hasher->param = param;
  32. hasher->param_size = param_size;
  33. switch (hasher->type) {
  34. case HASHER_SHA2:
  35. case HASHER_SHA2D:
  36. case HASHER_SHA2_RIPEMD:
  37. sha256_Init(&hasher->ctx.sha2);
  38. break;
  39. case HASHER_SHA2_TAPSIGHASH:
  40. sha256_Init_ex(&hasher->ctx.sha2, sha256_initial_tapsighash_state, 512);
  41. break;
  42. case HASHER_SHA3:
  43. #if USE_KECCAK
  44. case HASHER_SHA3K:
  45. #endif
  46. sha3_256_Init(&hasher->ctx.sha3);
  47. break;
  48. case HASHER_BLAKE:
  49. case HASHER_BLAKED:
  50. case HASHER_BLAKE_RIPEMD:
  51. blake256_Init(&hasher->ctx.blake);
  52. break;
  53. case HASHER_GROESTLD_TRUNC:
  54. groestl512_Init(&hasher->ctx.groestl);
  55. break;
  56. case HASHER_BLAKE2B:
  57. blake2b_Init(&hasher->ctx.blake2b, 32);
  58. break;
  59. case HASHER_BLAKE2B_PERSONAL:
  60. blake2b_InitPersonal(&hasher->ctx.blake2b, 32, hasher->param,
  61. hasher->param_size);
  62. break;
  63. }
  64. }
  65. void hasher_Init(Hasher *hasher, HasherType type) {
  66. hasher_InitParam(hasher, type, NULL, 0);
  67. }
  68. void hasher_Reset(Hasher *hasher) {
  69. hasher_InitParam(hasher, hasher->type, hasher->param, hasher->param_size);
  70. }
  71. void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length) {
  72. switch (hasher->type) {
  73. case HASHER_SHA2:
  74. case HASHER_SHA2D:
  75. case HASHER_SHA2_RIPEMD:
  76. case HASHER_SHA2_TAPSIGHASH:
  77. sha256_Update(&hasher->ctx.sha2, data, length);
  78. break;
  79. case HASHER_SHA3:
  80. #if USE_KECCAK
  81. case HASHER_SHA3K:
  82. #endif
  83. sha3_Update(&hasher->ctx.sha3, data, length);
  84. break;
  85. case HASHER_BLAKE:
  86. case HASHER_BLAKED:
  87. case HASHER_BLAKE_RIPEMD:
  88. blake256_Update(&hasher->ctx.blake, data, length);
  89. break;
  90. case HASHER_GROESTLD_TRUNC:
  91. groestl512_Update(&hasher->ctx.groestl, data, length);
  92. break;
  93. case HASHER_BLAKE2B:
  94. case HASHER_BLAKE2B_PERSONAL:
  95. blake2b_Update(&hasher->ctx.blake2b, data, length);
  96. break;
  97. }
  98. }
  99. void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]) {
  100. switch (hasher->type) {
  101. case HASHER_SHA2:
  102. case HASHER_SHA2_TAPSIGHASH:
  103. sha256_Final(&hasher->ctx.sha2, hash);
  104. break;
  105. case HASHER_SHA2D:
  106. sha256_Final(&hasher->ctx.sha2, hash);
  107. hasher_Raw(HASHER_SHA2, hash, HASHER_DIGEST_LENGTH, hash);
  108. break;
  109. case HASHER_SHA2_RIPEMD:
  110. sha256_Final(&hasher->ctx.sha2, hash);
  111. ripemd160(hash, HASHER_DIGEST_LENGTH, hash);
  112. break;
  113. case HASHER_SHA3:
  114. sha3_Final(&hasher->ctx.sha3, hash);
  115. break;
  116. #if USE_KECCAK
  117. case HASHER_SHA3K:
  118. keccak_Final(&hasher->ctx.sha3, hash);
  119. break;
  120. #endif
  121. case HASHER_BLAKE:
  122. blake256_Final(&hasher->ctx.blake, hash);
  123. break;
  124. case HASHER_BLAKED:
  125. blake256_Final(&hasher->ctx.blake, hash);
  126. hasher_Raw(HASHER_BLAKE, hash, HASHER_DIGEST_LENGTH, hash);
  127. break;
  128. case HASHER_BLAKE_RIPEMD:
  129. blake256_Final(&hasher->ctx.blake, hash);
  130. ripemd160(hash, HASHER_DIGEST_LENGTH, hash);
  131. break;
  132. case HASHER_GROESTLD_TRUNC:
  133. groestl512_DoubleTrunc(&hasher->ctx.groestl, hash);
  134. break;
  135. case HASHER_BLAKE2B:
  136. case HASHER_BLAKE2B_PERSONAL:
  137. blake2b_Final(&hasher->ctx.blake2b, hash, 32);
  138. break;
  139. }
  140. }
  141. void hasher_Raw(HasherType type, const uint8_t *data, size_t length,
  142. uint8_t hash[HASHER_DIGEST_LENGTH]) {
  143. Hasher hasher = {0};
  144. hasher_Init(&hasher, type);
  145. hasher_Update(&hasher, data, length);
  146. hasher_Final(&hasher, hash);
  147. }