Example #1
0
    def atcab_write(self, zone, address, value=None, mac=None):
        if not isinstance(value, _BYTES_LIKE_OBJECT):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        txsize = ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN
        data = bytearray(64)
        if zone & ATCA_CONSTANTS.ATCA_ZONE_READWRITE_32:
            # 32-byte write
            data[0:32] = value
            txsize += ATCA_CONSTANTS.ATCA_BLOCK_SIZE
            # Only 32-byte writes can have a MAC
            if isinstance(mac, _BYTES_LIKE_OBJECT):
                data[32:64] = mac
                txsize += ATCA_CONSTANTS.WRITE_MAC_SIZE
        else:
            # 4-byte write
            data[0:4] = value
            txsize += ATCA_CONSTANTS.ATCA_WORD_SIZE

        packet = ATCAPacket(
            txsize=txsize,
            opcode=ATCA_CONSTANTS.ATCA_WRITE,
            param1=zone,
            param2=address,
            request_data=data[:txsize - ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN])
        self.execute(packet)
        return packet
Example #2
0
    def atcab_sha_base(self, mode=0, data=b'', key_slot=None):
        if not isinstance(data, _BYTES_LIKE_OBJECT):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        txsize = 0
        cmd_mode = mode & ATCA_CONSTANTS.SHA_MODE_MASK
        if cmd_mode in (ATCA_CONSTANTS.SHA_MODE_SHA256_START,
                        ATCA_CONSTANTS.SHA_MODE_HMAC_START,
                        ATCA_CONSTANTS.SHA_MODE_SHA256_PUBLIC):
            txsize = ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN
        elif cmd_mode in (ATCA_CONSTANTS.SHA_MODE_SHA256_UPDATE,
                          ATCA_CONSTANTS.SHA_MODE_SHA256_END,
                          ATCA_CONSTANTS.SHA_MODE_HMAC_END):
            txsize = ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN + len(data)
        else:
            raise ATCA_EXCEPTIONS.BadArgumentError()

        packet = ATCAPacket(
            txsize=txsize,
            opcode=ATCA_CONSTANTS.ATCA_SHA,
            param1=mode,
            param2=len(data) if not isinstance(key_slot, int) else key_slot,
            request_data=data)
        self.execute(packet)
        return packet
Example #3
0
 def atcab_sign_base(self, mode, key_id):
     packet = ATCAPacket(txsize=ATCA_CONSTANTS.SIGN_COUNT,
                         opcode=ATCA_CONSTANTS.ATCA_SIGN,
                         param1=mode,
                         param2=key_id)
     self.execute(packet)
     return packet
Example #4
0
 def atcab_lock(self, mode, crc=0):
     packet = ATCAPacket(txsize=ATCA_CONSTANTS.LOCK_COUNT,
                         opcode=ATCA_CONSTANTS.ATCA_LOCK,
                         param1=mode,
                         param2=crc)
     self.execute(packet)
     return packet
Example #5
0
 def atcab_random(self):
     packet = ATCAPacket(
         opcode=ATCA_CONSTANTS.ATCA_RANDOM,
         param1=ATCA_CONSTANTS.RANDOM_SEED_UPDATE,
     )
     self.execute(packet)
     return packet
Example #6
0
 def atcab_info_base(self, mode=0):
     packet = ATCAPacket(
         opcode=ATCA_CONSTANTS.ATCA_INFO,
         param1=mode
     )
     self.execute(packet)
     return packet
