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

Added unit tests for UI signer authorization module #116

Merged
Show file tree
Hide file tree
Changes from 11 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 .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Ledger UI's tests
working-directory: ledger/src/ui/test/
run: |
for d in attestation communication onboard pin unlock bootloader bolos_ux_handlers; do
for d in attestation signer_authorization communication onboard pin unlock bootloader bolos_ux_handlers; do
(cd "$d" && make clean test)
done

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ledger/src/ui/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ endif
# Convert signer iteration to what the compiler expects
ifneq ($(SIGNER_ITERATION),)
CONVERTED_SIGNER_ITERATION = $(shell python make-initial-signer-iteration.py $(SIGNER_ITERATION))
$(info Building with signer hash set to "$(SIGNER_ITERATION)")
$(info Building with signer iteration set to "$(SIGNER_ITERATION)")
CFLAGS += -DPARAM_INITIAL_SIGNER_ITERATION="$(CONVERTED_SIGNER_ITERATION)"
endif

Expand Down
3 changes: 2 additions & 1 deletion ledger/src/ui/src/attestation.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
#include "err.h"
#include "memutil.h"
#include "ints.h"
#include "runtime.h"

// Utility macros to save memory
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define PAGESIZE (APDU_TOTAL_DATA_SIZE_OUT - 1)
#define PAGECOUNT(itemcount) (((itemcount) + PAGESIZE - 1) / PAGESIZE)

// Global onboarding flag
extern const unsigned char* N_onboarded_ui[1];
extern NON_VOLATILE unsigned char* N_onboarded_ui[1];

// Attestation message prefix
const char att_msg_prefix[ATT_MSG_PREFIX_LENGTH] = ATT_MSG_PREFIX;
Expand Down
3 changes: 2 additions & 1 deletion ledger/src/ui/src/onboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
#include "err.h"
#include "os.h"
#include "onboard.h"
#include "runtime.h"

// Global onboarding flag
const unsigned char N_onboarded_ui[1];
NON_VOLATILE unsigned char N_onboarded_ui[1];

