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 0db9de4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 46 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
33 changes: 16 additions & 17 deletions app/src/review_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,32 @@ zxerr_t getNumItemsViewKey(uint8_t *num_items) {
if (num_items == NULL) {
return zxerr_no_data;
}
// Display [viewKey | ivk | ovk | HD path]
*num_items = 4;
// Display [xfvk | HD path]
*num_items = 2;
return zxerr_ok;
}

#define CHECK_PARSER_OK(CALL) \
do { \
cx_err_t __cx_err = CALL; \
if (__cx_err != parser_ok) { \
return zxerr_unknown; \
} \
} while (0)

zxerr_t getItemViewKey(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outVal, uint16_t outValLen, uint8_t pageIdx,
uint8_t *pageCount) {
ZEMU_LOGF(50, "[addr_getItem] %d/%d\n", displayIdx, pageIdx)

switch (displayIdx) {
case 0:
snprintf(outKey, outKeyLen, "ViewKey");
const char* viewKey = (const char*)G_io_apdu_buffer;
pageStringHex(outVal, outValLen, viewKey, 2 * KEY_LENGTH, pageIdx, pageCount);
break;
case 1:
snprintf(outKey, outKeyLen, "IVK");
const char* ivk = (const char*)G_io_apdu_buffer + 3 * KEY_LENGTH;
pageStringHex(outVal, outValLen, ivk, KEY_LENGTH, pageIdx, pageCount);
snprintf(outKey, outKeyLen, "Ext Full View Key");
const uint8_t* xfvk = G_io_apdu_buffer;
char tmp_buf[300] = {0};
CHECK_PARSER_OK(crypto_encodeLargeBech32(xfvk, EXTENDED_FVK_LEN, (uint8_t*) tmp_buf, sizeof(tmp_buf), 0));
pageString(outVal, outValLen, (const char*) tmp_buf, pageIdx, pageCount);
break;
case 2:
snprintf(outKey, outKeyLen, "OVK");
const char* ovk = (const char*)G_io_apdu_buffer + 2 * KEY_LENGTH;
pageStringHex(outVal, outValLen, ovk, KEY_LENGTH, pageIdx, pageCount);
break;

case 3: {
case 1: {
snprintf(outKey, outKeyLen, "HD Path");
char buffer[200] = {0};
bip32_to_str(buffer, sizeof(buffer), hdPath, hdPathLen);
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 0db9de4

Please sign in to comment.