Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def atcab_read_bytes_zone(self, zone, slot=0, block=0, offset=0, length=0):
zone_size = self.atcab_get_zone_size(zone, slot=slot)
if offset + length > zone_size:
raise ATCA_EXCEPTIONS.BadArgumentError()
packets = []
BS = ATCA_CONSTANTS.ATCA_BLOCK_SIZE
WS = ATCA_CONSTANTS.ATCA_WORD_SIZE
r_sz = BS
d_idx = r_idx = r_of = c_blk = c_of = 0
c_blk = offset // BS
while d_idx < length:
if r_sz == BS and zone_size - c_blk * BS < BS:
r_sz = WS
c_of = ((d_idx + offset) // WS) % (BS // WS)
packet = self.atcab_read_zone(
zone,
def atcab_write_zone(self, zone, slot=0, block=0, offset=0, data=None):
if not isinstance(data, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
length = len(data)
if length not in (
ATCA_CONSTANTS.ATCA_WORD_SIZE,
ATCA_CONSTANTS.ATCA_BLOCK_SIZE
):
raise ATCA_EXCEPTIONS.BadArgumentError()
if length == ATCA_CONSTANTS.ATCA_BLOCK_SIZE:
zone = zone | ATCA_CONSTANTS.ATCA_ZONE_READWRITE_32
addr = self.atcab_get_addr(zone, slot=slot, block=block, offset=offset)
return self.atcab_write(zone, addr, data)
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:
def atcab_nonce_load(self, target, numbers=None):
if not isinstance(numbers, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
mode = ATCA_CONSTANTS.NONCE_MODE_PASSTHROUGH
mode = mode | (ATCA_CONSTANTS.NONCE_MODE_TARGET_MASK & target)
if len(numbers) == 32:
mode = mode | ATCA_CONSTANTS.NONCE_MODE_INPUT_LEN_32
elif len(numbers) == 64:
mode = mode | ATCA_CONSTANTS.NONCE_MODE_INPUT_LEN_64
else:
raise ATCA_EXCEPTIONS.BadArgumentError()
return self.atcab_nonce_base(mode, numbers=numbers)
def atcab_is_locked(self, zone):
if zone not in (
ATCA_CONSTANTS.LOCK_ZONE_CONFIG,
ATCA_CONSTANTS.LOCK_ZONE_DATA
):
raise ATCA_EXCEPTIONS.BadArgumentError()
# Read the word with the lock bytes
# (UserExtra, Selector, LockValue, LockConfig) (config block = 2, word offset = 5)
packet = self.atcab_read_zone(
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
slot=0,
block=2,
offset=5,
length=ATCA_CONSTANTS.ATCA_WORD_SIZE
)
if zone == ATCA_CONSTANTS.LOCK_ZONE_CONFIG:
return bool(packet[3+1] != 0x55)
elif zone == ATCA_CONSTANTS.LOCK_ZONE_DATA:
return bool(packet[2+1] != 0x55)
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:
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