/*
* Reset the given onboard context
Expand Down
3 changes: 2 additions & 1 deletion ledger/src/ui/src/signer_authorization.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "memutil.h"
#include "ints.h"
#include "compiletime.h"
#include "runtime.h"

// Initial signer hash taken from an external definition (see Makefile for
// details)
Expand All @@ -50,7 +51,7 @@ static const uint16_t INITIAL_SIGNER_ITERATION = PARAM_INITIAL_SIGNER_ITERATION;
#endif

// Current signer status
static const sigaut_signer_status_t N_current_signer_status_var;
NON_VOLATILE sigaut_signer_status_t N_current_signer_status_var;
#define N_current_signer_status \
(*(sigaut_signer_status_t*)PIC(&N_current_signer_status_var))

Expand Down
1 change: 1 addition & 0 deletions ledger/src/ui/test/attestation/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SRCDIR = ../../src
COMMONDIR = ../../../common/src
MOCKDIR = ../mock
CFLAGS = -I$(COMMONDIR) -I$(MOCKDIR) -I$(SRCDIR)
CFLAGS += -DHSM_SIMULATOR

PROG = test.out
OBJS = mock.o attestation.o test_attestation.o
Expand Down
23 changes: 10 additions & 13 deletions ledger/src/ui/test/attestation/test_attestation.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "apdu_utils.h"
#include "assert_utils.h"
#include "err.h"
#include "runtime.h"

#define PUBLIC_KEY \
"\x0b\xe6\xd7\x1d\x5c\x2b\x06\x36\x03\x53\xfb\xd8\x22\x7a\xb3\xab\xfc\x3d" \
Expand All @@ -53,7 +54,7 @@ static cx_ecfp_private_key_t G_priv_key;
static unsigned char G_path[PUBKEY_PATH_LENGTH];

// Global onboarding flag
const unsigned char N_onboarded_ui[1];
unsigned char N_onboarded_ui[1];

// Helper functions
void set_public_key(cx_ecfp_public_key_t *pubkey, char *rawkey) {
Expand Down Expand Up @@ -123,10 +124,6 @@ void os_perso_derive_node_bip32(cx_curve_t curve,
memcpy(privateKey, PRIVATE_KEY, sizeof(PRIVATE_KEY));
}

void os_memmove(void *dst, const void *src, unsigned int length) {
memmove(dst, src, length);
}

// signer_authorization mocks
sigaut_signer_t *get_authorized_signer_info() {
memcpy(G_signer_info.hash, SIGNER_HASH, sizeof(SIGNER_HASH));
Expand All @@ -153,7 +150,7 @@ void test_get_attestation_ud_value() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 1;
*N_onboarded_ui = 1;
G_att_ctx.state = att_state_wait_ud_value;
// CLA + INS_ATTESTATION + ATT_OP_UD_VALUE + UD_VALUE
SET_APDU("\x80\x50\x01\x46\x8d\xa8\x7f\x6a\x85\xe6\x40\x93\x27\xe1\x17\xe8"
Expand Down Expand Up @@ -182,7 +179,7 @@ void test_get_attestation_ud_value_wrong_state() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 1;
*N_onboarded_ui = 1;
G_att_ctx.state = att_state_ready;
// CLA + INS_ATTESTATION + ATT_OP_UD_VALUE + UD_VALUE
SET_APDU("\x80\x50\x01\x46\x8d\xa8\x7f\x6a\x85\xe6\x40\x93\x27\xe1\x17\xe8"
Expand All @@ -209,7 +206,7 @@ void test_get_attestation_get_msg() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 1;
*N_onboarded_ui = 1;
memcpy(
G_att_ctx.msg,
"HSM:UI:3.0"
Expand Down Expand Up @@ -250,7 +247,7 @@ void test_get_attestation_get_msg_wrong_state() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 1;
*N_onboarded_ui = 1;
memcpy(
&G_att_ctx.msg,
"HSM:UI:3.0"
Expand Down Expand Up @@ -287,7 +284,7 @@ void test_get_attestation_get() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 1;
*N_onboarded_ui = 1;
G_att_ctx.state = att_state_ready;

// CLA + INS_ATTESTATION + ATT_OP_GET
Expand All @@ -304,7 +301,7 @@ void test_get_attestation_get_wrong_state() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 1;
*N_onboarded_ui = 1;
G_att_ctx.state = att_state_wait_ud_value;

// CLA + INS_ATTESTATION + ATT_OP_GET
Expand All @@ -329,7 +326,7 @@ void test_get_attestation_invalid() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 1;
*N_onboarded_ui = 1;
G_att_ctx.state = att_state_ready;
// CLA + INS_ATTESTATION + Invalid command
SET_APDU("\x80\x50\x99", rx);
Expand All @@ -353,7 +350,7 @@ void test_get_attestation_not_onboarded() {
unsigned int rx;

reset_attestation(&G_att_ctx);
*(unsigned char *)N_onboarded_ui = 0;
*N_onboarded_ui = 0;
G_att_ctx.state = att_state_ready;
// CLA + INS_ATTESTATION + ATT_OP_GET
SET_APDU("\x80\x50\x03", rx);
Expand Down
6 changes: 5 additions & 1 deletion ledger/src/ui/test/mock/mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ void nvm_write(void *dst_adr, void *src_adr, unsigned int src_len) {
// Treat as normal copy
memmove(dst_adr, src_adr, src_len);
}
}
}

void os_memmove(void *dst, const void *src, unsigned int length) {
memmove(dst, src, length);
}
29 changes: 28 additions & 1 deletion ledger/src/ui/test/mock/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@

#include "os_exceptions.h"
#include "apdu_utils.h"
#include "defs.h"

#define PIC(x) (x)

#define PARAM_SIGNERS_FILE testing
#define CX_CURVE_256K1 33

#define CX_NONE 0
#define CX_LAST 1

#define APDU_RETURN(offset) \
((uint16_t)(G_io_apdu_buffer[offset] << 8) | (G_io_apdu_buffer[offset + 1]))

Expand All @@ -45,9 +49,15 @@ struct mock_struct {
void *mock_data;
};

typedef char cx_md_t;
typedef uint8_t cx_curve_t;

typedef struct mock_struct cx_sha3_t;
typedef struct cx_hash_s {
unsigned char hash[HASHSIZE];
int size_in_bytes;
} cx_hash_t;

typedef cx_hash_t cx_sha3_t;
typedef struct cx_ecfp_public_key_s {
unsigned int W_len;
unsigned char W[65];
Expand Down Expand Up @@ -107,5 +117,22 @@ unsigned int bolos_ux_mnemonic_from_data(unsigned char *in,
void explicit_bzero(void *s, size_t len);
void nvm_write(void *dst_adr, void *src_adr, unsigned int src_len);
unsigned char *cx_rng(unsigned char *buffer, unsigned int len);
int cx_keccak_init(cx_sha3_t *hash, int size);
int cx_hash(cx_hash_t *hash,
int mode,
unsigned char *in,
unsigned int len,
unsigned char *out);
int cx_ecfp_init_public_key(cx_curve_t curve,
unsigned char *rawkey,
unsigned int key_len,
cx_ecfp_public_key_t *key);
int cx_ecdsa_verify(cx_ecfp_public_key_t *key,
int mode,
cx_md_t hashID,
unsigned char *hash,
unsigned int hash_len,
unsigned char *sig,
unsigned int sig_len);

#endif // __MOCK_H
5 changes: 3 additions & 2 deletions ledger/src/ui/test/onboard/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
# SOFTWARE.

SRCDIR = ../../src
COMMONDIR = ../../src_common
COMMONDIR = ../../../common/src
MOCKDIR = ../mock
CFLAGS = -I $(SRCDIR) -I $(MOCKDIR) -I ./
CFLAGS = -I$(SRCDIR) -I$(MOCKDIR) -I./ -I$(COMMONDIR)
CFLAGS += -DHSM_SIMULATOR

PROG = test.out
OBJS = mock.o onboard.o test_onboard.o
Expand Down
59 changes: 59 additions & 0 deletions ledger/src/ui/test/signer_authorization/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# The MIT License (MIT)
#
# Copyright (c) 2021 RSK Labs Ltd
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

SRCDIR = ../../src
SIGNERDIRS=$(SRCDIR)/signer_authorization_signers
COMMONDIR = ../../../common/src
MOCKDIR = ../mock
CFLAGS = -I$(MOCKDIR) -I$(COMMONDIR) -I$(SRCDIR) -I$(SIGNERDIRS)
CFLAGS += -DHSM_SIMULATOR

PARAM_INITIAL_SIGNER_HASH="\x09\x09\x66\x04\x52\xeb\x7a\x3a\x44\xb6\xca\x07$\
\xed\x0b\x9c\xcf\xdd\xb9\xa6\x99\x9e\xb4\xad\xc3\x99\x50\x91\x71\xd2\x68\xe7$\
\x3e"
CFLAGS += -DPARAM_INITIAL_SIGNER_HASH=\"$(PARAM_INITIAL_SIGNER_HASH)\"
CFLAGS += -DPARAM_INITIAL_SIGNER_ITERATION=1

PROG = test.out
OBJS = mock.o signer_authorization.o test_signer_authorization.o

all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^

test_signer_authorization.o: test_signer_authorization.c
$(CC) $(CFLAGS) -c -o $@ $^

signer_authorization.o: $(SRCDIR)/signer_authorization.c
$(CC) $(CFLAGS) -c -o $@ $^

mock.o: $(MOCKDIR)/mock.c
$(CC) $(CFLAGS) -c -o $@ $^

.PHONY: clean test

clean:
rm -f $(PROG) ./*.o

test: all
./$(PROG)
Loading