Commit 4b159ca1 authored by Stefan Hackenberg's avatar Stefan Hackenberg
Browse files

Add base and lecture 1.1

parents
Pipeline #992 failed with stages
in 32 seconds
# Secure implementations on microcontrollers (WS 20/21)
In this repository the materials for the course "Sichere Implementierungen auf Mikrocontrollern" teached at Hochschule Augsburg in WS 2020/2021 can be found.
## How to install
### Requirements
* [ChipWhisperer v5 release](https://github.com/newaetech/chipwhisperer/releases). Either Virtual Machine or source code.
* ChipWhisperer Lite XMEGA (CW1173)
### 1. Install using ChipWhisperer Virtual Machine
1. Log in to Jupyter.
2. Create a new Notebook in the root folder.
3. Type in the first cell and execute:
```jupyter
! git clone https://github.com/hackenbergstefan/securec_ws2021.git
```
4. In the second cell:
```jupyter
pip install -e securec_ws2021
```
5. That's it. Now you can use the lecture notebooks.
#### Update
1. Log in to Jupyter.
2. Create a new Notebook in the root folder.
3. Type in the first cell and execute:
```jupyter
! cd securec_ws2021 && git pull
```
### 2. Install using ChipWhisperer source
1. Clone this repository inside (or in parallel) to the chipwhisperer repository.
2. Execute `pip install -e .` inside the clone of this repository.
5. That's it. Now you can use the lecture notebooks.
#### Update
1. Goto the clone and execute `git pull`.
This diff is collapsed.
This diff is collapsed.
/* -*- tab-width: 4; c-basic-offset: 4; c-file-style: "linux"; indent-tabs-mode: nil; -*-
*/
#include "hal.h"
#include "simpleserial.h"
#include "bigint.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
uint32_t binary_exp (uint32_t message, uint32_t exponent, uint32_t modulus)
{
trigger_high();
uint32_t res = 1;
while (exponent)
{
if (exponent & 1)
{
res = (res * message);
}
message = (message * message) % modulus;
exponent >>= 1;
}
trigger_low();
return res;
}
void bigint_exp(bigint_t *result, const bigint_t *message, const bigint_t *exponent, const bigint_t *modulus)
{
trigger_high();
bigint_expmod_u(result, message, exponent, modulus);
trigger_low();
}
uint8_t rsa(uint8_t *input)
{
bigint_t message = {
.length_B = 4,
.info = 0,
.wordv = alloca(4),
};
memcpy(message.wordv, input, message.length_B);
bigint_t exponent = {
.length_B = 4,
.info = 0,
.wordv = alloca(4),
};
memcpy(exponent.wordv, input + 4, exponent.length_B);
bigint_t modulus = {
.length_B = 4,
.info = 0,
.wordv = alloca(4),
};
memcpy(modulus.wordv, input + 8, modulus.length_B);
bigint_t result = {
.length_B = 4,
.info = 0,
.wordv = alloca(4),
};
memset(result.wordv, 0, 4);
bigint_exp(&result, &message, &exponent, &modulus);
uint8_t *serial_result = alloca(2 + 4);
*(uint16_t *)serial_result = result.length_B;
memcpy(serial_result + 2, result.wordv, 4);
simpleserial_put('r', 6, serial_result);
/* uint32_t message = ((uint32_t *)input)[0], */
/* exponent = ((uint32_t *)input)[1], */
/* modulus = ((uint32_t *)input)[2]; */
/* uint32_t result = binary_exp(message, exponent, modulus); */
return 0;
}
uint8_t rsa2(uint8_t *input)
{
uint8_t message= input[0],
exponent = input[1],
modulus = input[2];
trigger_high();
uint16_t tmp;
uint8_t res = 1;
while (exponent)
{
if (exponent & 1)
{
tmp = res * message;
res = tmp % modulus;
}
tmp = message * message;
message = tmp % modulus;
exponent >>= 1;
}
trigger_low();
simpleserial_put('r', 1, &res);
return 0;
}
int main(void)
{
platform_init();
init_uart();
trigger_setup();
simpleserial_init();
/* simpleserial_addcmd('r', 3 * sizeof(uint32_t), rsa); */
simpleserial_addcmd('r', 3, rsa2);
while(1)
simpleserial_get();
}
/* -*- tab-width: 4; c-basic-offset: 4; c-file-style: "linux"; indent-tabs-mode: nil; -*-
*
* RSA exponentation demo with uint8 integers giving parameters:
* p = 13, q = 17, n = 221
* phi(n) = 12 * 16 = 192
* private_exponent = 5
* public_exponent = 77
*/
#include "hal.h"
#include "simpleserial.h"
#include <stdint.h>
const uint8_t modulus = 221;
const uint8_t private_exponent = 5;
/**
* RSA exponentation on uint8_t integers.
*/
uint8_t rsa_uint8_fixed(uint8_t *input)
{
uint8_t exponent = private_exponent;
uint8_t message = 0xA0;
trigger_high();
uint16_t tmp;
uint8_t result = 1;
while (exponent)
{
if (exponent & 1)
{
tmp = result * message;
result = tmp % modulus;
}
tmp = message * message;
message = tmp % modulus;
exponent >>= 1;
}
trigger_low();
simpleserial_put('r', sizeof(uint8_t), &result);
return 0;
}
int main(void)
{
platform_init();
init_uart();
trigger_setup();
simpleserial_init();
simpleserial_addcmd('r', 0, rsa_uint8_fixed);
while(1)
simpleserial_get();
}
import os
cwpath = None
firmwarepath = None
for cwpath in (os.path.join(os.path.dirname(__file__), p) for p in (
'../..',
'../../chipwhisperer',
'../../../chipwhisperer',
)):
firmwarepath = os.path.abspath(os.path.join(cwpath, 'hardware/victims/firmware'))
if os.path.exists(firmwarepath):
break
if not os.path.exists(firmwarepath):
raise ValueError('FIRMWAREPATH not found')
print_output = True
"""Print output instead of returning. Looks better in Notebook."""
debug = False
"""Print debug output."""
scope = None
"""ChipWhisperer scope."""
target = None
"""ChipWhisperer target."""
# Hey Emacs, this is a -*- makefile -*-
#----------------------------------------------------------------------------
#
# Makefile for ChipWhisperer SimpleSerial-AES Program
#
#----------------------------------------------------------------------------
# On command line:
#
# make all = Make software.
#
# make clean = Clean out built project files.
#
# make coff = Convert ELF to AVR COFF.
#
# make extcoff = Convert ELF to AVR Extended COFF.
#
# make program = Download the hex file to the device, using avrdude.
# Please customize the avrdude settings below first!
#
# make debug = Start either simulavr or avarice as specified for debugging,
# with avr-gdb or avr-insight as the front end for debugging.
#
# make filename.s = Just compile filename.c into the assembler code only.
#
# make filename.i = Create a preprocessed source file for use in submitting
# bug reports to the GCC project.
#
# To rebuild project do "make clean" then "make all".
#----------------------------------------------------------------------------
# Target file name (without extension).
# This is the name of the compiled .hex file.
ifeq ($(TARGET),)
$(error "TARGET is not defined.")
endif
# Path to ChipWhisperer firmware folder
ifeq ($(FIRMWAREPATH),)
$(error "FIRMWAREPATH is not defined.")
endif
# List C source files here.
# Header files (.h) are automatically pulled in.
SRC += $(TARGET).c
SS_VER = SS_VER_1_1
#Add simpleserial project to build
include $(FIRMWAREPATH)/simpleserial/Makefile.simpleserial
include $(FIRMWAREPATH)/Makefile.inc
import os
import subprocess
import time
import warnings
import chipwhisperer as cw
from .. import config
def exit():
if config.target is not None:
config.target.dis()
config.target = None
if config.scope is not None:
config.scope.dis()
config.scope = None
def init():
exit()
config.scope = cw.scope()
config.target = cw.target(config.scope)
return config.scope, config.target
def compile(path, cryptooptions=None):
cryptooptions = cryptooptions or ['CRYPTO_TARGET=NONE']
try:
proc = subprocess.run(
[
'make',
'-f',
f'{os.path.join(os.path.dirname(__file__), "Makefile")}',
'PLATFORM=CWLITEXMEGA',
f'FIRMWAREPATH={config.firmwarepath}',
f'TARGET={os.path.splitext(os.path.basename(path))[0]}',
] + cryptooptions,
cwd=os.path.dirname(path),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
if config.print_output and config.debug:
print('\x1b[32m✓\x1b[0m ' + proc.stdout.decode())
except subprocess.CalledProcessError as e:
if config.print_output:
print(f'\x1b[31m✗ "{" ".join(e.args[1])}" returned:\x1b[0m\n' + e.stderr.decode())
raise
def flash(path):
prog = cw.programmers.XMEGAProgrammer
programmed = cw.program_target(
scope=config.scope,
prog_type=prog,
fw_path=os.path.join(
os.path.dirname(path),
f'{os.path.splitext(os.path.basename(path))[0]}-CWLITEXMEGA.hex',
)
)
if config.print_output and config.debug:
print(programmed)
def compile_and_flash(path, cryptooptions=None):
compile(path, cryptooptions)
flash(path)
if config.print_output:
print('\x1b[32m✓\x1b[0m')
def reset_target():
scope = config.scope
target = config.target
scope.io.nrst = 'low'
time.sleep(0.05)
target.flush()
scope.io.nrst = 'high'
time.sleep(0.05)
def capture():
scope = config.scope
target = config.target
ret = scope.capture()
for i in range(101):
if target.is_done():
break
time.sleep(0.05)
if i == 100:
warnings.warn("Target did not finish operation")
return None
if ret:
warnings.warn("Timeout happened during capture")
return None
return scope.get_last_trace()
#!/usr/bin/env python
from setuptools import setup, find_packages
setup(
name='securec',
version='0.0.1',
description='Utils for securec Course',
author='Stefan Hackenberg',
author_email='mail@stefan-hackenberg.de',
license='MIT',
packages=['securec'],
python_requires='~=3.6',
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment