From 23e6c2805515ecebb5fe19fda4ce4989f6f18dac Mon Sep 17 00:00:00 2001 From: Enrico Pozzobon Date: Mon, 15 Jul 2019 19:49:32 +0200 Subject: [PATCH] arduino uno measurements start --- compile_all.py | 14 +++++++++++--- measurements/.mkdir | 0 templates/uno/configure | 6 ++++-- templates/uno/src/main.ino | 8 ++++++++ templates/uno/test | 13 ++++++++++--- test.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 6 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 measurements/.mkdir diff --git a/compile_all.py b/compile_all.py index af09203..98360a2 100755 --- a/compile_all.py +++ b/compile_all.py @@ -16,6 +16,7 @@ def build(algo_dir, template_dir): d = os.path.join("build", r) if not os.path.isdir(d): build_dir = d + print("Building in %s" % build_dir) # copy all the files from the submitted algorithm into the build directory shutil.copytree(algo_dir, build_dir) @@ -65,6 +66,8 @@ def build(algo_dir, template_dir): assert p.returncode == 0 finally: + sys.stdout.flush() + sys.stderr.flush() os.chdir(wd) # if execution arrives here, the build was successful @@ -132,7 +135,7 @@ def main(argv): # For testing, we only do the first 1 - files = files[:1] + #files = files[:1] # Clear the build directory as it is a leftover from the previous execution if os.path.isdir('build'): @@ -148,8 +151,13 @@ def main(argv): for t, d in files: print() print(d) - b = build(d, template_dir) - test_script.write("./test.py %s %s\n" % (t, os.path.join(b, 'test'))) + try: + b = build(d, template_dir) + test_script.write("echo \"TESTING %s\"\n./test.py %s %s\n" % (d, t, os.path.join(b, 'test'))) + print("COMPILATION SUCCESS FOR %s" % d) + except Exception: + print("COMPILATION FAILED FOR %s" % d) + st = os.stat(test_script_path) os.chmod(test_script_path, st.st_mode | stat.S_IEXEC) diff --git a/measurements/.mkdir b/measurements/.mkdir new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/measurements/.mkdir diff --git a/templates/uno/configure b/templates/uno/configure index e2f7ac5..a02d3c1 100755 --- a/templates/uno/configure +++ b/templates/uno/configure @@ -1,3 +1,5 @@ #!/bin/bash -mv *.c src/ -mv *.h include/ +mv -n *.c *.C *.s *.S src/ +mv -n *.h *.H include/ +sed -i src/encrypt.c -e "s/\(\s\)init(/\1_init(/g" +exit 0 diff --git a/templates/uno/src/main.ino b/templates/uno/src/main.ino index 5d72782..c3545de 100644 --- a/templates/uno/src/main.ino +++ b/templates/uno/src/main.ino @@ -96,14 +96,22 @@ void loop() { case 'p': assert(l == CRYPTO_NPUBBYTES); memcpy(npub, var, l); break; case 's': assert(l == CRYPTO_NSECBYTES); memcpy(nsec, var, l); break; case 'e': + noInterrupts(); + asm("nop"); digitalWrite(LED_BUILTIN, LOW); res = crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k); digitalWrite(LED_BUILTIN, HIGH); + asm("nop"); + interrupts(); break; case 'd': + noInterrupts(); + asm("nop"); digitalWrite(LED_BUILTIN, LOW); res = crypto_aead_decrypt(m, &mlen, nsec, c, clen, ad, adlen, npub, k); digitalWrite(LED_BUILTIN, HIGH); + asm("nop"); + interrupts(); break; case'M': var = m; rl = mlen; break; case'C': var = c; rl = clen; break; diff --git a/templates/uno/test b/templates/uno/test index 81ff878..8934086 100755 --- a/templates/uno/test +++ b/templates/uno/test @@ -2,6 +2,7 @@ import os import sys +import time import struct import serial import subprocess @@ -12,9 +13,12 @@ def eprint(*args, **kargs): print(*args, file=sys.stderr, **kargs) -def flash(): +def flash(tty=None): pipe = subprocess.PIPE - p = subprocess.Popen(['platformio', 'run', '-e', 'uno', '--target', 'upload'], + cmd = ['platformio', 'run', '-e', 'uno', '--target', 'upload'] + if tty is not None: + cmd.extend(['--upload-port', tty]) + p = subprocess.Popen(cmd, stdout=sys.stderr, stdin=pipe) stdout, stderr = p.communicate("") @@ -105,9 +109,12 @@ def main(argv): script_dir = os.path.split(argv[0])[0] if len(script_dir) > 0: os.chdir(script_dir) - flash() dev = get_serial() + flash(dev) + eprint("Flashed") + time.sleep(1) + ser = serial.Serial(dev, baudrate=115200, timeout=5) uartp = UARTP(ser) diff --git a/test.py b/test.py index 50d621f..c6dd13a 100755 --- a/test.py +++ b/test.py @@ -7,14 +7,34 @@ import struct from subprocess import Popen, PIPE - - def main(argv): if len(argv) < 3: print("Usage: test.py LWC_AEAD_KAT.txt program [arguments]") test_file = open(argv[1], 'r') - p = Popen(argv[2:], bufsize=0, stdin=PIPE, stdout=PIPE) + cmd = argv[2:] + + for attempt in range(3): + print("beginning test %d of '%s' using test vectors '%s'" % (attempt, ' '.join(cmd), argv[1])) + measurements = begin_measurement() + try: + test(test_file, cmd) + print("TEST SUCCESSFUL") + return 0 + except Exception as ex: + print(str(ex)) + print("TEST FAILED") + finally: + end_measurement(measurements) + + sys.stdout.flush() + sys.stderr.flush() + + return 1 + +def test(test_file, cmd): + + p = Popen(cmd, bufsize=0, stdin=PIPE, stdout=PIPE) def write(data): l = p.stdin.write(data) @@ -22,6 +42,8 @@ def main(argv): raise Exception("could not write %d bytes of data (put %d)" % (len(data), l)) def read(l): + if l == 0: + return b"" data = p.stdout.read(l) if len(data) == 0: print("Unexpected end of stream", file=sys.stderr) @@ -40,8 +62,9 @@ def main(argv): (l, ) = struct.unpack(" %s\n" % (' '.join(sys.argv), outfile)) + mdbfile.close() if __name__ == "__main__": sys.exit(main(sys.argv)) -- libgit2 0.26.0