diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d722ae6..013b7a2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ target_link_libraries(hpcore pthread crypto ${CMAKE_DL_LIBS} # Needed for stacktrace support + libblake3.so ) add_dependencies(hpcore appbill diff --git a/README.md b/README.md index 5a6051f5..480cde60 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,15 @@ Instructions are based on [this](https://libsodium.gitbook.io/doc/installation). 3. Run `./configure && make && make check` 4. Run `sudo make install` +#### Install blake3 +1. Clone [blake3 library](https://github.com/BLAKE3-team/BLAKE3) repository +2. Navigate into the directory in a terminal. +3. `cd c` to navigate to the C implementation folder +4. `gcc -shared -fPIC -O3 -o libblake3.so blake3.c blake3_dispatch.c blake3_portable.c \` + `blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S blake3_avx512_x86-64_unix.S` +5. `sudo cp blake3.h /usr/local/include/` +6. `sudo cp libblake3.so /usr/local/lib/` + #### Install Boost Following Instructions are based on Boost [getting started](https://www.boost.org/doc/libs/1_71_0/more/getting_started/unix-variants.html#prepare-to-use-a-boost-library-binary) diff --git a/src/crypto.cpp b/src/crypto.cpp index 3c9cb286..5c6f0ec5 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -5,195 +5,195 @@ namespace crypto { -/** - * Initializes the crypto subsystem. Must be called once during application startup. - * @return 0 for successful initialization. -1 for failure. - */ -int init() -{ - if (sodium_init() < 0) + /** + * Initializes the crypto subsystem. Must be called once during application startup. + * @return 0 for successful initialization. -1 for failure. + */ + int init() { - std::cout << "sodium_init failed.\n"; - return -1; + if (sodium_init() < 0) + { + std::cout << "sodium_init failed.\n"; + return -1; + } + + return 0; } - return 0; -} + /** + * Generates a signing key pair using libsodium and assigns them to the provided strings. + */ + void generate_signing_keys(std::string &pubkey, std::string &seckey) + { + // Generate key pair using libsodium default algorithm. + // Currently using ed25519. So append prefix byte to represent that. -/** - * Generates a signing key pair using libsodium and assigns them to the provided strings. - */ -void generate_signing_keys(std::string &pubkey, std::string &seckey) -{ - // Generate key pair using libsodium default algorithm. - // Currently using ed25519. So append prefix byte to represent that. + pubkey.resize(PFXD_PUBKEY_BYTES); + pubkey[0] = KEYPFX_ed25519; - pubkey.resize(PFXD_PUBKEY_BYTES); - pubkey[0] = KEYPFX_ed25519; + seckey.resize(PFXD_SECKEY_BYTES); + seckey[0] = KEYPFX_ed25519; - seckey.resize(PFXD_SECKEY_BYTES); - seckey[0] = KEYPFX_ed25519; + crypto_sign_ed25519_keypair( + reinterpret_cast(pubkey.data() + 1), // +1 to skip the prefix byte. + reinterpret_cast(seckey.data() + 1)); // +1 to skip the prefix byte. + } - crypto_sign_ed25519_keypair( - reinterpret_cast(pubkey.data() + 1), // +1 to skip the prefix byte. - reinterpret_cast(seckey.data() + 1)); // +1 to skip the prefix byte. -} + /** + * Returns the signature bytes for a message. + * + * @param msg Message bytes to sign. + * @param seckey Secret key bytes. + * @return Signature bytes. + */ + std::string sign(std::string_view msg, std::string_view seckey) + { + //Generate the signature using libsodium. -/** - * Returns the signature bytes for a message. - * - * @param msg Message bytes to sign. - * @param seckey Secret key bytes. - * @return Signature bytes. - */ -std::string sign(std::string_view msg, std::string_view seckey) -{ - //Generate the signature using libsodium. + std::string sig; + sig.resize(crypto_sign_ed25519_BYTES); + crypto_sign_ed25519_detached( + reinterpret_cast(sig.data()), + NULL, + reinterpret_cast(msg.data()), + msg.length(), + reinterpret_cast(seckey.data() + 1)); // +1 to skip the prefix byte. - std::string sig; - sig.resize(crypto_sign_ed25519_BYTES); - crypto_sign_ed25519_detached( - reinterpret_cast(sig.data()), - NULL, - reinterpret_cast(msg.data()), - msg.length(), - reinterpret_cast(seckey.data() + 1)); // +1 to skip the prefix byte. - - return sig; -} + return sig; + } -/** - * Returns the hex signature string for a message. - * - * @param msg Message bytes to sign. - * @param seckeyhex hex secret key string. - * @return hex signature string. - */ -std::string sign_hex(std::string_view msg, std::string_view seckeyhex) -{ - //Decode hex string and generate the signature using libsodium. + /** + * Returns the hex signature string for a message. + * + * @param msg Message bytes to sign. + * @param seckeyhex hex secret key string. + * @return hex signature string. + */ + std::string sign_hex(std::string_view msg, std::string_view seckeyhex) + { + //Decode hex string and generate the signature using libsodium. - unsigned char seckey[PFXD_SECKEY_BYTES]; - util::hex2bin(seckey, PFXD_SECKEY_BYTES, seckeyhex); + unsigned char seckey[PFXD_SECKEY_BYTES]; + util::hex2bin(seckey, PFXD_SECKEY_BYTES, seckeyhex); - unsigned char sig[crypto_sign_ed25519_BYTES]; - crypto_sign_ed25519_detached( - sig, - NULL, - reinterpret_cast(msg.data()), - msg.length(), - seckey + 1); // +1 to skip prefix byte. + unsigned char sig[crypto_sign_ed25519_BYTES]; + crypto_sign_ed25519_detached( + sig, + NULL, + reinterpret_cast(msg.data()), + msg.length(), + seckey + 1); // +1 to skip prefix byte. - std::string sighex; - util::bin2hex(sighex, sig, crypto_sign_ed25519_BYTES); - return sighex; -} + std::string sighex; + util::bin2hex(sighex, sig, crypto_sign_ed25519_BYTES); + return sighex; + } -/** - * Verifies the given signature bytes for the message. - * - * @param msg Message bytes. - * @param sig Signature bytes. - * @param pubkey Public key bytes. - * @return 0 for successful verification. -1 for failure. - */ -int verify(std::string_view msg, std::string_view sig, std::string_view pubkey) -{ - return crypto_sign_ed25519_verify_detached( - reinterpret_cast(sig.data()), - reinterpret_cast(msg.data()), - msg.length(), - reinterpret_cast(pubkey.data() + 1)); // +1 to skip prefix byte. -} + /** + * Verifies the given signature bytes for the message. + * + * @param msg Message bytes. + * @param sig Signature bytes. + * @param pubkey Public key bytes. + * @return 0 for successful verification. -1 for failure. + */ + int verify(std::string_view msg, std::string_view sig, std::string_view pubkey) + { + return crypto_sign_ed25519_verify_detached( + reinterpret_cast(sig.data()), + reinterpret_cast(msg.data()), + msg.length(), + reinterpret_cast(pubkey.data() + 1)); // +1 to skip prefix byte. + } -/** - * Verifies the given hex signature for the message. - * - * @param msg hex message string. - * @param sighex hex signature string. - * @param pubkeyhex hex secret key. - * @return 0 for successful verification. -1 for failure. - */ -int verify_hex(std::string_view msg, std::string_view sighex, std::string_view pubkeyhex) -{ - //Decode hex string and verify the signature using libsodium. + /** + * Verifies the given hex signature for the message. + * + * @param msg hex message string. + * @param sighex hex signature string. + * @param pubkeyhex hex secret key. + * @return 0 for successful verification. -1 for failure. + */ + int verify_hex(std::string_view msg, std::string_view sighex, std::string_view pubkeyhex) + { + //Decode hex string and verify the signature using libsodium. - unsigned char decoded_pubkey[PFXD_PUBKEY_BYTES]; - util::hex2bin(decoded_pubkey, PFXD_PUBKEY_BYTES, pubkeyhex); + unsigned char decoded_pubkey[PFXD_PUBKEY_BYTES]; + util::hex2bin(decoded_pubkey, PFXD_PUBKEY_BYTES, pubkeyhex); - unsigned char decoded_sig[crypto_sign_ed25519_BYTES]; - util::hex2bin(decoded_sig, crypto_sign_ed25519_BYTES, sighex); + unsigned char decoded_sig[crypto_sign_ed25519_BYTES]; + util::hex2bin(decoded_sig, crypto_sign_ed25519_BYTES, sighex); - return crypto_sign_ed25519_verify_detached( - decoded_sig, - reinterpret_cast(msg.data()), - msg.length(), - decoded_pubkey + 1); // +1 to skip prefix byte. -} + return crypto_sign_ed25519_verify_detached( + decoded_sig, + reinterpret_cast(msg.data()), + msg.length(), + decoded_pubkey + 1); // +1 to skip prefix byte. + } -/** - * Generate blake2b hash for a given message. - * @param data String to hash. - * @return The blake2b hash of the given string. - */ -std::string get_hash(std::string_view data) -{ - std::string hash; - hash.resize(crypto_generichash_blake2b_BYTES); + /** + * Generate blake3 hash for a given message. + * @param data String to hash. + * @return The blake3 hash of the given string. + */ + std::string get_hash(std::string_view data) + { + std::string hash; + hash.resize(BLAKE3_OUT_LEN); - crypto_generichash_blake2b( - reinterpret_cast(hash.data()), - hash.length(), - reinterpret_cast(data.data()), - data.length(), - NULL, 0); + // Initialize the hasher. + blake3_hasher hasher; + blake3_hasher_init(&hasher); - return hash; -} + blake3_hasher_update(&hasher, reinterpret_cast(data.data()), data.length()); -/** - * Generate blake2b hash for a given message. + blake3_hasher_finalize(&hasher, reinterpret_cast(hash.data()), hash.length()); + + return hash; + } + + /** + * Generate blake3 hash for a given message. * @param data unsigned char array pointer to hash data. * @param data_length hash data length. - * @return The blake2b hash of the pointed buffer. + * @return The blake3 hash of the pointed buffer. */ -std::string get_hash(const unsigned char * data, size_t data_length) -{ - std::string hash; - hash.resize(crypto_generichash_blake2b_BYTES); + std::string get_hash(const unsigned char *data, size_t data_length) + { + std::string hash; + hash.resize(BLAKE3_OUT_LEN); - crypto_generichash_blake2b( - reinterpret_cast(hash.data()), - hash.length(), - data, - data_length, - NULL, 0); + // Initialize the hasher. + blake3_hasher hasher; + blake3_hasher_init(&hasher); - return hash; -} + blake3_hasher_update(&hasher, data, data_length); -/** - * Generates blake2b hash for the given set of strings using stream hashing. - */ -std::string get_hash(std::string_view s1, std::string_view s2) -{ - std::string hash; - hash.resize(crypto_generichash_blake2b_BYTES); + blake3_hasher_finalize(&hasher, reinterpret_cast(hash.data()), hash.length()); - // Init stream hashing. - crypto_generichash_blake2b_state state; - crypto_generichash_blake2b_init(&state, NULL, 0, hash.length()); + return hash; + } - crypto_generichash_blake2b_update(&state, reinterpret_cast(s1.data()), s1.length()); - crypto_generichash_blake2b_update(&state, reinterpret_cast(s2.data()), s2.length()); + /** + * Generates blake3 hash for the given set of strings using stream hashing. + */ + std::string get_hash(std::string_view s1, std::string_view s2) + { + std::string hash; + hash.resize(BLAKE3_OUT_LEN); - // Get the final hash. - crypto_generichash_blake2b_final( - &state, - reinterpret_cast(hash.data()), - hash.length()); + // Init stream hashing. + blake3_hasher hasher; + blake3_hasher_init(&hasher); - return hash; -} + // updating hash with given data + blake3_hasher_update(&hasher, reinterpret_cast(s1.data()), s1.length()); + blake3_hasher_update(&hasher, reinterpret_cast(s2.data()), s2.length()); + + // Get the final hash. + blake3_hasher_finalize(&hasher, reinterpret_cast(hash.data()), hash.length()); + + return hash; + } } // namespace crypto \ No newline at end of file diff --git a/src/pchheader.hpp b/src/pchheader.hpp index 23d1c0be..9c652085 100644 --- a/src/pchheader.hpp +++ b/src/pchheader.hpp @@ -63,5 +63,6 @@ #include #include #include +#include #endif \ No newline at end of file diff --git a/test/bin/hpfs b/test/bin/hpfs index ca9d6496..02e34e96 100755 Binary files a/test/bin/hpfs and b/test/bin/hpfs differ