Example #7
0
    def atcab_sha_base(self, mode=0, data=b''):
        txsize = 0
        cmd_mode = mode & ATCA_CONSTANTS.SHA_MODE_MASK
        if cmd_mode in (
            ATCA_CONSTANTS.SHA_MODE_SHA256_START,
            ATCA_CONSTANTS.SHA_MODE_HMAC_START,
            ATCA_CONSTANTS.SHA_MODE_SHA256_PUBLIC
        ):
            txsize = ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN
        elif cmd_mode in (
            ATCA_CONSTANTS.SHA_MODE_SHA256_UPDATE,
            ATCA_CONSTANTS.SHA_MODE_SHA256_END,
            ATCA_CONSTANTS.SHA_MODE_HMAC_END
        ):
            txsize = ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN + len(data)
        else:
            raise ValueError("bad params")

        packet = ATCAPacket(
            txsize=txsize,
            opcode=ATCA_CONSTANTS.ATCA_SHA,
            param1=mode,
            param2=len(data),
            request_data=data
        )
        self.execute(packet)
        return packet
Example #8
0
    def atcab_verify(self,
                     mode,
                     key_id,
                     signature,
                     public_key=None,
                     other_data=None,
                     mac=None):
        if not isinstance(signature, _BYTES_LIKE_OBJECT):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        verify_mode = (mode & ATCA_CONSTANTS.VERIFY_MODE_MASK)

        VME = ATCA_CONSTANTS.VERIFY_MODE_EXTERNAL
        has_public_key = isinstance(public_key, _BYTES_LIKE_OBJECT)
        if verify_mode == VME and not has_public_key:
            raise ATCA_EXCEPTIONS.BadArgumentError()

        VMV = ATCA_CONSTANTS.VERIFY_MODE_VALIDATE
        VMI = ATCA_CONSTANTS.VERIFY_MODE_INVALIDATE
        has_other_data = isinstance(other_data, _BYTES_LIKE_OBJECT)
        if verify_mode in (VMV, VMI) and not has_other_data:
            raise ATCA_EXCEPTIONS.BadArgumentError()

        txsize = 0
        if verify_mode == ATCA_CONSTANTS.VERIFY_MODE_STORED:
            txsize = ATCA_CONSTANTS.VERIFY_256_STORED_COUNT
        elif verify_mode in (ATCA_CONSTANTS.VERIFY_MODE_VALIDATE_EXTERNAL,
                             ATCA_CONSTANTS.VERIFY_MODE_EXTERNAL):
            txsize = ATCA_CONSTANTS.VERIFY_256_EXTERNAL_COUNT
        elif verify_mode in (ATCA_CONSTANTS.VERIFY_MODE_VALIDATE,
                             ATCA_CONSTANTS.VERIFY_MODE_INVALIDATE):
            txsize = ATCA_CONSTANTS.VERIFY_256_VALIDATE_COUNT

        SS = ATCA_CONSTANTS.ATCA_SIG_SIZE
        PKS = ATCA_CONSTANTS.ATCA_PUB_KEY_SIZE
        VODS = ATCA_CONSTANTS.VERIFY_OTHER_DATA_SIZE
        data_size = SS
        if has_public_key:
            data_size += PKS
        elif has_other_data:
            data_size += VODS

        data = bytearray(data_size)

        data[0:SS] = signature
        if has_public_key:
            data[SS:SS + PKS] = public_key
        elif has_other_data:
            data[SS:SS + VODS] = other_data

        packet = ATCAPacket(txsize=txsize,
                            opcode=ATCA_CONSTANTS.ATCA_VERIFY,
                            param1=mode,
                            param2=key_id,
                            request_data=data)
        self.execute(packet)
        return packet
Example #9
0
 def atcab_selftest(self, mode, param2=0):
     if self._device != "ATECC608A":
         raise ATCA_EXCEPTIONS.UnsupportedDeviceError("atcab_selftest")
     packet = ATCAPacket(opcode=ATCA_CONSTANTS.ATCA_SELFTEST,
                         param1=mode,
                         param2=param2)
     self.execute(packet)
     RSP_DATA_IDX = ATCA_CONSTANTS.ATCA_RSP_DATA_IDX
     return packet[RSP_DATA_IDX] & int(not mode) == ATCA_STATUS.ATCA_SUCCESS
