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 /pyecsca/codegen | |
| 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.
Diffstat (limited to 'pyecsca/codegen')
| -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 |
5 files changed, 37 insertions, 27 deletions
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); |
