mirror of
https://github.com/trezor/trezor-firmware.git
synced 2026-02-20 00:33:30 +01:00
deserialize public key
This commit is contained in:
@@ -33,16 +33,15 @@
|
||||
|
||||
#include "bip32.h"
|
||||
|
||||
|
||||
#ifndef USE_DBG_CONSOLE
|
||||
// temporary hack to allow compilation when DBG console is disabled
|
||||
ssize_t dbg_console_write(const void* data, size_t data_size);
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int (*hdnode_from_xpub) (uint32_t depth, uint32_t child_num,
|
||||
const uint8_t *chain_code, const uint8_t *public_key,
|
||||
const char *curve, HDNode *out);
|
||||
int (*hdnode_deserialize_public)(const char* str, uint32_t version,
|
||||
const char* curve, HDNode* node,
|
||||
uint32_t* fingerprint);
|
||||
} trezor_crypto_v1_t;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -27,7 +27,7 @@ ssize_t dbg_console_write(const void* data, size_t data_size) {
|
||||
#endif
|
||||
|
||||
const trezor_crypto_v1_t trezor_crypto_v1 = {
|
||||
.hdnode_from_xpub = hdnode_from_xpub,
|
||||
.hdnode_deserialize_public = hdnode_deserialize_public,
|
||||
};
|
||||
|
||||
const trezor_api_v1_t trezor_api_v1 = {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use crate::proto::ethereum::{EthereumGetPublicKey, EthereumPublicKey};
|
||||
use crate::proto::{
|
||||
common::HdNodeType,
|
||||
ethereum::{EthereumGetPublicKey, EthereumPublicKey},
|
||||
};
|
||||
use alloc::string::ToString;
|
||||
use trezor_app_sdk::{Result, crypto, log, ui};
|
||||
use trezor_app_sdk::{Result, crypto, log, ui, util};
|
||||
|
||||
pub fn get_public_key(msg: EthereumGetPublicKey) -> Result<EthereumPublicKey> {
|
||||
let long_string: &str = "asdfghjklqwertyuiopzxcvbnmASDFGHJKLQWERTYUIOPZXCVBNM0123456789\
|
||||
@@ -23,10 +26,26 @@ pub fn get_public_key(msg: EthereumGetPublicKey) -> Result<EthereumPublicKey> {
|
||||
let xpub = crypto::get_xpub(&msg.address_n)?;
|
||||
public_key.xpub = xpub.as_str().to_string();
|
||||
|
||||
// TODO: hexlify(resp.node.public_key).decode()
|
||||
let (node_ll, fingerprint) =
|
||||
trezor_app_sdk::util::hdnode_deserialize_public(xpub.as_str(), 0x0488b21e).unwrap();
|
||||
|
||||
|
||||
|
||||
let mut node = HdNodeType::default();
|
||||
node.depth = node_ll.depth;
|
||||
node.fingerprint = fingerprint;
|
||||
node.child_num = node_ll.child_num;
|
||||
node.chain_code = node_ll.chain_code.to_vec();
|
||||
node.public_key = node_ll.public_key.to_vec();
|
||||
|
||||
|
||||
if matches!(msg.show_display, Some(true)) {
|
||||
ui::show_public_key(&public_key.xpub)?;
|
||||
let xpub = util::hex_encode(&node.public_key);
|
||||
ui::show_public_key(xpub.as_str())?;
|
||||
}
|
||||
|
||||
public_key.node = node;
|
||||
|
||||
|
||||
Ok(public_key)
|
||||
}
|
||||
|
||||
@@ -100,6 +100,7 @@ unsafe extern "Rust" {
|
||||
|
||||
/// Result type alias
|
||||
pub use low_level_api::ApiError;
|
||||
pub use low_level_api::ffi::{HDNode, HasherType, curve_info, ecdsa_curve, bignum256};
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
pub use ipc::IpcMessage;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use core::ffi::c_void;
|
||||
|
||||
use super::ffi::{self, ipc_message_t};
|
||||
use super::ffi::{self, HDNode, ipc_message_t};
|
||||
use crate::log::info;
|
||||
|
||||
pub const TREZOR_API_SUPPORTED_VERSION: u32 = ffi::TREZOR_API_VERSION_1;
|
||||
@@ -212,27 +212,12 @@ pub fn system_exit_fatal(message: &str, file: &str, line: i32) -> ! {
|
||||
core::intrinsics::abort();
|
||||
}
|
||||
|
||||
pub fn hdnode_from_xpub(
|
||||
depth: u32,
|
||||
child_num: u32,
|
||||
chain_code: &[u8; 32],
|
||||
public_key: &[u8; 33],
|
||||
curve: &core::ffi::CStr,
|
||||
out: &mut ffi::HDNode,
|
||||
) -> Result<(), ApiError> {
|
||||
let res = unsafe {
|
||||
(get_crypto_or_die().hdnode_from_xpub)(
|
||||
depth,
|
||||
child_num,
|
||||
chain_code.as_ptr(),
|
||||
public_key.as_ptr(),
|
||||
curve.as_ptr(),
|
||||
out,
|
||||
)
|
||||
};
|
||||
if res == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ApiError::Failed)
|
||||
}
|
||||
unsafe extern "C" {
|
||||
pub fn hdnode_deserialize_public(
|
||||
str_: *const core::ffi::c_char,
|
||||
version: u32,
|
||||
curve: *const core::ffi::c_char,
|
||||
node: *mut HDNode,
|
||||
fingerprint: *mut u32,
|
||||
) -> core::ffi::c_int;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ extern crate alloc;
|
||||
|
||||
use ufmt::derive::uDebug;
|
||||
|
||||
use crate::low_level_api;
|
||||
use crate::sysevent::SysEvents;
|
||||
use crate::{HDNode, bignum256, curve_info, ecdsa_curve, low_level_api};
|
||||
|
||||
#[derive(uDebug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct Timeout(u32);
|
||||
@@ -69,3 +69,65 @@ impl<'a> AsRef<str> for SliceWriter<'a> {
|
||||
unsafe { core::str::from_utf8_unchecked(&self.slice[..self.pos]) }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hdnode_deserialize_public(serialized: &str, version: u32) -> Result<(HDNode, u32), ()> {
|
||||
let mut params = ecdsa_curve {
|
||||
prime: bignum256 { val: [0u32; 9] },
|
||||
G: crate::low_level_api::ffi::curve_point {
|
||||
x: bignum256 { val: [0u32; 9] },
|
||||
y: bignum256 { val: [0u32; 9] },
|
||||
},
|
||||
order: bignum256 { val: [0u32; 9] },
|
||||
order_half: bignum256 { val: [0u32; 9] },
|
||||
a: 0,
|
||||
b: bignum256 { val: [0u32; 9] },
|
||||
};
|
||||
|
||||
let curve_info = curve_info {
|
||||
bip32_name: b"secp256k1\0".as_ptr() as *const core::ffi::c_char,
|
||||
params: ¶ms,
|
||||
hasher_base58: 0,
|
||||
hasher_sign: 0,
|
||||
hasher_pubkey: 0,
|
||||
hasher_script: 0,
|
||||
};
|
||||
|
||||
let mut node = HDNode {
|
||||
depth: 0,
|
||||
child_num: 0,
|
||||
chain_code: [0u8; 32],
|
||||
private_key: [0u8; 32],
|
||||
public_key: [0u8; 33],
|
||||
private_key_extension: [0u8; 32],
|
||||
is_public_key_set: false,
|
||||
curve: &curve_info,
|
||||
};
|
||||
let mut fingerprint: u32 = 0;
|
||||
let result = unsafe {
|
||||
low_level_api::hdnode_deserialize_public(
|
||||
serialized.as_ptr() as *const core::ffi::c_char,
|
||||
version,
|
||||
b"secp256k1\0".as_ptr() as *const core::ffi::c_char,
|
||||
&mut node,
|
||||
&mut fingerprint,
|
||||
)
|
||||
};
|
||||
if result == 0 {
|
||||
Ok((node, fingerprint))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hex_encode(bytes: &[u8]) -> alloc::string::String {
|
||||
use alloc::string::String;
|
||||
let mut result = String::new();
|
||||
for byte in bytes {
|
||||
// Manually append hex digits
|
||||
let high = (byte >> 4) & 0x0F;
|
||||
let low = byte & 0x0F;
|
||||
result.push(char::from_digit(high as u32, 16).unwrap());
|
||||
result.push(char::from_digit(low as u32, 16).unwrap());
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user