cryptnoxpy package

Subpackages

Submodules

cryptnoxpy.binary_utils module

Utility module for handling binary data

cryptnoxpy.binary_utils.list_to_hexadecimal(data: List[int], sep: str = '') str[source]

Convert list of integers into hexadecimal representation

Parameters:
  • data (List[int]) – List of integer to return in hexadecimal string form

  • sep (str) – (optional) Separator to use to join the hexadecimal numbers

Returns:

list

Return type:

str

cryptnoxpy.binary_utils.hexadecimal_to_list(value: str) List[int][source]

Convert given string containing hexadecimal representation of numbers into list of integers

Parameters:

value (string) – String containing hexadecimal numbers

Returns:

List of hexadecimal values in integer form

Return type:

List[int]

cryptnoxpy.binary_utils.path_to_bytes(path_str: str) bytes[source]

Convert given path for format that the card uses

Parameters:

path_str (str) – path to convert

Returns:

path formatted for use with the card.

Return type:

bytes

cryptnoxpy.binary_utils.binary_to_list(data: bytes) List[int][source]

Convert given binary data to it’s representation as a list of hexadecimal values.

Parameters:

data (bytes) – Binary data to convert to

Returns:

List containing data in hexadecimal numbers in integer format

Return type:

List[int]

cryptnoxpy.binary_utils.pad_data(data: bytes) bytes[source]

Pad data with 0s to be length of 128.

Parameters:

data – Data to be padded.

Returns:

Data padded with 0s with length 128.

Return type:

bytes

cryptnoxpy.binary_utils.remove_padding(data: bytes) bytes[source]

Remove padding from the data

Parameters:

data (bytes) – Data from which to remove padding

Returns:

Data without the padding

Return type:

bytes

cryptnoxpy.connection module

Module for keeping the connection to the reader.

Sending and receiving information from the card through the reader.

class cryptnoxpy.connection.Connection(index: int = 0, debug: bool = False, conn: List = None, remote: bool = False)[source]

Bases: ContextDecorator

Connection to the reader.

Sends and receives messages from the card using the reader.

Parameters:
  • index (int) – Index of the reader to initialize the connection with

  • debug (bool) – Show debug information during requests

  • conn (List) – List of sockets to use for remote connections

  • remote (bool) – Use remote sockets for communications with the cards

Variables:

self.card (Card) – Information about the card.

__init__(index: int = 0, debug: bool = False, conn: List = None, remote: bool = False)[source]
send_apdu(apdu: List[int]) Tuple[List[int], int, int][source]

Send data to the card in plain format

Parameters:

apdu (int) – list of the APDU header

Return bytes:

Result of the query that was sent to the card

Return type:

bytes

Raises:

ConnectionException – Issue in the connection

send_encrypted(apdu: List[int], data: bytes, receive_long: bool = False) bytes[source]

Send data to the card in encrypted format

Parameters:
  • apdu (int) – list of the APDU header

  • data – bytes of the data payload (in clear, will be encrypted)

  • receive_long (bool)

Return bytes:

Result of the query that was sent to the card

Return type:

bytes

Raises:

CryptnoxException – General exceptions

remote_read(apdu: List[int]) Tuple[List[int], int, int][source]

cryptnoxpy.crypto_utils module

Module containing cryptographic methods used by the library

cryptnoxpy.crypto_utils.aes_encrypt(key: bytes, initialization_vector: bytes, data: bytes) bytes[source]

AES encrypt data using key and initialization vector.

Parameters:
  • key (bytes) – Key to use for encryption

  • initialization_vector (bytes) – Initialization vector

  • data (bytes) – Data to encrypt

Returns:

Encrypted data

Return type:

bytes

cryptnoxpy.crypto_utils.aes_decrypt(key: bytes, initialization_vector: bytes, data: bytes) bytes[source]

AES decrypt data using key and initialization vector.

Parameters:
  • key (bytes) – Key to use for encryption

  • initialization_vector (bytes) – Initialization vector

  • data (bytes) – Data to decrypt

Returns:

Decrypted data

Return type:

bytes

cryptnoxpy.enums module

Enum classes used by the module

