Skip to content

Commit

Permalink
feature: message signing (CIP-8)
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Feb 14, 2024
1 parent 8b0896f commit 0c5d7fa
Show file tree
Hide file tree
Showing 26 changed files with 892 additions and 27 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [7.1.0](TBD) - [TBD]

Message signing (CIP-8)

### Added

- support for basic message signing (CIP-8, CIP-30)

### Changed

- TODO updated list of native tokens recognized by the app with correct decimal places


## [7.0.2](TBD) - [TBD]

Conway era
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
APPNAME = "Cardano ADA"

APPVERSION_M = 7
APPVERSION_N = 0
APPVERSION_P = 2
APPVERSION_N = 1
APPVERSION_P = 0
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

ifeq ($(BOLOS_SDK),)
Expand Down
9 changes: 9 additions & 0 deletions src/cardano.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

STATIC_ASSERT(LOVELACE_MAX_SUPPLY < LOVELACE_INVALID, "bad LOVELACE_INVALID");

#define ED25519_SIGNATURE_LENGTH 64

#define ADDRESS_KEY_HASH_LENGTH 28
#define POOL_KEY_HASH_LENGTH 28
#define VRF_KEY_HASH_LENGTH 32
Expand Down Expand Up @@ -180,4 +182,11 @@ typedef enum {

#endif // APP_FEATURE_NATIVE_SCRIPT_HASH

// ============================== CIP8 MESSAGE SIGNING ==============================

typedef enum {
CIP8_ADDRESS_FIELD_ADDRESS = 1,
CIP8_ADDRESS_FIELD_KEYHASH = 2,
} cip8_address_field_type_t;

#endif // H_CARDANO_APP_CARDANO
2 changes: 2 additions & 0 deletions src/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "deriveAddress.h"
#include "deriveNativeScriptHash.h"
#include "signTx.h"
#include "signMsg.h"
#include "signOpCert.h"
#include "signCVote.h"

Expand Down Expand Up @@ -39,6 +40,7 @@ handler_fn_t* lookupHandler(uint8_t ins)
CASE(0x22, signOpCert_handleAPDU);
#endif // APP_FEATURE_OPCERT
CASE(0x23, signCVote_handleAPDU);
CASE(0x24, signMsg_handleAPDU);

#ifdef DEVEL
// 0xF* - debug_mode related
Expand Down
7 changes: 3 additions & 4 deletions src/messageSigning.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include "securityPolicy.h"
#include "crypto.h"

static void signRawMessageWithPath(bip44_path_t* pathSpec,
const uint8_t* messageBuffer, size_t messageSize,
uint8_t* outBuffer, size_t outSize)
void signRawMessageWithPath(bip44_path_t* pathSpec,
const uint8_t* messageBuffer, size_t messageSize,
uint8_t* outBuffer, size_t outSize)
{
size_t sigLen = outSize;

Expand Down Expand Up @@ -37,7 +37,6 @@ static void signRawMessageWithPath(bip44_path_t* pathSpec,
#endif

ASSERT(sigLen == ED25519_SIGNATURE_LENGTH);

}

// sign the given hash by the private key derived according to the given path
Expand Down
4 changes: 4 additions & 0 deletions src/messageSigning.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include "bip44.h"

void signRawMessageWithPath(bip44_path_t* pathSpec,
const uint8_t* messageBuffer, size_t messageSize,
uint8_t* outBuffer, size_t outSize);

void getWitness(bip44_path_t* pathSpec,
const uint8_t* txHashBuffer, size_t txHashSize,
uint8_t* outBuffer, size_t outSize);
Expand Down
8 changes: 4 additions & 4 deletions src/runTests.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifdef DEVEL

#ifndef H_CARDANO_APP_RUN_TESTS
#define H_CARDANO_APP_RUN_TESTS

#ifdef DEVEL

#include "handlers.h"

handler_fn_t handleRunTests;

#endif // H_CARDANO_APP_RUN_TESTS

#endif // DEVEL

#endif // H_CARDANO_APP_RUN_TESTS
43 changes: 43 additions & 0 deletions src/securityPolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2183,3 +2183,46 @@ security_policy_t policyForSignCVoteWitness(bip44_path_t* path)
break;
}
}

