test 2.49 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4
#!/usr/bin/env python3

import os
import sys
5 6
import pylink
from test_common import (
lwc-tester committed
7
    LogicMultiplexerTimeMeasurements,
8 9
    parse_nist_aead_test_vectors,
    DeviceUnderTestAeadUARTP,
lwc-tester committed
10 11 12
    compare_dumps,
    eprint,
    run_nist_aead_test_line,
13
)
Enrico Pozzobon committed
14 15


16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
def get_serial():
    import serial.tools.list_ports
    ports = serial.tools.list_ports.comports()
    for port in ports:
        print(port.serial_number)
    devices = [
        p.device
        for p in ports
        if p.serial_number == 'FT2XA9MY'
    ]
    devices.sort()
    return serial.Serial(
        devices[0],
        baudrate=115200,
        timeout=5)
31 32


33 34
class F7(DeviceUnderTestAeadUARTP):
    RAM_SIZE = 0x50000
lwc-tester committed
35 36

    def __init__(self, firmware_path, ram_pattern_path):
37 38 39
        DeviceUnderTestAeadUARTP.__init__(self, get_serial())
        self.jlink = pylink.JLink()
        self.jlink.open(779340002)
lwc-tester committed
40 41
        self.firmware_path = firmware_path
        self.ram_pattern_path = ram_pattern_path
42

43 44 45
    def flash(self):
        jlink = self.jlink
        jlink.connect('STM32F746ZG')
lwc-tester committed
46 47 48 49
        jlink.flash_file(self.firmware_path, 0x8000000)
        eprint("Firmware flashed.")
        jlink.flash_file(self.ram_pattern_path, 0x20000000)
        eprint("RAM flashed.")
50 51
        jlink.reset()
        jlink.restart()
52

53 54 55
    def dump_ram(self):
        jlink = self.jlink
        return bytes(jlink.memory_read8(0x20000000, F7.RAM_SIZE))
56

Enrico Pozzobon committed
57

58 59 60
def main(argv):
    if len(argv) < 2:
        print("Usage: test LWC_AEAD_KAT.txt")
Enrico Pozzobon committed
61

62
    kat = list(parse_nist_aead_test_vectors(argv[1]))
63

Enrico Pozzobon committed
64 65 66
    eprint(argv[0])
    script_dir = os.path.split(argv[0])[0]

lwc-tester committed
67 68 69
    dut = F7(
        os.path.join(script_dir, 'build', 'f7.bin'),
        os.path.join(script_dir, 'ram_pattern.bin'))
Enrico Pozzobon committed
70

71
    try:
lwc-tester committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        tool = LogicMultiplexerTimeMeasurements(0x000c)
        tool.begin_measurement()

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

        dump_a = dut.dump_ram()

        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)
91 92 93 94 95 96

    except Exception as ex:
        print("TEST FAILED")
        raise ex

    finally:
lwc-tester committed
97
        tool.end_measurement()
98 99
        sys.stdout.flush()
        sys.stderr.flush()
Enrico Pozzobon committed
100 101 102 103


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