class cryptnoxpy.enums.AuthType(*values)[source]

Bases: Enum

Predefined values for authentication type.

NO_AUTH = 0
PIN = 1
USER_KEY = 2
class cryptnoxpy.enums.Derivation(*values)[source]

Bases: IntEnum

Predefined values to use for parameters as Derivation.

CURRENT_KEY = 0
DERIVE = 1
DERIVE_AND_MAKE_CURRENT = 2
PINLESS_PATH = 3
class cryptnoxpy.enums.KeyType(*values)[source]

Bases: IntEnum

Predefined values to use for parameters as KeyType.

K1 = 0
R1 = 16
class cryptnoxpy.enums.Origin(*values)[source]

Bases: Enum

Predefined values for keeping the origin of the card

UNKNOWN = 0
ORIGINAL = 1
FAKE = 2
class cryptnoxpy.enums.SlotIndex(*values)[source]

Bases: IntEnum

Predefined values to use for parameters as SlotIndex.

EC256R1 = 1
RSA = 2
FIDO = 3
class cryptnoxpy.enums.SeedSource(*values)[source]

Bases: Enum

Predefined values for how seed was created

NO_SEED = 0
SINGLE = 75
EXTENDED = 88
EXTERNAL = 76
INTERNAL = 83
DUAL = 68
WRAPPED = 82

cryptnoxpy.exceptions module

Module containing all exceptions that CryptnoxPy module can raise.

exception cryptnoxpy.exceptions.CryptnoxException[source]

Bases: Exception

Base exception for the class exceptions.

exception cryptnoxpy.exceptions.CardClosedException[source]

Bases: Exception

The card wasn’t opened with PIN code or challenge-response

exception cryptnoxpy.exceptions.CardException[source]

Bases: CryptnoxException

No card was detected in the card reader.

exception cryptnoxpy.exceptions.CardTypeException[source]

Bases: CryptnoxException

The detected card is not supported by this library

exception cryptnoxpy.exceptions.CertificateException[source]

Bases: CryptnoxException

There was an issue with the certification

exception cryptnoxpy.exceptions.ConnectionException[source]

Bases: CryptnoxException

An issue occurred in the communication with the reader

exception cryptnoxpy.exceptions.DataException[source]

Bases: CryptnoxException

The reader returned an empty message.

exception cryptnoxpy.exceptions.DataValidationException[source]

Bases: CryptnoxException

The sent data is not valid.

exception cryptnoxpy.exceptions.DerivationSelectionException[source]

Bases: CryptnoxException

Not a valid derivation selection.

exception cryptnoxpy.exceptions.KeySelectionException[source]

Bases: CryptnoxException

Not a valid key type selection

exception cryptnoxpy.exceptions.EOSKeyError[source]

Bases: CryptnoxException

The signature wasn’t compatible with EOS standard after 10 tries

exception cryptnoxpy.exceptions.FirmwareException[source]

Bases: CryptnoxException

There is an issue with the firmware on the card

exception cryptnoxpy.exceptions.GenuineCheckException[source]

Bases: CryptnoxException

The detected card is not a genuine Cryptnox product.

exception cryptnoxpy.exceptions.GenericException(status: bytes)[source]

Bases: CryptnoxException

Generic exception that can mean multiple things depending on the call to the card

Process stats and throw a specific Exception from it.

__init__(status: bytes)[source]
exception cryptnoxpy.exceptions.InitializationException[source]

Bases: CryptnoxException

The card hasn’t been initialized.

exception cryptnoxpy.exceptions.KeyAlreadyGenerated[source]

Bases: CryptnoxException

Key can not be generated twice.

exception cryptnoxpy.exceptions.SeedException[source]

Bases: CryptnoxException

Keys weren’t found on the card.

exception cryptnoxpy.exceptions.KeyGenerationException[source]

Bases: CryptnoxException

Error in key generation.

exception cryptnoxpy.exceptions.PinAuthenticationException[source]

Bases: CryptnoxException

Error in turning off PIN authentication. There is no user key in the card

exception cryptnoxpy.exceptions.PinException(message: str = 'Invalid PIN code was provided', number_of_retries: int = 0)[source]

