Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/generate public key api call #58

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ include $(BOLOS_SDK)/Makefile.defines

APPVERSION_M = 0
APPVERSION_N = 8
APPVERSION_P = 6
APPVERSION_P = 7
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

APP_LOAD_PARAMS = --path "44'/1'" --curve ed25519 --appFlags 0x240 $(COMMON_LOAD_PARAMS)
Expand Down
85 changes: 84 additions & 1 deletion src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ui/nano/flow_user_confirm_new_address.h"
#include "ui/nano/flow_user_confirm_blindsigning.h"
#include "ui/nano/flow_generating_addresses.h"
#include "ui/nano/flow_generating_public_keys.h"
#include "ui/nano/flow_signing.h"

#pragma GCC diagnostic error "-Wall"
Expand Down Expand Up @@ -147,7 +148,8 @@ uint32_t api_write_data_block(uint8_t block_number, const uint8_t *input_data,

uint32_t api_read_data_block(uint8_t block_number)
{
if (api.data.type != GENERATED_ADDRESSES && api.data.type != SIGNATURES) {
if (api.data.type != GENERATED_ADDRESSES &&
api.data.type != GENERATED_PUBLIC_KEYS && api.data.type != SIGNATURES) {
THROW(SW_COMMAND_NOT_ALLOWED);
}

Expand Down Expand Up @@ -381,6 +383,87 @@ uint32_t api_generate_address(uint8_t show_on_screen, const uint8_t *data,
return IO_ASYNCH_REPLY;
}

uint32_t api_generate_public_key(uint8_t show_on_screen, const uint8_t *data,
uint32_t len)
{
// maybe we need it ...
UNUSED(show_on_screen);

// don't allow command if an interactive flow already is running
if (api.flow_locked) {
THROW(SW_COMMAND_NOT_ALLOWED);
}

// was account selected?
if (!(api.bip32_path[BIP32_ACCOUNT_INDEX] & 0x80000000)) {
THROW(SW_ACCOUNT_NOT_VALID);
}

// if buffer contains data, throw exception
if (api.data.type != EMPTY) {
THROW(SW_COMMAND_NOT_ALLOWED);
}

// disable external read and write access before doing anything else
api.data.type = LOCKED;

if (len != sizeof(API_GENERATE_PUBLIC_KEYS_REQUEST)) {
THROW(SW_INCORRECT_LENGTH);
}

API_GENERATE_PUBLIC_KEYS_REQUEST req;
memcpy(&req, data, sizeof(req));

// check if too many public keys to generate
if (req.count > API_GENERATE_PUBLIC_KEYS_MAX_COUNT) {
THROW(SW_COMMAND_INVALID_DATA);
}

// check if MSBs set
if (!(req.bip32_index & 0x80000000) || !(req.bip32_change & 0x80000000)) {
THROW(SW_COMMAND_INVALID_DATA);
}

// bip32 change can be 0x80000000 or 0x80000001
if (req.bip32_change & 0x7ffffffe) {
THROW(SW_COMMAND_INVALID_DATA);
}

// check if there would be an overflow when generating public keys
if (!((req.bip32_index + req.count) & 0x80000000)) {
THROW(SW_COMMAND_INVALID_DATA);
}

// show "generating public keys ..."
if (!show_on_screen) {
flow_generating_public_keys();
}

api.bip32_path[BIP32_ADDRESS_INDEX] = req.bip32_index;
api.bip32_path[BIP32_CHANGE_INDEX] = req.bip32_change;

// wipe all data before buffer is used again
memset(api.data.buffer, 0, API_BUFFER_SIZE_BYTES);
for (uint32_t i = 0; i < req.count; i++) {
// with address_type
uint8_t ret =
public_key_generate(api.bip32_path, BIP32_PATH_LEN,
&api.data.buffer[i * PUBKEY_SIZE_BYTES]);

if (!ret) {
THROW(SW_UNKNOWN);
}
// generate next address
api.bip32_path[BIP32_ADDRESS_INDEX]++;
}

api.data.length = req.count * PUBKEY_SIZE_BYTES;

api.data.type = GENERATED_PUBLIC_KEYS;
io_send(NULL, 0, SW_OK);
return 0;
}

uint32_t api_prepare_signing(uint8_t has_remainder, const uint8_t *data,
uint32_t len)
{
Expand Down
13 changes: 13 additions & 0 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "iota_io.h"

#include "iota/address.h"
#include "iota/public_key.h"

#define IO_STRUCT struct __attribute__((packed, may_alias))

Expand All @@ -19,6 +20,7 @@ typedef enum {
USER_CONFIRMED_ESSENCE = 3,
SIGNATURES = 4,
LOCKED = 5,
GENERATED_PUBLIC_KEYS = 6,
} DATA_TYPE;

typedef enum {
Expand Down Expand Up @@ -119,6 +121,14 @@ typedef IO_STRUCT
}
API_GENERATE_ADDRESS_REQUEST;

typedef IO_STRUCT
{
uint32_t bip32_index;
uint32_t bip32_change;
uint32_t count;
}
API_GENERATE_PUBLIC_KEYS_REQUEST;


typedef IO_STRUCT
{
Expand Down Expand Up @@ -284,6 +294,9 @@ uint32_t api_sign(uint8_t p1);
uint32_t api_generate_address(uint8_t show_on_screen, const uint8_t *data,
uint32_t len);

uint32_t api_generate_public_key(uint8_t show_on_screen, const uint8_t *data,
uint32_t len);

#ifdef APP_DEBUG
uint32_t api_dump_memory(uint8_t pagenr);
uint32_t api_set_non_interactive_mode(uint8_t mode);
Expand Down
13 changes: 2 additions & 11 deletions src/iota/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "api.h"
#include "bech32.h"
#include "address.h"
#include "public_key.h"
#include "ed25519.h"
#include "macros.h"
#include "lib_standard_app/crypto_helpers.h"
Expand Down Expand Up @@ -52,18 +53,8 @@ uint8_t address_encode_bech32_hrp(const uint8_t *addr_with_type, char *bech32,
uint8_t address_generate(uint32_t *bip32_path, uint32_t bip32_path_length,
uint8_t *addr)
{
uint8_t raw_pubkey[65];

MUST(bip32_derive_with_seed_get_pubkey_256(
HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32_path,
bip32_path_length, raw_pubkey, NULL, CX_SHA512, NULL, 0) == CX_OK);

// convert Ledger pubkey to pubkey bytes
uint8_t pubkey_bytes[PUBKEY_SIZE_BYTES];

MUST(ed25519_public_key_to_bytes(raw_pubkey, pubkey_bytes));

// debug_print_hex(pubkey_bytes, 32, 16);
MUST(public_key_generate(bip32_path, bip32_path_length, pubkey_bytes));

// set ed25519 address_type
addr[0] = ADDRESS_TYPE_ED25519;
Expand Down
3 changes: 3 additions & 0 deletions src/iota/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
#define API_GENERATE_ADDRESSES_MAX_COUNT \
(API_BUFFER_SIZE_BYTES / ADDRESS_WITH_TYPE_SIZE_BYTES)

#define API_GENERATE_PUBLIC_KEYS_MAX_COUNT \
(API_BUFFER_SIZE_BYTES / PUBKEY_SIZE_BYTES)

// very coarse estimation how many inputs the essence could have
// we can safely assume that we need at least 32 bytes per input
// this allows us to safe 100+ bytes in the essence struct
Expand Down
36 changes: 36 additions & 0 deletions src/iota/public_key.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* address.c
*
* Created on: 16.10.2020
* Author: thomas
*/

#include <string.h>

#include "os.h"
#include "cx.h"
#include "api.h"
#include "public_key.h"
#include "ed25519.h"
#include "macros.h"
#include "lib_standard_app/crypto_helpers.h"

#pragma GCC diagnostic error "-Wall"
#pragma GCC diagnostic error "-Wextra"
#pragma GCC diagnostic error "-Wmissing-prototypes"

//#include "debugprintf.h"


uint8_t public_key_generate(uint32_t *bip32_path, uint32_t bip32_path_length,
uint8_t pubkey[PUBKEY_SIZE_BYTES])
{
uint8_t raw_pubkey[65];

MUST(bip32_derive_with_seed_get_pubkey_256(
HDW_ED25519_SLIP10, CX_CURVE_Ed25519, bip32_path,
bip32_path_length, raw_pubkey, NULL, CX_SHA512, NULL, 0) == CX_OK);

MUST(ed25519_public_key_to_bytes(raw_pubkey, pubkey));
return 1;
}
13 changes: 13 additions & 0 deletions src/iota/public_key.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* address.h
*
* Created on: 16.10.2020
* Author: thomas
*/

#pragma once

#include <stdint.h>

uint8_t public_key_generate(uint32_t *bip32_path, uint32_t bip32_path_length,
uint8_t *addr);
3 changes: 3 additions & 0 deletions src/iota_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ unsigned int iota_dispatch(const uint8_t ins, const uint8_t p1,
case INS_GENERATE_ADDRESS:
return api_generate_address(p1, input_data, len);

case INS_GENERATE_PUBLIC_KEY:
return api_generate_public_key(p1, input_data, len);

case INS_GET_DATA_BUFFER_STATE:
return api_get_data_buffer_state();

Expand Down
2 changes: 2 additions & 0 deletions src/iota_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ enum {

INS_SIGN_SINGLE = 0xa4,

INS_GENERATE_PUBLIC_KEY = 0xa5,

#ifdef APP_DEBUG
INS_DUMP_MEMORY = 0x66,
INS_SET_NON_INTERACTIVE_MODE = 0x67,
Expand Down
47 changes: 47 additions & 0 deletions src/ui/nano/flow_generating_public_keys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* generic_error.c
*
* Created on: 26.10.2020
* Author: thomas
*/

#include "os.h"
#include "ux.h"
#include "glyphs.h"

#include "os_io_seproxyhal.h"

#include "flow_main_menu.h"

#pragma GCC diagnostic error "-Wall"
#pragma GCC diagnostic error "-Wextra"

extern const ux_flow_step_t *const ux_main_menu;

// clang-format off
UX_STEP_TIMEOUT(
ux_generating_public_keys,
pbb,
1000, // show main menu after 1s
&ux_main_menu,
{
&C_x_icon_load,
"Generating",
"Public Keys ..."
}
);

UX_FLOW(
ux_flow_generating_public_keys,
&ux_generating_public_keys,
FLOW_END_STEP
);
// clang-format on

void flow_generating_public_keys()
{
ux_flow_init(0, ux_flow_generating_public_keys, NULL);

// show flow immediately
UX_WAIT_DISPLAYED();
}
9 changes: 9 additions & 0 deletions src/ui/nano/flow_generating_public_keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
*
* Created on: 26.10.2020
* Author: thomas
*/

#pragma once

void flow_generating_public_keys(void);
Binary file not shown.
Loading
Loading