How to use cryptoauthlib - 10 common examples

To help you get started, we’ve selected a few cryptoauthlib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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,
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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]
                )
                packets.append(packet)
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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]
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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]
                )
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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
            )
github dmazzella / ucryptoauthlib / tests / ateccX08a / tests_read.py View on Github external
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
github dmazzella / ucryptoauthlib / tests / ateccX08a / tests_nonce.py View on Github external
def run(device=None):
    if not device:
        raise ValueError("device")

    numbers = b'\x00' * 32

    packet = device.atcab_nonce_base(NONCE_MODE_SEED_UPDATE, numbers=numbers)
    log.debug("atcab_nonce_base NONCE_MODE_SEED_UPDATE: %s", hexlify(packet.response_data))

    packet = device.atcab_nonce_base(NONCE_MODE_NO_SEED_UPDATE, numbers=numbers)
    log.debug("atcab_nonce_base NONCE_MODE_NO_SEED_UPDATE: %s", hexlify(packet.response_data))

    packet = device.atcab_nonce_base(NONCE_MODE_PASSTHROUGH, numbers=numbers)
    log.debug("atcab_nonce_base NONCE_MODE_PASSTHROUGH: %s", hexlify(packet.response_data))

    packet = device.atcab_nonce(numbers=numbers)
    log.debug("atcab_nonce: %s", hexlify(packet.response_data))

    packet = device.atcab_nonce_load(NONCE_MODE_TARGET_TEMPKEY, numbers=numbers)
    log.debug("atcab_nonce_load NONCE_MODE_TARGET_TEMPKEY: %s", hexlify(packet.response_data))

    packet = device.atcab_nonce_load(NONCE_MODE_TARGET_MSGDIGBUF, numbers=numbers)
    log.debug("atcab_nonce_load NONCE_MODE_TARGET_MSGDIGBUF: %s", hexlify(packet.response_data))

    packet = device.atcab_nonce_load(NONCE_MODE_TARGET_ALTKEYBUF, numbers=numbers)
    log.debug("atcab_nonce_load NONCE_MODE_TARGET_ALTKEYBUF: %s", hexlify(packet.response_data))

    packet = device.atcab_nonce_rand(numbers=numbers)
    log.debug("atcab_nonce_rand: %s", hexlify(packet.response_data))
github dmazzella / ucryptoauthlib / tests / ateccX08a / tests_write.py View on Github external
def run(device=None):
    if not device:
        raise ValueError("device")

    config = _TEST_CONFIG[device.device]

    log.debug("test_config for %s : %s", device.device, hexlify(config))
    # ATEC_UTIL.dump_configuration(config)

    if not device.atcab_is_locked(ATCA_CONSTANTS.LOCK_ZONE_CONFIG):
        device.atcab_write_config_zone(config)
        device.atcab_lock_config_zone()
    else:
        log.debug("configuration zone locked")

    if not device.atcab_is_locked(ATCA_CONSTANTS.LOCK_ZONE_DATA):
        device.atcab_lock_data_zone()
    else:
        log.debug("data zone locked")

    slot = 11
    if not device.atcab_is_slot_locked(slot):
        public_key = _TEST_KEYS["PUBLIC"]
        # # write public_key to slot
        device.atcab_write_pubkey(slot, public_key)
        # # verify wrote public_key
github dmazzella / ucryptoauthlib / tests / ateccX08a / tests_read.py View on Github external
sn0_1, sn8 = packet.response_data[1:1+2], packet.response_data[9+4:9+4+1]
    assert b'\x01#' == sn0_1, hexlify(sn0_1)
    assert b'\xee' == sn8, hexlify(sn8)
    log.debug("atcab_read_serial_number: %s", hexlify(packet.response_data))

    packets = device.atcab_read_config_zone()
    config = b''.join([bytes(packet.response_data[1:-2])
                       for packet in packets])
    log.debug("atcab_read_config_zone %d: %s", len(config), hexlify(config))
    # ATEC_UTIL.dump_configuration(config)

    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