Commit 062ba968 by Sebastian Renner

Working blackpill middleware

parent ec01da59
build/
all-lwc-submission-files/
*.log
......@@ -131,8 +131,8 @@ def main(argv):
# For testing, we only do the first 5
files = files[:5]
# For testing, we only do the first 1
files = files[:1]
# Clear the build directory as it is a leftover from the previous execution
if os.path.isdir('build'):
......
This source diff could not be displayed because it is too large. You can view the blob instead.
#define CRYPTO_KEYBYTES 16
#define CRYPTO_NSECBYTES 0
#define CRYPTO_NPUBBYTES 16
#define CRYPTO_ABYTES 16
#define CRYPTO_NOOVERLAP 1
......@@ -69,39 +69,45 @@ void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void read_variable_serial(unsigned char action);
static void write_variable_serial(unsigned char target[], unsigned long long len);
static void write_variable_serial(unsigned char target[], uint32_t len);
static void read_serial(void *dst, unsigned int len) {
unsigned char *buf = dst;
dbg_printf("Reading %d serial bytes\n", len);
for (int i = 0; i < len; i++) {
while (LL_USART_IsActiveFlag_RXNE(USART1));
while (!LL_USART_IsActiveFlag_RXNE(USART1));
buf[i] = LL_USART_ReceiveData8(USART1);
}
dbg_printf("done.\n", len);
}
static void write_serial(const void *src, unsigned int len) {
const unsigned char *buf = src;
for (int i = 0; i < len; i++) {
dbg_printf("Write to serial: %02x\n", buf[i]);
while (!(LL_USART_IsActiveFlag_TXE(USART1)));
LL_USART_TransmitData8(USART1, buf[i]);
dbg_printf("Done writing!\n");
}
}
static void read_variable_serial(unsigned char action) {
uint32_t len;
dbg_printf("Let's read our variable: %c\n", action);
read_serial(&len, sizeof(len));
dbg_printf("with length %lu \n", len);
switch(action) {
case 'k':
while (len != CRYPTO_KEYBYTES);
read_serial(k, len);
break;
case 'p':
while (len != CRYPTO_NPUBBYTES);
read_serial(k, len);
if (len != CRYPTO_NPUBBYTES) { dbg_printf("Assert failed %d != %d\n", len, CRYPTO_NPUBBYTES); for(;;); }
read_serial(npub, len);
break;
case 's':
while (len != CRYPTO_NSECBYTES);
read_serial(k, len);
read_serial(nsec, len);
break;
case 'a':
while (len > MAX_LEN);
......@@ -123,7 +129,7 @@ static void read_variable_serial(unsigned char action) {
}
}
static void write_variable_serial(unsigned char target[], unsigned long long len) {
static void write_variable_serial(unsigned char target[], uint32_t len) {
write_serial(&len, sizeof(len));
write_serial(target, len);
}
......@@ -175,8 +181,8 @@ int main(void)
int res;
while (1) {
read_serial(&rcv, sizeof(rcv));
dbg_printf("Received char: %c\n", rcv);
read_serial(&rcv, 1);
dbg_printf("Received action: %c\n", rcv);
switch (rcv) {
case 'c':
case 'm':
......@@ -199,11 +205,15 @@ int main(void)
break;
case 'e':
dbg_printf("m: "); for (int i = 0; i < mlen; i++) dbg_printf("%02x", m[i]); dbg_printf("\n");
dbg_printf("a: "); for (int i = 0; i < adlen; i++) dbg_printf("%02x", ad[i]); dbg_printf("\n");
dbg_printf("p: "); for (int i = 0; i < nplen; i++) dbg_printf("%02x", npub[i]); dbg_printf("\n");
dbg_printf("k: "); for (int i = 0; i < klen; i++) dbg_printf("%02x", k[i]); dbg_printf("\n");
res = crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k);
break;
default:
LL_mDelay(500);
continue;
}
/* USER CODE END WHILE */
......
#!/bin/bash
mv $HDR_FILES Inc/
......@@ -2,6 +2,7 @@
import os
import sys
import time
import struct
import serial
from subprocess import Popen, PIPE, run
......@@ -17,9 +18,11 @@ def read(channel, l):
if channel == 'ser':
data = ser.read(l)
elif channel == 'std':
data = sys.stdin.read(l)
data = sys.stdin.buffer.read(l)
else:
raise Exception("read() complains: no sai channelino")
# if channel == "std" and len(data) == 0:
# sys.exit(0)
if len(data) != l:
raise Exception("could not read %d bytes of data (got %d)" % (l, len(data)))
return data
......@@ -29,7 +32,7 @@ def write(channel, data):
l = ser.write(data)
f.write("Data sent to serial: " + str(data) + "\n")
elif channel == 'std':
l = sys.stdout.write(data)
l = sys.stdout.buffer.write(data)
else:
raise Exception("write() complains: no sai channelino")
if len(data) != l:
......@@ -37,34 +40,38 @@ def write(channel, data):
def obtain(channel):
l = read(channel, 4)
l = str.encode(l)
f.write("Trying to read " + str(l) + "bytes from " + channel + "\n")
(l, ) = struct.unpack("<I", l)
f.write("Trying to read %d bytes from %s\n" % (l, channel))
return read(channel, l)
def submit(channel, action, data):
if channel == "std":
h = struct.pack("<I", len(data))
else:
h = struct.pack("<BI", ord(action), len(data))
write(channel, h)
write(channel, str.encode(data))
write(channel, data)
time.sleep(0.1)
def main():
#flash()
print("Hello, World!")
while(1):
action = read('std', 1)
f.write("Read following action from stdin: " + action + "\n")
if action == 'c' or action == 'm' or action == 'a' or action == 'k' or action == 's' or action == 'p':
f.write("Read following action from stdin: %s\n" % action)
if action == b'c' or action == b'm' or action == b'a' or action == b'k' or action == b's' or action == b'p':
data = obtain('std')
submit('ser', action, data)
elif action == 'e' or action == 'd':
write('ser', str.encode(action))
elif action == b'e' or action == b'd':
write('ser', action)
elif action == 'C' or action == 'M' or action == 'A' or action == 'K' or action == 'S' or action == 'P':
write('ser', str.encode(action))
elif action == b'C' or action == b'M' or action == b'A' or action == b'K' or action == b'S' or action == b'P':
write('ser', action)
data = obtain('ser')
f.write("Obtained following data from serial: " + data + "\n")
f.write("Obtained following data from serial: %s\n" % data)
submit('std', action, data)
f.write("Submitted following data to stdout: " + data + "\n")
f.write("Submitted following data to stdout: %s\n" % data)
else:
raise Exception("no capiche aczione %s" % (action))
......
......@@ -37,8 +37,8 @@ def main(argv):
(l, ) = struct.unpack("<I", l)
return read(l)
#if read(14) != b"Hello, World!\n":
# raise Exception("Unexpected output")
if read(14) != b"Hello, World!\n":
raise Exception("Unexpected output")
m = b""
ad = b""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment