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

import os
import sys
5 6 7 8 9 10 11
import serial.tools.list_ports
from test_common import (
    LogicMultiplexerTimeMeasurements,
    parse_nist_aead_test_vectors,
    DeviceUnderTestAeadUARTP,
    compare_dumps,
    eprint,
lwc-tester committed
12 13
    OpenOcd,
    FileMutex,
14 15
    run_nist_aead_test_line,
)
16 17


18 19 20 21 22 23 24 25
def get_serial():
    ports = serial.tools.list_ports.comports()
    devices = [
        p.device
        for p in ports
        if p.serial_number == 'FT2XCRZ1'
    ]
    devices.sort()
lwc-tester committed
26 27
    eprint("Serial port for BluePill is %s" % devices[0])
    return devices[0]
28 29 30 31


class BluePill(DeviceUnderTestAeadUARTP):
    RAM_SIZE = 0x5000
lwc-tester committed
32
    LOCK_PATH = '/var/lock/lwc-compare.bluepill.lock'
33 34

    def __init__(self, build_dir):
lwc-tester committed
35 36 37
        DeviceUnderTestAeadUARTP.__init__(self)

        self.lock = FileMutex(BluePill.LOCK_PATH)
38 39 40 41 42 43 44 45 46 47

        self.firmware_path = os.path.join(
            build_dir, '.pio/build/bluepill_f103c8/firmware.elf')
        self.ram_pattern_path = os.path.join(
            build_dir, 'empty_ram.bin')
        self.ram_dump_path = os.path.join(
            build_dir, 'ram_dump.bin')
        self.openocd_cfg_path = os.path.join(
            build_dir, 'openocd.cfg')

lwc-tester committed
48 49
        self.ocd = OpenOcd(self.openocd_cfg_path)

50
    def flash(self):
lwc-tester committed
51 52 53 54
        ocd_cmd = 'program %s verify reset' % self.firmware_path
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''
55 56
        eprint("Firmware flashed.")

lwc-tester committed
57 58 59 60 61 62 63 64 65
        ocd_cmd = 'reset halt'
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''

        ocd_cmd = 'load_image %s 0x20000000' % self.ram_pattern_path
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''
66 67
        eprint("RAM flashed.")

lwc-tester committed
68 69 70 71 72 73 74 75 76 77 78 79
        ocd_cmd = 'resume'
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''

    def reset(self, halt=False):
        ocd_cmd = 'reset halt' if halt else 'reset run'
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''
        eprint("Reset!")

80
    def dump_ram(self):
lwc-tester committed
81 82 83 84 85 86
        res = self.ocd.send(
            'dump_image %s 0x20000000 0x%x' %
            (self.ram_dump_path, BluePill.RAM_SIZE))
        eprint(res)
        assert res == ''

87 88 89 90 91 92 93 94
        eprint("RAM dumped.")
        with open(self.ram_dump_path, 'rb') as ram:
            ram = ram.read()
            if len(ram) != BluePill.RAM_SIZE:
                raise Exception(
                    "RAM dump was %d bytes instead of %d" %
                    (len(ram), BluePill.RAM_SIZE))
            return ram
95 96


97 98 99 100
def main(argv):
    if len(argv) != 3:
        print("Usage: test LWC_AEAD_KAT.txt build_dir")
        return 1
101

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

105
    dut = BluePill(build_dir)
106

lwc-tester committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    dut.reset()
    dut.flash()

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

    dut.reset()

    dut.ser = ser

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

    dump_a = dut.dump_ram()

125 126 127
    try:
        tool = LogicMultiplexerTimeMeasurements(0x0003)
        tool.begin_measurement()
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        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()
lwc-tester committed
145 146 147

    sys.stdout.flush()
    sys.stderr.flush()
148 149 150 151


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