hasher.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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,
  26. 0xd7f8783bUL,
  27. 0x1363868aUL,
  28. 0xe3e55658UL,
  29. 0x6eee945dUL,
  30. 0xbc7888ddUL,
  31. 0x02a6e2c3UL,
  32. 0x1873fe9fUL,
  33. };
  34. void hasher_InitParam(Hasher* hasher, HasherType type, const void* param, uint32_t param_size) {
  35. hasher->type = type;
  36. hasher->param = param;
  37. hasher->param_size = param_size;
  38. switch(hasher->type) {
  39. case HASHER_SHA2:
  40. case HASHER_SHA2D:
  41. case HASHER_SHA2_RIPEMD:
  42. sha256_Init(&hasher->ctx.sha2);
  43. break;
  44. case HASHER_SHA2_TAPSIGHASH:
  45. sha256_Init_ex(&hasher->ctx.sha2, sha256_initial_tapsighash_state, 512);
  46. break;
  47. case HASHER_SHA3:
  48. #if USE_KECCAK
  49. case HASHER_SHA3K:
  50. #endif
  51. sha3_256_Init(&hasher->ctx.sha3);
  52. break;
  53. case HASHER_BLAKE:
  54. case HASHER_BLAKED:
  55. case HASHER_BLAKE_RIPEMD:
  56. blake256_Init(&hasher->ctx.blake);
  57. break;
  58. case HASHER_GROESTLD_TRUNC:
  59. groestl512_Init(&hasher->ctx.groestl);
  60. break;
  61. case HASHER_BLAKE2B:
  62. blake2b_Init(&hasher->ctx.blake2b, 32);
  63. break;
  64. case HASHER_BLAKE2B_PERSONAL:
  65. blake2b_InitPersonal(&hasher->ctx.blake2b, 32, hasher->param, hasher->param_size);
  66. break;
  67. }
  68. }
  69. void hasher_Init(Hasher* hasher, HasherType type) {
  70. hasher_InitParam(hasher, type, NULL, 0);
  71. }
  72. void hasher_Reset(Hasher* hasher) {
  73. hasher_InitParam(hasher, hasher->type, hasher->param, hasher->param_size);
  74. }
  75. void hasher_Update(Hasher* hasher, const uint8_t* data, size_t length) {
  76. switch(hasher->type) {
  77. case HASHER_SHA2:
  78. case HASHER_SHA2D:
  79. case HASHER_SHA2_RIPEMD:
  80. case HASHER_SHA2_TAPSIGHASH:
  81. sha256_Update(&hasher->ctx.sha2, data, length);
  82. break;
  83. case HASHER_SHA3:
  84. #if USE_KECCAK
  85. case HASHER_SHA3K:
  86. #endif
  87. sha3_Update(&hasher->ctx.sha3, data, length);
  88. break;
  89. case HASHER_BLAKE:
  90. case HASHER_BLAKED:
  91. case HASHER_BLAKE_RIPEMD:
  92. blake256_Update(&hasher->ctx.blake, data, length);
  93. break;
  94. case HASHER_GROESTLD_TRUNC:
  95. groestl512_Update(&hasher->ctx.groestl, data, length);
  96. break;
  97. case HASHER_BLAKE2B:
  98. case HASHER_BLAKE2B_PERSONAL:
  99. blake2b_Update(&hasher->ctx.blake2b, data, length);
  100. break;
  101. }
  102. }
  103. void hasher_Final(Hasher* hasher, uint8_t hash[HASHER_DIGEST_LENGTH]) {
  104. switch(hasher->type) {
  105. case HASHER_SHA2:
  106. case HASHER_SHA2_TAPSIGHASH:
  107. sha256_Final(&hasher->ctx.sha2, hash);
  108. break;
  109. case HASHER_SHA2D:
  110. sha256_Final(&hasher->ctx.sha2, hash);
  111. hasher_Raw(HASHER_SHA2, hash, HASHER_DIGEST_LENGTH, hash);
  112. break;
  113. case HASHER_SHA2_RIPEMD:
  114. sha256_Final(&hasher->ctx.sha2, hash);
  115. ripemd160(hash, HASHER_DIGEST_LENGTH, hash);
  116. break;
  117. case HASHER_SHA3:
  118. sha3_Final(&hasher->ctx.sha3, hash);
  119. break;
  120. #if USE_KECCAK
  121. case HASHER_SHA3K:
  122. keccak_Final(&hasher->ctx.sha3, hash);
  123. break;
  124. #endif
  125. case HASHER_BLAKE:
  126. blake256_Final(&hasher->ctx.blake, hash);
  127. break;
  128. case HASHER_BLAKED:
  129. blake256_Final(&hasher->ctx.blake, hash);
  130. hasher_Raw(HASHER_BLAKE, hash, HASHER_DIGEST_LENGTH, hash);
  131. break;
  132. case HASHER_BLAKE_RIPEMD:
  133. blake256_Final(&hasher->ctx.blake, hash);
  134. ripemd160(hash, HASHER_DIGEST_LENGTH, hash);
  135. break;
  136. case HASHER_GROESTLD_TRUNC:
  137. groestl512_DoubleTrunc(&hasher->ctx.groestl, hash);
  138. break;
  139. case HASHER_BLAKE2B:
  140. case HASHER_BLAKE2B_PERSONAL:
  141. blake2b_Final(&hasher->ctx.blake2b, hash, 32);
  142. break;
  143. }
  144. }
  145. void hasher_Raw(
  146. HasherType type,
  147. const uint8_t* data,
  148. size_t length,
  149. uint8_t hash[HASHER_DIGEST_LENGTH]) {
  150. Hasher hasher = {0};
  151. hasher_Init(&hasher, type);
  152. hasher_Update(&hasher, data, length);
  153. hasher_Final(&hasher, hash);
  154. }