diff options
| author | J08nY | 2020-02-28 15:03:36 +0100 |
|---|---|---|
| committer | J08nY | 2020-02-28 15:03:36 +0100 |
| commit | b387d00511a03dc20e15ac55fcbf07f3dfa79ce0 (patch) | |
| tree | 6f7c7cb0168366b2862a6e42e20067652cbc443b | |
| parent | 4eafe2d49fc7406861609c5af12b850741bbe5a0 (diff) | |
| download | pyecsca-codegen-b387d00511a03dc20e15ac55fcbf07f3dfa79ce0.tar.gz pyecsca-codegen-b387d00511a03dc20e15ac55fcbf07f3dfa79ce0.tar.zst pyecsca-codegen-b387d00511a03dc20e15ac55fcbf07f3dfa79ce0.zip | |
Update libtommath, use multi init and clear.
| -rw-r--r-- | ext/Makefile | 6 | ||||
| m--------- | ext/libtommath | 0 | ||||
| -rw-r--r-- | pyecsca/codegen/bn/bn.c | 6 | ||||
| -rw-r--r-- | pyecsca/codegen/bn/bn.h | 8 | ||||
| -rw-r--r-- | pyecsca/codegen/point.h | 14 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/ops.c | 31 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/point.c | 5 |
7 files changed, 40 insertions, 30 deletions
diff --git a/ext/Makefile b/ext/Makefile index 6f1bd1f..db0a50d 100644 --- a/ext/Makefile +++ b/ext/Makefile @@ -12,14 +12,14 @@ tommath_dir: mkdir -p ../pyecsca/codegen/tommath host: LIBNAME=libtommath-HOST.a -host: CFLAGS=-DMP_NO_DEV_URANDOM -DMP_LOW_MEM -DMP_PREC=10 +host: CFLAGS=-DMP_NO_DEV_URANDOM -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 host: tommath_dir $(MAKE) -C libtommath clean $(MAKE) -C libtommath cp libtommath/$(LIBNAME) ../pyecsca/codegen/tommath/$(LIBNAME) stm32f0: CROSS_COMPILE=arm-none-eabi- -stm32f0: CFLAGS=-mcpu=cortex-m0 -mthumb -mfloat-abi=soft -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_PREC=10 +stm32f0: CFLAGS=-mcpu=cortex-m0 -mthumb -mfloat-abi=soft -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 stm32f0: LDFLAGS=--specs=nano.specs --specs=nosys.specs -T ../pyecsca/codegen/hal/stm32f0/LinkerScript.ld -Wl,--gc-sections -lm -mthumb -mcpu=cortex-m0 stm32f0: COMPILE_SIZE=1 stm32f0: LIBNAME=libtommath-CW308_STM32F0.a @@ -29,7 +29,7 @@ stm32f0: tommath_dir cp libtommath/$(LIBNAME) ../pyecsca/codegen/tommath/$(LIBNAME) stm32f3: CROSS_COMPILE=arm-none-eabi- -stm32f3: CFLAGS=-mcpu=cortex-m4 -mthumb -mfloat-abi=soft -mfpu=fpv4-sp-d16 -fmessage-length=0 -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_PREC=10 +stm32f3: CFLAGS=-mcpu=cortex-m4 -mthumb -mfloat-abi=soft -mfpu=fpv4-sp-d16 -fmessage-length=0 -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 stm32f3: LDFLAGS=--specs=nano.specs -T ../pyecsca/codegen/hal/stm32f3/LinkerScript.ld -Wl,--gc-sections -lm -mthumb -mcpu=cortex-m4 stm32f3: COMPILE_SIZE=1 stm32f3: LIBNAME=libtommath-CW308_STM32F3.a diff --git a/ext/libtommath b/ext/libtommath -Subproject 220a4deb31736983b08ac81b77536fe8f2d9dad +Subproject ffd80665d12a492d1c72b6355b9a0332186967a diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c index 930dea7..1247c5d 100644 --- a/pyecsca/codegen/bn/bn.c +++ b/pyecsca/codegen/bn/bn.c @@ -14,15 +14,15 @@ void bn_clear(bn_t *bn) { mp_clear(bn); } -int bn_from_bin(const uint8_t *data, size_t size, bn_t *out) { +bn_err bn_from_bin(const uint8_t *data, size_t size, bn_t *out) { return mp_from_ubin(out, data, size); } -int bn_from_hex(const char *data, bn_t *out) { +bn_err bn_from_hex(const char *data, bn_t *out) { return mp_read_radix(out, data, 16); } -int bn_from_int(unsigned int value, bn_t *out) { +bn_err bn_from_int(unsigned int value, bn_t *out) { if (sizeof(unsigned int) == 8) { mp_set_u64(out, value); } else { diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h index 02b3e93..862b67c 100644 --- a/pyecsca/codegen/bn/bn.h +++ b/pyecsca/codegen/bn/bn.h @@ -34,12 +34,14 @@ typedef struct { } wnaf_t; bn_err bn_init(bn_t *bn); +#define bn_init_multi mp_init_multi bn_err bn_copy(const bn_t *from, bn_t *to); void bn_clear(bn_t *bn); +#define bn_clear_multi mp_clear_multi -int bn_from_bin(const uint8_t *data, size_t size, bn_t *out); -int bn_from_hex(const char *data, bn_t *out); -int bn_from_int(unsigned int value, bn_t *out); +bn_err bn_from_bin(const uint8_t *data, size_t size, bn_t *out); +bn_err bn_from_hex(const char *data, bn_t *out); +bn_err bn_from_int(unsigned int value, bn_t *out); bn_err bn_to_binpad(const bn_t *one, uint8_t *data, size_t size); bn_err bn_to_bin(const bn_t *one, uint8_t *data); diff --git a/pyecsca/codegen/point.h b/pyecsca/codegen/point.h index 1711e42..739078c 100644 --- a/pyecsca/codegen/point.h +++ b/pyecsca/codegen/point.h @@ -20,31 +20,31 @@ void point_to_affine(const point_t *point, const curve_t *curve, bn_t *out_x, bn void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out); void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one); -void point_add_init(void); +bool point_add_init(void); void point_add_clear(void); void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one); -void point_dbl_init(void); +bool point_dbl_init(void); void point_dbl_clear(void); void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one); -void point_tpl_init(void); +bool point_tpl_init(void); void point_tpl_clear(void); void point_neg(const point_t *one, const curve_t *curve, point_t *out_one); -void point_neg_init(void); +bool point_neg_init(void); void point_neg_clear(void); void point_scl(const point_t *one, const curve_t *curve, point_t *out_one); -void point_scl_init(void); +bool point_scl_init(void); void point_scl_clear(void); void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one); -void point_dadd_init(void); +bool point_dadd_init(void); void point_dadd_clear(void); void point_ladd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other); -void point_ladd_init(void); +bool point_ladd_init(void); void point_ladd_clear(void); #endif //POINT_H_
\ No newline at end of file diff --git a/pyecsca/codegen/templates/ops.c b/pyecsca/codegen/templates/ops.c index 623c585..64a4ab4 100644 --- a/pyecsca/codegen/templates/ops.c +++ b/pyecsca/codegen/templates/ops.c @@ -1,7 +1,8 @@ -{% macro render_full_allocs(allocations) -%} +{% macro render_full_allocs(allocations, err_name="err") -%} {%- for alloc in allocations %} - bn_t {{ alloc }}; bn_init(&{{ alloc }}); + bn_t {{ alloc }}; {%- endfor %} + {{ err_name }} = bn_init_multi(&{{ allocations | join(", &") }}, NULL); {%- endmacro %} {% macro render_static_allocs(allocations) -%} @@ -10,10 +11,8 @@ {%- endfor %} {%- endmacro %} -{% macro render_init_allocs(allocations) -%} - {%- for alloc in allocations %} - bn_init(&{{ alloc }}); - {%- endfor %} +{% macro render_init_allocs(allocations, err_name="err") -%} + {{err_name}} = bn_init_multi(&{{ allocations | join(", &") }}, NULL); {%- endmacro %} {% macro render_initializations(initializations) -%} @@ -35,17 +34,22 @@ {%- endmacro %} {% macro render_frees(frees) -%} - {%- for free in frees %} - bn_clear(&{{ free }}); - {%- endfor %} + {% if frees %} + bn_clear_multi(&{{ frees | join(", &") }}, NULL); + {%- endif %} {%- endmacro %} {% macro render_static_init(allocations, initializations, name) -%} {{ render_static_allocs(allocations) }} - void point_{{ name }}_init(void) { - {{ render_init_allocs(allocations) }} + bool point_{{ name }}_init(void) { + bn_err err; + {{ render_init_allocs(allocations, "err") }} + if (err != BN_OKAY) { + return false; + } {{ render_initializations(initializations) }} + return true; } {%- endmacro %} @@ -55,8 +59,9 @@ } {%- endmacro %} -{% macro render_all(allocations, initializations, operations, returns, frees) -%} - {{ render_full_allocs(allocations) }} +{% macro render_all(allocations, initializations, operations, returns, frees, err_name="err") -%} + bn_err {{err_name}}; + {{ render_full_allocs(allocations, err_name) }} {{ render_initializations(initializations) }} {{ render_ops(operations) }} {{ render_returns(returns) }} diff --git a/pyecsca/codegen/templates/point.c b/pyecsca/codegen/templates/point.c index aebb7c7..a3c9f59 100644 --- a/pyecsca/codegen/templates/point.c +++ b/pyecsca/codegen/templates/point.c @@ -75,7 +75,10 @@ bool point_equals_affine(const point_t *one, const point_t *other, const curve_t } void point_to_affine(const point_t *point, const curve_t *curve, bn_t *out_x, bn_t *out_y) { - {{ ops.render_all(allocations, initializations, operations, returns, frees) }} + {{ ops.render_all(allocations, initializations, operations, returns, frees, "err") }} + if (err != BN_OKAY) { + return; + } {%- if "x" in allocations %} if (out_x) { bn_copy(&x, out_x); |
