Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for slot in range(16):
slot_locked = device.atcab_is_slot_locked(slot)
log.debug("atcab_is_slot_locked %d: %s", slot, slot_locked)
locked_config = device.atcab_is_locked(ATCA_CONSTANTS.LOCK_ZONE_CONFIG)
log.debug("atcab_is_locked LOCK_ZONE_CONFIG: %r", locked_config)
locked_data = device.atcab_is_locked(ATCA_CONSTANTS.LOCK_ZONE_DATA)
log.debug("atcab_is_locked LOCK_ZONE_DATA: %r", locked_data)
try:
slot = 11
public_key = device.atcab_read_pubkey(slot)
log.debug("atcab_read_pubkey slot %d: %s", slot, hexlify(public_key))
except ATCA_EXCEPTIONS.ExecutionError:
pass
def atcab_get_addr(self, zone, slot=0, block=0, offset=0):
mem_zone = zone & ATCA_CONSTANTS.ATCA_ZONE_MASK
if mem_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()
addr = 0
offset = offset & 0x07
if mem_zone in (
ATCA_CONSTANTS.ATCA_ZONE_CONFIG,
ATCA_CONSTANTS.ATCA_ZONE_OTP
):
addr = block << 3
addr = addr | offset
elif mem_zone == ATCA_CONSTANTS.ATCA_ZONE_DATA:
addr = slot << 3
addr = addr | offset
addr = addr | block << 8
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
def decode_error(self, error):
return {
0x00: (self.ATCA_SUCCESS, None),
0x01: (self.ATCA_CHECKMAC_VERIFY_FAILED, ATCA_EXECUTIONS.CheckmacVerifyFailedError),
0x03: (self.ATCA_PARSE_ERROR, ATCA_EXECUTIONS.ParseError),
0x05: (self.ATCA_STATUS_ECC, ATCA_EXECUTIONS.EccFaultError),
0x07: (self.ATCA_STATUS_SELFTEST_ERROR, ATCA_EXECUTIONS.SelfTestError),
0x08: (self.ATCA_HEALTH_TEST_ERROR, ATCA_EXECUTIONS.HealthTestError),
0x0F: (self.ATCA_EXECUTION_ERROR, ATCA_EXECUTIONS.ExecutionError),
0x11: (self.ATCA_WAKE_SUCCESS, None),
0xEE: (self.ATCA_WATCHDOG_ABOUT_TO_EXPIRE, ATCA_EXECUTIONS.WatchDogAboutToExpireError),
0xFF: (self.ATCA_STATUS_CRC, ATCA_EXECUTIONS.CrcError),
}.get(error, (self.ATCA_GEN_FAIL, ATCA_EXECUTIONS.GenericError))
def decode_error(self, error):
return {
0x00: (self.ATCA_SUCCESS, None),
0x01: (self.ATCA_CHECKMAC_VERIFY_FAILED, ATCA_EXECUTIONS.CheckmacVerifyFailedError),
0x03: (self.ATCA_PARSE_ERROR, ATCA_EXECUTIONS.ParseError),
0x05: (self.ATCA_STATUS_ECC, ATCA_EXECUTIONS.EccFaultError),
0x07: (self.ATCA_STATUS_SELFTEST_ERROR, ATCA_EXECUTIONS.SelfTestError),
0x08: (self.ATCA_HEALTH_TEST_ERROR, ATCA_EXECUTIONS.HealthTestError),
0x0F: (self.ATCA_EXECUTION_ERROR, ATCA_EXECUTIONS.ExecutionError),
0x11: (self.ATCA_WAKE_SUCCESS, None),
0xEE: (self.ATCA_WATCHDOG_ABOUT_TO_EXPIRE, ATCA_EXECUTIONS.WatchDogAboutToExpireError),
0xFF: (self.ATCA_STATUS_CRC, ATCA_EXECUTIONS.CrcError),
}.get(error, (self.ATCA_GEN_FAIL, ATCA_EXECUTIONS.GenericError))
def __init__(
self,
bus=machine.I2C(1, freq=133000),
address=I2C_ADDRESS, retries=RX_RETRIES):
if address not in bus.scan():
raise ATCA_EXCEPTIONS.NoDevicesFoundError()
self._bus = bus
self._address = address
self._retries = retries
try:
self._device = SUPPORTED_DEVICES[self.atcab_info()[1+2]]
except KeyError:
raise ATCA_EXCEPTIONS.UnsupportedDeviceError()
# Load message into device
if self._device == "ATECC608A":
# Use the Message Digest Buffer for the ATECC608A
nonce_target = ATCA_CONSTANTS.NONCE_MODE_TARGET_MSGDIGBUF
verify_source = ATCA_CONSTANTS.VERIFY_MODE_SOURCE_MSGDIGBUF
self.atcab_nonce_load(nonce_target, message)
try:
packet = self.atcab_verify(
ATCA_CONSTANTS.VERIFY_MODE_STORED | verify_source,
key_id,
signature
)
return packet[1] == ATCA_STATUS.ATCA_SUCCESS
except ATCA_EXCEPTIONS.CheckmacVerifyFailedError:
# Verify failed, but command succeeded
return False
def atcab_verify_extern(self, message, signature, public_key):
if not isinstance(message, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
if not isinstance(signature, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
if not isinstance(public_key, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
nonce_target = ATCA_CONSTANTS.NONCE_MODE_TARGET_TEMPKEY
verify_source = ATCA_CONSTANTS.VERIFY_MODE_SOURCE_TEMPKEY
# Load message into device
if self._device == "ATECC608A":
# Use the Message Digest Buffer for the ATECC608A
nonce_target = ATCA_CONSTANTS.NONCE_MODE_TARGET_MSGDIGBUF
verify_source = ATCA_CONSTANTS.VERIFY_MODE_SOURCE_MSGDIGBUF
self.atcab_nonce_load(nonce_target, message)
try:
packet = self.atcab_verify(
ATCA_CONSTANTS.VERIFY_MODE_EXTERNAL | verify_source,
ATCA_CONSTANTS.VERIFY_KEY_P256,
signature,
err, exc = self.is_error(resp)
if err == ATCA_STATUS.ATCA_SUCCESS:
packet._response_data = resp[:resp[0]]
return
elif err == ATCA_STATUS.ATCA_WAKE_SUCCESS:
return
elif err == ATCA_STATUS.ATCA_WATCHDOG_ABOUT_TO_EXPIRE:
self.sleep()
else:
if exc is not None:
packet._response_data = resp[:resp[0]]
raise exc(ubinascii.hexlify(packet._response_data))
except OSError:
retries -= 1
else:
raise ATCA_EXCEPTIONS.GenericError("max retry")