test 2.17 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python3

import os
import sys
import subprocess
6 7 8 9 10 11 12 13 14
import serial.tools.list_ports
from test_common import (
    LogicMultiplexerTimeMeasurements,
    parse_nist_aead_test_vectors,
    DeviceUnderTestAeadUARTP,
    compare_dumps,
    eprint,
    run_nist_aead_test_line,
)
15 16


17 18 19 20 21 22 23 24 25 26 27
def get_serial():
    ports = serial.tools.list_ports.comports()
    for port in ports:
        print(port.serial_number)
    devices = [
        p.device
        for p in ports
        if (p.vid == 4292 and p.pid == 60000)
    ]
    devices.sort()
    return devices[0]
28 29


30 31
class ESP32(DeviceUnderTestAeadUARTP):
    RAM_SIZE = 0x5000
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    def __init__(self, build_dir):
        DeviceUnderTestAeadUARTP.__init__(self)

        self.build_dir = build_dir

    def flash(self):
        pipe = subprocess.PIPE
        previous_dir = os.path.abspath(os.curdir)
        os.chdir(self.build_dir)
        cmd = ['platformio', 'run', '-e', 'esp32dev', '--target', 'upload']
        cmd.extend(['--upload-port', get_serial()])
        cmd.extend(['--upload-port', get_serial()])
        p = subprocess.Popen(
            cmd, stdout=sys.stderr, stdin=pipe)
        stdout, stderr = p.communicate("")
        eprint("Firmware flashed.")
        os.chdir(previous_dir)

    def dump_ram(self):
        return None
53 54 55


def main(argv):
56 57
    if len(argv) != 3:
        print("Usage: test LWC_AEAD_KAT.txt build_dir")
58
        return 1
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    kat = list(parse_nist_aead_test_vectors(argv[1]))
    build_dir = argv[2]

    dut = ESP32(build_dir)

    try:
        tool = LogicMultiplexerTimeMeasurements(0x0030)
        tool.begin_measurement()

        dut.flash()

        dut.ser = serial.Serial(
            get_serial(),
            baudrate=500000,
            timeout=5)

        dut.prepare()
        sys.stdout.write("Board prepared\n")
        sys.stdout.flush()

        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()
        sys.stdout.flush()
        sys.stderr.flush()
93 94 95 96


if __name__ == "__main__":
    sys.exit(main(sys.argv))