hasher.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef __HASHER_H__
  23. #define __HASHER_H__
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include "blake256.h"
  27. #include "blake2b.h"
  28. #include "groestl.h"
  29. #include "sha2.h"
  30. #include "sha3.h"
  31. #define HASHER_DIGEST_LENGTH 32
  32. typedef enum {
  33. HASHER_SHA2,
  34. HASHER_SHA2D,
  35. HASHER_SHA2_RIPEMD,
  36. HASHER_SHA2_TAPSIGHASH,
  37. HASHER_SHA3,
  38. #if USE_KECCAK
  39. HASHER_SHA3K,
  40. #endif
  41. HASHER_BLAKE,
  42. HASHER_BLAKED,
  43. HASHER_BLAKE_RIPEMD,
  44. HASHER_GROESTLD_TRUNC, /* Double Groestl512 hasher truncated to 256 bits */
  45. HASHER_BLAKE2B,
  46. HASHER_BLAKE2B_PERSONAL,
  47. } HasherType;
  48. typedef struct {
  49. HasherType type;
  50. union {
  51. SHA256_CTX sha2; // for HASHER_SHA2{,D}
  52. SHA3_CTX sha3; // for HASHER_SHA3{,K}
  53. BLAKE256_CTX blake; // for HASHER_BLAKE{,D}
  54. GROESTL512_CTX groestl; // for HASHER_GROESTLD_TRUNC
  55. BLAKE2B_CTX blake2b; // for HASHER_BLAKE2B{,_PERSONAL}
  56. } ctx;
  57. const void *param;
  58. uint32_t param_size;
  59. } Hasher;
  60. void hasher_InitParam(Hasher *hasher, HasherType type, const void *param,
  61. uint32_t param_size);
  62. void hasher_Init(Hasher *hasher, HasherType type);
  63. void hasher_Reset(Hasher *hasher);
  64. void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length);
  65. void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]);
  66. void hasher_Raw(HasherType type, const uint8_t *data, size_t length,
  67. uint8_t hash[HASHER_DIGEST_LENGTH]);
  68. #endif