#!/usr/bin/env python3 import os import sys import pylink import serial.tools.list_ports from test_common import ( LogicMultiplexerTimeMeasurements, parse_nist_aead_test_vectors, DeviceUnderTestAeadUARTP, compare_dumps, eprint, FileMutex, run_nist_aead_test_line, ) def get_serial(): ports = serial.tools.list_ports.comports() devices = [ p.device for p in ports if p.serial_number == 'FT2XA9MY' ] devices.sort() return serial.Serial( devices[0], baudrate=115200, timeout=5) class F7(DeviceUnderTestAeadUARTP): RAM_SIZE = 0x50000 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 self.jlink = pylink.JLink() self.jlink.open(779340002) self.firmware_path = os.path.join(build_dir, 'build', 'f7.bin') self.ram_pattern_path = os.path.join(build_dir, 'ram_pattern.bin') def flash(self): jlink = self.jlink jlink.connect('STM32F746ZG') jlink.flash_file(self.firmware_path, 0x8000000) eprint("Firmware flashed.") jlink.flash_file(self.ram_pattern_path, 0x20000000) eprint("RAM flashed.") jlink.reset() jlink.restart() def dump_ram(self): jlink = self.jlink return bytes(jlink.memory_read8(0x20000000, F7.RAM_SIZE)) 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 = F7(build_dir) dut.flash() dut.prepare() sys.stdout.write("Board prepared\n") sys.stdout.flush() dump_a = dut.dump_ram() try: tool = LogicMultiplexerTimeMeasurements(0x0008) 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() if i == 1: dump_b = dut.dump_ram() longest = compare_dumps(dump_a, dump_b) print(" longest chunk of untouched memory = %d" % longest) except Exception as ex: print("TEST FAILED") raise ex finally: tool.end_measurement() if __name__ == "__main__": sys.exit(main(sys.argv))