#!/usr/bin/env python3 import os import sys import time import subprocess import serial.tools.list_ports from test_common import ( LogicMultiplexerTimeMeasurements, parse_nist_aead_test_vectors, DeviceUnderTestAeadUARTP, FileMutex, eprint, run_nist_aead_test_line, ) def get_serial(): import serial.tools.list_ports ports = serial.tools.list_ports.comports() ports = [ c for c in ports if c.product == 'Sipeed-Debug' ] ports.sort(key=lambda d: d.location) return ports[0].device class Maixduino(DeviceUnderTestAeadUARTP): def __init__(self, build_dir): DeviceUnderTestAeadUARTP.__init__(self) self.uart_device = get_serial() devname = os.path.basename(self.uart_device) self.lock = FileMutex('/var/lock/lwc-compare.%s.lock' % devname) self.build_dir = build_dir def reset(self): self.ser.setRTS(True) time.sleep(0.1) self.ser.setRTS(False) time.sleep(0.1) self.ser.setRTS(True) time.sleep(1) def prepare(self): self.ser = serial.Serial( self.uart_device, baudrate=1500000, timeout=5) self.reset() DeviceUnderTestAeadUARTP.prepare(self) def flash(self): pipe = subprocess.PIPE previous_dir = os.path.abspath(os.curdir) os.chdir(self.build_dir) cmd = ['platformio', 'run', '-e', 'sipeed-maixduino'] cmd.extend(['--target', 'upload']) cmd.extend(['--upload-port', self.uart_device]) p = subprocess.Popen( cmd, stdout=sys.stderr, stdin=pipe) stdout, stderr = p.communicate("") assert p.returncode == 0 eprint("Firmware flashed.") os.chdir(previous_dir) def dump_ram(self): return None def main(argv): if len(argv) != 3: print("Usage: test LWC_AEAD_KAT.txt build_dir") return 1 kat = list(parse_nist_aead_test_vectors(argv[1])) build_dir = argv[2] dut = Maixduino(build_dir) dut.flash() dut.prepare() sys.stdout.write("Board prepared\n") sys.stdout.flush() try: tool = LogicMultiplexerTimeMeasurements(0x0080) tool.begin_measurement() for i, m, ad, k, npub, c in kat: tool.arm() run_nist_aead_test_line(dut, i, m, ad, k, npub, c) tool.unarm() except Exception as ex: print("TEST FAILED") raise ex finally: tool.end_measurement() if __name__ == "__main__": sys.exit(main(sys.argv))