Example #10
0
    def atcab_gendig(self, zone, key_id, other_data):
        if (zone < 0x00 or zone > 0x05):
            raise ATCA_EXCEPTIONS.BadArgumentError(
                "Zone must be between zero and 5")

        packet = ATCAPacket(opcode=ATCA_CONSTANTS.ATCA_GENDIG,
                            param1=zone,
                            param2=key_id,
                            request_data=other_data)
        self.execute(packet)
        return packet
Example #11
0
    def atcab_read_zone(self, zone, slot=0, block=0, offset=0, length=0):
        if length not in (ATCA_CONSTANTS.ATCA_WORD_SIZE,
                          ATCA_CONSTANTS.ATCA_BLOCK_SIZE):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        addr = self.atcab_get_addr(zone, slot=slot, block=block, offset=offset)

        if length == ATCA_CONSTANTS.ATCA_BLOCK_SIZE:
            zone = zone | ATCA_CONSTANTS.ATCA_ZONE_READWRITE_32

        packet = ATCAPacket(opcode=ATCA_CONSTANTS.ATCA_READ,
                            param1=zone,
                            param2=addr)
        self.execute(packet)
        return packet
Example #12
0
    def atcab_genkey_base(self, mode, key_id, other_data=None):

        txsize = 0
        if mode & ATCA_CONSTANTS.GENKEY_MODE_PUBKEY_DIGEST:
            txsize = ATCA_CONSTANTS.GENKEY_COUNT_DATA
        else:
            txsize = ATCA_CONSTANTS.GENKEY_COUNT

        has_other_data = isinstance(other_data, _BYTES_LIKE_OBJECT)
        data = other_data if has_other_data else b''

        packet = ATCAPacket(txsize=txsize,
                            opcode=ATCA_CONSTANTS.ATCA_GENKEY,
                            param1=mode,
                            param2=key_id,
                            request_data=data)
        self.execute(packet)
        return packet
Example #13
0
    def atcab_nonce_base(self, mode, zero=0, numbers=None):
        nonce_mode = mode & ATCA_CONSTANTS.NONCE_MODE_MASK
        if nonce_mode not in (ATCA_CONSTANTS.NONCE_MODE_SEED_UPDATE,
                              ATCA_CONSTANTS.NONCE_MODE_NO_SEED_UPDATE,
                              ATCA_CONSTANTS.NONCE_MODE_PASSTHROUGH):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        if not isinstance(numbers, _BYTES_LIKE_OBJECT):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        txsize = 0
        if nonce_mode in (ATCA_CONSTANTS.NONCE_MODE_SEED_UPDATE,
                          ATCA_CONSTANTS.NONCE_MODE_NO_SEED_UPDATE):
            txsize = ATCA_CONSTANTS.NONCE_COUNT_SHORT
        elif nonce_mode == ATCA_CONSTANTS.NONCE_MODE_PASSTHROUGH:
            nonce_mode_input = mode & ATCA_CONSTANTS.NONCE_MODE_INPUT_LEN_MASK
            if nonce_mode_input == ATCA_CONSTANTS.NONCE_MODE_INPUT_LEN_64:
                txsize = ATCA_CONSTANTS.NONCE_COUNT_LONG_64
            else:
                txsize = ATCA_CONSTANTS.NONCE_COUNT_LONG
        else:
            raise ATCA_EXCEPTIONS.BadArgumentError()

        n_mv = memoryview(numbers)
        if len(n_mv) < txsize - ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN:
            raise ATCA_EXCEPTIONS.BadArgumentError()

        packet = ATCAPacket(
            txsize=txsize,
            opcode=ATCA_CONSTANTS.ATCA_NONCE,
            param1=mode,
            param2=zero,
            request_data=n_mv[:txsize - ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN])

        self.execute(packet)
        return packet
Example #14
0
 def atcab_updateextra(self, mode, value):
     packet = ATCAPacket(opcode=ATCA_CONSTANTS.ATCA_UPDATE_EXTRA,
                         param1=mode,
                         param2=value)
     self.execute(packet)
     return packet