How to use the cryptoauthlib.constant function in cryptoauthlib

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
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]
        )
        self.execute(packet)
        return packet
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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:
            data_size += PKS
        elif has_other_data:
            data_size += VODS
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
def atcab_genkey_base(self, mode, key_id, other_data=None):

        txsize = 0
        if mode & ATCA_CONSTANTS.GENKEY_MODE_PUBKEY_DIGEST:
            txsize = ATCA_CONSTANTS.GENKEY_COUNT_DATA
        else:
            txsize = ATCA_CONSTANTS.GENKEY_COUNT

        has_other_data = isinstance(other_data, _BYTES_LIKE_OBJECT)
        data = other_data if has_other_data else b''

        packet = ATCAPacket(
            txsize=txsize,
            opcode=ATCA_CONSTANTS.ATCA_GENKEY,
            param1=mode,
            param2=key_id,
            request_data=data
        )
        self.execute(packet)
        return packet
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
def atcab_sign_base(self, mode, key_id):
        packet = ATCAPacket(
            txsize=ATCA_CONSTANTS.SIGN_COUNT,
            opcode=ATCA_CONSTANTS.ATCA_SIGN,
            param1=mode,
            param2=key_id
        )
        self.execute(packet)
        return packet
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
def atcab_nonce_base(self, mode, zero=0, numbers=None):
        nonce_mode = mode & ATCA_CONSTANTS.NONCE_MODE_MASK
        if nonce_mode not in (
            ATCA_CONSTANTS.NONCE_MODE_SEED_UPDATE,
            ATCA_CONSTANTS.NONCE_MODE_NO_SEED_UPDATE,
            ATCA_CONSTANTS.NONCE_MODE_PASSTHROUGH
        ):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        if not isinstance(numbers, _BYTES_LIKE_OBJECT):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        txsize = 0
        if nonce_mode in (
            ATCA_CONSTANTS.NONCE_MODE_SEED_UPDATE,
            ATCA_CONSTANTS.NONCE_MODE_NO_SEED_UPDATE
        ):
            txsize = ATCA_CONSTANTS.NONCE_COUNT_SHORT
        elif nonce_mode == ATCA_CONSTANTS.NONCE_MODE_PASSTHROUGH:
            nonce_mode_input = mode & ATCA_CONSTANTS.NONCE_MODE_INPUT_LEN_MASK
            if nonce_mode_input == ATCA_CONSTANTS.NONCE_MODE_INPUT_LEN_64:
                txsize = ATCA_CONSTANTS.NONCE_COUNT_LONG_64
            else:
                txsize = ATCA_CONSTANTS.NONCE_COUNT_LONG
        else:
            raise ATCA_EXCEPTIONS.BadArgumentError()

        n_mv = memoryview(numbers)
        if len(n_mv) < txsize-ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN:
            raise ATCA_EXCEPTIONS.BadArgumentError()

        packet = ATCAPacket(
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
def atcab_sha_base(self, mode=0, data=b'', key_slot=None):
        if not isinstance(data, _BYTES_LIKE_OBJECT):
            raise ATCA_EXCEPTIONS.BadArgumentError()

        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,
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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
        ):
github dmazzella / ucryptoauthlib / cryptoauthlib / basic.py View on Github external
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