From 1c589ae419a19f1289d4652e57c3e761dfeeda16 Mon Sep 17 00:00:00 2001 From: Sarah GLINER Date: Wed, 21 Feb 2024 12:12:01 +0100 Subject: [PATCH] Clean: replace magic values with define --- src/monero_api.h | 4 +- src/monero_blind.c | 12 +- src/monero_crypto.c | 10 +- src/monero_init.c | 14 +-- src/monero_key.c | 254 ++++++++++++++++++++++--------------------- src/monero_monero.c | 4 +- src/monero_open_tx.c | 22 ++-- src/monero_prehash.c | 12 +- src/monero_stealth.c | 8 +- src/monero_types.h | 18 +-- src/monero_ux_nano.c | 8 +- src/monero_ux_stax.c | 8 +- 12 files changed, 189 insertions(+), 185 deletions(-) diff --git a/src/monero_api.h b/src/monero_api.h index 195cac4..2238d80 100644 --- a/src/monero_api.h +++ b/src/monero_api.h @@ -134,8 +134,8 @@ void display_account(void); /* ----------------------------------------------------------------------- */ /* --- KEYS & ADDRESS ---- */ /* ----------------------------------------------------------------------- */ -extern const unsigned char C_FAKE_SEC_VIEW_KEY[32]; -extern const unsigned char C_FAKE_SEC_SPEND_KEY[32]; +extern const unsigned char C_FAKE_SEC_VIEW_KEY[KEY_SIZE]; +extern const unsigned char C_FAKE_SEC_SPEND_KEY[KEY_SIZE]; int is_fake_view_key(unsigned char *s); int is_fake_spend_key(unsigned char *s); diff --git a/src/monero_blind.c b/src/monero_blind.c index 58289ae..47b15aa 100644 --- a/src/monero_blind.c +++ b/src/monero_blind.c @@ -28,10 +28,10 @@ int monero_apdu_blind() { unsigned char v[32]; unsigned char k[32]; - unsigned char AKout[32]; + unsigned char AKout[KEY_SIZE]; int err = 0; - err = monero_io_fetch_decrypt(AKout, 32, TYPE_AMOUNT_KEY); + err = monero_io_fetch_decrypt(AKout, KEY_SIZE, TYPE_AMOUNT_KEY); if (err) { return err; } @@ -132,10 +132,10 @@ int monero_unblind(unsigned char *v, unsigned char *k, unsigned char *AKout, int monero_apdu_unblind() { unsigned char v[32]; unsigned char k[32]; - unsigned char AKout[32]; + unsigned char AKout[KEY_SIZE]; int err = 0; - err = monero_io_fetch_decrypt(AKout, 32, TYPE_AMOUNT_KEY); + err = monero_io_fetch_decrypt(AKout, KEY_SIZE, TYPE_AMOUNT_KEY); if (err) { return err; } @@ -162,10 +162,10 @@ int monero_apdu_unblind() { /* ----------------------------------------------------------------------- */ int monero_apdu_gen_commitment_mask() { unsigned char k[32]; - unsigned char AKout[32]; + unsigned char AKout[KEY_SIZE]; int err = 0; - err = monero_io_fetch_decrypt(AKout, 32, TYPE_AMOUNT_KEY); + err = monero_io_fetch_decrypt(AKout, KEY_SIZE, TYPE_AMOUNT_KEY); if (err) { return err; } diff --git a/src/monero_crypto.c b/src/monero_crypto.c index 05155fc..f986f93 100644 --- a/src/monero_crypto.c +++ b/src/monero_crypto.c @@ -59,7 +59,7 @@ unsigned char const C_EIGHT[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0 /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_aes_derive(cx_aes_key_t *sk, unsigned char *seed32, unsigned char *a, unsigned char *b) { - unsigned char h1[32]; + unsigned char h1[KEY_SIZE]; int error; error = monero_keccak_init_H(); @@ -67,17 +67,17 @@ int monero_aes_derive(cx_aes_key_t *sk, unsigned char *seed32, unsigned char *a, return error; } - error = monero_keccak_update_H(seed32, 32); + error = monero_keccak_update_H(seed32, KEY_SIZE); if (error) { return error; } - error = monero_keccak_update_H(a, 32); + error = monero_keccak_update_H(a, KEY_SIZE); if (error) { return error; } - error = monero_keccak_update_H(b, 32); + error = monero_keccak_update_H(b, KEY_SIZE); if (error) { return error; } @@ -87,7 +87,7 @@ int monero_aes_derive(cx_aes_key_t *sk, unsigned char *seed32, unsigned char *a, return error; } - error = monero_keccak_H(h1, 32, h1); + error = monero_keccak_H(h1, KEY_SIZE, h1); if (error) { return error; } diff --git a/src/monero_init.c b/src/monero_init.c index b7bb3a1..f22d905 100644 --- a/src/monero_init.c +++ b/src/monero_init.c @@ -27,10 +27,10 @@ /* ----------------------*/ const unsigned char C_MAGIC[8] = {'M', 'O', 'N', 'E', 'R', 'O', 'H', 'W'}; -const unsigned char C_FAKE_SEC_VIEW_KEY[32] = { +const unsigned char C_FAKE_SEC_VIEW_KEY[KEY_SIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; -const unsigned char C_FAKE_SEC_SPEND_KEY[32] = { +const unsigned char C_FAKE_SEC_SPEND_KEY[KEY_SIZE] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; @@ -72,7 +72,7 @@ unsigned int monero_init() { int monero_init_private_key(void) { unsigned int path[5]; unsigned char seed[64]; - unsigned char chain[32]; + unsigned char chain[KEY_SIZE]; int error; // generate account keys @@ -91,7 +91,7 @@ int monero_init_private_key(void) { switch (N_monero_pstate->key_mode) { case KEY_MODE_SEED: - error = monero_keccak_F(seed, 32, G_monero_vstate.b); + error = monero_keccak_F(seed, KEY_SIZE, G_monero_vstate.b); if (error) { return error; } @@ -102,7 +102,7 @@ int monero_init_private_key(void) { return error; } - error = monero_keccak_F(G_monero_vstate.b, 32, G_monero_vstate.a); + error = monero_keccak_F(G_monero_vstate.b, KEY_SIZE, G_monero_vstate.a); if (error) { return error; } @@ -115,8 +115,8 @@ int monero_init_private_key(void) { break; case KEY_MODE_EXTERNAL: - memcpy(G_monero_vstate.a, (void*)N_monero_pstate->a, 32); - memcpy(G_monero_vstate.b, (void*)N_monero_pstate->b, 32); + memcpy(G_monero_vstate.a, (void*)N_monero_pstate->a, KEY_SIZE); + memcpy(G_monero_vstate.b, (void*)N_monero_pstate->b, KEY_SIZE); break; default: diff --git a/src/monero_key.c b/src/monero_key.c index 98fb1ee..ced2062 100644 --- a/src/monero_key.c +++ b/src/monero_key.c @@ -200,8 +200,8 @@ int monero_apdu_display_address() { unsigned int minor; unsigned char index[8]; unsigned char payment_id[8]; - unsigned char C[32]; - unsigned char D[32]; + unsigned char C[KEY_SIZE]; + unsigned char D[KEY_SIZE]; int error = 0; // fetch @@ -222,8 +222,8 @@ int monero_apdu_display_address() { return error; } } else { - memcpy(C, G_monero_vstate.A, 32); - memcpy(D, G_monero_vstate.B, 32); + memcpy(C, G_monero_vstate.A, KEY_SIZE); + memcpy(D, G_monero_vstate.B, KEY_SIZE); } // prepare UI @@ -249,52 +249,52 @@ int monero_apdu_display_address() { /* --- --- */ /* ----------------------------------------------------------------------- */ int is_fake_view_key(unsigned char *s) { - return memcmp(s, C_FAKE_SEC_VIEW_KEY, 32) == 0; + return memcmp(s, C_FAKE_SEC_VIEW_KEY, KEY_SIZE) == 0; } int is_fake_spend_key(unsigned char *s) { - return memcmp(s, C_FAKE_SEC_SPEND_KEY, 32) == 0; + return memcmp(s, C_FAKE_SEC_SPEND_KEY, KEY_SIZE) == 0; } /* ----------------------------------------------------------------------- */ /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_put_key() { - unsigned char raw[32]; - unsigned char pub[32]; - unsigned char sec[32]; + unsigned char raw[KEY_SIZE]; + unsigned char pub[KEY_SIZE]; + unsigned char sec[KEY_SIZE]; int err; // option + priv/pub view key + priv/pub spend key + base58 address - if (G_monero_vstate.io_length != (1 + 32 * 2 + 32 * 2 + 95)) { + if (G_monero_vstate.io_length != (1 + KEY_SIZE * 2 + KEY_SIZE * 2 + 95)) { return SW_WRONG_LENGTH; } // view key - monero_io_fetch(sec, 32); - monero_io_fetch(pub, 32); + monero_io_fetch(sec, KEY_SIZE); + monero_io_fetch(pub, KEY_SIZE); err = monero_ecmul_G(raw, sec, sizeof(raw), sizeof(sec)); if (err) { return err; } - if (memcmp(pub, raw, 32) != 0) { + if (memcmp(pub, raw, KEY_SIZE) != 0) { return SW_WRONG_DATA; } - nvm_write((void *)N_monero_pstate->a, sec, 32); + nvm_write((void *)N_monero_pstate->a, sec, KEY_SIZE); // spend key - monero_io_fetch(sec, 32); - monero_io_fetch(pub, 32); + monero_io_fetch(sec, KEY_SIZE); + monero_io_fetch(pub, KEY_SIZE); err = monero_ecmul_G(raw, sec, sizeof(raw), sizeof(sec)); if (err) { return err; } - if (memcmp(pub, raw, 32) != 0) { + if (memcmp(pub, raw, KEY_SIZE) != 0) { return SW_WRONG_DATA; } - nvm_write((void *)N_monero_pstate->b, sec, 32); + nvm_write((void *)N_monero_pstate->b, sec, KEY_SIZE); // change mode unsigned char key_mode = KEY_MODE_EXTERNAL; @@ -314,9 +314,9 @@ int monero_apdu_get_key() { // get pub case 1: // view key - monero_io_insert(G_monero_vstate.A, 32); + monero_io_insert(G_monero_vstate.A, KEY_SIZE); // spend key - monero_io_insert(G_monero_vstate.B, 32); + monero_io_insert(G_monero_vstate.B, KEY_SIZE); // public base address int error = monero_base58_public_key( (char *)G_monero_vstate.io_buffer + G_monero_vstate.io_offset, G_monero_vstate.A, @@ -331,7 +331,7 @@ int monero_apdu_get_key() { case 2: // view key if (G_monero_vstate.export_view_key == EXPORT_VIEW_KEY) { - monero_io_insert(G_monero_vstate.a, 32); + monero_io_insert(G_monero_vstate.a, KEY_SIZE); } else { ui_export_viewkey_display(0); return 0; @@ -354,18 +354,18 @@ int monero_apdu_get_key() { if (os_derive_bip32_no_throw(CX_CURVE_SECP256K1, path, 5, seed, G_monero_vstate.a)) { return SW_WRONG_DATA; } - monero_io_insert(seed, 32); + monero_io_insert(seed, KEY_SIZE); - monero_io_insert(G_monero_vstate.b, 32); - monero_io_insert(G_monero_vstate.a, 32); + monero_io_insert(G_monero_vstate.b, KEY_SIZE); + monero_io_insert(G_monero_vstate.a, KEY_SIZE); break; } // get info case 4: - monero_io_insert(G_monero_vstate.a, 32); - monero_io_insert(G_monero_vstate.b, 32); + monero_io_insert(G_monero_vstate.a, KEY_SIZE); + monero_io_insert(G_monero_vstate.b, KEY_SIZE); break; #endif @@ -379,9 +379,9 @@ int monero_apdu_get_key() { /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_verify_key() { - unsigned char pub[32]; - unsigned char priv[32]; - unsigned char computed_pub[32]; + unsigned char pub[KEY_SIZE]; + unsigned char priv[KEY_SIZE]; + unsigned char computed_pub[KEY_SIZE]; unsigned int verified = 0; int err = 0; @@ -390,7 +390,7 @@ int monero_apdu_verify_key() { explicit_bzero(priv, sizeof(priv)); return err; } - monero_io_fetch(pub, 32); + monero_io_fetch(pub, KEY_SIZE); switch (G_monero_vstate.io_p1) { case 0: err = monero_secret_key_to_public_key(computed_pub, priv, sizeof(computed_pub), @@ -401,16 +401,16 @@ int monero_apdu_verify_key() { } break; case 1: - memcpy(computed_pub, G_monero_vstate.A, 32); + memcpy(computed_pub, G_monero_vstate.A, KEY_SIZE); break; case 2: - memcpy(computed_pub, G_monero_vstate.B, 32); + memcpy(computed_pub, G_monero_vstate.B, KEY_SIZE); break; default: explicit_bzero(priv, sizeof(priv)); return SW_WRONG_P1P2; } - if (memcmp(computed_pub, pub, 32) == 0) { + if (memcmp(computed_pub, pub, KEY_SIZE) == 0) { verified = 1; } @@ -426,11 +426,11 @@ int monero_apdu_verify_key() { #define CHACHA8_KEY_TAIL 0x8c int monero_apdu_get_chacha8_prekey(/*char *prekey*/) { unsigned char abt[65]; - unsigned char pre[32]; + unsigned char pre[KEY_SIZE]; monero_io_discard(0); - memcpy(abt, G_monero_vstate.a, 32); - memcpy(abt + 32, G_monero_vstate.b, 32); + memcpy(abt, G_monero_vstate.a, KEY_SIZE); + memcpy(abt + KEY_SIZE, G_monero_vstate.b, KEY_SIZE); abt[64] = CHACHA8_KEY_TAIL; int error = monero_keccak_F(abt, 65, pre); if (error) { @@ -445,17 +445,17 @@ int monero_apdu_get_chacha8_prekey(/*char *prekey*/) { /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_sc_add(/*unsigned char *r, unsigned char *s1, unsigned char *s2*/) { - unsigned char s1[32]; - unsigned char s2[32]; - unsigned char r[32]; + unsigned char s1[KEY_SIZE]; + unsigned char s2[KEY_SIZE]; + unsigned char r[KEY_SIZE]; int err; // fetch - err = monero_io_fetch_decrypt(s1, 32, TYPE_SCALAR); + err = monero_io_fetch_decrypt(s1, KEY_SIZE, TYPE_SCALAR); if (err) { explicit_bzero(s1, sizeof(s1)); return err; } - err = monero_io_fetch_decrypt(s2, 32, TYPE_SCALAR); + err = monero_io_fetch_decrypt(s2, KEY_SIZE, TYPE_SCALAR); if (err) { explicit_bzero(s1, sizeof(s1)); explicit_bzero(s2, sizeof(s2)); @@ -468,8 +468,8 @@ int monero_apdu_sc_add(/*unsigned char *r, unsigned char *s1, unsigned char *s2* // https://github.com/monero-project/monero/blob/v0.15.0.5/src/cryptonote_basic/cryptonote_format_utils.cpp#L331 // // hwdev.sc_secret_add(scalar_step2, scalar_step1,subaddr_sk); - if ((memcmp(s1, G_monero_vstate.last_derive_secret_key, 32) != 0) || - (memcmp(s2, G_monero_vstate.last_get_subaddress_secret_key, 32) != 0)) { + if ((memcmp(s1, G_monero_vstate.last_derive_secret_key, KEY_SIZE) != 0) || + (memcmp(s2, G_monero_vstate.last_get_subaddress_secret_key, KEY_SIZE) != 0)) { explicit_bzero(s1, sizeof(s1)); explicit_bzero(s2, sizeof(s2)); return SW_WRONG_DATA; @@ -482,7 +482,7 @@ int monero_apdu_sc_add(/*unsigned char *r, unsigned char *s1, unsigned char *s2* explicit_bzero(r, sizeof(r)); return err; } - monero_io_insert_encrypt(r, 32, TYPE_SCALAR); + monero_io_insert_encrypt(r, KEY_SIZE, TYPE_SCALAR); explicit_bzero(s1, sizeof(s1)); explicit_bzero(s2, sizeof(s2)); explicit_bzero(r, sizeof(r)); @@ -493,13 +493,13 @@ int monero_apdu_sc_add(/*unsigned char *r, unsigned char *s1, unsigned char *s2* /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_scal_mul_key(/*const rct::key &pub, const rct::key &sec, rct::key mulkey*/) { - unsigned char pub[32]; - unsigned char sec[32]; - unsigned char r[32]; + unsigned char pub[KEY_SIZE]; + unsigned char sec[KEY_SIZE]; + unsigned char r[KEY_SIZE]; int err = 0; // fetch - monero_io_fetch(pub, 32); + monero_io_fetch(pub, KEY_SIZE); err = monero_io_fetch_decrypt_key(sec, sizeof(sec)); if (err) { explicit_bzero(sec, sizeof(sec)); @@ -512,7 +512,7 @@ int monero_apdu_scal_mul_key(/*const rct::key &pub, const rct::key &sec, rct::ke explicit_bzero(sec, sizeof(sec)); return err; } - monero_io_insert(r, 32); + monero_io_insert(r, KEY_SIZE); explicit_bzero(sec, sizeof(sec)); return SW_OK; } @@ -521,11 +521,11 @@ int monero_apdu_scal_mul_key(/*const rct::key &pub, const rct::key &sec, rct::ke /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_scal_mul_base(/*const rct::key &sec, rct::key mulkey*/) { - unsigned char sec[32]; - unsigned char r[32]; + unsigned char sec[KEY_SIZE]; + unsigned char r[KEY_SIZE]; int err; // fetch - err = monero_io_fetch_decrypt(sec, 32, TYPE_SCALAR); + err = monero_io_fetch_decrypt(sec, KEY_SIZE, TYPE_SCALAR); if (err) { explicit_bzero(sec, sizeof(sec)); return err; @@ -538,7 +538,7 @@ int monero_apdu_scal_mul_base(/*const rct::key &sec, rct::key mulkey*/) { return err; } - monero_io_insert(r, 32); + monero_io_insert(r, KEY_SIZE); explicit_bzero(sec, sizeof(sec)); return SW_OK; } @@ -547,8 +547,8 @@ int monero_apdu_scal_mul_base(/*const rct::key &sec, rct::key mulkey*/) { /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_generate_keypair(/*crypto::public_key &pub, crypto::secret_key &sec*/) { - unsigned char sec[32]; - unsigned char pub[32]; + unsigned char sec[KEY_SIZE]; + unsigned char pub[KEY_SIZE]; monero_io_discard(0); int error = monero_generate_keypair(pub, sec, sizeof(pub), sizeof(sec)); @@ -556,8 +556,8 @@ int monero_apdu_generate_keypair(/*crypto::public_key &pub, crypto::secret_key & explicit_bzero(sec, sizeof(sec)); return error; } - monero_io_insert(pub, 32); - monero_io_insert_encrypt(sec, 32, TYPE_SCALAR); + monero_io_insert(pub, KEY_SIZE); + monero_io_insert_encrypt(sec, KEY_SIZE, TYPE_SCALAR); explicit_bzero(sec, sizeof(sec)); return SW_OK; } @@ -567,10 +567,10 @@ int monero_apdu_generate_keypair(/*crypto::public_key &pub, crypto::secret_key & /* ----------------------------------------------------------------------- */ int monero_apdu_secret_key_to_public_key( /*const crypto::secret_key &sec, crypto::public_key &pub*/) { - unsigned char sec[32]; - unsigned char pub[32]; + unsigned char sec[KEY_SIZE]; + unsigned char pub[KEY_SIZE]; // fetch - int err = monero_io_fetch_decrypt(sec, 32, TYPE_SCALAR); + int err = monero_io_fetch_decrypt(sec, KEY_SIZE, TYPE_SCALAR); if (err) { explicit_bzero(sec, sizeof(sec)); return err; @@ -584,7 +584,7 @@ int monero_apdu_secret_key_to_public_key( } // pub key - monero_io_insert(pub, 32); + monero_io_insert(pub, KEY_SIZE); explicit_bzero(sec, sizeof(sec)); return SW_OK; } @@ -593,13 +593,13 @@ int monero_apdu_secret_key_to_public_key( /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_generate_key_derivation(/*const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_derivation &derivation*/) { - unsigned char pub[32]; - unsigned char sec[32]; - unsigned char drv[32]; + unsigned char pub[KEY_SIZE]; + unsigned char sec[KEY_SIZE]; + unsigned char drv[KEY_SIZE]; int err = 0; // fetch - monero_io_fetch(pub, 32); + monero_io_fetch(pub, KEY_SIZE); err = monero_io_fetch_decrypt_key(sec, sizeof(sec)); if (err) { explicit_bzero(sec, sizeof(sec)); @@ -616,7 +616,7 @@ int monero_apdu_generate_key_derivation(/*const crypto::public_key &pub, const c return err; } - monero_io_insert_encrypt(drv, 32, TYPE_DERIVATION); + monero_io_insert_encrypt(drv, KEY_SIZE, TYPE_DERIVATION); explicit_bzero(sec, sizeof(sec)); explicit_bzero(drv, sizeof(drv)); return SW_OK; @@ -627,12 +627,12 @@ int monero_apdu_generate_key_derivation(/*const crypto::public_key &pub, const c /* ----------------------------------------------------------------------- */ int monero_apdu_derivation_to_scalar( /*const crypto::key_derivation &derivation, const size_t output_index, ec_scalar &res*/) { - unsigned char derivation[32]; + unsigned char derivation[KEY_SIZE]; unsigned int output_index; - unsigned char res[32]; + unsigned char res[KEY_SIZE]; // fetch - int err = monero_io_fetch_decrypt(derivation, 32, TYPE_DERIVATION); + int err = monero_io_fetch_decrypt(derivation, KEY_SIZE, TYPE_DERIVATION); if (err) { explicit_bzero(derivation, sizeof(derivation)); return err; @@ -650,7 +650,7 @@ int monero_apdu_derivation_to_scalar( } // pub key - monero_io_insert_encrypt(res, 32, TYPE_SCALAR); + monero_io_insert_encrypt(res, KEY_SIZE, TYPE_SCALAR); explicit_bzero(derivation, sizeof(derivation)); explicit_bzero(res, sizeof(res)); return SW_OK; @@ -660,19 +660,19 @@ int monero_apdu_derivation_to_scalar( /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_derive_public_key(/*const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::public_key &pub, public_key &derived_pub*/) { - unsigned char derivation[32]; + unsigned char derivation[KEY_SIZE]; unsigned int output_index; - unsigned char pub[32]; - unsigned char drvpub[32]; + unsigned char pub[KEY_SIZE]; + unsigned char drvpub[KEY_SIZE]; // fetch - int err = monero_io_fetch_decrypt(derivation, 32, TYPE_DERIVATION); + int err = monero_io_fetch_decrypt(derivation, KEY_SIZE, TYPE_DERIVATION); if (err) { explicit_bzero(derivation, sizeof(derivation)); return err; } output_index = monero_io_fetch_u32(); - monero_io_fetch(pub, 32); + monero_io_fetch(pub, KEY_SIZE); monero_io_discard(0); // pub @@ -684,7 +684,7 @@ int monero_apdu_derive_public_key(/*const crypto::key_derivation &derivation, co } // pub key - monero_io_insert(drvpub, 32); + monero_io_insert(drvpub, KEY_SIZE); explicit_bzero(derivation, sizeof(derivation)); return SW_OK; } @@ -693,14 +693,14 @@ int monero_apdu_derive_public_key(/*const crypto::key_derivation &derivation, co /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_derive_secret_key(/*const crypto::key_derivation &derivation, const std::size_t output_index, const crypto::secret_key &sec, secret_key &derived_sec*/){ - unsigned char derivation[32]; + unsigned char derivation[KEY_SIZE]; unsigned int output_index; - unsigned char sec[32]; - unsigned char drvsec[32]; + unsigned char sec[KEY_SIZE]; + unsigned char drvsec[KEY_SIZE]; int err = 0; // fetch - err = monero_io_fetch_decrypt(derivation, 32, TYPE_DERIVATION); + err = monero_io_fetch_decrypt(derivation, KEY_SIZE, TYPE_DERIVATION); if (err) { explicit_bzero(derivation, sizeof(derivation)); return err; @@ -725,8 +725,8 @@ int monero_apdu_derive_secret_key(/*const crypto::key_derivation &derivation, co } // sec key - memcpy(G_monero_vstate.last_derive_secret_key, drvsec, 32); - monero_io_insert_encrypt(drvsec, 32, TYPE_SCALAR); + memcpy(G_monero_vstate.last_derive_secret_key, drvsec, KEY_SIZE); + monero_io_insert_encrypt(drvsec, KEY_SIZE, TYPE_SCALAR); explicit_bzero(derivation, sizeof(derivation)); explicit_bzero(sec, sizeof(sec)); @@ -739,13 +739,13 @@ int monero_apdu_derive_secret_key(/*const crypto::key_derivation &derivation, co /* ----------------------------------------------------------------------- */ int monero_apdu_generate_key_image( /*const crypto::public_key &pub, const crypto::secret_key &sec, crypto::key_image &image*/) { - unsigned char pub[32]; - unsigned char sec[32]; - unsigned char image[32]; + unsigned char pub[KEY_SIZE]; + unsigned char sec[KEY_SIZE]; + unsigned char image[KEY_SIZE]; // fetch - monero_io_fetch(pub, 32); - int err = monero_io_fetch_decrypt(sec, 32, TYPE_SCALAR); + monero_io_fetch(pub, KEY_SIZE); + int err = monero_io_fetch_decrypt(sec, KEY_SIZE, TYPE_SCALAR); if (err) { explicit_bzero(sec, sizeof(sec)); return err; @@ -760,7 +760,7 @@ int monero_apdu_generate_key_image( } // pub key - monero_io_insert(image, 32); + monero_io_insert(image, KEY_SIZE); explicit_bzero(sec, sizeof(sec)); return SW_OK; } @@ -770,13 +770,13 @@ int monero_apdu_generate_key_image( /* ----------------------------------------------------------------------- */ int monero_apdu_derive_view_tag( /*const crypto::key_derivation &derivation, const size_t output_index, crypto::view_tag &view_tag*/) { - unsigned char derivation[32]; + unsigned char derivation[KEY_SIZE]; unsigned int output_index; unsigned char res[1]; int err; // fetch - err = monero_io_fetch_decrypt(derivation, 32, TYPE_DERIVATION); + err = monero_io_fetch_decrypt(derivation, KEY_SIZE, TYPE_DERIVATION); if (err) { explicit_bzero(derivation, sizeof(derivation)); return err; @@ -800,14 +800,14 @@ int monero_apdu_derive_view_tag( /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_derive_subaddress_public_key(/*const crypto::public_key &pub, const crypto::key_derivation &derivation, const std::size_t output_index, public_key &derived_pub*/) { - unsigned char pub[32]; - unsigned char derivation[32]; + unsigned char pub[KEY_SIZE]; + unsigned char derivation[KEY_SIZE]; unsigned int output_index; - unsigned char sub_pub[32]; + unsigned char sub_pub[KEY_SIZE]; // fetch - monero_io_fetch(pub, 32); - int err = monero_io_fetch_decrypt(derivation, 32, TYPE_DERIVATION); + monero_io_fetch(pub, KEY_SIZE); + int err = monero_io_fetch_decrypt(derivation, KEY_SIZE, TYPE_DERIVATION); if (err) { explicit_bzero(derivation, sizeof(derivation)); return err; @@ -823,7 +823,7 @@ int monero_apdu_derive_subaddress_public_key(/*const crypto::public_key &pub, co return err; } // pub key - monero_io_insert(sub_pub, 32); + monero_io_insert(sub_pub, KEY_SIZE); explicit_bzero(derivation, sizeof(derivation)); return SW_OK; } @@ -834,8 +834,8 @@ int monero_apdu_derive_subaddress_public_key(/*const crypto::public_key &pub, co int monero_apdu_get_subaddress( /*const cryptonote::subaddress_index& index, cryptonote::account_public_address &address*/) { unsigned char index[8]; - unsigned char C[32]; - unsigned char D[32]; + unsigned char C[KEY_SIZE]; + unsigned char D[KEY_SIZE]; // fetch monero_io_fetch(index, 8); @@ -848,8 +848,8 @@ int monero_apdu_get_subaddress( } // pub key - monero_io_insert(C, 32); - monero_io_insert(D, 32); + monero_io_insert(C, KEY_SIZE); + monero_io_insert(D, KEY_SIZE); return SW_OK; } @@ -859,7 +859,7 @@ int monero_apdu_get_subaddress( int monero_apdu_get_subaddress_spend_public_key( /*const cryptonote::subaddress_index& index, crypto::public_key D*/) { unsigned char index[8]; - unsigned char D[32]; + unsigned char D[KEY_SIZE]; // fetch monero_io_fetch(index, 8); @@ -872,7 +872,7 @@ int monero_apdu_get_subaddress_spend_public_key( } // pub key - monero_io_insert(D, 32); + monero_io_insert(D, KEY_SIZE); return SW_OK; } @@ -881,9 +881,9 @@ int monero_apdu_get_subaddress_spend_public_key( /* --- --- */ /* ----------------------------------------------------------------------- */ int monero_apdu_get_subaddress_secret_key(/*const crypto::secret_key& sec, const cryptonote::subaddress_index& index, crypto::secret_key &sub_sec*/) { - unsigned char sec[32]; + unsigned char sec[KEY_SIZE]; unsigned char index[8]; - unsigned char sub_sec[32]; + unsigned char sub_sec[KEY_SIZE]; int err = 0; err = monero_io_fetch_decrypt_key(sec, sizeof(sec)); @@ -902,8 +902,8 @@ int monero_apdu_get_subaddress_secret_key(/*const crypto::secret_key& sec, const return err; } - memcpy(G_monero_vstate.last_get_subaddress_secret_key, sub_sec, 32); - monero_io_insert_encrypt(sub_sec, 32, TYPE_SCALAR); + memcpy(G_monero_vstate.last_get_subaddress_secret_key, sub_sec, KEY_SIZE); + monero_io_insert_encrypt(sub_sec, KEY_SIZE, TYPE_SCALAR); explicit_bzero(sec, sizeof(sec)); explicit_bzero(sub_sec, sizeof(sec)); return SW_OK; @@ -915,7 +915,7 @@ int monero_apdu_get_subaddress_secret_key(/*const crypto::secret_key& sec, const int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_sec, crypto::public_key Aout, crypto::public_key Bout, size_t output_index, bool is_change, bool is_subaddress, bool need_additional_key, bool use_view_tags*/) { // IN - unsigned char tx_key[32]; + unsigned char tx_key[KEY_SIZE]; unsigned char *txkey_pub; unsigned char *Aout; unsigned char *Bout; @@ -923,15 +923,15 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se unsigned char is_change; unsigned char is_subaddress; unsigned char need_additional_txkeys; - unsigned char additional_txkey_sec[32]; + unsigned char additional_txkey_sec[KEY_SIZE]; unsigned char use_view_tags; // OUT - unsigned char additional_txkey_pub[32]; + unsigned char additional_txkey_pub[KEY_SIZE]; #define amount_key tx_key #define out_eph_public_key additional_txkey_sec unsigned char view_tag[1]; // TMP - unsigned char derivation[32]; + unsigned char derivation[KEY_SIZE]; int err = 0; monero_io_fetch_u32(); // skip tx_version @@ -941,11 +941,11 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se return err; } txkey_pub = G_monero_vstate.io_buffer + G_monero_vstate.io_offset; - monero_io_skip(32); + monero_io_skip(KEY_SIZE); Aout = G_monero_vstate.io_buffer + G_monero_vstate.io_offset; - monero_io_skip(32); + monero_io_skip(KEY_SIZE); Bout = G_monero_vstate.io_buffer + G_monero_vstate.io_offset; - monero_io_skip(32); + monero_io_skip(KEY_SIZE); output_index = monero_io_fetch_u32(); is_change = monero_io_fetch_u8(); is_subaddress = monero_io_fetch_u8(); @@ -958,20 +958,20 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se return err; } } else { - monero_io_skip(32); + monero_io_skip(KEY_SIZE); } use_view_tags = monero_io_fetch_u8(); // update outkeys hash control if (G_monero_vstate.tx_sig_mode == TRANSACTION_CREATE_REAL) { if (G_monero_vstate.io_protocol_version >= 2) { - err = monero_sha256_outkeys_update(Aout, 32); + err = monero_sha256_outkeys_update(Aout, KEY_SIZE); if (err) { explicit_bzero(tx_key, sizeof(tx_key)); explicit_bzero(additional_txkey_sec, sizeof(additional_txkey_sec)); return err; } - err = monero_sha256_outkeys_update(Bout, 32); + err = monero_sha256_outkeys_update(Bout, KEY_SIZE); if (err) { explicit_bzero(tx_key, sizeof(tx_key)); explicit_bzero(additional_txkey_sec, sizeof(additional_txkey_sec)); @@ -990,7 +990,8 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se if (need_additional_txkeys) { if (is_subaddress) { err = monero_ecmul_k(additional_txkey_pub, Bout, additional_txkey_sec, - sizeof(additional_txkey_pub), 32, sizeof(additional_txkey_sec)); + sizeof(additional_txkey_pub), KEY_SIZE, + sizeof(additional_txkey_sec)); if (err) { explicit_bzero(tx_key, sizeof(tx_key)); explicit_bzero(additional_txkey_sec, sizeof(additional_txkey_sec)); @@ -1006,13 +1007,14 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se } } } else { - explicit_bzero(additional_txkey_pub, 32); + explicit_bzero(additional_txkey_pub, KEY_SIZE); } // derivation if (is_change) { - err = monero_generate_key_derivation(derivation, txkey_pub, G_monero_vstate.a, - sizeof(derivation), 32, sizeof(G_monero_vstate.a)); + err = + monero_generate_key_derivation(derivation, txkey_pub, G_monero_vstate.a, + sizeof(derivation), KEY_SIZE, sizeof(G_monero_vstate.a)); if (err) { explicit_bzero(tx_key, sizeof(tx_key)); explicit_bzero(additional_txkey_sec, sizeof(additional_txkey_sec)); @@ -1022,7 +1024,7 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se err = monero_generate_key_derivation( derivation, Aout, (is_subaddress && need_additional_txkeys) ? additional_txkey_sec : tx_key, - sizeof(derivation), 32, 32); + sizeof(derivation), KEY_SIZE, KEY_SIZE); if (err) { explicit_bzero(tx_key, sizeof(tx_key)); explicit_bzero(additional_txkey_sec, sizeof(additional_txkey_sec)); @@ -1041,7 +1043,7 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se } if (G_monero_vstate.tx_sig_mode == TRANSACTION_CREATE_REAL) { if (G_monero_vstate.io_protocol_version >= 2) { - err = monero_sha256_outkeys_update(amount_key, 32); + err = monero_sha256_outkeys_update(amount_key, KEY_SIZE); if (err) { explicit_bzero(tx_key, sizeof(tx_key)); explicit_bzero(additional_txkey_sec, sizeof(additional_txkey_sec)); @@ -1053,7 +1055,7 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se // compute ephemeral output key err = monero_derive_public_key(out_eph_public_key, derivation, output_index, Bout, - sizeof(out_eph_public_key), sizeof(derivation), 32); + sizeof(out_eph_public_key), sizeof(derivation), KEY_SIZE); if (err) { explicit_bzero(tx_key, sizeof(tx_key)); explicit_bzero(additional_txkey_sec, sizeof(additional_txkey_sec)); @@ -1074,10 +1076,10 @@ int monero_apu_generate_txout_keys(/*size_t tx_version, crypto::secret_key tx_se // send all monero_io_discard(0); - monero_io_insert_encrypt(amount_key, 32, TYPE_AMOUNT_KEY); - monero_io_insert(out_eph_public_key, 32); + monero_io_insert_encrypt(amount_key, KEY_SIZE, TYPE_AMOUNT_KEY); + monero_io_insert(out_eph_public_key, KEY_SIZE); if (need_additional_txkeys) { - monero_io_insert(additional_txkey_pub, 32); + monero_io_insert(additional_txkey_pub, KEY_SIZE); } if (use_view_tags) { monero_io_insert(view_tag, 1); diff --git a/src/monero_monero.c b/src/monero_monero.c index 297465d..a6b585d 100644 --- a/src/monero_monero.c +++ b/src/monero_monero.c @@ -163,8 +163,8 @@ int monero_base58_public_key(char* str_b58, unsigned char* view, unsigned char* return error; } - memcpy(data + offset, spend, 32); - memcpy(data + offset + 32, view, 32); + memcpy(data + offset, spend, KEY_SIZE); + memcpy(data + offset + KEY_SIZE, view, KEY_SIZE); offset += 64; if (paymanetID) { memcpy(data + offset, paymanetID, 8); diff --git a/src/monero_open_tx.c b/src/monero_open_tx.c index 54f3f3c..97afc80 100644 --- a/src/monero_open_tx.c +++ b/src/monero_open_tx.c @@ -27,9 +27,9 @@ /* ----------------------------------------------------------------------- */ int monero_reset_tx(int reset_tx_cnt) { int error; - explicit_bzero(G_monero_vstate.r, 32); - explicit_bzero(G_monero_vstate.R, 32); - cx_rng(G_monero_vstate.hmac_key, 32); + explicit_bzero(G_monero_vstate.r, sizeof(G_monero_vstate.r)); + explicit_bzero(G_monero_vstate.R, sizeof(G_monero_vstate.R)); + cx_rng(G_monero_vstate.hmac_key, KEY_SIZE); error = monero_keccak_init_H(); if (error) { @@ -71,9 +71,9 @@ int monero_apdu_open_tx_cont() { G_monero_vstate.tx_in_progress = 1; #ifdef DEBUG_HWDEVICE - memset(G_monero_vstate.hmac_key, 0xab, 32); + memset(G_monero_vstate.hmac_key, 0xab, sizeof(G_monero_vstate.hmac_key)); #else - cx_rng(G_monero_vstate.hmac_key, 32); + cx_rng(G_monero_vstate.hmac_key, KEY_SIZE); #endif error = monero_rng_mod_order(G_monero_vstate.r, sizeof(G_monero_vstate.r)); @@ -87,12 +87,12 @@ int monero_apdu_open_tx_cont() { return error; } - monero_io_insert(G_monero_vstate.R, 32); - monero_io_insert_encrypt(G_monero_vstate.r, 32, TYPE_SCALAR); - monero_io_insert(C_FAKE_SEC_VIEW_KEY, 32); - monero_io_insert_hmac_for((void*)C_FAKE_SEC_VIEW_KEY, 32, TYPE_SCALAR); - monero_io_insert(C_FAKE_SEC_SPEND_KEY, 32); - monero_io_insert_hmac_for((void*)C_FAKE_SEC_SPEND_KEY, 32, TYPE_SCALAR); + monero_io_insert(G_monero_vstate.R, KEY_SIZE); + monero_io_insert_encrypt(G_monero_vstate.r, KEY_SIZE, TYPE_SCALAR); + monero_io_insert(C_FAKE_SEC_VIEW_KEY, KEY_SIZE); + monero_io_insert_hmac_for((void*)C_FAKE_SEC_VIEW_KEY, KEY_SIZE, TYPE_SCALAR); + monero_io_insert(C_FAKE_SEC_SPEND_KEY, KEY_SIZE); + monero_io_insert_hmac_for((void*)C_FAKE_SEC_SPEND_KEY, KEY_SIZE, TYPE_SCALAR); return SW_OK; } diff --git a/src/monero_prehash.c b/src/monero_prehash.c index 1405f8b..403865f 100644 --- a/src/monero_prehash.c +++ b/src/monero_prehash.c @@ -74,7 +74,7 @@ int monero_apdu_mlsag_prehash_update() { unsigned char *Aout; unsigned char *Bout; unsigned char is_change; - unsigned char AKout[32]; + unsigned char AKout[KEY_SIZE]; unsigned char C[32]; unsigned char v[32]; unsigned char k[32]; @@ -94,7 +94,7 @@ int monero_apdu_mlsag_prehash_update() { monero_io_skip(32); Bout = G_monero_vstate.io_buffer + G_monero_vstate.io_offset; monero_io_skip(32); - err = monero_io_fetch_decrypt(AKout, 32, TYPE_AMOUNT_KEY); + err = monero_io_fetch_decrypt(AKout, KEY_SIZE, TYPE_AMOUNT_KEY); if (err) { return err; } @@ -134,11 +134,11 @@ int monero_apdu_mlsag_prehash_update() { } // update destination hash control if (G_monero_vstate.io_protocol_version >= 2) { - err = monero_sha256_outkeys_update(Aout, 32); + err = monero_sha256_outkeys_update(Aout, KEY_SIZE); if (err) { return err; } - err = monero_sha256_outkeys_update(Bout, 32); + err = monero_sha256_outkeys_update(Bout, KEY_SIZE); if (err) { return err; } @@ -146,7 +146,7 @@ int monero_apdu_mlsag_prehash_update() { if (err) { return err; } - err = monero_sha256_outkeys_update(AKout, 32); + err = monero_sha256_outkeys_update(AKout, KEY_SIZE); if (err) { return err; } @@ -195,7 +195,7 @@ int monero_apdu_mlsag_prehash_update() { if (err) { return err; } - if (memcmp(k, G_monero_vstate.OUTK, 32) != 0) { + if (memcmp(k, G_monero_vstate.OUTK, KEY_SIZE) != 0) { return SW_SECURITY_COMMITMENT_CHAIN_CONTROL; } } diff --git a/src/monero_stealth.c b/src/monero_stealth.c index ba83251..9c4936e 100644 --- a/src/monero_stealth.c +++ b/src/monero_stealth.c @@ -27,14 +27,14 @@ /* ----------------------------------------------------------------------- */ int monero_apdu_stealth() { int i; - unsigned char pub[32]; - unsigned char sec[32]; + unsigned char pub[KEY_SIZE]; + unsigned char sec[KEY_SIZE]; unsigned char drv[33]; unsigned char payID[8]; int err = 0; // fetch pub - monero_io_fetch(pub, 32); + monero_io_fetch(pub, KEY_SIZE); // fetch sec err = monero_io_fetch_decrypt_key(sec, sizeof(sec)); if (err) { @@ -54,7 +54,7 @@ int monero_apdu_stealth() { } // compute mask - drv[32] = ENCRYPTED_PAYMENT_ID_TAIL; + drv[KEY_SIZE] = ENCRYPTED_PAYMENT_ID_TAIL; err = monero_keccak_F(drv, 33, sec); if (err) { explicit_bzero(sec, sizeof(sec)); diff --git a/src/monero_types.h b/src/monero_types.h index 13eba01..7119fb6 100644 --- a/src/monero_types.h +++ b/src/monero_types.h @@ -46,6 +46,8 @@ #define TESTNET_CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX 54 #define TESTNET_CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX 63 +#define KEY_SIZE 32 + enum network_type { #ifndef MONERO_ALPHA MAINNET = 0, @@ -71,9 +73,9 @@ struct monero_nv_state_s { unsigned int account_id; /* spend key */ - unsigned char b[32]; + unsigned char b[KEY_SIZE]; /* view key */ - unsigned char a[32]; + unsigned char a[KEY_SIZE]; /*words*/ #define WORDS_MAX_LENGTH 20 @@ -139,8 +141,8 @@ struct monero_v_state_s { unsigned int tx_sign_cnt; /* sc_add control */ - unsigned char last_derive_secret_key[32]; - unsigned char last_get_subaddress_secret_key[32]; + unsigned char last_derive_secret_key[KEY_SIZE]; + unsigned char last_get_subaddress_secret_key[KEY_SIZE]; /* ------------------------------------------ */ /* --- Crypo --- */ @@ -152,11 +154,11 @@ struct monero_v_state_s { /* SPK */ cx_aes_key_t spk; - unsigned char hmac_key[32]; + unsigned char hmac_key[KEY_SIZE]; /* Tx key */ - unsigned char R[32]; - unsigned char r[32]; + unsigned char R[KEY_SIZE]; + unsigned char r[KEY_SIZE]; /* prefix/mlsag hash */ cx_sha3_t keccakF; @@ -167,7 +169,7 @@ struct monero_v_state_s { /* -- track tx-in/out and commitment -- */ cx_sha256_t sha256_out_keys; - unsigned char OUTK[32]; + unsigned char OUTK[KEY_SIZE]; cx_sha256_t sha256_commitment; unsigned char C[32]; diff --git a/src/monero_ux_nano.c b/src/monero_ux_nano.c index 1f3d8ca..cdd03e4 100644 --- a/src/monero_ux_nano.c +++ b/src/monero_ux_nano.c @@ -290,17 +290,17 @@ void ui_export_viewkey_display(unsigned int value __attribute__((unused))) { unsigned int ui_menu_export_viewkey_action(unsigned int value) { unsigned int sw; - unsigned char x[32]; + unsigned char x[KEY_SIZE]; monero_io_discard(0); - explicit_bzero(x, 32); + explicit_bzero(x, sizeof(x)); sw = SW_OK; if (value == ACCEPT) { - monero_io_insert(G_monero_vstate.a, 32); + monero_io_insert(G_monero_vstate.a, KEY_SIZE); G_monero_vstate.export_view_key = EXPORT_VIEW_KEY; } else { - monero_io_insert(x, 32); + monero_io_insert(x, KEY_SIZE); G_monero_vstate.export_view_key = 0; } monero_io_insert_u16(sw); diff --git a/src/monero_ux_stax.c b/src/monero_ux_stax.c index 174d848..7189fb7 100644 --- a/src/monero_ux_stax.c +++ b/src/monero_ux_stax.c @@ -397,17 +397,17 @@ int ui_menu_any_pubaddr_display(unsigned int value __attribute__((unused)), unsi static void ui_menu_export_viewkey_action(bool value) { unsigned int sw; - unsigned char x[32]; + unsigned char x[KEY_SIZE]; monero_io_discard(0); - explicit_bzero(x, 32); + explicit_bzero(x, sizeof(x)); sw = SW_OK; if (value) { - monero_io_insert(G_monero_vstate.a, 32); + monero_io_insert(G_monero_vstate.a, KEY_SIZE); G_monero_vstate.export_view_key = EXPORT_VIEW_KEY; } else { - monero_io_insert(x, 32); + monero_io_insert(x, KEY_SIZE); G_monero_vstate.export_view_key = 0; } monero_io_insert_u16(sw);