rurban / smhasher

Hash function quality and speed tests
https://rurban.github.io/smhasher/
Other
1.84k stars 177 forks source link

Issues adding hash, linker errors #52

Closed MatthewCushing closed 4 years ago

MatthewCushing commented 5 years ago

Hi, I've been trying for quite a while now to get our blake2s hash added however I've been running into issues when trying to compile. I'm not sure if these issues I'm having are staring me right in the face but unfortunately I can't figure it out. Essentially, I added 3 header files and a C file. I assumed all I needed to do was add the blake2s.c into the cmakelists.txt add_library() function but that doesn't seem to help. Can anyone give me a hand here? I'd really appreciate it.

Let me go through what all I've added to make sure you have all the info you may need to help. I have: blake2.h blake2-impl.h blake2s.h blake2s.c

I have included blake2s.c in the add_library() function in CMakeLists.txt

# CMakeLists.txt
add_library(
    SMHasherSupport
    # ......others
    blake2s.c
)

I have included blake2s.h in Hashes.h as well as given the following method definition:

// Hashes.h
#include "blake2s.h"
// ....other includes

void blake2s (const void *key, int len, uint32_t seed, void *out);
// ...….other has functions follow

In Hashes.cpp I have implemented the method.

// Hashes.cpp
void
blake2s(const void *key, int len, uint32_t seed, void *out)
{
    my_blake2s((char *)key, len, seed, (char *)out;
}

Lastly, in main.cpp I have put blake2s into g_hashes[]

// main.cpp
HashInfo g_hashes[] =
{
    { blake2s, 32, 0x0, "blake2s", "blake2s 32-bit cryptographic hash function"},
    // DoNothingHash and so on goes here

Note: I'm still having trouble understanding what values are coming through key, len, seed, and out but I'm assuming your definition of "key" is the string that you are trying to hash, len is the length of that string, seed is a random number to give an extra level of protection on the hash? Similar to a salt? And out is the final hash string? It seems everyone has a different term for things when it comes to hashing as the blake2s that my work is using says the key is the salt.
Anyways......this is what I got so far and everytime I try to compile, I receive this in terminal:

...
...
[98%] Building CXX object CMakeFiles/SMHasher.dir/main.cpp.o
[100%] Linking CXX executable SMHasher
./usr/bint/ld: libSMHasherSupport.a(Hashes.cpp.o): in function `blake2s(void const*, int, unsigned int, void*)':
Hashes.cpp:(.text+0x1): undefined reference to `my_blake2s(void const*, int, unsigned int, void*)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/SMHasher.dir/build.make:85: SMHasher] Error 1
make[1]: *** [CMakeFiles/Makefile2:110: CMakeFiles/SMHasher.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

So it seems everything goes fine until it's time to read from one my of files that aren't in the original repo (the my_blake2s file being in blake2s.c). I have added much smaller hash functions (like Djb2) directly to Hashes.cpp and after building first time I'm used to getting a verification hex code to slot in however I have not been able to get all of blake2s directly into Hashes.cpp without the multiple files (I'm an intern so don't have too much experience working with larger projects until recently - 4months ago).

Thanks! Matt

rurban commented 5 years ago

I'm assuming your definition of "key" is the string that you are trying to hash, len is the length of that string, seed is a random number to give an extra level of protection on the hash? Similar to a salt? And out is the final hash string?

Exactly. seed is like a salt. out as in/out param is there to have a uniform test_hash function. Most of those hash functions just return the result as uint32_t or uint64_t.

"blake2s 32-bit cryptographic hash function"

That cannot be cryptographic anymore. AES stripped to 32bit is also not cryptographic anymore.

I would rename your void blake2s (const void *key, int len, uint32_t seed, void *out); to test_blake2s_32.

undefined reference to my_blake2s

So you didn't link to your implementation, which should be in blake2s.c.

Bulat-Ziganshin commented 5 years ago

https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2 : blake2s uses 32-bit arithmetic to compute 256-bit hash, and blake2b employs 64-bit ops to compute 512-bit hash. yeah, exactly like sha2-256/512

rurban commented 4 years ago

blake2b and s are now integrated. It was trivial to add. It's C++, so C API's need to be declared as extern "C"