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_get_zone_size(self, zone, slot=0):
if zone not in (
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
ATCA_CONSTANTS.ATCA_ZONE_DATA,
ATCA_CONSTANTS.ATCA_ZONE_OTP
):
raise ATCA_EXCEPTIONS.BadArgumentError()
if slot < 0 or slot > 15:
raise ATCA_EXCEPTIONS.BadArgumentError()
if zone == ATCA_CONSTANTS.ATCA_ZONE_CONFIG:
return 128
elif zone == ATCA_CONSTANTS.ATCA_ZONE_OTP:
return 64
elif zone == ATCA_CONSTANTS.ATCA_ZONE_DATA:
if slot < 8:
return 36
elif slot == 8:
return 412
elif slot < 16:
return 72
# 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] |
PKD = ATCA_CONSTANTS.ATCA_PUB_KEY_PAD
KS = ATCA_CONSTANTS.ATCA_KEY_SIZE
# Copy X to padded position
public_key_formatted[PKD:PKD+KS] = public_key[0:KS]
# Copy Y to padded position
public_key_formatted[KS+PKD+PKD:KS+PKD+PKD+KS] = public_key[KS:KS+KS]
packets = self.atcab_write_bytes_zone(
ATCA_CONSTANTS.ATCA_ZONE_DATA,
slot=slot,
data=public_key_formatted
)
return packets
def atcab_get_zone_size(self, zone, slot=0):
if zone not in (
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
ATCA_CONSTANTS.ATCA_ZONE_DATA,
ATCA_CONSTANTS.ATCA_ZONE_OTP
):
raise ATCA_EXCEPTIONS.BadArgumentError()
if slot < 0 or slot > 15:
raise ATCA_EXCEPTIONS.BadArgumentError()
if zone == ATCA_CONSTANTS.ATCA_ZONE_CONFIG:
return 128
elif zone == ATCA_CONSTANTS.ATCA_ZONE_OTP:
return 64
elif zone == ATCA_CONSTANTS.ATCA_ZONE_DATA:
if slot < 8:
return 36
elif slot == 8:
return 412
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]