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_write_bytes_zone(self, zone, slot=0, offset=0, data=None):
if not isinstance(data, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
zone_size = self.atcab_get_zone_size(zone, slot=slot)
length = len(data)
if offset + length > zone_size:
raise ATCA_EXCEPTIONS.BadArgumentError()
packets = []
BS = ATCA_CONSTANTS.ATCA_BLOCK_SIZE
WS = ATCA_CONSTANTS.ATCA_WORD_SIZE
ZC = ATCA_CONSTANTS.ATCA_ZONE_CONFIG
d_idx = 0
c_blk = offset // BS
c_wrd = (offset % BS) // WS
d_mv = memoryview(data)
while d_idx < length:
# The last item makes sure we handle the selector, user extra, and lock bytes in the config properly
if c_wrd == 0 and length - d_idx >= BS and not (zone == ZC and c_blk == 2):
packet = self.atcab_write_zone(
zone,
slot=slot,
block=c_blk,
offset=0,
data=d_mv[d_idx:d_idx+BS]
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,
slot=slot,
block=c_blk,
offset=c_of,
length=r_sz
def atcab_read_pubkey(self, slot):
# Check the value of the slot. Only slots 8 to 15 are large enough to store a public key
if slot < 8 or slot > 15:
raise ATCA_EXCEPTIONS.BadArgumentError(
"Only slots 8 to 15 are large enough to store a public key")
# The 64 byte P256 public key gets written to a 72 byte slot in the following pattern
#
# | Block 1 | Block 2 | Block 3 |
# | Pad: 4 Bytes | PubKey[0:27] | PubKey[28:31] | Pad: 4 Bytes | PubKey[32:55] | PubKey[56:63] |
ZD = ATCA_CONSTANTS.ATCA_ZONE_DATA
BS = ATCA_CONSTANTS.ATCA_BLOCK_SIZE
PKD = ATCA_CONSTANTS.ATCA_PUB_KEY_PAD
KS = ATCA_CONSTANTS.ATCA_KEY_SIZE
public_key = b''
packet = self.atcab_read_zone(ZD, slot=slot, block=0, length=BS)
public_key += packet[1:-2]
packet = self.atcab_read_zone(ZD, slot=slot, block=1, length=BS)
public_key += packet[1:-2]
packet = self.atcab_read_zone(ZD, slot=slot, block=2, length=BS)
public_key += packet[1:-2]
return public_key[PKD:PKD+KS] + public_key[KS+PKD+PKD:KS+PKD+PKD+KS]
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]
)
def atcab_read_serial_number(self):
return self.atcab_read_zone(
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
length=ATCA_CONSTANTS.ATCA_BLOCK_SIZE
)