from .tls import tls, psk_encryption_key, psk_validation_key, hs_key, crt_hardcoded from hashlib import sha256 import hmac from struct import pack, unpack from binascii import hexlify, unhexlify from .usb import usb from .flash import write_flash, erase_flash, flush_changes, PartitionInfo, get_flash_info from .sensor import reboot from Crypto.Random import get_random_bytes from Crypto.Cipher import AES from fastecdsa.encoding.der import DEREncoder from fastecdsa.curve import P256 from fastecdsa.ecdsa import sign from fastecdsa.keys import gen_private_key, get_public_key from .util import assert_status, unhex from .blobs import reset_blob flash_layout_hardcoded=[ # id type access offset size # lvl PartitionInfo(1, 4, 7, 0x00001000, 0x00001000), # cert store PartitionInfo(2, 1, 2, 0x00002000, 0x0003e000), # xpfwext PartitionInfo(5, 5, 3, 0x00040000, 0x00008000), # ??? PartitionInfo(6, 6, 3, 0x00048000, 0x00008000), # calibration data PartitionInfo(4, 3, 5, 0x00050000, 0x00030000), # template database ] def with_hdr(id, buf): return pack(' 0: raise Exception('Flash is already partitioned') cmd = unhex('4f 0000 0000') cmd += with_hdr(0, serialize_flash_params(info.ic)) cmd += with_hdr(1, b''.join([serialize_partition(p) for p in layout])) cmd += with_hdr(5, make_cert(client_public)) cmd += with_hdr(3, crt_hardcoded) rsp = tls.cmd(cmd) assert_status(rsp) rsp = rsp[2:] crt_len, rsp=rsp[:4], rsp[4:] crt_len, = unpack('