diff --git a/src/Nimiq/Address.cpp b/src/Nimiq/Address.cpp index ae58500cfc0..46ab1bb1f5e 100644 --- a/src/Nimiq/Address.cpp +++ b/src/Nimiq/Address.cpp @@ -88,7 +88,7 @@ Address::Address(const std::vector& data) { Address::Address(const PublicKey& publicKey) { auto hash = std::array(); - blake2b(publicKey.bytes.data(), 32, hash.data(), hash.size()); + tc_blake2b(publicKey.bytes.data(), 32, hash.data(), hash.size()); std::copy(hash.begin(), hash.begin() + Address::size, bytes.begin()); } diff --git a/src/interface/TWHash.cpp b/src/interface/TWHash.cpp index b66adc07fb3..a6a5528627d 100644 --- a/src/interface/TWHash.cpp +++ b/src/interface/TWHash.cpp @@ -86,7 +86,7 @@ TWData *_Nonnull TWHashBlake2bPersonal(TWData *_Nonnull data, TWData * _Nonnull auto dataBytes = TWDataBytes(data); auto personalBytes = TWDataBytes(personal); auto personalSize = TWDataSize(personal); - blake2b_Personal(dataBytes, static_cast(TWDataSize(data)), personalBytes, personalSize, resultBytes.data(), outlen); + tc_blake2b_Personal(dataBytes, static_cast(TWDataSize(data)), personalBytes, personalSize, resultBytes.data(), outlen); auto result = TWDataCreateWithBytes(resultBytes.data(), outlen); return result; } diff --git a/trezor-crypto/crypto/blake2b.c b/trezor-crypto/crypto/blake2b.c index 5ec488bfe00..528a4fcd65f 100644 --- a/trezor-crypto/crypto/blake2b.c +++ b/trezor-crypto/crypto/blake2b.c @@ -93,7 +93,7 @@ static void blake2b_init0( blake2b_state *S ) } /* init xors IV with input parameter block */ -int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) +static int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) { const uint8_t *p = ( const uint8_t * )( P ); size_t i = 0; @@ -109,8 +109,8 @@ int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) } -/* Sequential blake2b initialization */ -int blake2b_Init( blake2b_state *S, size_t outlen ) +/* Sequential tc_blake2b initialization */ +int tc_blake2b_Init( blake2b_state *S, size_t outlen ) { blake2b_param P[1] = {0}; @@ -131,7 +131,7 @@ int blake2b_Init( blake2b_state *S, size_t outlen ) return blake2b_init_param( S, P ); } -int blake2b_InitPersonal( blake2b_state *S, size_t outlen, const void *personal, size_t personal_len) +int tc_blake2b_InitPersonal( blake2b_state *S, size_t outlen, const void *personal, size_t personal_len) { blake2b_param P[1] = {0}; @@ -153,7 +153,7 @@ int blake2b_InitPersonal( blake2b_state *S, size_t outlen, const void *personal, return blake2b_init_param( S, P ); } -int blake2b_InitKey( blake2b_state *S, size_t outlen, const void *key, size_t keylen ) +int tc_blake2b_InitKey( blake2b_state *S, size_t outlen, const void *key, size_t keylen ) { blake2b_param P[1] = {0}; @@ -180,7 +180,7 @@ int blake2b_InitKey( blake2b_state *S, size_t outlen, const void *key, size_t ke uint8_t block[BLAKE2B_BLOCKBYTES] = {0}; memzero( block, BLAKE2B_BLOCKBYTES ); memcpy( block, key, keylen ); - blake2b_Update( S, block, BLAKE2B_BLOCKBYTES ); + tc_blake2b_Update( S, block, BLAKE2B_BLOCKBYTES ); memzero( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ } return 0; @@ -254,7 +254,7 @@ static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOC #undef G #undef ROUND -int blake2b_Update( blake2b_state *S, const void *pin, size_t inlen ) +int tc_blake2b_Update( blake2b_state *S, const void *pin, size_t inlen ) { const unsigned char * in = (const unsigned char *)pin; if( inlen > 0 ) @@ -281,7 +281,7 @@ int blake2b_Update( blake2b_state *S, const void *pin, size_t inlen ) return 0; } -int blake2b_Final( blake2b_state *S, void *out, size_t outlen ) +int tc_blake2b_Final( blake2b_state *S, void *out, size_t outlen ) { uint8_t buffer[BLAKE2B_OUTBYTES] = {0}; size_t i = 0; @@ -305,30 +305,30 @@ int blake2b_Final( blake2b_state *S, void *out, size_t outlen ) return 0; } -int blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen) +int tc_blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen) { BLAKE2B_CTX ctx; - if (0 != blake2b_Init(&ctx, outlen)) return -1; - if (0 != blake2b_Update(&ctx, msg, msg_len)) return -1; - if (0 != blake2b_Final(&ctx, out, outlen)) return -1; + if (0 != tc_blake2b_Init(&ctx, outlen)) return -1; + if (0 != tc_blake2b_Update(&ctx, msg, msg_len)) return -1; + if (0 != tc_blake2b_Final(&ctx, out, outlen)) return -1; return 0; } // [wallet-core] -int blake2b_Personal(const uint8_t *msg, uint32_t msg_len, const void *personal, size_t personal_len, void *out, size_t outlen) +int tc_blake2b_Personal(const uint8_t *msg, uint32_t msg_len, const void *personal, size_t personal_len, void *out, size_t outlen) { BLAKE2B_CTX ctx; - if (0 != blake2b_InitPersonal(&ctx, outlen, personal, personal_len)) return -1; - if (0 != blake2b_Update(&ctx, msg, msg_len)) return -1; - if (0 != blake2b_Final(&ctx, out, outlen)) return -1; + if (0 != tc_blake2b_InitPersonal(&ctx, outlen, personal, personal_len)) return -1; + if (0 != tc_blake2b_Update(&ctx, msg, msg_len)) return -1; + if (0 != tc_blake2b_Final(&ctx, out, outlen)) return -1; return 0; } -int blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen) +int tc_blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen) { BLAKE2B_CTX ctx; - if (0 != blake2b_InitKey(&ctx, outlen, key, keylen)) return -1; - if (0 != blake2b_Update(&ctx, msg, msg_len)) return -1; - if (0 != blake2b_Final(&ctx, out, outlen)) return -1; + if (0 != tc_blake2b_InitKey(&ctx, outlen, key, keylen)) return -1; + if (0 != tc_blake2b_Update(&ctx, msg, msg_len)) return -1; + if (0 != tc_blake2b_Final(&ctx, out, outlen)) return -1; return 0; } diff --git a/trezor-crypto/crypto/hasher.c b/trezor-crypto/crypto/hasher.c index d6dd613e905..59c10181497 100644 --- a/trezor-crypto/crypto/hasher.c +++ b/trezor-crypto/crypto/hasher.c @@ -50,10 +50,10 @@ void hasher_InitParam(Hasher *hasher, HasherType type, const void *param, groestl512_Init(&hasher->ctx.groestl); break; case HASHER_BLAKE2B: - blake2b_Init(&hasher->ctx.blake2b, 32); + tc_blake2b_Init(&hasher->ctx.blake2b, 32); break; case HASHER_BLAKE2B_PERSONAL: - blake2b_InitPersonal(&hasher->ctx.blake2b, 32, hasher->param, + tc_blake2b_InitPersonal(&hasher->ctx.blake2b, 32, hasher->param, hasher->param_size); break; } @@ -90,7 +90,7 @@ void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length) { break; case HASHER_BLAKE2B: case HASHER_BLAKE2B_PERSONAL: - blake2b_Update(&hasher->ctx.blake2b, data, length); + tc_blake2b_Update(&hasher->ctx.blake2b, data, length); break; } } @@ -132,7 +132,7 @@ void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]) { break; case HASHER_BLAKE2B: case HASHER_BLAKE2B_PERSONAL: - blake2b_Final(&hasher->ctx.blake2b, hash, 32); + tc_blake2b_Final(&hasher->ctx.blake2b, hash, 32); break; } } diff --git a/trezor-crypto/crypto/nano.c b/trezor-crypto/crypto/nano.c index 15dc643fa13..54f4968d1ef 100644 --- a/trezor-crypto/crypto/nano.c +++ b/trezor-crypto/crypto/nano.c @@ -66,9 +66,9 @@ size_t nano_get_address( uint8_t checksum[NANO_CHECKSUM_LEN]; blake2b_state hash; - blake2b_Init(&hash, NANO_CHECKSUM_LEN); - blake2b_Update(&hash, public_key, sizeof(ed25519_public_key)); - blake2b_Final(&hash, checksum, NANO_CHECKSUM_LEN); + tc_blake2b_Init(&hash, NANO_CHECKSUM_LEN); + tc_blake2b_Update(&hash, public_key, sizeof(ed25519_public_key)); + tc_blake2b_Final(&hash, checksum, NANO_CHECKSUM_LEN); for (int i = 0; i < NANO_CHECKSUM_LEN; i++) { raw.data.checksum[NANO_CHECKSUM_LEN - (i + 1)] = checksum[i]; @@ -132,9 +132,9 @@ bool nano_validate_address( // Validate the checksum uint8_t checksum[NANO_CHECKSUM_LEN]; blake2b_state hash; - blake2b_Init(&hash, NANO_CHECKSUM_LEN); - blake2b_Update(&hash, raw.data.public_key, sizeof(ed25519_public_key)); - blake2b_Final(&hash, checksum, NANO_CHECKSUM_LEN); + tc_blake2b_Init(&hash, NANO_CHECKSUM_LEN); + tc_blake2b_Update(&hash, raw.data.public_key, sizeof(ed25519_public_key)); + tc_blake2b_Final(&hash, checksum, NANO_CHECKSUM_LEN); for (int i = 0; i < NANO_CHECKSUM_LEN; i++) { if (raw.data.checksum[NANO_CHECKSUM_LEN - (i + 1)] != checksum[i]) { diff --git a/trezor-crypto/crypto/tests/test_check.c b/trezor-crypto/crypto/tests/test_check.c index 0227f0146a8..138dd253fa0 100644 --- a/trezor-crypto/crypto/tests/test_check.c +++ b/trezor-crypto/crypto/tests/test_check.c @@ -4837,21 +4837,21 @@ START_TEST(test_blake2b) { uint8_t digest[BLAKE2B_DIGEST_LENGTH]; for (size_t i = 0; i < (sizeof(tests) / sizeof(*tests)); i++) { size_t msg_len = strlen(tests[i].msg) / 2; - blake2b_Key(fromhex(tests[i].msg), msg_len, key, sizeof(key), digest, + tc_blake2b_Key(fromhex(tests[i].msg), msg_len, key, sizeof(key), digest, sizeof(digest)); ck_assert_mem_eq(digest, fromhex(tests[i].hash), sizeof(digest)); // Test progressive hashing. size_t part_len = msg_len / 2; BLAKE2B_CTX ctx; - ck_assert_int_eq(blake2b_InitKey(&ctx, sizeof(digest), key, sizeof(key)), + ck_assert_int_eq(tc_blake2b_InitKey(&ctx, sizeof(digest), key, sizeof(key)), 0); - ck_assert_int_eq(blake2b_Update(&ctx, fromhex(tests[i].msg), part_len), 0); - ck_assert_int_eq(blake2b_Update(&ctx, NULL, 0), 0); - ck_assert_int_eq(blake2b_Update(&ctx, fromhex(tests[i].msg) + part_len, + ck_assert_int_eq(tc_blake2b_Update(&ctx, fromhex(tests[i].msg), part_len), 0); + ck_assert_int_eq(tc_blake2b_Update(&ctx, NULL, 0), 0); + ck_assert_int_eq(tc_blake2b_Update(&ctx, fromhex(tests[i].msg) + part_len, msg_len - part_len), 0); - ck_assert_int_eq(blake2b_Final(&ctx, digest, sizeof(digest)), 0); + ck_assert_int_eq(tc_blake2b_Final(&ctx, digest, sizeof(digest)), 0); ck_assert_mem_eq(digest, fromhex(tests[i].hash), BLAKE2B_DIGEST_LENGTH); } } @@ -4897,15 +4897,15 @@ START_TEST(test_blake2bp) { size_t part_len = msg_len / 2; BLAKE2B_CTX ctx; ck_assert_int_eq( - blake2b_InitPersonal(&ctx, sizeof(digest), tests[i].personal, + tc_blake2b_InitPersonal(&ctx, sizeof(digest), tests[i].personal, strlen(tests[i].personal)), 0); - ck_assert_int_eq(blake2b_Update(&ctx, fromhex(tests[i].msg), part_len), 0); - ck_assert_int_eq(blake2b_Update(&ctx, NULL, 0), 0); - ck_assert_int_eq(blake2b_Update(&ctx, fromhex(tests[i].msg) + part_len, + ck_assert_int_eq(tc_blake2b_Update(&ctx, fromhex(tests[i].msg), part_len), 0); + ck_assert_int_eq(tc_blake2b_Update(&ctx, NULL, 0), 0); + ck_assert_int_eq(tc_blake2b_Update(&ctx, fromhex(tests[i].msg) + part_len, msg_len - part_len), 0); - ck_assert_int_eq(blake2b_Final(&ctx, digest, sizeof(digest)), 0); + ck_assert_int_eq(tc_blake2b_Final(&ctx, digest, sizeof(digest)), 0); ck_assert_mem_eq(digest, fromhex(tests[i].hash), sizeof(digest)); } } diff --git a/trezor-crypto/include/TrezorCrypto/blake2b.h b/trezor-crypto/include/TrezorCrypto/blake2b.h index c002dd7afc5..c4e20bb7c34 100644 --- a/trezor-crypto/include/TrezorCrypto/blake2b.h +++ b/trezor-crypto/include/TrezorCrypto/blake2b.h @@ -33,16 +33,16 @@ typedef struct __blake2b_state #define BLAKE2B_DIGEST_LENGTH BLAKE2B_OUTBYTES #define BLAKE2B_KEY_LENGTH BLAKE2B_KEYBYTES -int blake2b_Init(blake2b_state *S, size_t outlen); -int blake2b_InitKey(blake2b_state *S, size_t outlen, const void *key, size_t keylen); -int blake2b_InitPersonal(blake2b_state *S, size_t outlen, const void *personal, size_t personal_len); -int blake2b_Update(blake2b_state *S, const void *pin, size_t inlen); -int blake2b_Final(blake2b_state *S, void *out, size_t outlen); +int tc_blake2b_Init(blake2b_state *S, size_t outlen); +int tc_blake2b_InitKey(blake2b_state *S, size_t outlen, const void *key, size_t keylen); +int tc_blake2b_InitPersonal(blake2b_state *S, size_t outlen, const void *personal, size_t personal_len); +int tc_blake2b_Update(blake2b_state *S, const void *pin, size_t inlen); +int tc_blake2b_Final(blake2b_state *S, void *out, size_t outlen); -int blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen); +int tc_blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen); // [wallet-core] -int blake2b_Personal(const uint8_t *msg, uint32_t msg_len, const void *personal, size_t personal_len, void *out, size_t outlen); -int blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen); +int tc_blake2b_Personal(const uint8_t *msg, uint32_t msg_len, const void *personal, size_t personal_len, void *out, size_t outlen); +int tc_blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen); #ifdef __cplusplus } /* extern "C" */ diff --git a/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-hash-custom-blake2b.h b/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-hash-custom-blake2b.h index 9c46630a9bd..68706f37795 100644 --- a/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-hash-custom-blake2b.h +++ b/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-hash-custom-blake2b.h @@ -15,9 +15,9 @@ #include #define ed25519_hash_context BLAKE2B_CTX -#define ed25519_hash_init(ctx) blake2b_Init(ctx, 64) -#define ed25519_hash_update(ctx, in, inlen) blake2b_Update((ctx), (in), (inlen)) -#define ed25519_hash_final(ctx, hash) blake2b_Final((ctx), (hash), 64) -#define ed25519_hash(hash, in, inlen) blake2b((in), (inlen), (hash), 64) +#define ed25519_hash_init(ctx) tc_blake2b_Init(ctx, 64) +#define ed25519_hash_update(ctx, in, inlen) tc_blake2b_Update((ctx), (in), (inlen)) +#define ed25519_hash_final(ctx, hash) tc_blake2b_Final((ctx), (hash), 64) +#define ed25519_hash(hash, in, inlen) tc_blake2b((in), (inlen), (hash), 64) #endif // ED25519_HASH_CUSTOM