diff options
| author | J08nY | 2023-10-05 13:01:06 +0200 |
|---|---|---|
| committer | J08nY | 2023-10-05 13:01:06 +0200 |
| commit | 17c03a2bc34598a91cd897012270ad0cc875f23a (patch) | |
| tree | 5493b148c411b60938208103f3d696dcd0b78b87 /pyecsca/codegen | |
| parent | 8c170b7bbbf2503472f0cd5bbf1900454209eda6 (diff) | |
| download | pyecsca-codegen-17c03a2bc34598a91cd897012270ad0cc875f23a.tar.gz pyecsca-codegen-17c03a2bc34598a91cd897012270ad0cc875f23a.tar.zst pyecsca-codegen-17c03a2bc34598a91cd897012270ad0cc875f23a.zip | |
Add option to pass C defines to compiler.
Diffstat (limited to 'pyecsca/codegen')
| -rw-r--r-- | pyecsca/codegen/builder.py | 22 | ||||
| -rw-r--r-- | pyecsca/codegen/common.py | 8 | ||||
| -rw-r--r-- | pyecsca/codegen/render.py | 6 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/Makefile | 4 |
4 files changed, 33 insertions, 7 deletions
diff --git a/pyecsca/codegen/builder.py b/pyecsca/codegen/builder.py index d0ec648..c595f60 100644 --- a/pyecsca/codegen/builder.py +++ b/pyecsca/codegen/builder.py @@ -4,7 +4,7 @@ import shutil import subprocess from copy import copy from os import path -from typing import List, Optional, Tuple, Type, MutableMapping +from typing import List, Optional, Tuple, Type, MutableMapping, Any import click from public import public @@ -77,6 +77,20 @@ def get_multiplier(ctx: click.Context, param, value: Optional[str]) -> Optional[ return mult +def get_define(ctx: click.Context, param, values: Optional[List[str]]) -> Optional[MutableMapping[str, Any]]: + if values is None: + return None + res = {} + for val in values: + try: + k, v = val.split("=") + except: + k = val + v = 1 + res[k] = v + return res + + @click.group(context_settings={"help_option_names": ["-h", "--help"]}) @click.version_option() @public @@ -122,6 +136,8 @@ def main(): show_default=True) @click.option("--ecdsa/--no-ecdsa", help="Whether to enable ECDSA.", is_flag=True, default=True, show_default=True) +@click.option("-D", "--define", help="Set a custom C define.", multiple=True, + type=str, callback=get_define) @click.option("--strip", help="Whether to strip the binary or not.", is_flag=True) @click.option("--remove/--no-remove", help="Whether to remove the dir.", is_flag=True, default=True, show_default=True) @@ -138,7 +154,7 @@ def main(): @click.argument("outdir") @click.pass_context @public -def build_impl(ctx, platform, hash, rand, mul, sqr, red, inv, keygen, ecdh, ecdsa, strip, remove, +def build_impl(ctx, platform, hash, rand, mul, sqr, red, inv, keygen, ecdh, ecdsa, define, strip, remove, verbose, model, coords, formulas, scalarmult, outdir): """This command builds an ECC implementation. @@ -156,7 +172,7 @@ def build_impl(ctx, platform, hash, rand, mul, sqr, red, inv, keygen, ecdh, ecds click.echo("[ ] Rendering...") config = DeviceConfiguration(model, coords, formulas, scalarmult, hash, rand, mul, sqr, red, - inv, platform, keygen, ecdh, ecdsa) + inv, platform, keygen, ecdh, ecdsa, define) dir, elf_file, hex_file = render(config) click.echo("[*] Rendered.") diff --git a/pyecsca/codegen/common.py b/pyecsca/codegen/common.py index b970039..6bffbdb 100644 --- a/pyecsca/codegen/common.py +++ b/pyecsca/codegen/common.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Type, Optional +from typing import Type, Optional, MutableMapping, Any import click from public import public @@ -37,9 +37,15 @@ class DeviceConfiguration(Configuration): """A device configuration that includes the platform and choices specific to the pyecsca-codegened implementations.""" platform: Platform + """The platform to build for.""" keygen: bool + """Whether the key-generation command is present.""" ecdh: bool + """Whether the ECDH command is present.""" ecdsa: bool + """Whether the ECDSA command is present.""" + defines: Optional[MutableMapping[str, Any]] = None + """Optional defines passed to the compilation.""" MULTIPLIERS = [ diff --git a/pyecsca/codegen/render.py b/pyecsca/codegen/render.py index ba06a72..ee07324 100644 --- a/pyecsca/codegen/render.py +++ b/pyecsca/codegen/render.py @@ -244,10 +244,10 @@ def render_main(model: CurveModel, coords: CoordinateModel, keygen: bool, ecdh: def render_makefile(platform: Platform, hash_type: HashType, mod_rand: RandomMod, - reduction: Reduction, mul: Multiplication, sqr: Squaring) -> str: + reduction: Reduction, mul: Multiplication, sqr: Squaring, defines: Optional[MutableMapping[str, Any]]) -> str: return env.get_template("Makefile").render(platform=str(platform), hash_type=str(hash_type), mod_rand=str(mod_rand), reduction=str(reduction), - mul=str(mul), sqr=str(sqr)) + mul=str(mul), sqr=str(sqr), defines=defines) def save_render(dir: str, fname: str, rendered: str): @@ -272,7 +272,7 @@ def render(config: DeviceConfiguration) -> Tuple[str, str, str]: makedirs(gen_dir, exist_ok=True) save_render(temp, "Makefile", - render_makefile(config.platform, config.hash_type, config.mod_rand, config.red, config.mult, config.sqr)) + render_makefile(config.platform, config.hash_type, config.mod_rand, config.red, config.mult, config.sqr, config.defines)) save_render(temp, "main.c", render_main(config.model, config.coords, config.keygen, config.ecdh, config.ecdsa)) save_render(gen_dir, "defs.h", render_defs(config.model, config.coords)) diff --git a/pyecsca/codegen/templates/Makefile b/pyecsca/codegen/templates/Makefile index 79e5c20..1b6922c 100644 --- a/pyecsca/codegen/templates/Makefile +++ b/pyecsca/codegen/templates/Makefile @@ -6,6 +6,10 @@ PLATFORM = {{ platform }} CDEFS += -DHASH={{ hash_type }} -DMOD_RAND={{ mod_rand }} -DREDUCTION={{ reduction }} -DMUL={{ mul }} -DSQR={{ sqr }} +{%- if defines %} +CDEFS += {%- for def, value in defines.items() -%}-D{{def}}={{value}} {%- endfor -%} +{%- endif %} + MKDIR_LIST += hash prng asn1 bn gen EXTRAINCDIRS += hash prng asn1 bn gen tommath |
