diff options
| author | J08nY | 2020-02-28 17:25:51 +0100 |
|---|---|---|
| committer | J08nY | 2020-02-28 17:35:08 +0100 |
| commit | 0341d359dc67ced3f1e65d1d11af3590c1f0992f (patch) | |
| tree | 660140e64e8abe7647554e1424b1678a3ab98631 /pyecsca/codegen | |
| parent | b387d00511a03dc20e15ac55fcbf07f3dfa79ce0 (diff) | |
| download | pyecsca-codegen-0341d359dc67ced3f1e65d1d11af3590c1f0992f.tar.gz pyecsca-codegen-0341d359dc67ced3f1e65d1d11af3590c1f0992f.tar.zst pyecsca-codegen-0341d359dc67ced3f1e65d1d11af3590c1f0992f.zip | |
Add dynamic triggering.
Diffstat (limited to 'pyecsca/codegen')
23 files changed, 248 insertions, 14 deletions
diff --git a/pyecsca/codegen/action.h b/pyecsca/codegen/action.h new file mode 100644 index 0000000..183f03b --- /dev/null +++ b/pyecsca/codegen/action.h @@ -0,0 +1,14 @@ +#ifndef ACTION_H_ +#define ACTION_H_ + +#include <stdlib.h> + +extern uint32_t action_vector; + +void action_start(uint32_t action); + +void action_end(uint32_t action); + +void action_set(uint32_t new_vector); + +#endif //ACTION_H_
\ No newline at end of file diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h index 862b67c..eb6e942 100644 --- a/pyecsca/codegen/bn/bn.h +++ b/pyecsca/codegen/bn/bn.h @@ -50,12 +50,6 @@ size_t bn_to_bin_size(const bn_t *one); bn_err bn_rand_mod_sample(bn_t *out, const bn_t *mod); bn_err bn_rand_mod_reduce(bn_t *out, const bn_t *mod); -#if MOD_RAND == MOD_RAND_SAMPLE -#define bn_rand_mod bn_rand_mod_sample -#elif MOD_RAND == MOD_RAND_REDUCE -#define bn_rand_mod bn_rand_mod_reduce -#endif - bn_err bn_mod_add(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); bn_err bn_mod_sub(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); bn_err bn_mod_neg(const bn_t *one, const bn_t *mod, bn_t *out); diff --git a/pyecsca/codegen/client.py b/pyecsca/codegen/client.py index c9f7b67..6eaec79 100644 --- a/pyecsca/codegen/client.py +++ b/pyecsca/codegen/client.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import re from binascii import hexlify, unhexlify +from enum import IntFlag from os import path from typing import Mapping, Union, Optional, Tuple @@ -21,6 +22,23 @@ from pyecsca.sca.target import (SimpleSerialTarget, ChipWhispererTarget, BinaryT from .common import wrap_enum, Platform, get_model, get_coords +class Triggers(IntFlag): + add = 1 << 0 + dadd = 1 << 1 + dbl = 1 << 2 + ladd = 1 << 3 + neg = 1 << 4 + scl = 1 << 5 + tpl = 1 << 6 + mult = 1 << 7 + keygen = 1 << 8 + ecdh = 1 << 9 + ecdsa_sign = 1 << 10 + ecdsa_verify = 1 << 11 + coord_map = 1 << 12 + random_mod = 1 << 13 + + def encode_scalar(val: Union[int, Mod]) -> bytes: if isinstance(val, int): return val.to_bytes((val.bit_length() + 7) // 8, "big") @@ -120,6 +138,12 @@ def cmd_ecdsa_verify(data: bytes, sig: bytes) -> str: @public +def cmd_set_trigger(actions: Triggers) -> str: + vector_bytes = actions.to_bytes(4, "little") + return "t" + hexlify(vector_bytes) + + +@public def cmd_debug() -> str: return "d" @@ -131,6 +155,7 @@ class ImplTarget(SimpleSerialTarget): params: Optional[DomainParameters] privkey: Optional[int] pubkey: Optional[Point] + trigger: Optional[Triggers] timeout: int def __init__(self, model: CurveModel, coords: CoordinateModel, **kwargs): @@ -145,6 +170,7 @@ class ImplTarget(SimpleSerialTarget): self.params = None self.privkey = None self.pubkey = None + self.trigger = None def init_prng(self, seed: bytes) -> None: self.send_cmd(SMessage.from_raw(cmd_init_prng(seed)), self.timeout) @@ -203,6 +229,10 @@ class ImplTarget(SimpleSerialTarget): model, coords = unhexlify(resp.data).decode().split(",") return model, coords + def set_trigger(self, actions: Triggers) -> None: + self.send_cmd(SMessage.from_raw(cmd_set_trigger(actions)), self.timeout) + self.trigger = actions + def disconnect(self): self.write(b"x\n") super().disconnect() diff --git a/pyecsca/codegen/rand.h b/pyecsca/codegen/rand.h new file mode 100644 index 0000000..9305af8 --- /dev/null +++ b/pyecsca/codegen/rand.h @@ -0,0 +1,8 @@ +#ifndef RAND_H_ +#define RAND_H_ + +#include "bn/bn.h" + +bn_err bn_rand_mod(bn_t *out, const bn_t *mod); + +#endif //RAND_H_
\ No newline at end of file diff --git a/pyecsca/codegen/render.py b/pyecsca/codegen/render.py index 5cfe569..281bd9c 100644 --- a/pyecsca/codegen/render.py +++ b/pyecsca/codegen/render.py @@ -175,6 +175,13 @@ def render_scalarmult_impl(scalarmult: ScalarMultiplier) -> str: BinaryNAFMultiplier=BinaryNAFMultiplier) +def render_action() -> str: + return env.get_template("action.c").render() + + +def render_rand() -> str: + return env.get_template("rand.c").render() + def render_main(model: CurveModel, coords: CoordinateModel, keygen: bool, ecdh: bool, ecdsa: bool) -> str: return env.get_template("main.c").render(model=model, coords=coords, @@ -202,11 +209,12 @@ def render(config: DeviceConfiguration) -> Tuple[str, str, str]: """ temp = tempfile.mkdtemp() symlinks = ["asn1", "bn", "hal", "hash", "mult", "prng", "simpleserial", "tommath", "fat.h", - "point.h", "curve.h", "mult.h", "formulas.h", "Makefile.inc"] + "rand.h", "point.h", "curve.h", "mult.h", "formulas.h", "action.h", "Makefile.inc"] for sym in symlinks: os.symlink(resource_filename("pyecsca.codegen", sym), path.join(temp, sym)) gen_dir = path.join(temp, "gen") os.mkdir(gen_dir) + save_render(temp, "Makefile", render_makefile(config.platform, config.hash_type, config.mod_rand)) save_render(temp, "main.c", @@ -215,7 +223,10 @@ def render(config: DeviceConfiguration) -> Tuple[str, str, str]: save_render(gen_dir, "point.c", render_coords_impl(config.coords)) save_render(gen_dir, "formulas.c", render_formulas_impl(config.formulas)) for formula in config.formulas: - save_render(gen_dir, f"formula_{formula.shortname}.c", render_formula_impl(formula, config.scalarmult.short_circuit)) + save_render(gen_dir, f"formula_{formula.shortname}.c", + render_formula_impl(formula, config.scalarmult.short_circuit)) + save_render(gen_dir, "action.c", render_action()) + save_render(gen_dir, "rand.c", render_rand()) save_render(gen_dir, "curve.c", render_curve_impl(config.model)) save_render(gen_dir, "mult.c", render_scalarmult_impl(config.scalarmult)) return temp, "pyecsca-codegen-{}.elf".format( diff --git a/pyecsca/codegen/templates/action.c b/pyecsca/codegen/templates/action.c new file mode 100644 index 0000000..258e599 --- /dev/null +++ b/pyecsca/codegen/templates/action.c @@ -0,0 +1,84 @@ +{% macro start_action(action) %} + {% if action == "add" %} + action_start((uint32_t) (1 << 0)); + {% elif action == "dadd" %} + action_start((uint32_t) (1 << 1)); + {% elif action == "dbl" %} + action_start((uint32_t) (1 << 2)); + {% elif action == "ladd" %} + action_start((uint32_t) (1 << 3)); + {% elif action == "neg" %} + action_start((uint32_t) (1 << 4)); + {% elif action == "scl" %} + action_start((uint32_t) (1 << 5)); + {% elif action == "tpl" %} + action_start((uint32_t) (1 << 6)); + {% elif action == "mult" %} + action_start((uint32_t) (1 << 7)); + {% elif action == "keygen" %} + action_start((uint32_t) (1 << 8)); + {% elif action == "ecdh" %} + action_start((uint32_t) (1 << 9)); + {% elif action == "ecdsa_sign" %} + action_start((uint32_t) (1 << 10)); + {% elif action == "ecdsa_verify" %} + action_start((uint32_t) (1 << 11)); + {% elif action == "coord_map" %} + action_start((uint32_t) (1 << 12)); + {% elif action == "random_mod" %} + action_start((uint32_t) (1 << 13)); + {% endif %} +{%- endmacro %} + +{% macro end_action(action) %} + {% if action == "add" %} + action_end((uint32_t) (1 << 0)); + {% elif action == "dadd" %} + action_end((uint32_t) (1 << 1)); + {% elif action == "dbl" %} + action_end((uint32_t) (1 << 2)); + {% elif action == "ladd" %} + action_end((uint32_t) (1 << 3)); + {% elif action == "neg" %} + action_end((uint32_t) (1 << 4)); + {% elif action == "scl" %} + action_end((uint32_t) (1 << 5)); + {% elif action == "tpl" %} + action_end((uint32_t) (1 << 6)); + {% elif action == "mult" %} + action_end((uint32_t) (1 << 7)); + {% elif action == "keygen" %} + action_end((uint32_t) (1 << 8)); + {% elif action == "ecdh" %} + action_end((uint32_t) (1 << 9)); + {% elif action == "ecdsa_sign" %} + action_end((uint32_t) (1 << 10)); + {% elif action == "ecdsa_verify" %} + action_end((uint32_t) (1 << 11)); + {% elif action == "coord_map" %} + action_end((uint32_t) (1 << 12)); + {% elif action == "random_mod" %} + action_end((uint32_t) (1 << 13)); + {% endif %} +{%- endmacro %} + +#include "hal/hal.h" +#include <stdint.h> + +uint32_t action_vector = 0; + +void action_start(uint32_t action) { + if (action_vector & action) { + trigger_high(); + } +} + +void action_end(uint32_t action) { + if (action_vector & action) { + trigger_low(); + } +} + +void action_set(uint32_t new_vector) { + action_vector = new_vector; +} diff --git a/pyecsca/codegen/templates/formula_add.c b/pyecsca/codegen/templates/formula_add.c index 0a04757..4b4ea88 100644 --- a/pyecsca/codegen/templates/formula_add.c +++ b/pyecsca/codegen/templates/formula_add.c @@ -1,11 +1,14 @@ #include "point.h" +#include "action.h" {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} {{ ops.render_static_init(allocations, initializations, formula.shortname) }} {{ ops.render_static_clear(frees, formula.shortname) }} void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) { + {{ start_action("add") }} {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(other, out_one); @@ -18,4 +21,5 @@ void point_add(const point_t *one, const point_t *other, const curve_t *curve, p {%- endif %} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} + {{ end_action("add") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_dadd.c b/pyecsca/codegen/templates/formula_dadd.c index 0cdefe6..d409cce 100644 --- a/pyecsca/codegen/templates/formula_dadd.c +++ b/pyecsca/codegen/templates/formula_dadd.c @@ -1,12 +1,16 @@ #include "point.h" +#include "action.h" {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} {{ ops.render_static_init(allocations, initializations, formula.shortname) }} {{ ops.render_static_clear(frees, formula.shortname) }} void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one) { + {{ start_action("dadd") }} // TODO: short-circuits {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} + {{ end_action("dadd") }} } diff --git a/pyecsca/codegen/templates/formula_dbl.c b/pyecsca/codegen/templates/formula_dbl.c index 6410e7c..e70846e 100644 --- a/pyecsca/codegen/templates/formula_dbl.c +++ b/pyecsca/codegen/templates/formula_dbl.c @@ -1,11 +1,14 @@ #include "point.h" +#include "action.h" {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} {{ ops.render_static_init(allocations, initializations, formula.shortname) }} {{ ops.render_static_clear(frees, formula.shortname) }} void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) { + {{ start_action("dbl") }} {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -14,4 +17,5 @@ void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) { {%- endif %} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} + {{ end_action("dbl") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_ladd.c b/pyecsca/codegen/templates/formula_ladd.c index 32903e9..1ac62ec 100644 --- a/pyecsca/codegen/templates/formula_ladd.c +++ b/pyecsca/codegen/templates/formula_ladd.c @@ -1,12 +1,16 @@ #include "point.h" +#include "action.h" {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} {{ ops.render_static_init(allocations, initializations, formula.shortname) }} {{ ops.render_static_clear(frees, formula.shortname) }} 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) { + {{ start_action("ladd") }} // TODO: short-circuits {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} + {{ end_action("ladd") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_neg.c b/pyecsca/codegen/templates/formula_neg.c index c728e70..39a4f5c 100644 --- a/pyecsca/codegen/templates/formula_neg.c +++ b/pyecsca/codegen/templates/formula_neg.c @@ -1,11 +1,14 @@ #include "point.h" +#include "action.h" {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} {{ ops.render_static_init(allocations, initializations, formula.shortname) }} {{ ops.render_static_clear(frees, formula.shortname) }} void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) { + {{ start_action("neg") }} {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -14,4 +17,5 @@ void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) { {%- endif %} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} + {{ end_action("neg") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_scl.c b/pyecsca/codegen/templates/formula_scl.c index b49fa8a..cc46724 100644 --- a/pyecsca/codegen/templates/formula_scl.c +++ b/pyecsca/codegen/templates/formula_scl.c @@ -1,11 +1,14 @@ #include "point.h" +#include "action.h" {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} {{ ops.render_static_init(allocations, initializations, formula.shortname) }} {{ ops.render_static_clear(frees, formula.shortname) }} void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) { + {{ start_action("scl") }} {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -14,4 +17,5 @@ void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) { {%- endif %} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} + {{ end_action("scl") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_tpl.c b/pyecsca/codegen/templates/formula_tpl.c index 73f34d2..b1863c2 100644 --- a/pyecsca/codegen/templates/formula_tpl.c +++ b/pyecsca/codegen/templates/formula_tpl.c @@ -1,11 +1,14 @@ #include "point.h" +#include "action.h" {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} {{ ops.render_static_init(allocations, initializations, formula.shortname) }} {{ ops.render_static_clear(frees, formula.shortname) }} void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) { + {{ start_action("tpl") }} {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -14,4 +17,5 @@ void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) { {%- endif %} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} + {{ end_action("tpl") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c index d3616aa..9ca0c7b 100644 --- a/pyecsca/codegen/templates/main.c +++ b/pyecsca/codegen/templates/main.c @@ -10,11 +10,16 @@ #include "curve.h" #include "fat.h" #include "formulas.h" +#include "action.h" +#include "rand.h" + #include <stdlib.h> #include <stdint.h> #include <string.h> #include <stdbool.h> +{% from "action.c" import start_action, end_action %} + static point_t *pubkey; static bn_t privkey; @@ -106,7 +111,7 @@ static uint8_t cmd_set_params(uint8_t *data, uint16_t len) { static uint8_t cmd_generate(uint8_t *data, uint16_t len) { // generate a keypair, export privkey and affine pubkey - trigger_high(); + {{ start_action("keygen") }} bn_rand_mod(&privkey, &curve->n); size_t priv_size = bn_to_bin_size(&privkey); size_t coord_size = bn_to_bin_size(&curve->p); @@ -126,7 +131,7 @@ static uint8_t cmd_generate(uint8_t *data, uint16_t len) { bn_to_binpad(&y, pub + coord_size, coord_size); bn_clear(&x); bn_clear(&y); - trigger_low(); + {{ end_action("keygen") }} simpleserial_put('s', priv_size, priv); simpleserial_put('w', coord_size * 2, pub); @@ -189,7 +194,6 @@ static void parse_scalar_mult(const char *path, const uint8_t *data, size_t len, static uint8_t cmd_scalar_mult(uint8_t *data, uint16_t len) { // perform base point scalar mult with supplied scalar. - trigger_high(); bn_t scalar; bn_init(&scalar); parse_data(data, len, "", parse_scalar_mult, (void *) &scalar); size_t coord_size = bn_to_bin_size(&curve->p); @@ -204,7 +208,6 @@ static uint8_t cmd_scalar_mult(uint8_t *data, uint16_t len) { {%- endfor %} bn_clear(&scalar); point_free(result); - trigger_low(); simpleserial_put('w', coord_size * {{ curve_variables | length }}, res); return 0; @@ -228,7 +231,7 @@ static void parse_ecdh(const char *path, const uint8_t *data, size_t len, void * static uint8_t cmd_ecdh(uint8_t *data, uint16_t len) { //perform ECDH with provided point (and current privkey), output shared secret - trigger_high(); + {{ start_action("ecdh") }} point_t *other = point_new(); fat_t affine[2] = {fat_empty, fat_empty}; parse_data(data, len, "", parse_ecdh, (void *) affine); @@ -267,7 +270,7 @@ static uint8_t cmd_ecdh(uint8_t *data, uint16_t len) { bn_clear(&y); point_free(result); point_free(other); - trigger_low(); + {{ end_action("ecdh") }} simpleserial_put('r', h_size, h_out); return 0; @@ -295,6 +298,7 @@ static void parse_ecdsa_sig(const char *path, const uint8_t *data, size_t len, v static uint8_t cmd_ecdsa_sign(uint8_t *data, uint16_t len) { //perform ECDSA signature on supplied data, output signature + {{ start_action("ecdsa_sign") }} fat_t msg = fat_empty; parse_data(data, len, "", parse_ecdsa_msg, (void *) &msg); @@ -339,6 +343,7 @@ static uint8_t cmd_ecdsa_sign(uint8_t *data, uint16_t len) { size_t result_len = 0; uint8_t *result = asn1_der_encode(&r, &s, &result_len); + {{ end_action("ecdsa_sign") }} simpleserial_put('s', result_len, result); free(result); @@ -352,6 +357,7 @@ static uint8_t cmd_ecdsa_sign(uint8_t *data, uint16_t len) { static uint8_t cmd_ecdsa_verify(uint8_t *data, uint16_t len) { //perform ECDSA verification on supplied data and signature (and current pubkey), output status + {{ start_action("ecdsa_verify") }} fat_t msg = fat_empty; parse_data(data, len, "", parse_ecdsa_msg, (void *) &msg); fat_t sig = fat_empty; @@ -403,6 +409,7 @@ static uint8_t cmd_ecdsa_verify(uint8_t *data, uint16_t len) { bool result = bn_eq(&orig_r, &x); uint8_t res_data[1] = {(uint8_t) result}; + {{ end_action("ecdsa_verify") }} simpleserial_put('v', 1, res_data); point_free(p1); @@ -426,6 +433,13 @@ static uint8_t cmd_debug(uint8_t *data, uint16_t len) { return 0; } +static uint8_t cmd_set_trigger(uint8_t *data, uint16_t len) { + uint32_t vector = data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24; + action_set(vector); + + return 0; +} + int main(void) { platform_init(); init_uart(); @@ -454,6 +468,7 @@ int main(void) { simpleserial_addcmd('a', MAX_SS_LEN, cmd_ecdsa_sign); simpleserial_addcmd('r', MAX_SS_LEN, cmd_ecdsa_verify); {%- endif %} + simpleserial_addcmd('t', MAX_SS_LEN, cmd_set_trigger); simpleserial_addcmd('d', MAX_SS_LEN, cmd_debug); led_ok(1); diff --git a/pyecsca/codegen/templates/mult_bnaf.c b/pyecsca/codegen/templates/mult_bnaf.c index 9ede934..10a93fe 100644 --- a/pyecsca/codegen/templates/mult_bnaf.c +++ b/pyecsca/codegen/templates/mult_bnaf.c @@ -1,7 +1,10 @@ #include "mult.h" #include "point.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + {{ start_action("mult") }} point_t *neg = point_new(); point_neg(point, curve, neg); point_t *q = point_copy(curve->neutral); @@ -25,4 +28,5 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { point_set(q, out); point_free(neg); point_free(q); + {{ end_action("mult") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_coron.c b/pyecsca/codegen/templates/mult_coron.c index d8e8a0d..a2b1085 100644 --- a/pyecsca/codegen/templates/mult_coron.c +++ b/pyecsca/codegen/templates/mult_coron.c @@ -1,7 +1,10 @@ #include "mult.h" #include "point.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + {{ start_action("mult") }} point_t *p0 = point_copy(point); point_t *p1 = point_new(); @@ -19,4 +22,5 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { point_set(p0, out); point_free(p0); point_free(p1); + {{ end_action("mult") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_diff_ldr.c b/pyecsca/codegen/templates/mult_diff_ldr.c index 2683116..3dd445e 100644 --- a/pyecsca/codegen/templates/mult_diff_ldr.c +++ b/pyecsca/codegen/templates/mult_diff_ldr.c @@ -1,7 +1,10 @@ #include "mult.h" #include "point.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + {{ start_action("mult") }} point_t *p0 = point_copy(&curve->neutral); point_t *p1 = point_copy(point); {%- if scalarmult.complete %} @@ -26,4 +29,5 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { point_set(p0, out); point_free(p0); point_free(p1); + {{ end_action("mult") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_ldr.c b/pyecsca/codegen/templates/mult_ldr.c index c7c5246..b51f3fa 100644 --- a/pyecsca/codegen/templates/mult_ldr.c +++ b/pyecsca/codegen/templates/mult_ldr.c @@ -1,7 +1,10 @@ #include "mult.h" #include "point.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + {{ start_action("mult") }} {%- if scalarmult.complete %} point_t *p0 = point_copy(curve->neutral); point_t *p1 = point_copy(point); @@ -27,4 +30,5 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { point_set(p0, out); point_free(p0); point_free(p1); + {{ end_action("mult") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_ltr.c b/pyecsca/codegen/templates/mult_ltr.c index 5b08b80..187b536 100644 --- a/pyecsca/codegen/templates/mult_ltr.c +++ b/pyecsca/codegen/templates/mult_ltr.c @@ -1,7 +1,10 @@ #include "mult.h" #include "point.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + {{ start_action("mult") }} {%- if scalarmult.complete %} point_t *q = point_copy(point); point_t *r = point_copy(curve->neutral); @@ -36,4 +39,5 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { {%- if scalarmult.always %} point_free(dummy); {%- endif %} + {{ end_action("mult") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_rtl.c b/pyecsca/codegen/templates/mult_rtl.c index ba40a66..acddf45 100644 --- a/pyecsca/codegen/templates/mult_rtl.c +++ b/pyecsca/codegen/templates/mult_rtl.c @@ -1,7 +1,10 @@ #include "mult.h" #include "point.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + {{ start_action("mult") }} point_t *q = point_copy(point); point_t *r = point_copy(curve->neutral); @@ -34,4 +37,5 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { {%- if scalarmult.always %} point_free(dummy); {%- endif %} + {{ end_action("mult") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_simple_ldr.c b/pyecsca/codegen/templates/mult_simple_ldr.c index 438a44b..8db5b41 100644 --- a/pyecsca/codegen/templates/mult_simple_ldr.c +++ b/pyecsca/codegen/templates/mult_simple_ldr.c @@ -1,7 +1,10 @@ #include "mult.h" #include "point.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + {{ start_action("mult") }} point_t *p0 = point_copy(&curve->neutral); point_t *p1 = point_copy(point); {%- if scalarmult.complete %} @@ -26,4 +29,5 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { point_set(p0, out); point_free(p0); point_free(p1); + {{ end_action("mult") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/point.c b/pyecsca/codegen/templates/point.c index a3c9f59..0096516 100644 --- a/pyecsca/codegen/templates/point.c +++ b/pyecsca/codegen/templates/point.c @@ -1,6 +1,8 @@ #include "point.h" +#include "action.h" #include <stdlib.h> {% import "ops.c" as ops %} +{% from "action.c" import start_action, end_action %} point_t *point_new(void) { point_t *result = malloc(sizeof(point_t)); @@ -75,6 +77,7 @@ 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) { + {{ start_action("coord_map") }} {{ ops.render_all(allocations, initializations, operations, returns, frees, "err") }} if (err != BN_OKAY) { return; @@ -92,9 +95,11 @@ void point_to_affine(const point_t *point, const curve_t *curve, bn_t *out_x, bn {%- for free in to_affine_frees %} bn_clear(&{{ free }}); {%- endfor %} + {{ end_action("coord_map") }} } void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out) { + {{ start_action("coord_map") }} {# XXX: This just works for the stuff currently in EFD. #} {%- for variable in variables %} {%- if variable in ("X", "Y") %} @@ -107,4 +112,5 @@ void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out) { bn_mod_mul(x, y, &curve->p, &out->T); {%- endif %} {%- endfor %} + {{ end_action("coord_map") }} } diff --git a/pyecsca/codegen/templates/rand.c b/pyecsca/codegen/templates/rand.c new file mode 100644 index 0000000..e39c829 --- /dev/null +++ b/pyecsca/codegen/templates/rand.c @@ -0,0 +1,16 @@ +#include "bn/bn.h" +#include "action.h" +{% from "action.c" import start_action, end_action %} + +bn_err bn_rand_mod(bn_t *out, const bn_t *mod) { + {{ start_action("random_mod") }} + + #if MOD_RAND == MOD_RAND_SAMPLE + bn_err err = bn_rand_mod_sample(out, mod); + #elif MOD_RAND == MOD_RAND_REDUCE + bn_err err = bn_rand_mod_reduce(out, mod); + #endif + + {{ end_action("random_mod") }} + return err; +}
\ No newline at end of file |
