algorithms.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* algorithms.h - rhash library algorithms */
  2. #ifndef RHASH_ALGORITHMS_H
  3. #define RHASH_ALGORITHMS_H
  4. #include "rhash.h"
  5. #include "byte_order.h"
  6. #include <stddef.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #ifndef RHASH_API
  11. /* modifier for RHash library functions */
  12. # define RHASH_API
  13. #endif
  14. /**
  15. * Bit flag: default hash output format is base32.
  16. */
  17. #define RHASH_INFO_BASE32 1
  18. /**
  19. * Information about a hash function.
  20. */
  21. typedef struct rhash_info
  22. {
  23. /**
  24. * Hash function indentifier.
  25. */
  26. unsigned hash_id;
  27. /**
  28. * Flags bit-mask, including RHASH_INFO_BASE32 bit.
  29. */
  30. unsigned flags;
  31. /**
  32. The size of of the raw message digest in bytes.
  33. */
  34. size_t digest_size;
  35. /**
  36. * The hash function name.
  37. */
  38. const char* name;
  39. /**
  40. * The corresponding paramenter name in a magnet link.
  41. */
  42. const char* magnet_name;
  43. } rhash_info;
  44. typedef void (*pinit_t)(void* ctx);
  45. typedef void (*pupdate_t)(void* ctx, const void* msg, size_t size);
  46. typedef void (*pfinal_t)(void* ctx, unsigned char* result);
  47. typedef void (*pcleanup_t)(void* ctx);
  48. /**
  49. * Information about a hash function
  50. */
  51. typedef struct rhash_hash_info
  52. {
  53. rhash_info* info;
  54. size_t context_size;
  55. ptrdiff_t digest_diff;
  56. pinit_t init;
  57. pupdate_t update;
  58. pfinal_t final;
  59. pcleanup_t cleanup;
  60. } rhash_hash_info;
  61. /**
  62. * Information on a hash function and its context
  63. */
  64. typedef struct rhash_vector_item
  65. {
  66. struct rhash_hash_info* hash_info;
  67. void* context;
  68. } rhash_vector_item;
  69. /**
  70. * The rhash context containing contexts for several hash functions
  71. */
  72. typedef struct rhash_context_ext
  73. {
  74. struct rhash_context rc;
  75. unsigned hash_vector_size; /* number of contained hash sums */
  76. unsigned flags;
  77. volatile unsigned state;
  78. rhash_callback_t callback;
  79. void* callback_data;
  80. void* bt_ctx;
  81. rhash_vector_item vector[]; /* contexts of contained hash sums */
  82. } rhash_context_ext;
  83. extern rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT];
  84. extern rhash_hash_info* rhash_info_table;
  85. extern int rhash_info_size;
  86. extern unsigned rhash_uninitialized_algorithms;
  87. extern rhash_info info_crc32;
  88. extern rhash_info info_crc32c;
  89. extern rhash_info info_md4;
  90. extern rhash_info info_md5;
  91. extern rhash_info info_sha1;
  92. extern rhash_info info_tiger;
  93. extern rhash_info info_tth ;
  94. extern rhash_info info_btih;
  95. extern rhash_info info_ed2k;
  96. extern rhash_info info_aich;
  97. extern rhash_info info_whirlpool;
  98. extern rhash_info info_rmd160;
  99. extern rhash_info info_gost;
  100. extern rhash_info info_gostpro;
  101. extern rhash_info info_has160;
  102. extern rhash_info info_snf128;
  103. extern rhash_info info_snf256;
  104. extern rhash_info info_sha224;
  105. extern rhash_info info_sha256;
  106. extern rhash_info info_sha384;
  107. extern rhash_info info_sha512;
  108. extern rhash_info info_sha3_224;
  109. extern rhash_info info_sha3_256;
  110. extern rhash_info info_sha3_384;
  111. extern rhash_info info_sha3_512;
  112. extern rhash_info info_edr256;
  113. extern rhash_info info_edr512;
  114. /* rhash_info flags */
  115. #define F_BS32 1 /* default output in base32 */
  116. #define F_SWAP32 2 /* big endian flag */
  117. #define F_SWAP64 4
  118. #define F_SPCEXP 8 /* needs special import/export logic */
  119. /* define endianness flags */
  120. #if IS_LITTLE_ENDIAN
  121. #define F_LE32 0
  122. #define F_LE64 0
  123. #define F_BE32 F_SWAP32
  124. #define F_BE64 F_SWAP64
  125. #else
  126. #define F_LE32 F_SWAP32
  127. #define F_LE64 F_SWAP64
  128. #define F_BE32 0
  129. #define F_BE64 0
  130. #endif
  131. void rhash_init_algorithms(unsigned mask);
  132. const rhash_info* rhash_info_by_id(unsigned hash_id); /* get hash sum info by hash id */
  133. #if !defined(NO_IMPORT_EXPORT)
  134. size_t rhash_export_alg(unsigned hash_id, const void* ctx, void* out, size_t size);
  135. size_t rhash_import_alg(unsigned hash_id, void* ctx, const void* in, size_t size);
  136. #endif /* !defined(NO_IMPORT_EXPORT) */
  137. #if defined(OPENSSL_RUNTIME) && !defined(USE_OPENSSL)
  138. # define USE_OPENSSL
  139. #endif
  140. #ifdef USE_OPENSSL
  141. typedef struct rhash_hashing_methods
  142. {
  143. pinit_t init;
  144. pupdate_t update;
  145. pfinal_t final;
  146. } rhash_hashing_methods;
  147. enum rhash_methods_type
  148. {
  149. METHODS_RHASH,
  150. METHODS_OPENSSL,
  151. METHODS_SELECTED,
  152. };
  153. void rhash_load_sha1_methods(rhash_hashing_methods* methods, int methods_type);
  154. #define ARE_OPENSSL_METHODS(methods) ((methods).init != (void (*)(void*))&rhash_sha1_init)
  155. #endif
  156. #ifdef __cplusplus
  157. } /* extern "C" */
  158. #endif /* __cplusplus */
  159. #endif /* RHASH_ALGORITHMS_H */