aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen/templates
diff options
context:
space:
mode:
authorJ08nY2020-02-19 18:29:53 +0100
committerJ08nY2020-02-19 18:29:53 +0100
commit5da1d167c203395103d220c450e29fece08f4198 (patch)
treefdc8fb7f0fbf2d2aaab46c2cb8a90a8176058292 /pyecsca/codegen/templates
parentf04c640c05e9ffe894f67194832623e28a8000f5 (diff)
downloadpyecsca-codegen-5da1d167c203395103d220c450e29fece08f4198.tar.gz
pyecsca-codegen-5da1d167c203395103d220c450e29fece08f4198.tar.zst
pyecsca-codegen-5da1d167c203395103d220c450e29fece08f4198.zip
Flesh out client, add implementation tests.
Diffstat (limited to 'pyecsca/codegen/templates')
-rw-r--r--pyecsca/codegen/templates/main.c12
-rw-r--r--pyecsca/codegen/templates/mult.c14
-rw-r--r--pyecsca/codegen/templates/mult_ltr.c3
-rw-r--r--pyecsca/codegen/templates/point.c34
4 files changed, 45 insertions, 18 deletions
diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c
index c2e250f..ab69858 100644
--- a/pyecsca/codegen/templates/main.c
+++ b/pyecsca/codegen/templates/main.c
@@ -56,7 +56,7 @@ static uint8_t cmd_init_prng(uint8_t *data, uint16_t len) {
return 0;
}
-static void parse_set_curve(const char *path, const uint8_t *data, size_t len, void *arg) {
+static void parse_set_params(const char *path, const uint8_t *data, size_t len, void *arg) {
{%- for param in curve_parameters + ["p", "n", "h"] %}
if (strcmp(path, "{{ param }}") == 0) {
bn_from_bin(data, len, &curve->{{ param }});
@@ -90,10 +90,10 @@ static void parse_set_curve(const char *path, const uint8_t *data, size_t len, v
{%- endfor %}
}
-static uint8_t cmd_set_curve(uint8_t *data, uint16_t len) {
+static uint8_t cmd_set_params(uint8_t *data, uint16_t len) {
// need p, [params], n, h, g[xy], i[variables]
fat_t affine[2] = {fat_empty, fat_empty};
- parse_data(data, len, "", parse_set_curve, (void *) affine);
+ parse_data(data, len, "", parse_set_params, (void *) affine);
bn_t x; bn_init(&x);
bn_t y; bn_init(&y);
bn_from_bin(affine[0].value, affine[0].len, &x);
@@ -416,10 +416,10 @@ static uint8_t cmd_ecdsa_verify(uint8_t *data, uint16_t len) {
}
static uint8_t cmd_debug(uint8_t *data, uint16_t len) {
- const char *debug_string = "{{ ','.join((model.shortname, coords.name))}}";
+ char *debug_string = "{{ ','.join((model.shortname, coords.name))}}";
size_t debug_len = strlen(debug_string);
- simpleserial_put('d', debug_len, debug_string);
+ simpleserial_put('d', debug_len, (uint8_t *) debug_string);
return 0;
}
@@ -435,7 +435,7 @@ int main(void) {
simpleserial_init();
simpleserial_addcmd('i', MAX_SS_LEN, cmd_init_prng);
- simpleserial_addcmd('c', MAX_SS_LEN, cmd_set_curve);
+ simpleserial_addcmd('c', MAX_SS_LEN, cmd_set_params);
{%- if keygen %}
simpleserial_addcmd('g', 0, cmd_generate);
{%- endif %}
diff --git a/pyecsca/codegen/templates/mult.c b/pyecsca/codegen/templates/mult.c
index 61ec825..6851b6b 100644
--- a/pyecsca/codegen/templates/mult.c
+++ b/pyecsca/codegen/templates/mult.c
@@ -2,17 +2,17 @@
#include "mult.h"
{%- if isinstance(scalarmult, LTRMultiplier) -%}
-{%- include "mult_ltr.c" %}
+{% include "mult_ltr.c" %}
{%- elif isinstance(scalarmult, RTLMultiplier) -%}
-{%- include "mult_rtl.c" %}
+{% include "mult_rtl.c" %}
{%- elif isinstance(scalarmult, CoronMultiplier) -%}
-{%- include "mult_coron.c" %}
+{% include "mult_coron.c" %}
{%- elif isinstance(scalarmult, LadderMultiplier) -%}
-{%- include "mult_ldr.c" %}
+{% include "mult_ldr.c" %}
{%- elif isinstance(scalarmult, SimpleLadderMultiplier) -%}
-{%- include "mult_simple_ldr.c" %}
+{% include "mult_simple_ldr.c" %}
{%- elif isinstance(scalarmult, DifferentialLadderMultiplier) -%}
-{%- include "mult_diff_ldr.c" %}
+{% include "mult_diff_ldr.c" %}
{%- elif isinstance(scalarmult, BinaryNAFMultiplier) -%}
-{%- include "mult_bnaf.c" %}
+{% include "mult_bnaf.c" %}
{%- endif -%}
diff --git a/pyecsca/codegen/templates/mult_ltr.c b/pyecsca/codegen/templates/mult_ltr.c
index 9b7d87e..dbd4704 100644
--- a/pyecsca/codegen/templates/mult_ltr.c
+++ b/pyecsca/codegen/templates/mult_ltr.c
@@ -7,8 +7,7 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *r = point_copy(curve->neutral);
int nbits = bn_bit_length(&curve->n) - 1;
{%- else %}
- point_t *q = point_new();
- point_dbl(point, curve, q);
+ point_t *q = point_copy(point);
point_t *r = point_copy(point);
int nbits = bn_bit_length(scalar) - 2;
{%- endif %}
diff --git a/pyecsca/codegen/templates/point.c b/pyecsca/codegen/templates/point.c
index c12c50a..445bdc4 100644
--- a/pyecsca/codegen/templates/point.c
+++ b/pyecsca/codegen/templates/point.c
@@ -20,6 +20,7 @@ void point_set(const point_t *from, point_t *out) {
{%- for variable in variables %}
bn_copy(&from->{{ variable }}, &out->{{ variable }});
{%- endfor %}
+ out->infinity = from->infinity;
}
void point_free(point_t *point) {
@@ -30,7 +31,7 @@ void point_free(point_t *point) {
}
bool point_equals(const point_t *one, const point_t *other) {
- if (one->infinity && !other->infinity || other->infinity && !one->infinity) {
+ if ((one->infinity && !other->infinity) || (other->infinity && !one->infinity)) {
return false;
}
if (one->infinity && other->infinity) {
@@ -45,7 +46,34 @@ bool point_equals(const point_t *one, const point_t *other) {
return true;
}
-void point_to_affine(point_t *point, curve_t *curve, bn_t *out_x, bn_t *out_y) {
+bool point_equals_affine(const point_t *one, const point_t *other, const curve_t *curve) {
+ if ((one->infinity && !other->infinity) || (other->infinity && !one->infinity)) {
+ return false;
+ }
+ if (one->infinity && other->infinity) {
+ return true;
+ }
+ bool result = true;
+ bn_t ax; bn_init(&ax);
+ bn_t ay; bn_init(&ay);
+ bn_t bx; bn_init(&bx);
+ bn_t by; bn_init(&by);
+ point_to_affine(one, curve, &ax, &ay);
+ point_to_affine(other, curve, &bx, &by);
+ if (!bn_eq(&ax, &bx)) {
+ result = false;
+ }
+ if (!bn_eq(&ay, &by)) {
+ result = false;
+ }
+ bn_clear(&ax);
+ bn_clear(&ay);
+ bn_clear(&bx);
+ bn_clear(&by);
+ return result;
+}
+
+void point_to_affine(const point_t *point, const curve_t *curve, bn_t *out_x, bn_t *out_y) {
{%- include "ops.c" %}
{%- if "x" in allocations %}
if (out_x) {
@@ -62,7 +90,7 @@ void point_to_affine(point_t *point, curve_t *curve, bn_t *out_x, bn_t *out_y) {
{%- endfor %}
}
-void 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, const 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") %}