aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen
diff options
context:
space:
mode:
authorJ08nY2020-02-27 13:38:47 +0100
committerJ08nY2020-02-27 13:38:47 +0100
commit4eafe2d49fc7406861609c5af12b850741bbe5a0 (patch)
tree49355d5cce44be8bd100fa8260b3b488f15be4a5 /pyecsca/codegen
parenta071822d9966ac150e7ed3d4efffbf3feaa500f5 (diff)
downloadpyecsca-codegen-4eafe2d49fc7406861609c5af12b850741bbe5a0.tar.gz
pyecsca-codegen-4eafe2d49fc7406861609c5af12b850741bbe5a0.tar.zst
pyecsca-codegen-4eafe2d49fc7406861609c5af12b850741bbe5a0.zip
Proper LED thingies.
Diffstat (limited to 'pyecsca/codegen')
-rw-r--r--pyecsca/codegen/builder.py36
-rw-r--r--pyecsca/codegen/client.py20
-rw-r--r--pyecsca/codegen/hal/hal.h9
-rw-r--r--pyecsca/codegen/hal/host/host_hal.h3
-rw-r--r--pyecsca/codegen/hal/stm32f0/stm32f0_hal.c40
-rw-r--r--pyecsca/codegen/hal/stm32f0/stm32f0_hal.h2
-rw-r--r--pyecsca/codegen/hal/stm32f3/stm32f3_hal.c38
-rw-r--r--pyecsca/codegen/hal/stm32f3/stm32f3_hal.h11
-rw-r--r--pyecsca/codegen/hal/xmega/xmega_hal.c4
-rw-r--r--pyecsca/codegen/hal/xmega/xmega_hal.h2
-rw-r--r--pyecsca/codegen/simpleserial/simpleserial.c3
-rw-r--r--pyecsca/codegen/templates/main.c2
12 files changed, 84 insertions, 86 deletions
diff --git a/pyecsca/codegen/builder.py b/pyecsca/codegen/builder.py
index 1919c09..54d4475 100644
--- a/pyecsca/codegen/builder.py
+++ b/pyecsca/codegen/builder.py
@@ -78,7 +78,7 @@ def get_multiplier(ctx: click.Context, param, value: Optional[str]) -> Optional[
@public
def main():
"""
- A tool for building, querying and flashing ECC implementations on devices.
+ A tool for building ECC implementations on devices.
"""
pass
@@ -228,39 +228,5 @@ def list_impl(model: Optional[CurveModel], coords: Optional[CoordinateModel],
"Scalar multplier:\n\t" + ", ".join(map(lambda m: m["name"][-1], MULTIPLIERS)),
subsequent_indent="\t"))
-
-@main.command("flash")
-@click.option("--platform", envvar="PLATFORM", required=True,
- type=click.Choice(Platform.names()),
- callback=wrap_enum(Platform),
- help="The platform to flash.")
-@click.argument("dir")
-@public
-def flash_impl(platform, dir): # pragma: no cover
- """This command flashes a chip through the ChipWhisperer framework with the built implementation.
-
- \b
- DIR: The directory containing the built implementation (output directory of the build command).
- """
- try:
- import chipwhisperer as cw
- except ImportError:
- click.secho("ChipWhisperer not installed, flashing requires it.", fg="red", err=True)
- raise click.Abort
- if platform in (Platform.STM32F0, Platform.STM32F3):
- prog = cw.programmers.STM32FProgrammer
- elif platform == Platform.XMEGA:
- prog = cw.programmers.XMEGAProgrammer
- else:
- click.secho(
- "Flashing the HOST is not required, just run the ELF and communicate with it via the standard IO.",
- fg="red", err=True)
- raise click.Abort
- fw_path = path.join(dir, "pyecsca-codegen-{}.hex".format(platform))
- scope = cw.scope()
- scope.default_setup()
- cw.program_target(scope, prog, fw_path)
-
-
if __name__ == "__main__":
main(obj={})
diff --git a/pyecsca/codegen/client.py b/pyecsca/codegen/client.py
index e166704..c9f7b67 100644
--- a/pyecsca/codegen/client.py
+++ b/pyecsca/codegen/client.py
@@ -203,9 +203,13 @@ class ImplTarget(SimpleSerialTarget):
model, coords = unhexlify(resp.data).decode().split(",")
return model, coords
+ def disconnect(self):
+ self.write(b"x\n")
+ super().disconnect()
+
@public
-class DeviceTarget(ImplTarget, ChipWhispererTarget):
+class DeviceTarget(ImplTarget, ChipWhispererTarget): # pragma: no cover
def __init__(self, model: CurveModel, coords: CoordinateModel, platform: Platform, **kwargs):
scope = cw.scope()
@@ -291,15 +295,15 @@ def get_pubkey(ctx: click.Context, param, value: Optional[str]) -> Point:
return None
ctx.ensure_object(dict)
curve: DomainParameters = ctx.obj["params"]
- if re.match("04([0-9a-fA-F]{2})+", value):
+ if re.match("^04([0-9a-fA-F]{2})+$", value):
value = value[2:]
plen = len(value) // 2
x = int(value[:plen], 16)
y = int(value[plen:], 16)
- elif re.match("[0-9]+,[0-9]+", value):
- x, y = value.split(",")
- x = int(x)
- y = int(y)
+ elif re.match("^[0-9]+,[0-9]+$", value):
+ xs, ys = value.split(",")
+ x = int(xs)
+ y = int(ys)
else:
raise click.BadParameter("Couldn't parse pubkey: {}.".format(value))
x = Mod(x, curve.curve.prime)
@@ -334,6 +338,7 @@ def ecdh(ctx: click.Context, timeout, curve, pubkey):
@public
def ecdsa_sign(ctx: click.Context, timeout, curve):
ctx.ensure_object(dict)
+ # TODO
@main.command("ecdsa-verify")
@@ -341,8 +346,9 @@ def ecdsa_sign(ctx: click.Context, timeout, curve):
@click.argument("curve", required=True, callback=get_curve)
@click.pass_context
@public
-def ecdsa_sign(ctx: click.Context, timeout, curve):
+def ecdsa_verify(ctx: click.Context, timeout, curve):
ctx.ensure_object(dict)
+ # TODO
if __name__ == "__main__":
diff --git a/pyecsca/codegen/hal/hal.h b/pyecsca/codegen/hal/hal.h
index b14dacf..fc7fc37 100644
--- a/pyecsca/codegen/hal/hal.h
+++ b/pyecsca/codegen/hal/hal.h
@@ -41,13 +41,4 @@ void platform_init(void);
#error "Unsupported HAL Type"
#endif
-#ifndef led_error
-#define led_error(a)
-#endif
-
-#ifndef led_ok
-#define led_ok(a)
-#endif
-
-
#endif //HAL_H_
diff --git a/pyecsca/codegen/hal/host/host_hal.h b/pyecsca/codegen/hal/host/host_hal.h
index 26d8f4d..ba2fbbe 100644
--- a/pyecsca/codegen/hal/host/host_hal.h
+++ b/pyecsca/codegen/hal/host/host_hal.h
@@ -12,4 +12,7 @@
#define getch input_ch_0
#define flush flush_ch_0
+#define led_error(X)
+#define led_ok(X)
+
#endif //HOST_HAL_H_
diff --git a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c
index ee70dd7..613a20a 100644
--- a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c
+++ b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c
@@ -27,8 +27,7 @@ void platform_init(void)
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- uint32_t flash_latency = 0;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
+ HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
#else
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI;
@@ -44,6 +43,17 @@ void platform_init(void)
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
#endif
+
+ __HAL_RCC_GPIOC_CLK_ENABLE();
+ GPIO_InitTypeDef GpioInit;
+ GpioInit.Pin = GPIO_PIN_13 | GPIO_PIN_14;
+ GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
+ GpioInit.Pull = GPIO_NOPULL;
+ GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
+ HAL_GPIO_Init(GPIOC, &GpioInit);
+
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET);
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, RESET);
}
void init_uart(void)
@@ -69,6 +79,8 @@ void init_uart(void)
HAL_UART_Init(&UartHandle);
}
+static bool trig;
+
void trigger_setup(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
@@ -81,15 +93,23 @@ void trigger_setup(void)
HAL_GPIO_Init(GPIOA, &GpioInit);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
+ trig = false;
}
void trigger_high(void)
{
+ trig = true;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
}
+bool trigger_status(void)
+{
+ return trig;
+}
+
void trigger_low(void)
{
+ trig = false;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
}
@@ -106,3 +126,19 @@ void putch(char c)
HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
}
+void led_error(unsigned int x)
+{
+ if (!x)
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET);
+ else
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, SET);
+}
+
+void led_ok(unsigned int x)
+{
+ if (!x)
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, RESET);
+ else
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, SET);
+}
+
diff --git a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h
index 1cbdefc..d432304 100644
--- a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h
+++ b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h
@@ -1,5 +1,6 @@
#ifndef STM32F0_HAL_H
#define STM32F0_HAL_H
+#include <stdbool.h>
void init_uart(void);
void putch(char c);
@@ -8,6 +9,7 @@ char getch(void);
void trigger_setup(void);
void trigger_low(void);
+bool trigger_status(void);
void trigger_high(void);
void led_error(unsigned int status);
diff --git a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c
index b5480f3..c07fe87 100644
--- a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c
+++ b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c
@@ -47,10 +47,6 @@ void platform_init(void)
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
#endif
-
-
-
-#if (PLATFORM==CWLITEARM)
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GpioInit;
GpioInit.Pin = GPIO_PIN_13 | GPIO_PIN_14;
@@ -59,9 +55,8 @@ void platform_init(void)
GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOC, &GpioInit);
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, SET);
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, SET);
-#endif
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET);
+ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, RESET);
}
void init_uart(void)
@@ -86,6 +81,8 @@ void init_uart(void)
HAL_UART_Init(&UartHandle);
}
+static bool trig;
+
void trigger_setup(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
@@ -98,15 +95,23 @@ void trigger_setup(void)
HAL_GPIO_Init(GPIOA, &GpioInit);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
+ trig = false;
}
void trigger_high(void)
{
+ trig = true;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
}
+bool trigger_status(void)
+{
+ return trig;
+}
+
void trigger_low(void)
{
+ trig = false;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
}
@@ -122,28 +127,19 @@ void putch(char c)
uint8_t d = c;
HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
}
-#if (PLATFORM==CWLITEARM || PLATFORM==CW308_STM32F3)
-void change_err_led(int x)
+
+void led_error(unsigned int x)
{
- if (x)
+ if (!x)
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET);
else
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, SET);
}
-void change_ok_led(int x)
+void led_ok(unsigned int x)
{
- if (x)
+ if (!x)
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, RESET);
else
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, SET);
}
-#else
-void change_err_led(int x)
-{
-}
-
-void change_ok_led(int x)
-{
-}
-#endif //PLATFORM==CWLITEARM
diff --git a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h
index b5fec45..156a012 100644
--- a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h
+++ b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h
@@ -1,6 +1,7 @@
#ifndef STM32F3_HAL_H
#define STM32F3_HAL_H
#include <stdint.h>
+#include <stdbool.h>
//You probably don't need this from rest of code
//#include "stm32f4_hal_lowlevel.h"
@@ -28,14 +29,10 @@ char getch(void);
void trigger_setup(void);
void trigger_low(void);
+bool trigger_status(void);
void trigger_high(void);
-
-#if (PLATFORM==CWLITEARM)
-void change_err_led(int x);
-void change_ok_led(int x);
-#define led_error(X) (change_err_led(X))
-#define led_ok(X) (change_ok_led(X))
-#endif
+void led_error(unsigned int x);
+void led_ok(unsigned int x);
#endif // STM32F3_HAL_H
diff --git a/pyecsca/codegen/hal/xmega/xmega_hal.c b/pyecsca/codegen/hal/xmega/xmega_hal.c
index c9fc12a..52efe88 100644
--- a/pyecsca/codegen/hal/xmega/xmega_hal.c
+++ b/pyecsca/codegen/hal/xmega/xmega_hal.c
@@ -34,9 +34,7 @@ void platform_init(void)
//Turn off other sources besides external
OSC.CTRL = OSC_XOSCEN_bm;
-
- #if PLATFORM == CW303
+
PORTA.DIRSET = PIN5_bm | PIN6_bm;
PORTA.OUTSET = PIN5_bm | PIN6_bm;
- #endif
} \ No newline at end of file
diff --git a/pyecsca/codegen/hal/xmega/xmega_hal.h b/pyecsca/codegen/hal/xmega/xmega_hal.h
index 1647824..cb53bb7 100644
--- a/pyecsca/codegen/hal/xmega/xmega_hal.h
+++ b/pyecsca/codegen/hal/xmega/xmega_hal.h
@@ -32,10 +32,8 @@
#define getch input_ch_0
#define flush()
-#if PLATFORM == CW303
#define led_error(a) if (a) {PORTA.OUTCLR = PIN6_bm;} else {PORTA.OUTSET = PIN6_bm;}
#define led_ok(a) if (a) {PORTA.OUTCLR = PIN5_bm;} else {PORTA.OUTSET = PIN5_bm;}
-#endif
void HW_AES128_Init(void);
void HW_AES128_LoadKey(uint8_t * key);
diff --git a/pyecsca/codegen/simpleserial/simpleserial.c b/pyecsca/codegen/simpleserial/simpleserial.c
index 9a8be4c..4e0f0b3 100644
--- a/pyecsca/codegen/simpleserial/simpleserial.c
+++ b/pyecsca/codegen/simpleserial/simpleserial.c
@@ -93,6 +93,9 @@ int simpleserial_get(void)
if (ci == -1)
return 0;
char c = (char) ci;
+ if (c == 'x') {
+ return 0;
+ }
int cmd;
for(cmd = 0; cmd < num_commands; cmd++)
diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c
index 467a417..d3616aa 100644
--- a/pyecsca/codegen/templates/main.c
+++ b/pyecsca/codegen/templates/main.c
@@ -456,7 +456,9 @@ int main(void) {
{%- endif %}
simpleserial_addcmd('d', MAX_SS_LEN, cmd_debug);
+ led_ok(1);
while(simpleserial_get());
+ led_ok(0);
bn_clear(&privkey);
curve_free(curve);