From b461d960b634dedac734a92fbb66fa26b4f006ec Mon Sep 17 00:00:00 2001 From: YI Date: Wed, 6 Jul 2022 00:07:41 +0800 Subject: [PATCH] Add options to use blank external dependencies This commit makes external dependencies unnecessary when `WITH_BLANK_EXTERNAL_DEPENDENCIES` is specified. It is useful in some restricted environments (e.g. webassembly) or in some specific applications (e.g. deterministic signature generation). --- README.md | 11 +++++++++-- src/external_deps/print.c | 6 ++++++ src/external_deps/rand.c | 5 +++++ src/external_deps/time.c | 6 ++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4df86554..41d3a068 100644 --- a/README.md +++ b/README.md @@ -693,7 +693,8 @@ compiler is not gcc/clang compatible, the user can modify the CFLAGS as well as for 8-bit MCUs to 64-bit CPUs. If the toolchain does not have a [`stdint.h`](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdint.h.html) header, it is still possible to compile libecc by exporting LIBECC_NOSTDLIB=1: in this case, the code will try to guess and fit to native C types or throw an error so that the user can adapt [src/words/types.h](src/words/types.h) to its specific case. -* The library core is platform independent. However, when the platform is not recognized (i.e. everything aside UNIX/Windows/Mac OS), +* The library core is platform independent. However, when the platform is not recognized (i.e. everything aside UNIX/Windows/Mac OS) +and when not compiled with `WITH_BLANK_EXTERNAL_DEPENDENCIES` specified, an error is thrown at compilation time asking the user to provide implementations for **external dependencies** in [src/external_deps/](src/external_deps), namely: * The printing helper in [src/external_deps/print.c](src/external_deps/print.c). This helper serves output debugging purposes. @@ -703,6 +704,11 @@ in [src/external_deps/](src/external_deps), namely: schemes. One should notice that a **good random source** is **crucial** for the security of Elliptic Curve based signature schemes, so great care must be taken when implementing this. +If `WITH_BLANK_EXTERNAL_DEPENDENCIES` is specified in compiling time, blank implementation for these helpers are provided. +As using many algorithms with no reliable CSRNGs can be quite dangerous, with `WITH_BLANK_EXTERNAL_DEPENDENCIES` `get_random` +will just return error, you should not using algorithms depending on random number generation. They are provided +because it is useful in some restricted environments or in some specific applications (e.g. deterministic signature generation). + Some other external dependencies could arise depending on the compilation chain and/or the platform. Such an example is the implementation of the gcc and clang stack protection option, usually expecting the user to provide stack canaries generation (with random values) and failover behavior. @@ -890,7 +896,8 @@ other compilers (`-c` flag to generate object files, `-o` flag to define output [partially implemented](http://sdcc.sourceforge.net/mediawiki/index.php/Standard_compliance). * The compiler has "exotic" targets such as the Zilog Z80 MCU. -We suppose that the user has also provided the **external dependencies** for print, random and time +Unless compiling with `WITH_BLANK_EXTERNAL_DEPENDENCIES` specified, +we suppose that the user has also provided the **external dependencies** for print, random and time functions (otherwise explicit errors will be thrown by #error directives). We will show how overloading the Makefile flags can be of use in this case. Say that we want diff --git a/src/external_deps/print.c b/src/external_deps/print.c index 03a8ef12..e13ff03f 100644 --- a/src/external_deps/print.c +++ b/src/external_deps/print.c @@ -26,6 +26,12 @@ void ext_printf(const char *format, ...) vprintf(format, arglist); va_end(arglist); } + +#elif defined(WITH_BLANK_EXTERNAL_DEPENDENCIES) +void ext_printf(const char *format, ...) { + FORCE_USED_VAR(format); +} + #else #error "print.c: you have to implement ext_printf" #endif diff --git a/src/external_deps/rand.c b/src/external_deps/rand.c index 4150b520..2f964ff0 100644 --- a/src/external_deps/rand.c +++ b/src/external_deps/rand.c @@ -106,6 +106,11 @@ int get_random(unsigned char *buf, u16 len) return ret; } +#elif defined(WITH_BLANK_EXTERNAL_DEPENDENCIES) +int get_random(unsigned char *buf, u16 len) { + return -1; +} + /* No platform detected, the user must provide an implementation! */ #else /* WARNING: when providing/implementing the get_random function, one must: diff --git a/src/external_deps/time.c b/src/external_deps/time.c index d9280ac6..914680ce 100644 --- a/src/external_deps/time.c +++ b/src/external_deps/time.c @@ -66,6 +66,12 @@ int get_ms_time(u64 *time) return ret; } +#elif defined(WITH_BLANK_EXTERNAL_DEPENDENCIES) +int get_ms_time(u64 *time) { + *time = 0; + return 0; +} + /* No platform detected, the used must provide an implementation! */ #else #error "time.c: you have to implement get_ms_time()"