Skip to content

Commit

Permalink
Consolidate the view key components into one object.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Aug 30, 2024
1 parent 66c819b commit e91aabd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
19 changes: 10 additions & 9 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 2 additions & 13 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions rs/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions rs/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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 {
Expand Down

0 comments on commit e91aabd

Please sign in to comment.