mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Blake2 to Blake3 hash migration (#111)
* hash call update from blake2b to blake3 * including installation steps for blake3 in readme
This commit is contained in:
committed by
GitHub
parent
465573ad29
commit
648b70892c
@@ -73,6 +73,7 @@ target_link_libraries(hpcore
|
||||
pthread
|
||||
crypto
|
||||
${CMAKE_DL_LIBS} # Needed for stacktrace support
|
||||
libblake3.so
|
||||
)
|
||||
add_dependencies(hpcore
|
||||
appbill
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
316
src/crypto.cpp
316
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<unsigned char *>(pubkey.data() + 1), // +1 to skip the prefix byte.
|
||||
reinterpret_cast<unsigned char *>(seckey.data() + 1)); // +1 to skip the prefix byte.
|
||||
}
|
||||
|
||||
crypto_sign_ed25519_keypair(
|
||||
reinterpret_cast<unsigned char *>(pubkey.data() + 1), // +1 to skip the prefix byte.
|
||||
reinterpret_cast<unsigned char *>(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<unsigned char *>(sig.data()),
|
||||
NULL,
|
||||
reinterpret_cast<const unsigned char *>(msg.data()),
|
||||
msg.length(),
|
||||
reinterpret_cast<const unsigned char *>(seckey.data() + 1)); // +1 to skip the prefix byte.
|
||||
|
||||
std::string sig;
|
||||
sig.resize(crypto_sign_ed25519_BYTES);
|
||||
crypto_sign_ed25519_detached(
|
||||
reinterpret_cast<unsigned char *>(sig.data()),
|
||||
NULL,
|
||||
reinterpret_cast<const unsigned char *>(msg.data()),
|
||||
msg.length(),
|
||||
reinterpret_cast<const unsigned char *>(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<const unsigned char *>(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<const unsigned char *>(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<const unsigned char *>(sig.data()),
|
||||
reinterpret_cast<const unsigned char *>(msg.data()),
|
||||
msg.length(),
|
||||
reinterpret_cast<const unsigned char *>(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<const unsigned char *>(sig.data()),
|
||||
reinterpret_cast<const unsigned char *>(msg.data()),
|
||||
msg.length(),
|
||||
reinterpret_cast<const unsigned char *>(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<const unsigned char *>(msg.data()),
|
||||
msg.length(),
|
||||
decoded_pubkey + 1); // +1 to skip prefix byte.
|
||||
}
|
||||
return crypto_sign_ed25519_verify_detached(
|
||||
decoded_sig,
|
||||
reinterpret_cast<const unsigned char *>(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<unsigned char *>(hash.data()),
|
||||
hash.length(),
|
||||
reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.length(),
|
||||
NULL, 0);
|
||||
// Initialize the hasher.
|
||||
blake3_hasher hasher;
|
||||
blake3_hasher_init(&hasher);
|
||||
|
||||
return hash;
|
||||
}
|
||||
blake3_hasher_update(&hasher, reinterpret_cast<const unsigned char *>(data.data()), data.length());
|
||||
|
||||
/**
|
||||
* Generate blake2b hash for a given message.
|
||||
blake3_hasher_finalize(&hasher, reinterpret_cast<unsigned char *>(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<unsigned char *>(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<unsigned char *>(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<const unsigned char *>(s1.data()), s1.length());
|
||||
crypto_generichash_blake2b_update(&state, reinterpret_cast<const unsigned char *>(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<unsigned char *>(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<const unsigned char *>(s1.data()), s1.length());
|
||||
blake3_hasher_update(&hasher, reinterpret_cast<const unsigned char *>(s2.data()), s2.length());
|
||||
|
||||
// Get the final hash.
|
||||
blake3_hasher_finalize(&hasher, reinterpret_cast<unsigned char *>(hash.data()), hash.length());
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
} // namespace crypto
|
||||
@@ -63,5 +63,6 @@
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include <blake3.h>
|
||||
|
||||
#endif
|
||||
BIN
test/bin/hpfs
BIN
test/bin/hpfs
Binary file not shown.
Reference in New Issue
Block a user