diff options
| author | librhash upstream <kwrobot@kitware.com> | 2023-07-14 13:06:29 (GMT) |
|---|---|---|
| committer | Brad King <brad.king@kitware.com> | 2023-11-28 23:53:34 (GMT) |
| commit | 71d86c4e985f04579dbd84e5ad3d985e768491f3 (patch) | |
| tree | 95bf8ea60d88fdcb52bd8c6907782dfaf41a229a /librhash/algorithms.c | |
| parent | 641cc0030c45731298bcb3c237b122256d2855ae (diff) | |
| download | CMake-71d86c4e985f04579dbd84e5ad3d985e768491f3.zip CMake-71d86c4e985f04579dbd84e5ad3d985e768491f3.tar.gz CMake-71d86c4e985f04579dbd84e5ad3d985e768491f3.tar.bz2 | |
librhash 2023-07-14 (cfe77846)
Code extracted from:
https://github.com/rhash/rhash.git
at commit cfe77846a4c1bc7424eb2da2b164577a16bbcb19 (v1.4.4).
Diffstat (limited to 'librhash/algorithms.c')
| -rw-r--r-- | librhash/algorithms.c | 107 |
1 files changed, 96 insertions, 11 deletions
diff --git a/librhash/algorithms.c b/librhash/algorithms.c index f73a7c7..2a8ea46 100644 --- a/librhash/algorithms.c +++ b/librhash/algorithms.c @@ -14,15 +14,14 @@ * PERFORMANCE OF THIS SOFTWARE. */ -#include <stdio.h> -#include <assert.h> - +#include "algorithms.h" #include "byte_order.h" #include "rhash.h" -#include "algorithms.h" -/* header files of all supported hash sums */ +/* header files of all supported hash functions */ #include "aich.h" +#include "blake2b.h" +#include "blake2s.h" #include "crc32.h" #include "ed2k.h" #include "edonr.h" @@ -43,6 +42,11 @@ #include "whirlpool.h" #ifdef USE_OPENSSL +# include "plug_openssl.h" +#endif /* USE_OPENSSL */ +#include <assert.h> + +#ifdef USE_OPENSSL /* note: BTIH and AICH depends on the used SHA1 algorithm */ # define NEED_OPENSSL_INIT (RHASH_MD4 | RHASH_MD5 | \ RHASH_SHA1 | RHASH_SHA224 | RHASH_SHA256 | RHASH_SHA384 | RHASH_SHA512 | \ @@ -50,6 +54,7 @@ #else # define NEED_OPENSSL_INIT 0 #endif /* USE_OPENSSL */ + #ifdef GENERATE_GOST94_LOOKUP_TABLE # define NEED_GOST94_INIT (RHASH_GOST94 | RHASH_GOST94_CRYPTOPRO) #else @@ -75,10 +80,10 @@ rhash_info info_md4 = { RHASH_MD4, F_LE32, 16, "MD4", "md4" }; rhash_info info_md5 = { RHASH_MD5, F_LE32, 16, "MD5", "md5" }; rhash_info info_sha1 = { RHASH_SHA1, F_BE32, 20, "SHA1", "sha1" }; rhash_info info_tiger = { RHASH_TIGER, F_LE64, 24, "TIGER", "tiger" }; -rhash_info info_tth = { RHASH_TTH, F_BS32, 24, "TTH", "tree:tiger" }; -rhash_info info_btih = { RHASH_BTIH, 0, 20, "BTIH", "btih" }; +rhash_info info_tth = { RHASH_TTH, F_BS32 | F_SPCEXP, 24, "TTH", "tree:tiger" }; +rhash_info info_btih = { RHASH_BTIH, F_SPCEXP, 20, "BTIH", "btih" }; rhash_info info_ed2k = { RHASH_ED2K, F_LE32, 16, "ED2K", "ed2k" }; -rhash_info info_aich = { RHASH_AICH, F_BS32, 20, "AICH", "aich" }; +rhash_info info_aich = { RHASH_AICH, F_BS32 | F_SPCEXP, 20, "AICH", "aich" }; rhash_info info_whirlpool = { RHASH_WHIRLPOOL, F_BE64, 64, "WHIRLPOOL", "whirlpool" }; rhash_info info_rmd160 = { RHASH_RIPEMD160, F_LE32, 20, "RIPEMD-160", "ripemd160" }; rhash_info info_gost12_256 = { RHASH_GOST12_256, F_LE64, 32, "GOST12-256", "gost12-256" }; @@ -94,20 +99,21 @@ rhash_info info_sha384 = { RHASH_SHA384, F_BE64, 48, "SHA-384", "sha384" }; rhash_info info_sha512 = { RHASH_SHA512, F_BE64, 64, "SHA-512", "sha512" }; rhash_info info_edr256 = { RHASH_EDONR256, F_LE32, 32, "EDON-R256", "edon-r256" }; rhash_info info_edr512 = { RHASH_EDONR512, F_LE64, 64, "EDON-R512", "edon-r512" }; +rhash_info info_blake2s = { RHASH_BLAKE2S, F_LE32, 32, "BLAKE2S", "blake2s" }; +rhash_info info_blake2b = { RHASH_BLAKE2B, F_LE64, 64, "BLAKE2B", "blake2b" }; rhash_info info_sha3_224 = { RHASH_SHA3_224, F_LE64, 28, "SHA3-224", "sha3-224" }; rhash_info info_sha3_256 = { RHASH_SHA3_256, F_LE64, 32, "SHA3-256", "sha3-256" }; rhash_info info_sha3_384 = { RHASH_SHA3_384, F_LE64, 48, "SHA3-384", "sha3-384" }; rhash_info info_sha3_512 = { RHASH_SHA3_512, F_LE64, 64, "SHA3-512", "sha3-512" }; /* some helper macros */ -#define dgshft(name) (((char*)&((name##_ctx*)0)->hash) - (char*)0) -#define dgshft2(name, field) (((char*)&((name##_ctx*)0)->field) - (char*)0) +#define dgshft(name) ((uintptr_t)((char*)&((name##_ctx*)0)->hash)) +#define dgshft2(name, field) ((uintptr_t)((char*)&((name##_ctx*)0)->field)) #define ini(name) ((pinit_t)(name##_init)) #define upd(name) ((pupdate_t)(name##_update)) #define fin(name) ((pfinal_t)(name##_final)) #define iuf(name) ini(name), upd(name), fin(name) #define iuf2(name1, name2) ini(name1), upd(name2), fin(name2) -#define diuf(name) dgshft(name), ini(name), upd(name), fin(name) /* information about all supported hash functions */ rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] = @@ -141,6 +147,8 @@ rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] = { &info_crc32c, sizeof(uint32_t), 0, iuf(rhash_crc32c), 0 }, /* 32 bit */ { &info_snf128, sizeof(snefru_ctx), dgshft(snefru), iuf2(rhash_snefru128, rhash_snefru), 0 }, /* 128 bit */ { &info_snf256, sizeof(snefru_ctx), dgshft(snefru), iuf2(rhash_snefru256, rhash_snefru), 0 }, /* 256 bit */ + { &info_blake2s, sizeof(blake2s_ctx), dgshft(blake2s), iuf(rhash_blake2s), 0 }, /* 256 bit */ + { &info_blake2b, sizeof(blake2b_ctx), dgshft(blake2b), iuf(rhash_blake2b), 0 }, /* 512 bit */ }; /** @@ -258,3 +266,80 @@ static void rhash_crc32c_final(uint32_t* crc32c, unsigned char* result) result[2] = (unsigned char)(*crc32c >> 8), result[3] = (unsigned char)(*crc32c); #endif } + +#if !defined(NO_IMPORT_EXPORT) +/** + * Export a hash function context to a memory region, + * or calculate the size required for context export. + * + * @param hash_id identifier of the hash function + * @param ctx the algorithm context containing current hashing state + * @param out pointer to the memory region or NULL + * @param size size of memory region + * @return the size of the exported data on success, 0 on fail. + */ +size_t rhash_export_alg(unsigned hash_id, const void* ctx, void* out, size_t size) +{ + switch (hash_id) + { + case RHASH_TTH: + return rhash_tth_export((const tth_ctx*)ctx, out, size); + case RHASH_BTIH: + return bt_export((const torrent_ctx*)ctx, out, size); + case RHASH_AICH: + return rhash_aich_export((const aich_ctx*)ctx, out, size); + } + return 0; +} + +/** + * Import a hash function context from a memory region. + * + * @param hash_id identifier of the hash function + * @param ctx pointer to the algorithm context + * @param in pointer to the data to import + * @param size size of data to import + * @return the size of the imported data on success, 0 on fail. + */ +size_t rhash_import_alg(unsigned hash_id, void* ctx, const void* in, size_t size) +{ + switch (hash_id) + { + case RHASH_TTH: + return rhash_tth_import((tth_ctx*)ctx, in, size); + case RHASH_BTIH: + return bt_import((torrent_ctx*)ctx, in, size); + case RHASH_AICH: + return rhash_aich_import((aich_ctx*)ctx, in, size); + } + return 0; +} +#endif /* !defined(NO_IMPORT_EXPORT) */ + +#ifdef USE_OPENSSL +void rhash_load_sha1_methods(rhash_hashing_methods* methods, int methods_type) +{ + int use_openssl; + switch (methods_type) { + case METHODS_OPENSSL: + use_openssl = 1; + break; + case METHODS_SELECTED: + assert(rhash_info_table[3].info->hash_id == RHASH_SHA1); + use_openssl = ARE_OPENSSL_METHODS(rhash_info_table[3]); + break; + default: + use_openssl = 0; + break; + } + if (use_openssl) { + methods->init = rhash_ossl_sha1_init(); + methods->update = rhash_ossl_sha1_update(); + methods->final = rhash_ossl_sha1_final(); + } else { + methods->init = (pinit_t)&rhash_sha1_init; + methods->update = (pupdate_t)&rhash_sha1_update; + methods->final = (pfinal_t)&rhash_sha1_final; + } +} +#endif |