Bases: CryptnoxException

Sent PIN code is not valid.

Parameters:
  • number_of_retries (int) – Number of retries to send the PIN code before the card is locked.

  • message (str) – Optional message

__init__(message: str = 'Invalid PIN code was provided', number_of_retries: int = 0)[source]
exception cryptnoxpy.exceptions.PukException(message: str = 'Invalid PUK code was provided', number_of_retries: int = 0)[source]

Bases: CryptnoxException

Sent PUK code is not valid.

Parameters:
  • number_of_retries (int) – Number of retries to send the PIN code before the card is locked.

  • message (str) – Optional message

__init__(message: str = 'Invalid PUK code was provided', number_of_retries: int = 0)[source]
exception cryptnoxpy.exceptions.ReadPublicKeyException[source]

Bases: CryptnoxException

Data received during public key reading is not valid.

exception cryptnoxpy.exceptions.ReaderException[source]

Bases: CryptnoxException

Card reader wasn’t found attached to the device.

exception cryptnoxpy.exceptions.SecureChannelException[source]

Bases: CryptnoxException

Secure channel couldn’t be established.

exception cryptnoxpy.exceptions.SoftLock[source]

Bases: CryptnoxException

The card is soft locked, and requires power cycle before it can be opened

exception cryptnoxpy.exceptions.CardNotBlocked[source]

Bases: CryptnoxException

Trying to unlock unblocked card

cryptnoxpy.factory module

Module for getting Cryptnox cards information and getting instance of card from connection

cryptnoxpy.factory.get_card(connection: Connection, debug: bool = False) Base[source]

Get card instance that is using given connection.

Parameters:
  • connection (Connection) – Connection to use for operation

  • debug (bool) – Prints information about communication

Returns:

Instance of card

Return type:

Base

Raises:

CardException – Card with given serial number not found

cryptnoxpy.reader module

Module that handles different card reader types and their drivers.

exception cryptnoxpy.reader.ReaderException[source]

Bases: Exception

Reader hasn’t been found or other reader related issues

exception cryptnoxpy.reader.CardException[source]

Bases: Exception

The reader is present but there is an issue in connecting to the card

exception cryptnoxpy.reader.ConnectionException[source]

Bases: Exception

An issue has occurred in the communication with the card.

class cryptnoxpy.reader.Reader[source]

Bases: object

Abstract class describing methods to be implemented. Holds the connection.

__init__()[source]
abstractmethod connect() None[source]

Connect to the card found in the selected reader.

Returns:

None

abstractmethod send(apdu: List[int]) Tuple[List[str], int, int][source]

Send APDU to the reader and card and retrieve the result with status codes.

Parameters:

apdu (List[int]) – Command to be sent

Returns:

Return the result of the query and two status codes

Return type:

Tuple[List[str], int, int]

bool() bool[source]

Is there an active connection

Return type:

Is there an active connection

Returns:

bool

class cryptnoxpy.reader.NfcReader[source]

Bases: Reader

Specialized reader using xantares/nfc-binding

__init__()[source]
connect()[source]

Connect to the card found in the selected reader.

Returns:

None

send(apdu: List[int]) Tuple[List[str], int, int][source]

Send APDU to the reader and card and retrieve the result with status codes.

Parameters:

apdu (List[int]) – Command to be sent

Returns:

Return the result of the query and two status codes

Return type:

Tuple[List[str], int, int]

class cryptnoxpy.reader.SmartCard(index: int = 0)[source]

Bases: Reader

Generic smart card reader class

Parameters:

index (int) – Index of the reader to initialize.

__init__(index: int = 0)[source]
connect() None[source]

Connect to the card found in the selected reader.

Returns:

None

send(apdu: List[int]) Tuple[List[str], int, int][source]

Send APDU to the reader and card and retrieve the result with status codes.

Parameters:

apdu (List[int]) – Command to be sent

Returns:

Return the result of the query and two status codes

Return type:

Tuple[List[str], int, int]

cryptnoxpy.reader.get(index: int = 0) Reader[source]

Get the reader that can be found on the given position.

