From e91aabd2d4235e35f7cddae7f86a8d197f91bd06 Mon Sep 17 00:00:00 2001 From: Murisi Tarusenga Date: Fri, 30 Aug 2024 10:37:05 +0200 Subject: [PATCH] Consolidate the view key components into one object. --- app/src/crypto.c | 19 ++++++++++--------- rs/src/lib.rs | 15 ++------------- rs/src/params.rs | 2 ++ rs/src/utils.rs | 9 ++------- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/app/src/crypto.c b/app/src/crypto.c index 38a6c52..540c6e4 100644 --- a/app/src/crypto.c +++ b/app/src/crypto.c @@ -528,16 +528,17 @@ __Z_INLINE zxerr_t copyKeys(keys_t *saplingKeys, key_kind_e requestedKeys, uint8 break; case ViewKeys: - if (outputLen < 6 * KEY_LENGTH + TAG_LENGTH) { + if (outputLen < 5 * KEY_LENGTH + 2 * TAG_LENGTH + 1) { return zxerr_buffer_too_small; } - memcpy(output, saplingKeys->ak, KEY_LENGTH); - memcpy(output + KEY_LENGTH, saplingKeys->nk, KEY_LENGTH); - memcpy(output + 2 * KEY_LENGTH, saplingKeys->ovk, KEY_LENGTH); - memcpy(output + 3 * KEY_LENGTH, saplingKeys->ivk, KEY_LENGTH); - memcpy(output + 4 * KEY_LENGTH, saplingKeys->dk, KEY_LENGTH); - memcpy(output + 5 * KEY_LENGTH, saplingKeys->chain_code, KEY_LENGTH); - memcpy(output + 6 * KEY_LENGTH, saplingKeys->parent_fvk_tag, TAG_LENGTH); + memcpy(output, &hdPathLen, 1); + memcpy(output + 1, saplingKeys->parent_fvk_tag, TAG_LENGTH); + memcpy(output + 5, &hdPath[hdPathLen - 1], TAG_LENGTH); + memcpy(output + 9, saplingKeys->chain_code, KEY_LENGTH); + memcpy(output + 41, saplingKeys->ak, KEY_LENGTH); + memcpy(output + 73, saplingKeys->nk, KEY_LENGTH); + memcpy(output + 105, saplingKeys->ovk, KEY_LENGTH); + memcpy(output + 137, saplingKeys->dk, KEY_LENGTH); break; case ProofGenerationKey: @@ -616,7 +617,7 @@ zxerr_t crypto_fillMASP(uint8_t *buffer, uint16_t bufferLen, uint16_t *cmdRespon break; case ViewKeys: - *cmdResponseLen = 6 * KEY_LENGTH + TAG_LENGTH; + *cmdResponseLen = 5 * KEY_LENGTH + 2 * TAG_LENGTH + 1; break; case ProofGenerationKey: diff --git a/rs/src/lib.rs b/rs/src/lib.rs index 7896d70..d4d56b2 100644 --- a/rs/src/lib.rs +++ b/rs/src/lib.rs @@ -34,7 +34,7 @@ pub use params::{ InstructionCode, KeyResponse, NamadaKeys, ADDRESS_LEN, CLA, ED25519_PUBKEY_LEN, PK_LEN_PLUS_TAG, SIG_LEN_PLUS_TAG, }; -use params::{KEY_LEN, SALT_LEN, TAG_LEN}; +use params::{KEY_LEN, SALT_LEN, XFVK_LEN}; use utils::{ ResponseAddress, ResponseGetConvertRandomness, ResponseGetOutputRandomness, ResponseGetSpendRandomness, ResponseMaspSign, ResponseProofGenKey, ResponsePubAddress, @@ -369,19 +369,8 @@ where public_address: response_data[..KEY_LEN].try_into().unwrap(), })), NamadaKeys::ViewKey => { - let (view_key, rest) = response_data.split_at(2 * KEY_LEN); - let (ovk, rest) = rest.split_at(KEY_LEN); - let (ivk, rest) = rest.split_at(KEY_LEN); - let (dk, rest) = rest.split_at(KEY_LEN); - let (chain_code, rest) = rest.split_at(KEY_LEN); - let (parent_fvk_tag, _) = rest.split_at(TAG_LEN); Ok(KeyResponse::ViewKey(ResponseViewKey { - view_key: view_key.try_into().unwrap(), - ovk: ovk.try_into().unwrap(), - ivk: ivk.try_into().unwrap(), - dk: dk.try_into().unwrap(), - chain_code: chain_code.try_into().unwrap(), - parent_fvk_tag: parent_fvk_tag.try_into().unwrap(), + xfvk: response_data[..XFVK_LEN].try_into().unwrap(), })) } NamadaKeys::ProofGenerationKey => { diff --git a/rs/src/params.rs b/rs/src/params.rs index c4c8358..5ea101f 100644 --- a/rs/src/params.rs +++ b/rs/src/params.rs @@ -26,6 +26,8 @@ pub const CLA: u8 = 0x57; pub const KEY_LEN: usize = 32; /// MASP tag length pub const TAG_LEN: usize = 4; +/// MASP extended full viewing key length +pub const XFVK_LEN: usize = 1 + 2*TAG_LEN + 5*KEY_LEN; /// Public Key Length pub const ED25519_PUBKEY_LEN: usize = 32; /// Public Key + Tag Length diff --git a/rs/src/utils.rs b/rs/src/utils.rs index f9036d6..f5aa539 100644 --- a/rs/src/utils.rs +++ b/rs/src/utils.rs @@ -22,7 +22,7 @@ use std::error::Error; const HARDENED: u32 = 0x80000000; use crate::params::{ - ADDRESS_LEN, ED25519_PUBKEY_LEN, KEY_LEN, PK_LEN_PLUS_TAG, SALT_LEN, SIG_LEN_PLUS_TAG, TAG_LEN, + ADDRESS_LEN, ED25519_PUBKEY_LEN, KEY_LEN, PK_LEN_PLUS_TAG, SALT_LEN, SIG_LEN_PLUS_TAG, XFVK_LEN, }; use byteorder::{LittleEndian, WriteBytesExt}; @@ -48,12 +48,7 @@ pub struct ResponsePubAddress { } pub struct ResponseViewKey { - pub view_key: [u8; 2 * KEY_LEN], - pub ivk: [u8; KEY_LEN], - pub ovk: [u8; KEY_LEN], - pub dk: [u8; KEY_LEN], - pub chain_code: [u8; KEY_LEN], - pub parent_fvk_tag: [u8; TAG_LEN], + pub xfvk: [u8; XFVK_LEN], } pub struct ResponseProofGenKey {