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

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


18 19 20 21 22 23 24 25 26 27 28 29
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)
30 31


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

35 36 37 38 39 40 41 42
    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

43 44
        self.jlink = pylink.JLink()
        self.jlink.open(779340002)
45 46 47

        self.firmware_path = os.path.join(build_dir, 'build', 'f7.bin')
        self.ram_pattern_path = os.path.join(build_dir, 'ram_pattern.bin')
48

49 50 51
    def flash(self):
        jlink = self.jlink
        jlink.connect('STM32F746ZG')
lwc-tester committed
52 53 54 55
        jlink.flash_file(self.firmware_path, 0x8000000)
        eprint("Firmware flashed.")
        jlink.flash_file(self.ram_pattern_path, 0x20000000)
        eprint("RAM flashed.")
56 57
        jlink.reset()
        jlink.restart()
58

59 60 61
    def dump_ram(self):
        jlink = self.jlink
        return bytes(jlink.memory_read8(0x20000000, F7.RAM_SIZE))
62

Enrico Pozzobon committed
63

64
def main(argv):
65 66 67
    if len(argv) != 3:
        print("Usage: test LWC_AEAD_KAT.txt build_dir")
        return 1
Enrico Pozzobon committed
68

69
    kat = list(parse_nist_aead_test_vectors(argv[1]))
70
    build_dir = argv[2]
Enrico Pozzobon committed
71

72 73 74 75 76 77 78 79
    dut = F7(build_dir)

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

    dump_a = dut.dump_ram()
Enrico Pozzobon committed
80

81
    try:
lwc-tester committed
82 83 84 85 86 87 88 89 90 91 92 93
        tool = LogicMultiplexerTimeMeasurements(0x000c)
        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)
94 95 96 97 98 99

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

    finally:
lwc-tester committed
100
        tool.end_measurement()
Enrico Pozzobon committed
101 102 103 104


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