From d073a5a2e31cdabcdd1d2d1fcac7fd15f198e617 Mon Sep 17 00:00:00 2001 From: Aido Date: Fri, 31 May 2024 20:19:27 +0100 Subject: [PATCH] Use internal bn_gf2_n_mul() instead of cx_bn_gf2_n_mul() syscall --- CHANGELOG.md | 5 +++++ Makefile | 13 +++++++++---- src/sskr/sskr.c | 20 +++++++++---------- src/sskr/sss/interpolate.c | 40 ++++++++++++++++++-------------------- src/sskr/sss/sss.c | 12 ++++++------ tests/unit/CMakeLists.txt | 2 +- 6 files changed, 49 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ef72fc8..1bb48851 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.7.4-rc.1] - 2024-05-31 + +### Fixed +- Use internal `bn_gf2_n_mul()` instead of `cx_bn_gf2_n_mul()` syscall + ## [1.7.3] - 2024-05-29 ### Fixed diff --git a/Makefile b/Makefile index 272f898b..d4d51819 100755 --- a/Makefile +++ b/Makefile @@ -26,10 +26,15 @@ all: default # Main app configuration APPNAME = "Seed Tool" -APPVERSION_M = 1 -APPVERSION_N = 7 -APPVERSION_P = 3 -APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)" +APPVERSION_M = 1 +APPVERSION_N = 7 +APPVERSION_P = 4 +APPVERSION_RC = 1 +APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)" + +ifdef APPVERSION_RC + APPVERSION := $(APPVERSION)-rc.$(APPVERSION_RC) +endif APP_LOAD_PARAMS = --appFlags 0x10 $(COMMON_LOAD_PARAMS) --curve secp256k1 --path "" diff --git a/src/sskr/sskr.c b/src/sskr/sskr.c index e3b3f98d..c7c995ef 100644 --- a/src/sskr/sskr.c +++ b/src/sskr/sskr.c @@ -143,9 +143,9 @@ static int16_t sskr_deserialize_shard(const uint8_t *source, shard->value_len = source_len - SSKR_METADATA_LENGTH_BYTES; memcpy(shard->value, source + SSKR_METADATA_LENGTH_BYTES, shard->value_len); - int16_t err = sskr_check_secret_length(shard->value_len); - if (err) { - return err; + int16_t error = sskr_check_secret_length(shard->value_len); + if (error) { + return error; } return shard->value_len; } @@ -231,9 +231,9 @@ static int16_t sskr_generate_shards_internal(uint8_t group_threshold, uint16_t shards_size, unsigned char *(*random_generator)(uint8_t *, size_t)) { - int16_t err = sskr_check_secret_length(master_secret_len); - if (err) { - return err; + int16_t error = sskr_check_secret_length(master_secret_len); + if (error) { + return error; } // Figure out how many shards we are dealing with @@ -314,9 +314,9 @@ int16_t sskr_generate_shards(uint8_t group_threshold, uint8_t *output, uint16_t buffer_size, unsigned char *(*random_generator)(uint8_t *, size_t)) { - int16_t err = sskr_check_secret_length(master_secret_len); - if (err) { - return err; + int16_t error = sskr_check_secret_length(master_secret_len); + if (error) { + return error; } // Figure out how many shards we are dealing with @@ -332,8 +332,6 @@ int16_t sskr_generate_shards(uint8_t group_threshold, return SSKR_ERROR_INSUFFICIENT_SPACE; } - int16_t error = 0; - // allocate space for shard representations sskr_shard_t shards[SSS_MAX_SHARE_COUNT * SSKR_MAX_GROUP_COUNT]; diff --git a/src/sskr/sss/interpolate.c b/src/sskr/sss/interpolate.c index de1e49c8..77d243b2 100644 --- a/src/sskr/sss/interpolate.c +++ b/src/sskr/sss/interpolate.c @@ -23,7 +23,6 @@ // Minimal required bytes for BN storing a GF(256) value #define GF2_8_MPI_BYTES 16 -#if defined(TARGET_NANOS) && !defined API_LEVEL /** * @brief Performs a multiplication over GF(2^n). * @@ -45,11 +44,11 @@ * - CX_INVALID_PARAMETER * - CX_MEMORY_FULL */ -cx_err_t cx_bn_gf2_n_mul(cx_bn_t bn_r, - const cx_bn_t bn_a, - const cx_bn_t bn_b, - const cx_bn_t bn_n, - const cx_bn_t bn_h __attribute__((unused))) { +cx_err_t bn_gf2_n_mul(cx_bn_t bn_r, + const cx_bn_t bn_a, + const cx_bn_t bn_b, + const cx_bn_t bn_n, + const cx_bn_t bn_h __attribute__((unused))) { cx_err_t error = CX_OK; uint32_t degree, nbits_a, nbits_b; @@ -110,7 +109,6 @@ cx_err_t cx_bn_gf2_n_mul(cx_bn_t bn_r, end: return error; } -#endif cx_err_t interpolate(uint8_t n, const uint8_t* xi, @@ -167,31 +165,31 @@ cx_err_t interpolate(uint8_t n, // Calculate the inverse of the denominator // In GF(2^8) the inverse of x = x^254 // bn_result = bn_denominator^2 - CX_CHECK(cx_bn_gf2_n_mul(bn_result, bn_denominator, bn_denominator, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_result, bn_denominator, bn_denominator, bn_n, bn_r2)); // bn_result = bn_denominator^4 - CX_CHECK(cx_bn_gf2_n_mul(bn_result, bn_result, bn_result, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_result, bn_result, bn_result, bn_n, bn_r2)); // bn_tempa = bn_denominator^8 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempa, bn_result, bn_result, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempa, bn_result, bn_result, bn_n, bn_r2)); // bn_tempb = bn_denominator^9 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempb, bn_tempa, bn_denominator, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempb, bn_tempa, bn_denominator, bn_n, bn_r2)); // bn_tempa = bn_denominator^16 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempa, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempa, bn_n, bn_r2)); // bn_tempa = bn_denominator^25 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempb, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempb, bn_n, bn_r2)); // bn_tempa = bn_denominator^50 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempa, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempa, bn_n, bn_r2)); // bn_tempb = bn_denominator^100 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempb, bn_tempa, bn_tempa, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempb, bn_tempa, bn_tempa, bn_n, bn_r2)); // bn_tempb = bn_denominator^200 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempb, bn_tempb, bn_tempb, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempb, bn_tempb, bn_tempb, bn_n, bn_r2)); // bn_tempa = bn_denominator^250 - CX_CHECK(cx_bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempb, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempa, bn_tempa, bn_tempb, bn_n, bn_r2)); // bn_denominator = bn_denominator^254 - CX_CHECK(cx_bn_gf2_n_mul(bn_denominator, bn_result, bn_tempa, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_denominator, bn_result, bn_tempa, bn_n, bn_r2)); // Calculate the lagrange basis coefficient - CX_CHECK(cx_bn_gf2_n_mul(bn_lagrange, bn_numerator, bn_lagrange, bn_n, bn_r2)); - CX_CHECK(cx_bn_gf2_n_mul(bn_lagrange, bn_denominator, bn_lagrange, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_lagrange, bn_numerator, bn_lagrange, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_lagrange, bn_denominator, bn_lagrange, bn_n, bn_r2)); } } @@ -199,7 +197,7 @@ cx_err_t interpolate(uint8_t n, CX_CHECK(cx_bn_set_u32(bn_tempa, (uint32_t) yij[i][j])); CX_CHECK(cx_bn_set_u32(bn_tempb, (uint32_t) result[j])); - CX_CHECK(cx_bn_gf2_n_mul(bn_tempa, bn_lagrange, bn_tempa, bn_n, bn_r2)); + CX_CHECK(bn_gf2_n_mul(bn_tempa, bn_lagrange, bn_tempa, bn_n, bn_r2)); CX_CHECK(cx_bn_xor(bn_result, bn_tempa, bn_tempb)); CX_CHECK(cx_bn_get_u32(bn_result, &result_u32)); result[j] = (uint8_t) result_u32; diff --git a/src/sskr/sss/sss.c b/src/sskr/sss/sss.c index 21c03fb3..e70e4ce0 100644 --- a/src/sskr/sss/sss.c +++ b/src/sskr/sss/sss.c @@ -88,9 +88,9 @@ int16_t sss_split_secret(uint8_t threshold, uint8_t secret_length, uint8_t *result, unsigned char *(*random_generator)(uint8_t *, size_t)) { - int16_t err = sss_validate_parameters(threshold, share_count, secret_length); - if (err) { - return err; + int16_t error = sss_validate_parameters(threshold, share_count, secret_length); + if (error) { + return error; } if (threshold == 1) { @@ -146,9 +146,9 @@ int16_t sss_recover_secret(uint8_t threshold, const uint8_t **shares, uint8_t share_length, uint8_t *secret) { - int16_t err = sss_validate_parameters(threshold, threshold, share_length); - if (err) { - return err; + int16_t error = sss_validate_parameters(threshold, threshold, share_length); + if (error) { + return error; } uint8_t digest[SSS_MAX_SECRET_SIZE]; diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index fee8040a..57649e95 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -110,7 +110,7 @@ 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(TARGET_NANOS HAVE_HASH HAVE_HMAC HAVE_SHA224 HAVE_SHA256 HAVE_SHA512 HAVE_PBKDF2 HAVE_ECC HAVE_CRC HAVE_RNG IO_HID_EP_LENGTH=64) +add_compile_definitions(HAVE_HASH HAVE_HMAC HAVE_SHA224 HAVE_SHA256 HAVE_SHA512 HAVE_PBKDF2 HAVE_ECC HAVE_CRC HAVE_RNG IO_HID_EP_LENGTH=64) 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)