From c2688a0298768a6c1e3a485e5de6e8c957428c83 Mon Sep 17 00:00:00 2001 From: Nick Brook Date: Wed, 10 May 2023 17:22:37 +0100 Subject: [PATCH] Added makefile with strict warnings, resolved warnings # Conflicts: # uECC.c --- .gitignore | 3 + asm_arm.inc | 2 +- common.mk | 278 ++++++++++++++++++++++++++++++++++++++++++ curve-specific.inc | 30 ++--- makefile | 149 ++++++++++++++++++++++ platform-specific.inc | 2 +- test/test_compress.c | 2 +- test/test_compute.c | 2 +- test/test_ecdh.c | 2 +- test/test_ecdsa.c | 2 +- types.h | 6 +- uECC.c | 137 ++++++++++----------- uECC_test.h | 24 ++++ 13 files changed, 540 insertions(+), 99 deletions(-) create mode 100644 common.mk create mode 100644 makefile create mode 100644 uECC_test.h diff --git a/.gitignore b/.gitignore index 561bd57..41f0fa4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ __pycache__ *.a *.o *.so +*.d +*.su +build/ .DS_Store diff --git a/asm_arm.inc b/asm_arm.inc index 12e747f..a2e7f2b 100644 --- a/asm_arm.inc +++ b/asm_arm.inc @@ -463,7 +463,7 @@ uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result, #define asm_sub 1 #endif -#if !asm_mult +#if !defined(asm_mult) || !asm_mult uECC_VLI_API void uECC_vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right, diff --git a/common.mk b/common.mk new file mode 100644 index 0000000..10d2a2f --- /dev/null +++ b/common.mk @@ -0,0 +1,278 @@ +# uECC makefile common. Partially copied and modified from https://github.com/libecc/libecc + +# Detect mingw, since some versions throw a warning with the -fPIC option +# (which would be caught as an error in our case with -Werror) +# The ELF PIE related hardening flags are also non sense for Windows +MINGW := $(shell $(CROSS_COMPILE)$(CC) -dumpmachine 2>&1 | grep -v mingw) +# Detect Mac OS compilers: these usually don't like ELF pie related flags ... +APPLE := $(shell $(CROSS_COMPILE)$(CC) -dumpmachine 2>&1 | grep -v apple) +SYS_ROOT := +ifneq ($(MINGW),) + FPIC_CFLAG=-fPIC + ifneq ($(APPLE),) + FPIE_CFLAG=-fPIE + FPIE_LDFLAGS=-pie -Wl,-z,relro,-z,now + endif +endif + +ifeq ($(APPLE),) + SYS_ROOT_PATH := $(shell xcode-select --print-path) + ifneq ($(SYS_ROOT_PATH),) + SYS_ROOT_PATH := $(SYS_ROOT_PATH)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk + SYS_ROOT := --sysroot=$(SYS_ROOT_PATH) + $(info Using MacOS SDK $(SYS_ROOT_PATH)) + endif +endif + +# NOTE: with mingw, FORTIFY_SOURCE=2 must be used +# in conjuction with stack-protector as check functions +# are implemented in libssp +STACK_PROT_FLAG=-fstack-protector-strong +FORTIFY_FLAGS=-D_FORTIFY_SOURCE=2 + +# The first goal here is to define a meaningful set of CFLAGS based on compiler, +# debug mode, expected word size (16, 32, 64), etc. Those are then used to +# define two differents kinds of CFLAGS we will use for building our library +# (LIB_CFLAGS) and binaries (BIN_CFLAGS) objects. + +# Detect if we are using clang or gcc +CLANG := $(shell $(CROSS_COMPILE)$(CC) -v 2>&1 | grep clang) + +ifneq ($(CLANG),) + # get clang version e.g. 14.1.3 + CLANG_VERSION := $(shell $(CROSS_COMPILE)$(CC) -dumpversion) + # convert to single number e.g. 14 * 100 + 1 + CLANG_VERSION := $(shell echo $(CLANG_VERSION) | cut -f1-2 -d. | sed -e 's/\./*100+/g') + # Calculate value - e.g. 1401 + CLANG_VERSION := $(shell echo $$(($(CLANG_VERSION)))) + # Comparison results (true if true, empty if false) + CLANG_VERSION_GTE_12 := $(shell [ $(CLANG_VERSION) -ge 1200 ] && echo true) + CLANG_VERSION_GTE_13 := $(shell [ $(CLANG_VERSION) -ge 1300 ] && echo true) + CLANG_VERSION_GTE_17 := $(shell [ $(CLANG_VERSION) -ge 1700 ] && echo true) +endif + +# Default warning flags +# -Werror: treat warnings as errors +# +# Pedantic mode: enable more warnings +# -Wshadow: warn the user if a variable declaration shadows one from a parent context +# -Wdouble-promotion: warn about implicit conversion from float to double +# -Wformat=2: warn about format string vulnerabilities +# -fno-common: disallow global variables with same name and type +# -Wconversion: warn about implicit conversion +# -Wformat-security: warn about format string vulnerabilities +WARNING_CFLAGS = -Werror +ifeq ($(PEDANTIC),1) + $(info Using pedantic mode) + WARNING_CFLAGS += -Wshadow -Wdouble-promotion -Wformat=2 -fno-common -Wconversion -Wformat-security +endif + +# Disable certain warnings: +# -Wno-unused-parameter: commonly a false positive. Functions may be required to have a certain signature. +# -Wno-declaration-after-statement: our C standard supports declaration after statements +WARNING_CFLAGS += -Wno-unused-parameter -Wno-declaration-after-statement + +# When compiler is *explicitly* set to clang, use its -Weverything option by +# default but disable the sepcific options we cannot support: +# +# -Wno-reserved-id-macro: our header files use __XXX___ protection macros. +# -Wno-padded: padding warnings +# -Wno-packed: warning about packed structure we want to keep that way +# -Wno-covered-switch-default +# -Wno-used-but-marked-unused +# +ifneq ($(CLANG),) + WARNING_CFLAGS += -Weverything \ + -Wno-reserved-id-macro -Wno-padded \ + -Wno-packed -Wno-covered-switch-default \ + -Wno-used-but-marked-unused -Wno-switch-enum + # Add warnings if we are in pedantic mode + ifeq ($(PEDANTIC),1) + WARNING_CFLAGS += -Walloca -Wcast-qual -Wnull-dereference -Wstack-protector -Wvla -Warray-bounds -Warray-bounds-pointer-arithmetic -Wassign-enum -Wbad-function-cast -Wconditional-uninitialized -Wfloat-equal -Wformat-type-confusion -Widiomatic-parentheses -Wimplicit-fallthrough -Wloop-analysis -Wpointer-arith -Wshift-sign-overflow -Wshorten-64-to-32 -Wtautological-constant-in-range-compare -Wunreachable-code-aggressive -Wthread-safety -Wthread-safety-beta -Wcomma + endif + ifeq ($(CLANG_VERSION_GTE_13), true) + # We have to do this because the '_' prefix seems now reserved to builtins + WARNING_CFLAGS += -Wno-reserved-identifier + endif + ifeq ($(CLANG_VERSION_GTE_17), true) + # NOTE: XXX: this is really a shame to remove this, but + # we have to wait until this is less sensitive and false positive + # prone to use it! + WARNING_CFLAGS += -Wno-unsafe-buffer-usage + endif +else + WARNING_CFLAGS += -W -Wextra -Wall -Wunreachable-code + # Add warnings if we are in pedantic mode + ifeq ($(PEDANTIC),1) + WARNING_CFLAGS += -Wpedantic -Wformat-overflow=2 -Wformat-truncation=2 -Wnull-dereference -Wstack-protector -Wtrampolines -Walloca -Wvla -Warray-bounds=2 -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wcast-qual -Wstringop-overflow=4 -Warith-conversion -Wlogical-op -Wduplicated-cond -Wduplicated-branches -Wformat-signedness -Wstrict-overflow=2 -Wundef -Wstrict-prototypes -Wswitch-default -Wcast-align=strict -Wjump-misses-init + endif +endif + +ifeq ($(WNOERROR), 1) + # Sometimes "-Werror" might be too much, this can be overriden + WARNING_CFLAGS := $(subst -Werror,,$(WARNING_CFLAGS)) +endif + +# If the user has overridden the CFLAGS or LDFLAGS, let's detect it +# and adapt our compilation process +ifdef CFLAGS +USER_DEFINED_CFLAGS = $(CFLAGS) +endif +ifdef LDFLAGS +USER_DEFINED_LDFLAGS = $(LDFLAGS) +endif + +CFLAGS ?= $(WARNING_CFLAGS) $(SYS_ROOT) -pedantic -fno-builtin -std=c99 \ + $(FORTIFY_FLAGS) $(STACK_PROT_FLAG) -O3 +LDFLAGS ?= + +# Default AR and RANLIB if not overriden by user +AR ?= ar +RANLIB ?= ranlib +# Default AR flags and RANLIB flags if not overriden by user +AR_FLAGS ?= rcs +RANLIB_FLAGS ?= + +# Our debug flags +DEBUG_CFLAGS = -DDEBUG -O -g + +ifeq ($(VERBOSE_INNER_VALUES),1) +CFLAGS += -DVERBOSE_INNER_VALUES +endif + +# Default all and clean target that will be expanded +# later in the Makefile +all: +clean: + +debug: CFLAGS += $(DEBUG_CFLAGS) +debug: clean all + +# Force 64-bit word size +64: CFLAGS += -DWORDSIZE=64 +64: clean all +debug64: CFLAGS += -DWORDSIZE=64 $(DEBUG_CFLAGS) +debug64: clean all + +# Force 32-bit word size +32: CFLAGS += -DWORDSIZE=32 +32: clean all +debug32: CFLAGS += -DWORDSIZE=32 $(DEBUG_CFLAGS) +debug32: clean all + +# Force 16-bit word size +16: CFLAGS += -DWORDSIZE=16 +16: clean all +debug16: CFLAGS += -DWORDSIZE=16 $(DEBUG_CFLAGS) +debug16: clean all + +# Force to compile with 64-bit arch +force_arch64: CFLAGS += -m64 +force_arch64: clean all + +# Force to compile with 32-bit arch +force_arch32: CFLAGS += -m32 +force_arch32: clean all + +# By default, we use an stdlib +ifneq ($(LIBECC_NOSTDLIB),1) +CFLAGS += -DWITH_STDLIB +endif + +# Let's now define the two kinds of CFLAGS we will use for building our +# library (LIB_CFLAGS) and binaries (BIN_CFLAGS) objects. +# If the user has not overriden the CFLAGS, we add the usual gcc/clang +# flags to produce binaries compatible with hardening technologies. +ifndef USER_DEFINED_CFLAGS +BIN_CFLAGS ?= $(CFLAGS) $(FPIE_CFLAG) -MMD -MP +LIB_CFLAGS ?= $(CFLAGS) $(FPIC_CFLAG) -MMD -MP -ffreestanding +else +BIN_CFLAGS ?= $(USER_DEFINED_CFLAGS) +LIB_CFLAGS ?= $(USER_DEFINED_CFLAGS) +endif +ifndef USER_DEFINED_LDFLAGS +BIN_LDFLAGS ?= $(LDFLAGS) $(FPIE_LDFLAGS) +else +BIN_LDFLAGS ?= $(USER_DEFINED_LDFLAGS) +endif + +# If the user wants to add extra flags to the existing flags, +# check it and add them +ifdef EXTRA_LIB_CFLAGS +LIB_CFLAGS += $(EXTRA_LIB_CFLAGS) +endif +ifdef EXTRA_LIB_DYN_LDFLAGS +LIB_DYN_LDFLAGS += $(EXTRA_LIB_DYN_LDFLAGS) +endif +ifdef EXTRA_BIN_CFLAGS +BIN_CFLAGS += $(EXTRA_BIN_CFLAGS) +endif +ifdef EXTRA_BIN_LDFLAGS +BIN_LDFLAGS += $(EXTRA_BIN_LDFLAGS) +endif +ifdef EXTRA_CFLAGS +CFLAGS += $(EXTRA_CFLAGS) +endif +ifdef EXTRA_LDFLAGS +LDFLAGS += $(EXTRA_LDFLAGS) +endif + +TEST_FLAGS := -DUECC_TEST -Wno-missing-prototypes -Wno-missing-declarations + +# Static libraries to produce or link to +LIBUECC = $(BUILD_DIR)/libuecc.a + +# Compile dynamic libraries if the user asked to +ifeq ($(WITH_DYNAMIC_LIBS),1) +# Dynamic libraries to produce or link to +LIBUECC_DYN = $(BUILD_DIR)/libuecc.so +# The ld flags to generate shared library +ifeq ($(APPLE),) +LIB_DYN_LDFLAGS ?= -shared -Wl,-undefined,dynamic_lookup +else +LIB_DYN_LDFLAGS ?= -shared -Wl,-z,relro,-z,now +endif +endif + +# Do we want to use clang or gcc sanitizers? +ifeq ($(USE_SANITIZERS),1) +CFLAGS += -fsanitize=undefined -fsanitize=address -fsanitize=leak + ifneq ($(CLANG),) + # Clang version < 12 do not support unsigned-shift-base + ifeq ($(CLANG_VERSION_GTE_12), true) + CFLAGS += -fsanitize=integer -fno-sanitize=unsigned-integer-overflow -fno-sanitize=unsigned-shift-base + endif + endif +endif + +# Do we have a C++ compiler instead of a C compiler? +GPP := $(shell $(CROSS_COMPILE)$(CC) -v 2>&1 | grep g++) +CLANGPP := $(shell echo $(CROSS_COMPILE)$(CC) | grep clang++) + +# g++ case +ifneq ($(GPP),) +CFLAGS := $(patsubst -std=c99, -std=c++2a, $(CFLAGS)) +CFLAGS += -Wno-deprecated +# Remove C++ unused pedantic flags +CFLAGS := $(patsubst -Wstrict-prototypes,,$(CFLAGS)) +CFLAGS := $(patsubst -Wjump-misses-init,,$(CFLAGS)) +CFLAGS := $(patsubst -Wduplicated-branches,,$(CFLAGS)) +endif +# clang++ case +ifneq ($(CLANGPP),) +CFLAGS := $(patsubst -std=c99, -std=c++2a, $(CFLAGS)) +CFLAGS += -Wno-deprecated -Wno-c++98-c++11-c++14-c++17-compat-pedantic -Wno-old-style-cast -Wno-zero-as-null-pointer-constant -Wno-c++98-compat-pedantic +endif + +# Makefile verbosity +ifeq ($(VERBOSE),1) +VERBOSE_MAKE= +else +VERBOSE_MAKE=@ +endif + +# Self tests parallelization +ifeq ($(OPENMP_SELF_TESTS),1) +CFLAGS += -DWITH_OPENMP_SELF_TESTS -fopenmp +LDFLAGS += -fopenmp +endif diff --git a/curve-specific.inc b/curve-specific.inc index 0453b21..b76bcc0 100644 --- a/curve-specific.inc +++ b/curve-specific.inc @@ -168,7 +168,7 @@ static const struct uECC_Curve_t curve_secp160r1 = { uECC_Curve uECC_secp160r1(void) { return &curve_secp160r1; } -#if (uECC_OPTIMIZATION_LEVEL > 0 && !asm_mmod_fast_secp160r1) +#if (uECC_OPTIMIZATION_LEVEL > 0 && (!defined(asm_mmod_fast_secp160r1) || !asm_mmod_fast_secp160r1)) /* Computes result = product % curve_p see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354 @@ -771,7 +771,7 @@ static const struct uECC_Curve_t curve_secp256r1 = { uECC_Curve uECC_secp256r1(void) { return &curve_secp256r1; } -#if (uECC_OPTIMIZATION_LEVEL > 0 && !asm_mmod_fast_secp256r1) +#if (uECC_OPTIMIZATION_LEVEL > 0 && (!defined(asm_mmod_fast_secp256r1) || !asm_mmod_fast_secp256r1)) /* Computes result = product % curve_p from http://www.nsa.gov/ia/_files/nist-routines.pdf */ #if uECC_WORD_SIZE == 1 @@ -894,8 +894,8 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[5] = product[13]; tmp[6] = product[14]; tmp[7] = product[15]; - carry = uECC_vli_add(tmp, tmp, tmp, num_words_secp256r1); - carry += uECC_vli_add(result, result, tmp, num_words_secp256r1); + carry = (int)uECC_vli_add(tmp, tmp, tmp, num_words_secp256r1); + carry += (int)uECC_vli_add(result, result, tmp, num_words_secp256r1); /* s2 */ tmp[3] = product[12]; @@ -903,8 +903,8 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[5] = product[14]; tmp[6] = product[15]; tmp[7] = 0; - carry += uECC_vli_add(tmp, tmp, tmp, num_words_secp256r1); - carry += uECC_vli_add(result, result, tmp, num_words_secp256r1); + carry += (int)uECC_vli_add(tmp, tmp, tmp, num_words_secp256r1); + carry += (int)uECC_vli_add(result, result, tmp, num_words_secp256r1); /* s3 */ tmp[0] = product[8]; @@ -913,7 +913,7 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[3] = tmp[4] = tmp[5] = 0; tmp[6] = product[14]; tmp[7] = product[15]; - carry += uECC_vli_add(result, result, tmp, num_words_secp256r1); + carry += (int)uECC_vli_add(result, result, tmp, num_words_secp256r1); /* s4 */ tmp[0] = product[9]; @@ -924,7 +924,7 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[5] = product[15]; tmp[6] = product[13]; tmp[7] = product[8]; - carry += uECC_vli_add(result, result, tmp, num_words_secp256r1); + carry += (int)uECC_vli_add(result, result, tmp, num_words_secp256r1); /* d1 */ tmp[0] = product[11]; @@ -933,7 +933,7 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[3] = tmp[4] = tmp[5] = 0; tmp[6] = product[8]; tmp[7] = product[10]; - carry -= uECC_vli_sub(result, result, tmp, num_words_secp256r1); + carry -= (int)uECC_vli_sub(result, result, tmp, num_words_secp256r1); /* d2 */ tmp[0] = product[12]; @@ -943,7 +943,7 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[4] = tmp[5] = 0; tmp[6] = product[9]; tmp[7] = product[11]; - carry -= uECC_vli_sub(result, result, tmp, num_words_secp256r1); + carry -= (int)uECC_vli_sub(result, result, tmp, num_words_secp256r1); /* d3 */ tmp[0] = product[13]; @@ -954,7 +954,7 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[5] = product[10]; tmp[6] = 0; tmp[7] = product[12]; - carry -= uECC_vli_sub(result, result, tmp, num_words_secp256r1); + carry -= (int)uECC_vli_sub(result, result, tmp, num_words_secp256r1); /* d4 */ tmp[0] = product[14]; @@ -965,15 +965,15 @@ static void vli_mmod_fast_secp256r1(uint32_t *result, uint32_t *product) { tmp[5] = product[11]; tmp[6] = 0; tmp[7] = product[13]; - carry -= uECC_vli_sub(result, result, tmp, num_words_secp256r1); + carry -= (int)uECC_vli_sub(result, result, tmp, num_words_secp256r1); if (carry < 0) { do { - carry += uECC_vli_add(result, result, curve_secp256r1.p, num_words_secp256r1); + carry += (int)uECC_vli_add(result, result, curve_secp256r1.p, num_words_secp256r1); } while (carry < 0); } else { while (carry || uECC_vli_cmp_unsafe(curve_secp256r1.p, result, num_words_secp256r1) != 1) { - carry -= uECC_vli_sub(result, result, curve_secp256r1.p, num_words_secp256r1); + carry -= (int)uECC_vli_sub(result, result, curve_secp256r1.p, num_words_secp256r1); } } } @@ -1152,7 +1152,7 @@ static void x_side_secp256k1(uECC_word_t *result, const uECC_word_t *x, uECC_Cur uECC_vli_modAdd(result, result, curve->b, curve->p, num_words_secp256k1); /* r = x^3 + b */ } -#if (uECC_OPTIMIZATION_LEVEL > 0 && !asm_mmod_fast_secp256k1) +#if (uECC_OPTIMIZATION_LEVEL > 0 && (!defined(asm_mmod_fast_secp256k1) || !asm_mmod_fast_secp256k1)) static void omega_mult_secp256k1(uECC_word_t *result, const uECC_word_t *right); static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) { uECC_word_t tmp[2 * num_words_secp256k1]; diff --git a/makefile b/makefile new file mode 100644 index 0000000..418c91b --- /dev/null +++ b/makefile @@ -0,0 +1,149 @@ +# uECC makefile. Partially copied and modified from https://github.com/libecc/libecc + +.SUFFIXES: + +# Where to put generated objects +BUILD_DIR ?= build +# Default to the previous behaviour and keep generated .o & .d files next to the source code +OBJ_DIR ?=. +include common.mk + + +# Static libraries to build +LIBS = $(LIBUECC) + +# Compile dynamic libraries if the user asked to +ifeq ($(WITH_DYNAMIC_LIBS),1) +LIBS += $(LIBUECC_DYN) +endif + +# Executables to build +TESTS_EXEC = $(BUILD_DIR)/test_compress $(BUILD_DIR)/test_compute $(BUILD_DIR)/test_ecdh $(BUILD_DIR)/test_ecdsa +# We also compile executables with dynamic linking if asked to +ifeq ($(WITH_DYNAMIC_LIBS),1) +TESTS_EXEC += $(BUILD_DIR)/test_compress_dyn $(BUILD_DIR)/test_compute_dyn $(BUILD_DIR)/test_ecdh_dyn $(BUILD_DIR)/test_ecdsa_dyn +endif + +EXEC_TO_CLEAN = $(TESTS_EXEC) + +# Run all tests +test: $(TESTS_EXEC) + @for test in $(TESTS_EXEC); do \ + echo "Running $$test"; \ + $$test; \ + done + +# all and clean, as you might expect +all: $(LIBS) $(TESTS_EXEC) + +# Default object files extension +OBJ_FILES_EXTENSION ?= o + +clean: + @rm -f $(LIBS) $(EXEC_TO_CLEAN) + @find $(OBJ_DIR)/ -name '*.$(OBJ_FILES_EXTENSION)' -exec rm -f '{}' \; + @find $(OBJ_DIR)/ -name '*.d' -exec rm -f '{}' \; + @find $(BUILD_DIR)/ -name '*.a' -exec rm -f '{}' \; + @find $(BUILD_DIR)/ -name '*.so' -exec rm -f '{}' \; + @find $(BUILD_DIR)/ -name '*.su' -exec rm -f '{}' \; + @find . -name '*~' -exec rm -f '{}' \; + + + +# --- Source Code --- + +UECC_SRC = uECC.c + +# --- Static Libraries --- + +LIBUECC_SRC = $(UECC_SRC) +LIBUECC_OBJECTS = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(LIBUECC_SRC))) +$(LIBUECC): $(LIBUECC_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(AR) $(AR_FLAGS) $@ $^ + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(RANLIB) $(RANLIB_FLAGS) $@ + +# --- Dynamic Libraries --- + +$(LIBUECC_DYN): $(LIBUECC_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(LIB_CFLAGS) $(LIB_DYN_LDFLAGS) $^ -o $@ + + +# --- Executables (Static linkage with libsign object files) --- + +TEST_INCLUDE_DIRS = -I. -Itest + +TEST_COMPRESS_SRC = test/test_compress.c +TEST_COMPRESS_OBJECTS = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(TEST_COMPRESS_SRC))) +$(BUILD_DIR)/test_compress: $(TEST_COMPRESS_OBJECTS) $(LIBUECC_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) $^ -o $@ + +TEST_COMPUTE_SRC = test/test_compute.c +TEST_COMPUTE_OBJECTS = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(TEST_COMPUTE_SRC))) +$(BUILD_DIR)/test_compute: $(TEST_COMPUTE_OBJECTS) $(LIBUECC_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) $^ -o $@ + +TEST_ECDH_SRC = test/test_ecdh.c +TEST_ECDH_OBJECTS = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(TEST_ECDH_SRC))) +$(BUILD_DIR)/test_ecdh: $(TEST_ECDH_OBJECTS) $(LIBUECC_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) $^ -o $@ + +TEST_ECDSA_SRC = test/test_ecdsa.c +TEST_ECDSA_OBJECTS = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(TEST_ECDSA_SRC))) +$(BUILD_DIR)/test_ecdsa: $(TEST_ECDSA_OBJECTS) $(LIBUECC_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) $^ -o $@ + +# --- Excutables (Dynamic linkage with libsign shared library) --- + +$(BUILD_DIR)/test_compress_dyn: $(TEST_COMPRESS_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) -L$(BUILD_DIR) $^ -luecc -o $@ + +$(BUILD_DIR)/test_compute_dyn: $(TEST_COMPUTE_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) -L$(BUILD_DIR) $^ -luecc -o $@ + +$(BUILD_DIR)/test_ecdh_dyn: $(TEST_ECDH_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) -L$(BUILD_DIR) $^ -luecc -o $@ + +$(BUILD_DIR)/test_ecdsa_dyn: $(TEST_ECDSA_OBJECTS) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) $(BIN_CFLAGS) $(BIN_LDFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) -L$(BUILD_DIR) $^ -luecc -o $@ + +.PHONY: all clean 16 32 64 debug debug16 debug32 debug64 force_arch32 force_arch64 + +LIB_SRC = $(UECC_SRC) +LIB_OBJ = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(LIB_SRC))) + +BIN_SRC = $(TEST_COMPRESS_SRC) $(TEST_COMPUTE_SRC) $(TEST_ECDH_SRC) $(TEST_ECDSA_SRC) +BIN_OBJ = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(BIN_SRC))) + +# All source files, used to construct general rules +SRC += $(LIB_SRC) +SRC += $(BIN_SRC) + +# All object files +OBJS = $(patsubst %,$(OBJ_DIR)/%.$(OBJ_FILES_EXTENSION),$(basename $(SRC))) + +# General dependency rule between .o and .d files +DEPS = $(OBJS:.$(OBJ_FILES_EXTENSION)=.d) + +# General rule for creating .o (and .d) file from .c +$(LIB_OBJ): $(LIB_SRC) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) -c $(LIB_CFLAGS) -o $@ $< + +$(BIN_OBJ): $(BIN_SRC) + $(VERBOSE_MAKE)$(CROSS_COMPILE)$(CC) -c $(BIN_CFLAGS) $(TEST_FLAGS) $(TEST_INCLUDE_DIRS) -o $@ $< + +# Populate the directory structure to contain the .o and .d files, if necessary +$(shell mkdir -p $(dir $(OBJS)) >/dev/null) +$(shell mkdir -p $(BUILD_DIR) >/dev/null) + +# Make a note of the MAKEFILE_LIST at this stage of parsing the Makefile +# It is important here to use the ':=' operator so it is evaluated only once, +# and to do this before all the DEPS files are included as makefiles. +MAKEFILES:=$(MAKEFILE_LIST) + +# Make object files depend on all makefiles used - this forces a rebuild if any +# of the makefiles are changed +$(OBJS) : $(MAKEFILES) + +# Dep files are makefiles that keep track of which header files are used by the +# c source code. Include them to allow them to work correctly. +-include $(DEPS) diff --git a/platform-specific.inc b/platform-specific.inc index 7e0373f..10918cc 100644 --- a/platform-specific.inc +++ b/platform-specific.inc @@ -57,7 +57,7 @@ static int default_RNG(uint8_t *dest, unsigned size) { close(fd); return 0; } - left -= bytes_read; + left -= (size_t)bytes_read; ptr += bytes_read; } diff --git a/test/test_compress.c b/test/test_compress.c index aef374c..490a3f9 100644 --- a/test/test_compress.c +++ b/test/test_compress.c @@ -17,7 +17,7 @@ void vli_print(char *str, uint8_t *vli, unsigned int size) { printf("\n"); } -int main() { +int main(void) { uint8_t public[64]; uint8_t private[32]; uint8_t compressed_point[33]; diff --git a/test/test_compute.c b/test/test_compute.c index 7b936d8..851184d 100644 --- a/test/test_compute.c +++ b/test/test_compute.c @@ -13,7 +13,7 @@ void vli_print(char *str, uint8_t *vli, unsigned int size) { printf("\n"); } -int main() { +int main(void) { int i; int success; uint8_t private[32]; diff --git a/test/test_ecdh.c b/test/test_ecdh.c index 7315b75..d9025ff 100644 --- a/test/test_ecdh.c +++ b/test/test_ecdh.c @@ -11,7 +11,7 @@ void vli_print(uint8_t *vli, unsigned int size) { } } -int main() { +int main(void) { int i, c; uint8_t private1[32] = {0}; uint8_t private2[32] = {0}; diff --git a/test/test_ecdsa.c b/test/test_ecdsa.c index 8699794..d966de4 100644 --- a/test/test_ecdsa.c +++ b/test/test_ecdsa.c @@ -5,7 +5,7 @@ #include #include -int main() { +int main(void) { int i, c; uint8_t private[32] = {0}; uint8_t public[64] = {0}; diff --git a/types.h b/types.h index 9ee8143..3f64bb1 100644 --- a/types.h +++ b/types.h @@ -4,7 +4,7 @@ #define _UECC_TYPES_H_ #ifndef uECC_PLATFORM - #if __AVR__ + #if defined(__AVR__) && __AVR__ #define uECC_PLATFORM uECC_avr #elif defined(__thumb2__) || defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */ #define uECC_PLATFORM uECC_arm_thumb2 @@ -26,7 +26,7 @@ #ifndef uECC_ARM_USE_UMAAL #if (uECC_PLATFORM == uECC_arm) && (__ARM_ARCH >= 6) #define uECC_ARM_USE_UMAAL 1 - #elif (uECC_PLATFORM == uECC_arm_thumb2) && (__ARM_ARCH >= 6) && !__ARM_ARCH_7M__ + #elif (uECC_PLATFORM == uECC_arm_thumb2) && (__ARM_ARCH >= 6) && (!defined(__ARM_ARCH_7M__) || !__ARM_ARCH_7M__) #define uECC_ARM_USE_UMAAL 1 #else #define uECC_ARM_USE_UMAAL 0 @@ -61,7 +61,7 @@ #define uECC_WORD_SIZE 4 #endif -#if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302) +#if defined(__SIZEOF_INT128__) || (defined(__clang_major__) && (__clang_major__ * 100 + __clang_minor__) >= 302) #define SUPPORTS_INT128 1 #else #define SUPPORTS_INT128 0 diff --git a/uECC.c b/uECC.c index a3d502c..a28d9fd 100644 --- a/uECC.c +++ b/uECC.c @@ -1,6 +1,9 @@ /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */ #include "uECC.h" +#ifdef UECC_TEST +#include "uECC_test.h" +#endif #include "uECC_vli.h" #ifndef uECC_RNG_MAX_TRIES @@ -89,52 +92,34 @@ #include "platform-specific.inc" #if (uECC_WORD_SIZE == 1) - #if uECC_SUPPORTS_secp160r1 - #define uECC_MAX_WORDS 21 /* Due to the size of curve_n. */ - #endif - #if uECC_SUPPORTS_secp192r1 - #undef uECC_MAX_WORDS - #define uECC_MAX_WORDS 24 - #endif - #if uECC_SUPPORTS_secp224r1 - #undef uECC_MAX_WORDS - #define uECC_MAX_WORDS 28 - #endif #if (uECC_SUPPORTS_secp256r1 || uECC_SUPPORTS_secp256k1) - #undef uECC_MAX_WORDS #define uECC_MAX_WORDS 32 + #elif uECC_SUPPORTS_secp224r1 + #define uECC_MAX_WORDS 28 + #elif uECC_SUPPORTS_secp192r1 + #define uECC_MAX_WORDS 24 + #elif uECC_SUPPORTS_secp160r1 + #define uECC_MAX_WORDS 21 /* Due to the size of curve_n. */ #endif #elif (uECC_WORD_SIZE == 4) - #if uECC_SUPPORTS_secp160r1 - #define uECC_MAX_WORDS 6 /* Due to the size of curve_n. */ - #endif - #if uECC_SUPPORTS_secp192r1 - #undef uECC_MAX_WORDS - #define uECC_MAX_WORDS 6 - #endif - #if uECC_SUPPORTS_secp224r1 - #undef uECC_MAX_WORDS - #define uECC_MAX_WORDS 7 - #endif #if (uECC_SUPPORTS_secp256r1 || uECC_SUPPORTS_secp256k1) - #undef uECC_MAX_WORDS #define uECC_MAX_WORDS 8 + #elif uECC_SUPPORTS_secp224r1 + #define uECC_MAX_WORDS 7 + #elif uECC_SUPPORTS_secp192r1 + #define uECC_MAX_WORDS 6 + #elif uECC_SUPPORTS_secp160r1 + #define uECC_MAX_WORDS 6 /* Due to the size of curve_n. */ #endif #elif (uECC_WORD_SIZE == 8) - #if uECC_SUPPORTS_secp160r1 - #define uECC_MAX_WORDS 3 - #endif - #if uECC_SUPPORTS_secp192r1 - #undef uECC_MAX_WORDS - #define uECC_MAX_WORDS 3 - #endif - #if uECC_SUPPORTS_secp224r1 - #undef uECC_MAX_WORDS - #define uECC_MAX_WORDS 4 - #endif #if (uECC_SUPPORTS_secp256r1 || uECC_SUPPORTS_secp256k1) - #undef uECC_MAX_WORDS #define uECC_MAX_WORDS 4 + #elif uECC_SUPPORTS_secp224r1 + #define uECC_MAX_WORDS 4 + #elif uECC_SUPPORTS_secp192r1 + #define uECC_MAX_WORDS 3 + #elif uECC_SUPPORTS_secp160r1 + #define uECC_MAX_WORDS 3 /* Due to the size of curve_n. */ #endif #endif /* uECC_WORD_SIZE */ @@ -186,7 +171,7 @@ static cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, #include "asm_avr.inc" #endif -#if default_RNG_defined +#if defined(default_RNG_defined) && default_RNG_defined static uECC_RNG_Function g_rng_function = &default_RNG; #else static uECC_RNG_Function g_rng_function = 0; @@ -208,7 +193,7 @@ int uECC_curve_public_key_size(uECC_Curve curve) { return 2 * curve->num_bytes; } -#if !asm_clear +#if !defined(asm_clear) || !asm_clear uECC_VLI_API void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words) { wordcount_t i; for (i = 0; i < num_words; ++i) { @@ -259,11 +244,11 @@ uECC_VLI_API bitcount_t uECC_vli_numBits(const uECC_word_t *vli, const wordcount digit >>= 1; } - return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); + return (bitcount_t)(((num_digits - 1) << uECC_WORD_BITS_SHIFT) + (int)i); } /* Sets dest = src. */ -#if !asm_set +#if !defined(asm_set) || !asm_set uECC_VLI_API void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src, wordcount_t num_words) { wordcount_t i; for (i = 0; i < num_words; ++i) { @@ -312,11 +297,11 @@ uECC_VLI_API cmpresult_t uECC_vli_cmp(const uECC_word_t *left, uECC_word_t tmp[uECC_MAX_WORDS]; uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words); uECC_word_t equal = uECC_vli_isZero(tmp, num_words); - return (!equal - 2 * neg); + return (cmpresult_t)(!equal - 2 * neg); } /* Computes vli = vli >> 1. */ -#if !asm_rshift1 +#if !defined(asm_rshift1) || !asm_rshift1 uECC_VLI_API void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) { uECC_word_t *end = vli; uECC_word_t carry = 0; @@ -331,7 +316,7 @@ uECC_VLI_API void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) { #endif /* !asm_rshift1 */ /* Computes result = left + right, returning carry. Can modify in place. */ -#if !asm_add +#if !defined(asm_add) || !asm_add uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right, @@ -350,7 +335,7 @@ uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result, #endif /* !asm_add */ /* Computes result = left - right, returning borrow. Can modify in place. */ -#if !asm_sub +#if !defined(asm_sub) || !asm_sub uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right, @@ -368,7 +353,7 @@ uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result, } #endif /* !asm_sub */ -#if !asm_mult || (uECC_SQUARE_FUNC && !asm_square) || \ +#if !defined(asm_mult) || !asm_mult || (uECC_SQUARE_FUNC && !asm_square) || \ (uECC_SUPPORTS_secp256k1 && (uECC_OPTIMIZATION_LEVEL > 0) && \ ((uECC_WORD_SIZE == 1) || (uECC_WORD_SIZE == 8))) static void muladd(uECC_word_t a, @@ -412,7 +397,7 @@ static void muladd(uECC_word_t a, } #endif /* muladd needed */ -#if !asm_mult +#if !defined(asm_mult) || !asm_mult uECC_VLI_API void uECC_vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right, @@ -447,7 +432,7 @@ uECC_VLI_API void uECC_vli_mult(uECC_word_t *result, #if uECC_SQUARE_FUNC -#if !asm_square +#if !defined(asm_square) || !asm_square static void mul2add(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, @@ -576,14 +561,14 @@ uECC_VLI_API void uECC_vli_mmod(uECC_word_t *result, uECC_word_t index; /* Shift mod so its highest set bit is at the maximum position. */ - bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) - uECC_vli_numBits(mod, num_words); - wordcount_t word_shift = shift / uECC_WORD_BITS; - wordcount_t bit_shift = shift % uECC_WORD_BITS; + bitcount_t shift = (bitcount_t)(num_words * 2 * uECC_WORD_BITS) - uECC_vli_numBits(mod, num_words); + wordcount_t word_shift = (wordcount_t)(shift / uECC_WORD_BITS); + wordcount_t bit_shift = (wordcount_t)(shift % uECC_WORD_BITS); uECC_word_t carry = 0; uECC_vli_clear(mod_multiple, word_shift); if (bit_shift > 0) { for(index = 0; index < (uECC_word_t)num_words; ++index) { - mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry; + mod_multiple[(uECC_word_t)word_shift + index] = (mod[index] << bit_shift) | carry; carry = mod[index] >> (uECC_WORD_BITS - bit_shift); } } else { @@ -908,7 +893,7 @@ static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0, uECC_word_t *k1, uECC_Curve curve) { - wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); + wordcount_t num_n_words = (wordcount_t)BITS_TO_WORDS(curve->num_n_bits); bitcount_t num_n_bits = curve->num_n_bits; uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) || (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) && @@ -931,7 +916,7 @@ uECC_VLI_API int uECC_generate_random_int(uECC_word_t *random, } for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { - if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) { + if (!g_rng_function((uint8_t *)random, (unsigned int) (num_words * uECC_WORD_SIZE))) { return 0; } random[num_words - 1] &= mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits)); @@ -996,8 +981,8 @@ uECC_VLI_API void uECC_vli_nativeToBytes(uint8_t *bytes, const uECC_word_t *native) { int i; for (i = 0; i < num_bytes; ++i) { - unsigned b = num_bytes - 1 - i; - bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE)); + unsigned b = (unsigned)(num_bytes - 1 - i); + bytes[i] = (uint8_t)(native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE))); } } @@ -1005,9 +990,9 @@ uECC_VLI_API void uECC_vli_bytesToNative(uECC_word_t *native, const uint8_t *bytes, int num_bytes) { int i; - uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE); + uECC_vli_clear(native, (wordcount_t)((num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE)); for (i = 0; i < num_bytes; ++i) { - unsigned b = num_bytes - 1 - i; + unsigned b = (unsigned)(num_bytes - 1 - i); native[b / uECC_WORD_SIZE] |= (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE)); } @@ -1028,7 +1013,7 @@ int uECC_make_key(uint8_t *public_key, uECC_word_t tries; for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { - if (!uECC_generate_random_int(_private, curve->n, BITS_TO_WORDS(curve->num_n_bits))) { + if (!uECC_generate_random_int(_private, curve->n, (wordcount_t)BITS_TO_WORDS(curve->num_n_bits))) { return 0; } @@ -1181,11 +1166,11 @@ int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, uEC #endif /* Make sure the private key is in the range [1, n-1]. */ - if (uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) { + if (uECC_vli_isZero(_private, (wordcount_t)BITS_TO_WORDS(curve->num_n_bits))) { return 0; } - if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) { + if (uECC_vli_cmp(curve->n, _private, (wordcount_t)BITS_TO_WORDS(curve->num_n_bits)) != 1) { return 0; } @@ -1209,8 +1194,8 @@ static void bits2int(uECC_word_t *native, const uint8_t *bits, unsigned bits_size, uECC_Curve curve) { - unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits); - unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits); + unsigned num_n_bytes = (unsigned)BITS_TO_BYTES(curve->num_n_bits); + unsigned num_n_words = (unsigned)BITS_TO_WORDS(curve->num_n_bits); int shift; uECC_word_t carry; uECC_word_t *ptr; @@ -1219,16 +1204,16 @@ static void bits2int(uECC_word_t *native, bits_size = num_n_bytes; } - uECC_vli_clear(native, num_n_words); + uECC_vli_clear(native, (wordcount_t)num_n_words); #if uECC_VLI_NATIVE_LITTLE_ENDIAN bcopy((uint8_t *) native, bits, bits_size); #else - uECC_vli_bytesToNative(native, bits, bits_size); + uECC_vli_bytesToNative(native, bits, (int)bits_size); #endif if (bits_size * 8 <= (unsigned)curve->num_n_bits) { return; } - shift = bits_size * 8 - curve->num_n_bits; + shift = (int)((int)bits_size * 8 - curve->num_n_bits); carry = 0; ptr = native + num_n_words; while (ptr-- > native) { @@ -1238,8 +1223,8 @@ static void bits2int(uECC_word_t *native, } /* Reduce mod curve_n */ - if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) { - uECC_vli_sub(native, native, curve->n, num_n_words); + if (uECC_vli_cmp_unsafe(curve->n, native, (wordcount_t)num_n_words) != 1) { + uECC_vli_sub(native, native, curve->n, (wordcount_t)num_n_words); } } @@ -1261,7 +1246,7 @@ static int uECC_sign_with_k_internal(const uint8_t *private_key, #endif uECC_word_t carry; wordcount_t num_words = curve->num_words; - wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); + wordcount_t num_n_words = (wordcount_t)BITS_TO_WORDS(curve->num_n_bits); bitcount_t num_n_bits = curve->num_n_bits; /* Make sure 0 < k < curve_n */ @@ -1326,6 +1311,7 @@ static int uECC_sign_with_k_internal(const uint8_t *private_key, return 1; } +#ifdef UECC_TEST /* For testing - sign with an explicitly specified k value */ int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash, @@ -1334,9 +1320,10 @@ int uECC_sign_with_k(const uint8_t *private_key, uint8_t *signature, uECC_Curve curve) { uECC_word_t k2[uECC_MAX_WORDS]; - bits2int(k2, k, BITS_TO_BYTES(curve->num_n_bits), curve); + bits2int(k2, k, (unsigned int)BITS_TO_BYTES(curve->num_n_bits), curve); return uECC_sign_with_k_internal(private_key, message_hash, hash_size, k2, signature, curve); } +#endif int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash, @@ -1347,7 +1334,7 @@ int uECC_sign(const uint8_t *private_key, uECC_word_t tries; for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { - if (!uECC_generate_random_int(k, curve->n, BITS_TO_WORDS(curve->num_n_bits))) { + if (!uECC_generate_random_int(k, curve->n, (wordcount_t)BITS_TO_WORDS(curve->num_n_bits))) { return 0; } @@ -1418,7 +1405,7 @@ int uECC_sign_deterministic(const uint8_t *private_key, uint8_t *K = hash_context->tmp; uint8_t *V = K + hash_context->result_size; wordcount_t num_bytes = curve->num_bytes; - wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); + wordcount_t num_n_words = (wordcount_t)BITS_TO_WORDS(curve->num_n_bits); bitcount_t num_n_bits = curve->num_n_bits; uECC_word_t tries; unsigned i; @@ -1431,7 +1418,7 @@ int uECC_sign_deterministic(const uint8_t *private_key, HMAC_init(hash_context, K); V[hash_context->result_size] = 0x00; HMAC_update(hash_context, V, hash_context->result_size + 1); - HMAC_update(hash_context, private_key, num_bytes); + HMAC_update(hash_context, private_key, (unsigned)num_bytes); HMAC_update(hash_context, message_hash, hash_size); HMAC_finish(hash_context, K, K); @@ -1441,7 +1428,7 @@ int uECC_sign_deterministic(const uint8_t *private_key, HMAC_init(hash_context, K); V[hash_context->result_size] = 0x01; HMAC_update(hash_context, V, hash_context->result_size + 1); - HMAC_update(hash_context, private_key, num_bytes); + HMAC_update(hash_context, private_key, (unsigned)num_bytes); HMAC_update(hash_context, message_hash, hash_size); HMAC_finish(hash_context, K, K); @@ -1510,7 +1497,7 @@ int uECC_verify(const uint8_t *public_key, #endif uECC_word_t r[uECC_MAX_WORDS], s[uECC_MAX_WORDS]; wordcount_t num_words = curve->num_words; - wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); + wordcount_t num_n_words = (wordcount_t)BITS_TO_WORDS(curve->num_n_bits); rx[num_n_words - 1] = 0; r[num_n_words - 1] = 0; @@ -1574,7 +1561,7 @@ int uECC_verify(const uint8_t *public_key, uECC_word_t index; curve->double_jacobian(rx, ry, z, curve); - index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1); + index = (uECC_word_t)((!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1)); point = points[index]; if (point) { uECC_vli_set(tx, point, num_words); diff --git a/uECC_test.h b/uECC_test.h new file mode 100644 index 0000000..97c469a --- /dev/null +++ b/uECC_test.h @@ -0,0 +1,24 @@ +/* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */ + +#ifndef _UECC_TEST_H_ +#define _UECC_TEST_H_ + +#include "uECC.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +int uECC_sign_with_k(const uint8_t *private_key, + const uint8_t *message_hash, + unsigned hash_size, + const uint8_t *k, + uint8_t *signature, + uECC_Curve curve); + +#ifdef __cplusplus +} /* end of extern "C" */ +#endif + +#endif /* _UECC_TEST_H_ */