Commit 062ba968 by Sebastian Renner

Working blackpill middleware

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