Parameters:

index (int) – Index of reader to be initialized and used

Returns:

Reader object that can be used.

Return type:

Reader

Module contents

This is a library for communicating with Cryptnox cards

See the README.md for API details and general information.

cryptnoxpy.Card

alias of Base

class cryptnoxpy.Connection(index: int = 0, debug: bool = False, conn: List = None, remote: bool = False)[source]

Bases: ContextDecorator

Connection to the reader.

Sends and receives messages from the card using the reader.

Parameters:
  • index (int) – Index of the reader to initialize the connection with

  • debug (bool) – Show debug information during requests

  • conn (List) – List of sockets to use for remote connections

  • remote (bool) – Use remote sockets for communications with the cards

Variables:

self.card (Card) – Information about the card.

__init__(index: int = 0, debug: bool = False, conn: List = None, remote: bool = False)[source]
remote_read(apdu: List[int]) Tuple[List[int], int, int][source]
send_apdu(apdu: List[int]) Tuple[List[int], int, int][source]

Send data to the card in plain format

Parameters:

apdu (int) – list of the APDU header

Return bytes:

Result of the query that was sent to the card

Return type:

bytes

Raises:

ConnectionException – Issue in the connection

send_encrypted(apdu: List[int], data: bytes, receive_long: bool = False) bytes[source]

Send data to the card in encrypted format

Parameters:
  • apdu (int) – list of the APDU header

  • data – bytes of the data payload (in clear, will be encrypted)

  • receive_long (bool)

Return bytes:

Result of the query that was sent to the card

Return type:

bytes

Raises:

CryptnoxException – General exceptions

class cryptnoxpy.SlotIndex(*values)[source]

Bases: IntEnum

Predefined values to use for parameters as SlotIndex.

EC256R1 = 1
RSA = 2
FIDO = 3
class cryptnoxpy.Derivation(*values)[source]

Bases: IntEnum

Predefined values to use for parameters as Derivation.

CURRENT_KEY = 0
DERIVE = 1
DERIVE_AND_MAKE_CURRENT = 2
PINLESS_PATH = 3
class cryptnoxpy.KeyType(*values)[source]

Bases: IntEnum

Predefined values to use for parameters as KeyType.

K1 = 0
R1 = 16
class cryptnoxpy.AuthType(*values)[source]

Bases: Enum

Predefined values for authentication type.

NO_AUTH = 0
PIN = 1
USER_KEY = 2
class cryptnoxpy.SeedSource(*values)[source]

Bases: Enum

Predefined values for how seed was created

NO_SEED = 0
SINGLE = 75
EXTENDED = 88
EXTERNAL = 76
INTERNAL = 83
DUAL = 68
WRAPPED = 82
class cryptnoxpy.Origin(*values)[source]

Bases: Enum

Predefined values for keeping the origin of the card

UNKNOWN = 0
ORIGINAL = 1
FAKE = 2

The cryptnoxpy package is a library for communicating with Cryptnox cards. It exports:

class cryptnoxpy.Card

Main card interface class. Alias for cryptnoxpy.card.base.Base.

class cryptnoxpy.Connection[source]

Connection handler for card communication. See cryptnoxpy.connection.Connection for details.

Factory module for creating card instances. See cryptnoxpy.factory for details.

Enumeration types module. See cryptnoxpy.enums for details.

Exception classes module. See cryptnoxpy.exceptions for details.

class exceptions.SlotIndex

Card slot index enumeration. See cryptnoxpy.enums.SlotIndex for details.

class exceptions.Derivation

Key derivation method enumeration. See cryptnoxpy.enums.Derivation for details.

class exceptions.KeyType

Cryptographic key type enumeration. See cryptnoxpy.enums.KeyType for details.

class exceptions.AuthType

Authentication type enumeration. See cryptnoxpy.enums.AuthType for details.

class exceptions.SeedSource

Seed source enumeration. See cryptnoxpy.enums.SeedSource for details.

class exceptions.Origin

Origin enumeration. See cryptnoxpy.enums.Origin for details.

exceptions.__version__: str = "2.5.5"

Current version of the cryptnoxpy library.