Skip to content

Commit

Permalink
Added unit tests for BIP39 <-> SSKR roundtrip
Browse files Browse the repository at this point in the history
  • Loading branch information
aido committed Nov 9, 2023
1 parent ff99f62 commit d5a076d
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 33 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Change log

## [1.5.1] - 2023-11-04
## [1.5.1] - 2023-11-09
### Added
- Added unit tests for shamir
- Added unit tests for SSKR
- Added unit tests for BIP39 <-> SSKR roundtrip

### Changed
- Reduce size of Nano binaries slightly by removing duplicate flows
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

# Seed Tool: A Ledger application that provides some useful seed management utilities

[![License](https://img.shields.io/github/license/aido/app-seed-tool)](https://github.com/aido/app-seed-tool/blob/develop/LICENSE)
[![Release](https://img.shields.io/github/release/aido/app-seed-tool)](https://github.com/aido/app-seed-tool/releases)
![nanos](https://img.shields.io/badge/nanos-working-green)
![nanox](https://img.shields.io/badge/nanox-working-green])
![nanosp](https://img.shields.io/badge/nanosp-working-green)
![stax](https://img.shields.io/badge/stax-in_progress-orange)
[![License](https://img.shields.io/github/license/aido/app-seed-tool)](https://github.com/aido/app-seed-tool/blob/develop/LICENSE)

[![Build app-seed-tool](https://github.com/aido/app-seed-tool/actions/workflows/ci-workflow.yml/badge.svg)](https://github.com/aido/app-seed-tool/actions/workflows/ci-workflow.yml)
[![CodeQL](https://github.com/aido/app-seed-tool/actions/workflows/codeql-workflow.yml/badge.svg)](https://github.com/aido/app-seed-tool/actions/workflows/codeql-workflow.yml)
[![Code style check](https://github.com/aido/app-seed-tool/actions/workflows/lint-workflow.yml/badge.svg)](https://github.com/aido/app-seed-tool/actions/workflows/lint-workflow.yml)
[![Ledger rule enforcer](https://github.com/aido/app-seed-tool/actions/workflows/ledger-rule-enforcer.yml/badge.svg)](https://github.com/aido/app-seed-tool/actions/workflows/ledger-rule-enforcer.yml)
[![codecov](https://codecov.io/gh/aido/app-seed-tool/branch/develop/graph/badge.svg?token=uCkGEbhGl3)](https://codecov.io/gh/aido/app-seed-tool/tree/develop)

Use the utilities provided by this Ledger application to check a backed up seed or generate [Shamir's Secret Sharing (SSS)](https://en.wikipedia.org/wiki/Shamir%27s_secret_sharing) for a seed.
Expand Down
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

### Todo

- [ ] Update automated tests to test on nanox and nanosp
- [ ] Update automated function tests to test on nanox and nanosp
- [ ] Save memory by setting the SSKR word buffer (G_bolos_ux_context.sskr_words_buffer) to a sensible size. Maybe just store SSKR Bytewords as shorter two letter minimal Bytewords rather than a 4 letter Byteword plus spaace for each share. Convert minimal ByteWords back to four letter Bytewords just prior to display.
- [ ] If/when the `cx_bn_gf2_n_mul()` syscall is available on Ledger Nano S change all Galois Field functionality to use syscalls.
- See [gf_syscalls](https://github.com/aido/app-seed-tool/tree/gf_syscalls) branch of repo.

### In Progress

Expand Down
2 changes: 1 addition & 1 deletion src/nano/nanos_enter_phrase.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ void compare_recovery_phrase(void) {
}
PRINTF("Root key from device: \n%.*H\n", 64, buffer_device);

bool memcmp_ret = os_secure_memcmp(buffer, buffer_device, 64) == 0 ? 0 : 1;
bool memcmp_ret = (os_secure_memcmp(buffer, buffer_device, 64) == 0) ? 0 : 1;
memzero(buffer, 64);
memzero(buffer_device, 64);

Expand Down
2 changes: 1 addition & 1 deletion src/nano/nanox_enter_phrase.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ static uint8_t compare_recovery_phrase(void) {
PRINTF("Root key from device: \n%.*H\n", 64, buffer_device);

// compare both rootkey
uint8_t ret = os_secure_memcmp(buffer, buffer_device, 64) != 0 ? 0 : 1;
uint8_t ret = (os_secure_memcmp(buffer, buffer_device, 64) != 0) ? 0 : 1;
memzero(buffer, 64);
memzero(buffer_device, 64);
return ret;
Expand Down
10 changes: 5 additions & 5 deletions src/ux_common/onboarding_seed_sskr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

// Returns the CRC-32 checksum of the input buffer in network byte order (big endian).
uint32_t cx_crc32_hw_nbo(const uint8_t *bytes, size_t len) {
#if BYTE_ORDER == BIG_ENDIAN
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return cx_crc32_hw(bytes, len);
#elif BYTE_ORDER == LITTLE_ENDIAN
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return os_swap_u32(cx_crc32_hw(bytes, len));
#else
#error "What kind of system is this?"
Expand All @@ -23,7 +23,7 @@ unsigned int bolos_ux_sskr_size_get(unsigned int bip39_onboarding_kind,
unsigned int groups_threshold,
unsigned int *group_descriptor,
unsigned int groups_len,
unsigned int *share_len) {
size_t *share_len) {
sskr_group_descriptor groups[groups_len];
for (uint8_t i = 0; i < (uint8_t) groups_len; i++) {
groups[i].threshold = *(group_descriptor + i * sizeof(*(group_descriptor)) / groups_len);
Expand Down Expand Up @@ -87,10 +87,10 @@ unsigned int bolos_ux_sskr_generate(unsigned int groups_threshold,
unsigned int groups_len,
unsigned char *seed,
unsigned int seed_len,
unsigned int *share_len,
size_t *share_len,
unsigned char *share_buffer,
unsigned int share_buffer_len,
unsigned int share_len_expected,
size_t share_len_expected,
unsigned int share_count_expected) {
sskr_group_descriptor groups[groups_len];
for (uint8_t i = 0; i < (uint8_t) groups_len; i++) {
Expand Down
20 changes: 14 additions & 6 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,31 @@ set(PICKY_DEVELOPER OFF CACHE BOOL "CMocka: Build with picky developer flags" FO
FetchContent_MakeAvailable(cmocka)

add_compile_definitions(TEST DEBUG=0 SKIP_FOR_CMOCKA)
add_compile_definitions(HAVE_HASH HAVE_HMAC HAVE_SHA256)
add_compile_definitions(HAVE_HASH HAVE_HMAC HAVE_SHA256 HAVE_SHA512 HAVE_PBKDF2 HAVE_ECC HAVE_CRC HAVE_RNG IO_HID_EP_LENGTH=64)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../src/bc-sskr ${CMAKE_CURRENT_SOURCE_DIR}/../../src/bc-sskr/bc-shamir ${CMAKE_CURRENT_SOURCE_DIR}/lib $ENV{LEDGER_SECURE_SDK}/include $ENV{LEDGER_SECURE_SDK}/lib_cxng/src $ENV{LEDGER_SECURE_SDK}/lib_cxng/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib $ENV{LEDGER_SECURE_SDK}/include $ENV{LEDGER_SECURE_SDK}/lib_cxng/src $ENV{LEDGER_SECURE_SDK}/lib_cxng/include $ENV{LEDGER_SECURE_SDK}/lib_ux/include $ENV{LEDGER_SECURE_SDK}/lib_bagl/include)

# add src
set(LIB_SOURCES ./lib/random.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_ram.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_hash.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_sha256.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_hmac.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_utils.c)
set(LIB_SOURCES ./lib/testutils.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_ram.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_hash.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_sha256.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_sha512.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_pbkdf2.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_hmac.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_utils.c)
add_library(testutils SHARED ${LIB_SOURCES})

# add cmocka tests
add_library(shamir SHARED ../../src/bc-sskr/bc-shamir/shamir.c ../../src/bc-sskr/bc-shamir/interpolate.c ../../src/bc-sskr/bc-shamir/hazmat.c)
target_include_directories(shamir PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../src/bc-sskr/bc-shamir)

add_library(sskr SHARED ../../src/bc-sskr/sskr.c)
target_include_directories(sskr PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../src/bc-sskr/bc-shamir ${CMAKE_CURRENT_SOURCE_DIR}/../../src/bc-sskr)

# add cmocka tests
add_executable(test_shamir tests/shamir.c)
target_link_libraries(test_shamir PUBLIC cmocka gcov testutils shamir)

add_library(sskr SHARED ../../src/bc-sskr/sskr.c)
add_executable(test_sskr tests/sskr.c)
target_link_libraries(test_sskr PUBLIC cmocka gcov testutils sskr shamir)

foreach(target test_shamir test_sskr)
add_executable(test_roundtrip ./tests/roundtrip.c ../../src/ux_common/onboarding_seed_rom_variables.c ../../src/ux_common/onboarding_seed_bip39.c ../../src/ux_common/onboarding_seed_sskr.c)
target_include_directories(test_roundtrip PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../src ${CMAKE_CURRENT_SOURCE_DIR}/../../src/ux_common)
target_link_libraries(test_roundtrip PUBLIC cmocka gcov testutils sskr shamir)

foreach(target test_shamir test_sskr test_roundtrip)
add_test(NAME ${target} COMMAND ${target})
endforeach()
Empty file added tests/unit/lib/glyphs.h
Empty file.
10 changes: 0 additions & 10 deletions tests/unit/lib/random.c

This file was deleted.

91 changes: 91 additions & 0 deletions tests/unit/lib/testutils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdint.h>
#include <stddef.h>

// Fake random generator used only for testing
void cx_rng_no_throw(uint8_t *buffer, size_t len)
{
for (size_t i = 0; i < len; i++) {
buffer[i] = (uint8_t) i;
}
}

// Fake secure memory comparison used only for testing
char os_secure_memcmp(const void *src1, const void *src2, size_t length)
{
#define SRC1 ((unsigned char const *) src1)
#define SRC2 ((unsigned char const *) src2)
unsigned int l = length;
unsigned char xoracc = 0;
// don't || to ensure all condition are evaluated
while (!(!length && !l)) {
length--;
xoracc |= SRC1[length] ^ SRC2[length];
l--;
}
return xoracc;
}

static unsigned int cx_ccitt32[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};

unsigned int cx_crc32_update(unsigned int crc, const void *buf, size_t len)
{
const uint8_t *p = buf;
size_t i;

for (i = 0; i < len; i++) {
crc = cx_ccitt32[(crc ^ *p++) & 0xff] ^ (crc >> 8u);
}

return crc ^ 0xFFFFFFFF;
}

uint32_t cx_crc32_hw(const void *buf, size_t len)
{
uint32_t crc;
crc = cx_crc32_update(0xFFFFFFFF, buf, len);
return crc;
}
2 changes: 1 addition & 1 deletion tests/unit/lib/testutils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef TESTUTILS_H
#define TESTUTILS_H

unsigned char *fake_rng(uint8_t *buffer, size_t len);
#define WIDE

#endif /* TESTUTILS_H */
Loading

0 comments on commit d5a076d

Please sign in to comment.