From b387d00511a03dc20e15ac55fcbf07f3dfa79ce0 Mon Sep 17 00:00:00 2001 From: J08nY Date: Fri, 28 Feb 2020 15:03:36 +0100 Subject: Update libtommath, use multi init and clear. --- pyecsca/codegen/templates/ops.c | 31 ++++++++++++++++++------------- pyecsca/codegen/templates/point.c | 5 ++++- 2 files changed, 22 insertions(+), 14 deletions(-) (limited to 'pyecsca/codegen/templates') 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); -- cgit v1.3.1