aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--pyecsca/codegen/Makefile16
-rw-r--r--pyecsca/codegen/__init__.py103
-rw-r--r--pyecsca/codegen/bn/bn.c4
-rw-r--r--pyecsca/codegen/bn/bn.h1
-rw-r--r--pyecsca/codegen/curve.h12
-rw-r--r--pyecsca/codegen/mult/double_and_add.c6
-rw-r--r--pyecsca/codegen/point.h32
-rw-r--r--pyecsca/codegen/prng/prng.c2
-rw-r--r--pyecsca/codegen/prng/prng.h2
-rw-r--r--pyecsca/codegen/templates/Makefile18
-rw-r--r--pyecsca/codegen/templates/coords.h36
-rw-r--r--pyecsca/codegen/templates/curve.c6
-rw-r--r--pyecsca/codegen/templates/defs.h (renamed from pyecsca/codegen/templates/curve.h)20
-rw-r--r--pyecsca/codegen/templates/formula_add.c2
-rw-r--r--pyecsca/codegen/templates/formula_dadd.c2
-rw-r--r--pyecsca/codegen/templates/formula_dbl.c2
-rw-r--r--pyecsca/codegen/templates/formula_ladd.c2
-rw-r--r--pyecsca/codegen/templates/formula_neg.c2
-rw-r--r--pyecsca/codegen/templates/formula_scl.c2
-rw-r--r--pyecsca/codegen/templates/formula_tpl.c2
-rw-r--r--pyecsca/codegen/templates/main.c62
-rw-r--r--pyecsca/codegen/templates/point.c (renamed from pyecsca/codegen/templates/coords.c)7
22 files changed, 226 insertions, 115 deletions
diff --git a/pyecsca/codegen/Makefile b/pyecsca/codegen/Makefile
deleted file mode 100644
index d0dab03..0000000
--- a/pyecsca/codegen/Makefile
+++ /dev/null
@@ -1,16 +0,0 @@
-TARGET = pyecsca-codegen
-
-SRC += main.c bn/bn.c asn1/asn1.c hash/hash.c mult/mult.c prng/prng.c
-
-CDEFS += -DHASH=HASH_SHA224 -DMOD_RAND=MOD_RAND_SAMPLE
-
-MKDIR_LIST += hash prng mult asn1 bn
-
-EXTRAINCDIRS += hash prng mult asn1 bn ../../ext/libtommath/
-
-LDFLAGS += ../../ext/libtommath/libtommath.a
-
-include simpleserial/Makefile.simpleserial
-
-FIRMWAREPATH = .
-include Makefile.inc
diff --git a/pyecsca/codegen/__init__.py b/pyecsca/codegen/__init__.py
index 104b43f..06c85c2 100644
--- a/pyecsca/codegen/__init__.py
+++ b/pyecsca/codegen/__init__.py
@@ -1,7 +1,12 @@
+import os
+import tempfile
from ast import operator, Add, Sub, Mult, Div, Pow
+from enum import Enum
+from os import path
from typing import List, Set, Mapping
from jinja2 import Environment, PackageLoader
+from pkg_resources import resource_filename
from pyecsca.ec.coordinates import CoordinateModel
from pyecsca.ec.formula import (Formula, AdditionFormula, DoublingFormula, TriplingFormula,
NegationFormula, ScalingFormula, DifferentialAdditionFormula,
@@ -25,21 +30,53 @@ def render_op(op: operator, result: str, left: str, right: str, mod: str):
return "bn_mod_div(&{}, &{}, &{}, &{});".format(left, right, mod, result)
elif isinstance(op, Pow) and right == 2:
return "bn_mod_sqr(&{}, &{}, &{});".format(left, mod, result)
+ elif isinstance(op, Pow):
+ return "bn_mod_pow(&{}, &{}, &{}, &{});".format(left, right, mod, result)
+ else:
+ print(op, result, left, right, mod)
env.globals["render_op"] = render_op
-def render_curve_definition(model: CurveModel):
- return env.get_template("curve.h").render(params=model.parameter_names)
+class EnumDefine(Enum):
+ def __str__(self):
+ return self.value
-def render_curve_impl(model: CurveModel):
- return env.get_template("curve.c").render(params=model.parameter_names)
+class Platform(EnumDefine):
+ HOST = "HOST"
+ XMEGA = "CW308_XMEGA"
+ STM32F0 = "CW308_STM32F0"
+ STM32F3 = "CW308_STM32F3"
+
+
+class HashType(EnumDefine):
+ NONE = "HASH_NONE"
+ SHA1 = "HASH_SHA1"
+ SHA224 = "HASH_SHA224"
+ SHA256 = "HASH_SHA256"
+ SHA384 = "HASH_SHA384"
+ SHA512 = "HASH_SHA512"
-def render_coords_definition(coords: CoordinateModel):
- return env.get_template("coords.h").render(variables=coords.variables)
+class MultAlgo(EnumDefine):
+ NONE = "MULT_NONE"
+ DOUBLE_AND_ADD = "MULT_DOUBLE_AND_ADD"
+
+
+class RandomMod(EnumDefine):
+ SAMPLE = "MOD_RAND_SAMPLE"
+ REDUCE = "MOD_RAND_REDUCE"
+
+
+def render_defs(model: CurveModel, coords: CoordinateModel):
+ return env.get_template("defs.h").render(params=model.parameter_names,
+ variables=coords.variables)
+
+
+def render_curve_impl(model: CurveModel):
+ return env.get_template("curve.c").render(params=model.parameter_names)
def transform_ops(ops: List[CodeOp], parameters: List[str], outputs: Set[str],
@@ -69,6 +106,15 @@ def transform_ops(ops: List[CodeOp], parameters: List[str], outputs: Set[str],
const_mapping[const] = name
frees.append(name)
operations.append((op.operator, op.result, rename(op.left), rename(op.right)))
+ mapped = []
+ for op in operations:
+ o2 = op[2]
+ if o2 in const_mapping:
+ o2 = const_mapping[o2]
+ o3 = op[3]
+ if o3 in const_mapping and not (isinstance(op[0], Pow) and o3 == 2):
+ o3 = const_mapping[o3]
+ mapped.append((op[0], op[1], o2, o3))
returns = {}
if renames:
for r_from, r_to in renames.items():
@@ -77,7 +123,7 @@ def transform_ops(ops: List[CodeOp], parameters: List[str], outputs: Set[str],
return dict(allocations=allocations,
initializations=initializations,
- const_mapping=const_mapping, operations=operations,
+ const_mapping=const_mapping, operations=mapped,
frees=frees, returns=returns)
@@ -97,6 +143,8 @@ def render_coords_impl(coords: CoordinateModel):
renames = {"x": "out_x", "y": "out_y"}
for variable in coords.variables:
renames[variable] = "point->{}".format(variable)
+ for param in coords.curve_model.parameter_names:
+ renames[param] = "curve->{}".format(param)
namespace = transform_ops(ops, coords.curve_model.parameter_names,
coords.curve_model.coordinate_names, renames)
returns = namespace["returns"]
@@ -104,8 +152,8 @@ def render_coords_impl(coords: CoordinateModel):
frees = namespace["frees"]
namespace["frees"] = {}
- return env.get_template("coords.c").render(variables=coords.variables, **namespace,
- to_affine_rets=returns, to_affine_frees=frees)
+ return env.get_template("point.c").render(variables=coords.variables, **namespace,
+ to_affine_rets=returns, to_affine_frees=frees)
def render_formula_impl(formula: Formula):
@@ -133,6 +181,8 @@ def render_formula_impl(formula: Formula):
var = input[0]
num = int(input[1:]) - formula.input_index
renames[input] = "{}->{}".format(inputs[num], var)
+ for param in formula.coordinate_model.curve_model.parameter_names:
+ renames[param] = "curve->{}".format(param)
for output in formula.outputs:
var = output[0]
num = int(output[1:]) - formula.output_index
@@ -147,7 +197,40 @@ def render_main(model: CurveModel, coords: CoordinateModel):
curve_parameters=model.parameter_names)
+def render_makefile(platform: Platform, hash_type: HashType, mult_algo: MultAlgo,
+ mod_rand: RandomMod):
+ return env.get_template("Makefile").render(platform=str(platform), hash_type=str(hash_type),
+ mult_algo=str(mult_algo), mod_rand=str(mod_rand))
+
+
+def save_render(dir: str, fname: str, render: str):
+ with open(path.join(dir, fname), "w") as f:
+ f.write(render)
+
+
+def build(platform: Platform, hash_type: HashType, mult_algo: MultAlgo, mod_rand: RandomMod,
+ model: CurveModel, coords: CoordinateModel, *formulas: Formula):
+ temp = tempfile.mkdtemp()
+ symlinks = ["asn1", "bn", "hal", "hash", "mult", "prng", "simpleserial", "tommath", "fat.h",
+ "point.h", "curve.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(platform, hash_type, mult_algo, mod_rand))
+ save_render(temp, "main.c", render_main(model, coords))
+ save_render(gen_dir, "defs.h", render_defs(model, coords))
+ point_render = render_coords_impl(coords)
+ for formula in formulas:
+ point_render += "\n"
+ point_render += render_formula_impl(formula)
+ save_render(gen_dir, "point.c", point_render)
+ save_render(gen_dir, "curve.c", render_curve_impl(model))
+ print(temp)
+
+
if __name__ == "__main__":
model = ShortWeierstrassModel()
coords = model.coordinates["projective"]
- print(render_coords_impl(coords))
+ build(Platform.HOST, HashType.SHA1, MultAlgo.DOUBLE_AND_ADD, RandomMod.SAMPLE, model, coords,
+ coords.formulas["add-1998-cmo"], coords.formulas["dbl-1998-cmo"])
diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c
index 9b805d1..b34b789 100644
--- a/pyecsca/codegen/bn/bn.c
+++ b/pyecsca/codegen/bn/bn.c
@@ -89,6 +89,10 @@ void bn_mod_inv(const bn_t *one, const bn_t *mod, bn_t *out) {
mp_invmod(one, mod, out);
}
+void bn_mod_pow(const bn_t *one, const bn_t *exp, const bn_t *mod, bn_t *out) {
+ mp_exptmod(one, exp, mod, out);
+}
+
void bn_mod(const bn_t *one, const bn_t *mod, bn_t *out) {
mp_mod(one, mod, out);
}
diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h
index 2ff723f..68ede2f 100644
--- a/pyecsca/codegen/bn/bn.h
+++ b/pyecsca/codegen/bn/bn.h
@@ -38,6 +38,7 @@ void bn_mod_mul(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out);
void bn_mod_sqr(const bn_t *one, const bn_t *mod, bn_t *out);
void bn_mod_div(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out);
void bn_mod_inv(const bn_t *one, const bn_t *mod, bn_t *out);
+void bn_mod_pow(const bn_t *one, const bn_t *exp, const bn_t *mod, bn_t *out);
void bn_mod(const bn_t *one, const bn_t *mod, bn_t *out);
void bn_lsh(const bn_t *one, int amount, bn_t *out);
diff --git a/pyecsca/codegen/curve.h b/pyecsca/codegen/curve.h
new file mode 100644
index 0000000..f498695
--- /dev/null
+++ b/pyecsca/codegen/curve.h
@@ -0,0 +1,12 @@
+#ifndef CURVE_H_
+#define CURVE_H_
+
+#include "defs.h"
+
+curve_t* curve_new(void);
+
+void curve_free(curve_t *curve);
+
+void curve_set_param(curve_t *curve, char name, const bn_t *value);
+
+#endif //CURVE_H_ \ No newline at end of file
diff --git a/pyecsca/codegen/mult/double_and_add.c b/pyecsca/codegen/mult/double_and_add.c
index a795654..1cead25 100644
--- a/pyecsca/codegen/mult/double_and_add.c
+++ b/pyecsca/codegen/mult/double_and_add.c
@@ -1,18 +1,18 @@
#include "mult.h"
-#include "formulas.h"
+#include "point.h"
void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *q = point_copy(point);
point_t *r = point_copy(curve->neutral);
- int nbits = bn_bit_length(&curve->n);
+ int nbits = bn_bit_length(scalar);
for (int i = nbits; i >= 0; i--) {
point_dbl(r, curve, r);
if (bn_get_bit(scalar, i) == 1) {
point_add(q, r, curve, r);
}
}
- point_scl(r, curve, r);
+ //point_scl(r, curve, r);
point_set(r, out);
point_free(q);
point_free(r);
diff --git a/pyecsca/codegen/point.h b/pyecsca/codegen/point.h
new file mode 100644
index 0000000..61e7a7d
--- /dev/null
+++ b/pyecsca/codegen/point.h
@@ -0,0 +1,32 @@
+#ifndef POINT_H_
+#define POINT_H_
+
+#include "defs.h"
+
+point_t *point_new(void);
+
+point_t *point_copy(const point_t *from);
+
+void point_set(const point_t *from, point_t *out);
+
+void point_free(point_t *point);
+
+void point_to_affine(point_t *point, curve_t *curve, bn_t *out_x, bn_t *out_y);
+
+void point_from_affine(bn_t *x, bn_t *y, 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_dbl(const point_t *one, const curve_t *curve, point_t *out_one);
+
+void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one);
+
+void point_neg(const point_t *one, const curve_t *curve, point_t *out_one);
+
+void point_scl(const point_t *one, const curve_t *curve, point_t *out_one);
+
+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_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);
+
+#endif //POINT_H_ \ No newline at end of file
diff --git a/pyecsca/codegen/prng/prng.c b/pyecsca/codegen/prng/prng.c
index e3cb755..38ec615 100644
--- a/pyecsca/codegen/prng/prng.c
+++ b/pyecsca/codegen/prng/prng.c
@@ -21,7 +21,7 @@ int prng_get(uint8_t *out, size_t size) {
return KeccakWidth200_SpongePRG_Fetch(&keccak, out, size);
}
-void prng_seed(uint8_t *seed, size_t size) {
+void prng_seed(const uint8_t *seed, size_t size) {
KeccakWidth200_SpongePRG_Feed(&keccak, seed, size);
KeccakWidth200_SpongePRG_Forget(&keccak);
}
diff --git a/pyecsca/codegen/prng/prng.h b/pyecsca/codegen/prng/prng.h
index b399a6c..94a3ea6 100644
--- a/pyecsca/codegen/prng/prng.h
+++ b/pyecsca/codegen/prng/prng.h
@@ -4,5 +4,5 @@ void prng_init(void);
int prng_get(uint8_t *out, size_t size);
-void prng_seed(uint8_t *seed, size_t size);
+void prng_seed(const uint8_t *seed, size_t size);
diff --git a/pyecsca/codegen/templates/Makefile b/pyecsca/codegen/templates/Makefile
new file mode 100644
index 0000000..27402e4
--- /dev/null
+++ b/pyecsca/codegen/templates/Makefile
@@ -0,0 +1,18 @@
+TARGET = pyecsca-codegen
+
+SRC += main.c bn/bn.c asn1/asn1.c hash/hash.c mult/mult.c prng/prng.c gen/point.c gen/curve.c
+
+PLATFORM = {{ platform }}
+
+CDEFS += -DHASH={{ hash_type }} -DMULT={{ mult_algo }} -DMOD_RAND={{ mod_rand }}
+
+MKDIR_LIST += hash prng mult asn1 bn gen
+
+EXTRAINCDIRS += hash prng mult asn1 bn gen tommath
+
+LDFLAGS += tommath/libtommath-{{ platform }}.a
+
+include simpleserial/Makefile.simpleserial
+
+FIRMWAREPATH = .
+include Makefile.inc
diff --git a/pyecsca/codegen/templates/coords.h b/pyecsca/codegen/templates/coords.h
deleted file mode 100644
index 91b6626..0000000
--- a/pyecsca/codegen/templates/coords.h
+++ /dev/null
@@ -1,36 +0,0 @@
-#ifndef POINT_H_
-#define POINT_H_
-
-typedef struct {
- {%- for variable in variables %}
- bn_t {{ variable }};
- {%- endfor %}
-} point_t;
-
-point_t *point_new(void);
-
-point_t *point_copy(const point_t *from);
-
-void point_set(const point_t *from, point_t *out);
-
-void point_free(point_t *point);
-
-int point_to_affine(point_t *point, curve_t *curve, bn_t *out_x, bn_t *out_y);
-
-int point_from_affine(bn_t *x, bn_t *y, curve_t *curve, point_t *out);
-
-int point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one);
-
-int point_dbl(const point_t *one, const curve_t *curve, point_t *out_one);
-
-int point_tpl(const point_t *one, const curve_t *curve, point_t *out_one);
-
-int point_neg(const point_t *one, const curve_t *curve, point_t *out_one);
-
-int point_scl(const point_t *one, const curve_t *curve, point_t *out_one);
-
-int point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one);
-
-int 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);
-
-#endif //POINT_H_ \ No newline at end of file
diff --git a/pyecsca/codegen/templates/curve.c b/pyecsca/codegen/templates/curve.c
index 425bf2d..a3a6592 100644
--- a/pyecsca/codegen/templates/curve.c
+++ b/pyecsca/codegen/templates/curve.c
@@ -1,4 +1,8 @@
-curve_t* curve_new() {
+#include "curve.h"
+#include "point.h"
+#include <stdlib.h>
+
+curve_t* curve_new(void) {
curve_t *result = malloc(sizeof(curve_t));
{%- for param in params + ["p", "n", "h"] %}
bn_init(&result->{{ param }});
diff --git a/pyecsca/codegen/templates/curve.h b/pyecsca/codegen/templates/defs.h
index f9b1507..f517ea3 100644
--- a/pyecsca/codegen/templates/curve.h
+++ b/pyecsca/codegen/templates/defs.h
@@ -1,5 +1,13 @@
-#ifndef CURVE_H_
-#define CURVE_H_
+#ifndef DEFS_H_
+#define DEFS_H_
+
+#include "bn.h"
+
+typedef struct {
+ {%- for variable in variables %}
+ bn_t {{ variable }};
+ {%- endfor %}
+} point_t;
typedef struct {
bn_t p;
@@ -12,10 +20,4 @@ typedef struct {
point_t *neutral;
} curve_t;
-curve_t* curve_new();
-
-void curve_free(curve_t *curve);
-
-void curve_set_param(curve_t *curve, char name, const bn_t *value);
-
-#endif //CURVE_H_ \ No newline at end of file
+#endif //DEFS_H_ \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_add.c b/pyecsca/codegen/templates/formula_add.c
index 483fdac..971dd0a 100644
--- a/pyecsca/codegen/templates/formula_add.c
+++ b/pyecsca/codegen/templates/formula_add.c
@@ -1,3 +1,3 @@
-int point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) {
+void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) {
{%- include "ops.c" %}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_dadd.c b/pyecsca/codegen/templates/formula_dadd.c
index 16d49c1..5d3bd05 100644
--- a/pyecsca/codegen/templates/formula_dadd.c
+++ b/pyecsca/codegen/templates/formula_dadd.c
@@ -1,3 +1,3 @@
-int 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(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one) {
{%- include "ops.c" %}
}
diff --git a/pyecsca/codegen/templates/formula_dbl.c b/pyecsca/codegen/templates/formula_dbl.c
index 13da1c0..89543a1 100644
--- a/pyecsca/codegen/templates/formula_dbl.c
+++ b/pyecsca/codegen/templates/formula_dbl.c
@@ -1,3 +1,3 @@
-int point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) {
+void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- include "ops.c" %}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_ladd.c b/pyecsca/codegen/templates/formula_ladd.c
index d1c5c91..73f2325 100644
--- a/pyecsca/codegen/templates/formula_ladd.c
+++ b/pyecsca/codegen/templates/formula_ladd.c
@@ -1,3 +1,3 @@
-int 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(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other) {
{%- include "ops.c" %}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_neg.c b/pyecsca/codegen/templates/formula_neg.c
index 0b7703a..156053c 100644
--- a/pyecsca/codegen/templates/formula_neg.c
+++ b/pyecsca/codegen/templates/formula_neg.c
@@ -1,3 +1,3 @@
-int point_neg(const point_t *one, const curve_t *curve, point_t *out_one) {
+void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- include "ops.c" %}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_scl.c b/pyecsca/codegen/templates/formula_scl.c
index 3f1338d..a9595eb 100644
--- a/pyecsca/codegen/templates/formula_scl.c
+++ b/pyecsca/codegen/templates/formula_scl.c
@@ -1,3 +1,3 @@
-int point_scl(const point_t *one, const curve_t *curve, point_t *out_one) {
+void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- include "ops.c" %}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_tpl.c b/pyecsca/codegen/templates/formula_tpl.c
index dbef1ae..debfd37 100644
--- a/pyecsca/codegen/templates/formula_tpl.c
+++ b/pyecsca/codegen/templates/formula_tpl.c
@@ -1,3 +1,3 @@
-int point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) {
+void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- include "ops.c" %}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c
index a3051f1..ac620b5 100644
--- a/pyecsca/codegen/templates/main.c
+++ b/pyecsca/codegen/templates/main.c
@@ -1,13 +1,18 @@
#include "hal/hal.h"
#include "simpleserial/simpleserial.h"
+#include "asn1/asn1.h"
#include "hash/hash.h"
-#include "bn.h"
-#include "prng.h"
-#include "defs.h"
+#include "mult/mult.h"
+#include "bn/bn.h"
+#include "prng/prng.h"
+#include "gen/defs.h"
+#include "point.h"
+#include "curve.h"
#include "fat.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
+#include <stdbool.h>
static point_t *pubkey;
static bn_t privkey;
@@ -90,7 +95,7 @@ static uint8_t cmd_generate(uint8_t *data, uint16_t len) {
simpleserial_put('s', priv_size, priv);
uint8_t pub[coord_size * {{ curve_parameters | length }}];
{%- for variable in curve_variables %}
- bn_to_binpad(pubkey->{{ variable }}, pub + coord_size * {{ loop.index0 }}, coord_size);
+ bn_to_binpad(&pubkey->{{ variable }}, pub + coord_size * {{ loop.index0 }}, coord_size);
{%- endfor %}
simpleserial_put('w', coord_size * {{ curve_parameters | length }}, pub);
return 0;
@@ -136,6 +141,7 @@ static uint8_t cmd_scalar_mult(uint8_t *data, uint16_t len) {
// perform base point scalar mult with supplied scalar, return affine point.
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);
point_t *result = point_new();
@@ -143,11 +149,11 @@ static uint8_t cmd_scalar_mult(uint8_t *data, uint16_t len) {
uint8_t res[coord_size * {{ curve_parameters | length }}];
{%- for variable in curve_variables %}
- bn_to_binpad(result->{{ variable }}, res + coord_size * {{ loop.index0 }}, coord_size);
+ bn_to_binpad(&result->{{ variable }}, res + coord_size * {{ loop.index0 }}, coord_size);
{%- endfor %}
simpleserial_put('w', coord_size * {{ curve_parameters | length }}, res);
bn_clear(&scalar);
- point_free(&result);
+ point_free(result);
return 0;
}
@@ -178,7 +184,7 @@ static uint8_t cmd_ecdh(uint8_t *data, uint16_t len) {
size_t size = bn_to_bin_size(&curve->p);
uint8_t x_raw[size];
- bn_to_binpad(x, x_raw, size);
+ bn_to_binpad(&x, x_raw, size);
size_t h_size = hash_size(size);
void *h_ctx = hash_new_ctx();
@@ -217,21 +223,21 @@ 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
- fat_t data = fat_empty;
- parse_data(data, len, "", parse_ecdsa_msg, (void *) &data);
+ fat_t msg = fat_empty;
+ parse_data(data, len, "", parse_ecdsa_msg, (void *) &msg);
- size_t h_size = hash_size(data.len);
+ size_t h_size = hash_size(msg.len);
void *h_ctx = hash_new_ctx();
hash_init(h_ctx);
uint8_t h_out[h_size];
- hash_final(h_ctx, data.len, data.value, h_out);
+ hash_final(h_ctx, msg.len, msg.value, h_out);
hash_free_ctx(h_ctx);
- free(data.value);
+ free(msg.value);
bn_t h; bn_init(&h);
bn_from_bin(h_out, h_size, &h);
- int mod_len = bn_bit_length(&curve->n)
+ int mod_len = bn_bit_length(&curve->n);
if (h_size * 8 > mod_len) {
bn_rsh(&h, (h_size * 8) - mod_len, &h);
@@ -249,13 +255,13 @@ static uint8_t cmd_ecdsa_sign(uint8_t *data, uint16_t len) {
bn_mod(&r, &curve->n, &r);
bn_t s; bn_init(&s);
- bn_copy(&privkey, s);
- bn_mul_mod(&s, &r, &curve->n, &s);
- bn_add_mod(&s, &h, &curve->n, &s);
- bn_div_mod(&s, &k, &curve->n, &s);
+ bn_copy(&privkey, &s);
+ bn_mod_mul(&s, &r, &curve->n, &s);
+ bn_mod_add(&s, &h, &curve->n, &s);
+ bn_mod_div(&s, &k, &curve->n, &s);
size_t result_len = 0;
- uint8_t *result = as1n_der_encode(&r, &s, &result_len);
+ uint8_t *result = asn1_der_encode(&r, &s, &result_len);
simpleserial_put('s', result_len, result);
free(result);
@@ -269,23 +275,23 @@ 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
- fat_t data = fat_empty;
- parse_data(data, len, "", parse_ecdsa_msg, (void *) &data);
+ fat_t msg = fat_empty;
+ parse_data(data, len, "", parse_ecdsa_msg, (void *) &msg);
fat_t sig = fat_empty;
parse_data(data, len, "", parse_ecdsa_sig, (void *) &sig);
- size_t h_size = hash_size(data.len);
+ size_t h_size = hash_size(msg.len);
void *h_ctx = hash_new_ctx();
hash_init(h_ctx);
uint8_t h_out[h_size];
- hash_final(h_ctx, data.len, data.value, h_out);
+ hash_final(h_ctx, msg.len, msg.value, h_out);
hash_free_ctx(h_ctx);
- free(data.value);
+ free(msg.value);
bn_t h; bn_init(&h);
bn_from_bin(h_out, h_size, &h);
- int mod_len = bn_bit_length(&curve->n)
+ int mod_len = bn_bit_length(&curve->n);
if (h_size * 8 > mod_len) {
bn_rsh(&h, (h_size * 8) - mod_len, &h);
@@ -294,7 +300,7 @@ static uint8_t cmd_ecdsa_verify(uint8_t *data, uint16_t len) {
bn_t r; bn_init(&r);
bn_t s; bn_init(&s);
if (!asn1_der_decode(sig.value, sig.len, &r, &s)) {
- simpleserial_put('v', 1, "\0");
+ simpleserial_put('v', 1, (uint8_t *) "\0");
bn_clear(&r);
bn_clear(&s);
bn_clear(&h);
@@ -304,9 +310,9 @@ static uint8_t cmd_ecdsa_verify(uint8_t *data, uint16_t len) {
bn_t orig_r; bn_init(&orig_r);
bn_copy(&r, &orig_r);
- bn_inv_mod(&s, &curve->n, &s);
- bn_mul_mod(&r, &s, &curve->n, &r); //r = u2
- bn_mul_mod(&h, &s, &curve->n, &h); //h = u1
+ bn_mod_inv(&s, &curve->n, &s);
+ bn_mod_mul(&r, &s, &curve->n, &r); //r = u2
+ bn_mod_mul(&h, &s, &curve->n, &h); //h = u1
point_t *p1 = point_new();
point_t *p2 = point_new();
diff --git a/pyecsca/codegen/templates/coords.c b/pyecsca/codegen/templates/point.c
index 016dc36..973fc42 100644
--- a/pyecsca/codegen/templates/coords.c
+++ b/pyecsca/codegen/templates/point.c
@@ -1,4 +1,5 @@
-#include "coords.h"
+#include "point.h"
+#include <stdlib.h>
point_t *point_new(void) {
point_t *result = malloc(sizeof(point_t));
@@ -28,7 +29,7 @@ void point_free(point_t *point) {
free(point);
}
-int point_to_affine(point_t *point, curve_t *curve, bn_t *out_x, bn_t *out_y) {
+void point_to_affine(point_t *point, curve_t *curve, bn_t *out_x, bn_t *out_y) {
{%- include "ops.c" %}
{%- if "x" in allocations %}
if (out_x) {
@@ -45,7 +46,7 @@ int point_to_affine(point_t *point, curve_t *curve, bn_t *out_x, bn_t *out_y) {
{%- endfor %}
}
-int point_from_affine(bn_t *x, bn_t *y, curve_t *curve, point_t *out) {
+void point_from_affine(bn_t *x, bn_t *y, curve_t *curve, point_t *out) {
{# XXX: This just works for the stuff currently in EFD. #}
{%- for variable in variables %}
{%- if variable in ("X", "Y") %}