security_policy_t policyForSignMsg(
const bip44_path_t* witnessPath,
cip8_address_field_type_t addressFieldType,
const addressParams_t* addressParams
)
{
switch (bip44_classifyPath(witnessPath)) {
case PATH_ORDINARY_SPENDING_KEY:
case PATH_ORDINARY_STAKING_KEY:
case PATH_MULTISIG_SPENDING_KEY:
case PATH_MULTISIG_STAKING_KEY:
case PATH_MINT_KEY:
case PATH_DREP_KEY:
case PATH_COMMITTEE_COLD_KEY:
case PATH_COMMITTEE_HOT_KEY:
case PATH_POOL_COLD_KEY:
// OK
break;
default:
DENY();
break;
}

if (addressFieldType == CIP8_ADDRESS_FIELD_ADDRESS) {
DENY_UNLESS(isValidAddressParams(addressParams));

switch (addressParams->type) {
case BASE_PAYMENT_KEY_STAKE_KEY:
case BASE_PAYMENT_KEY_STAKE_SCRIPT:
case REWARD_KEY:
case ENTERPRISE_KEY:
// OK
break;

default:
DENY();
break;
}
}

PROMPT();
}
6 changes: 6 additions & 0 deletions src/securityPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,10 @@ security_policy_t policyForSignCVoteInit();
security_policy_t policyForSignCVoteConfirm();
security_policy_t policyForSignCVoteWitness(bip44_path_t* path);

security_policy_t policyForSignMsg(
const bip44_path_t* witnessPath,
cip8_address_field_type_t addressFieldType,
const addressParams_t* addressParams
);

#endif // H_CARDANO_APP_SECURITY_POLICY
4 changes: 2 additions & 2 deletions src/signCVote.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void signCVote_handleInitAPDU(
default:
THROW(ERR_NOT_IMPLEMENTED);
}
handleInit_ui_runStep();
signCVote_handleInit_ui_runStep();
}

// ============================== VOTECAST CHUNK ==============================
Expand Down Expand Up @@ -190,7 +190,7 @@ void signCVote_handleConfirmAPDU(
}
}

handleConfirm_ui_runStep();
signCVote_handleConfirm_ui_runStep();
}

// ============================== WITNESS ==============================
Expand Down
8 changes: 4 additions & 4 deletions src/signCVote_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ static ins_sign_cvote_context_t* ctx = &(instructionState.signCVoteContext);

// ============================== INIT ==============================

void handleInit_ui_runStep()
void signCVote_handleInit_ui_runStep()
{
ui_callback_fn_t* this_fn = handleInit_ui_runStep;
ui_callback_fn_t* this_fn = signCVote_handleInit_ui_runStep;

UI_STEP_BEGIN(ctx->ui_step, this_fn);

Expand Down Expand Up @@ -92,11 +92,11 @@ void handleInit_ui_runStep()

// ============================== CONFIRM ==============================

void handleConfirm_ui_runStep()
void signCVote_handleConfirm_ui_runStep()
{
TRACE("UI step %d", ctx->ui_step);
TRACE_STACK_USAGE();
ui_callback_fn_t* this_fn = handleConfirm_ui_runStep;
ui_callback_fn_t* this_fn = signCVote_handleConfirm_ui_runStep;

UI_STEP_BEGIN(ctx->ui_step, this_fn);

Expand Down
5 changes: 3 additions & 2 deletions src/signCVote_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define H_CARDANO_APP_SIGN_CVOTE_UI

#include "uiHelpers.h"

// ============================== INIT ==============================

enum {
Expand All @@ -13,7 +14,7 @@ enum {
HANDLE_INIT_INVALID,
};

void handleInit_ui_runStep();
void signCVote_handleInit_ui_runStep();

// ============================== CONFIRM ==============================

Expand All @@ -23,7 +24,7 @@ enum {
HANDLE_CONFIRM_STEP_INVALID,
};

void handleConfirm_ui_runStep();
void signCVote_handleConfirm_ui_runStep();

// ============================== WITNESS ==============================

Expand Down
Loading

0 comments on commit 0c5d7fa

Please sign in to comment.