libp2p.crypto package

Subpackages

Submodules

libp2p.crypto.authenticated_encryption module

class libp2p.crypto.authenticated_encryption.EncryptionParameters(cipher_type: str, hash_type: str, iv: bytes, mac_key: bytes, cipher_key: bytes)

Bases: object

cipher_key: bytes
cipher_type: str
hash_type: str
iv: bytes
mac_key: bytes
exception libp2p.crypto.authenticated_encryption.InvalidMACException

Bases: Exception

class libp2p.crypto.authenticated_encryption.MacAndCipher(parameters: EncryptionParameters)

Bases: object

authenticate(data: bytes) bytes
decrypt_if_valid(data_with_tag: bytes) bytes
encrypt(data: bytes) bytes
libp2p.crypto.authenticated_encryption.initialize_pair(cipher_type: str, hash_type: str, secret: bytes) tuple[EncryptionParameters, EncryptionParameters]

Return a pair of Keys for use in securing a communications channel with authenticated encryption derived from the secret and using the requested cipher_type and hash_type.

libp2p.crypto.ecc module

class libp2p.crypto.ecc.ECCPrivateKey(impl: int, curve: <MagicMock name='mock.curve.Curve' id='132160889003488'>)

Bases: PrivateKey

get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(curve: str) ECCPrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.ecc.ECCPublicKey(impl: <MagicMock name='mock.point.Point' id='132160889008096'>, curve: <MagicMock name='mock.curve.Curve' id='132160889003488'>)

Bases: PublicKey

classmethod from_bytes(data: bytes, curve: str) ECCPublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.ecc.create_new_key_pair(curve: str) KeyPair

Return a new ECC keypair with the requested curve type, e.g. “P-256”.

libp2p.crypto.ecc.infer_local_type(curve: str) <MagicMock name='mock.curve.Curve' id='132160889003488'>

Convert a str representation of some elliptic curve to a representation understood by the backend of this module.

libp2p.crypto.ed25519 module

Ed25519 cryptographic key implementation.

This module provides Ed25519 public and private key implementations using the PyNaCl library. Ed25519 is used for libp2p peer identity signatures.

class libp2p.crypto.ed25519.Ed25519PrivateKey(impl: SigningKey)

Bases: PrivateKey

Ed25519 private key implementation.

Provides cryptographic operations for Ed25519 private keys including signature generation and public key derivation. Used for libp2p peer identity signing.

classmethod from_bytes(data: bytes) Ed25519PrivateKey
get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(seed: bytes | None = None) Ed25519PrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.ed25519.Ed25519PublicKey(impl: VerifyKey)

Bases: PublicKey

Ed25519 public key implementation.

Provides cryptographic operations for Ed25519 public keys including signature verification. Used for libp2p peer identity verification.

classmethod from_bytes(key_bytes: bytes) Ed25519PublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.ed25519.create_new_key_pair(seed: bytes | None = None) KeyPair

libp2p.crypto.exceptions module

exception libp2p.crypto.exceptions.CryptographyError

Bases: BaseLibp2pError

exception libp2p.crypto.exceptions.MissingDeserializerError

Bases: CryptographyError

Raise if the requested deserialization routine is missing for some type of cryptographic key.

libp2p.crypto.key_exchange module

libp2p.crypto.key_exchange.create_ephemeral_key_pair(curve_type: str) tuple[PublicKey, Callable[[bytes], bytes]]

Facilitates ECDH key exchange.

libp2p.crypto.keys module

Key types and interfaces.

class libp2p.crypto.keys.Key

Bases: ABC

A Key represents a cryptographic key.

abstract get_type() KeyType

Returns the KeyType for self.

abstract to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.keys.KeyPair(private_key: libp2p.crypto.keys.PrivateKey, public_key: libp2p.crypto.keys.PublicKey)

Bases: object

private_key: PrivateKey
public_key: PublicKey
class libp2p.crypto.keys.KeyType(value)

Bases: Enum

An enumeration.

ECC_P256 = 4
ECDSA = 3
Ed25519 = 1
RSA = 0
Secp256k1 = 2
X25519 = 5
class libp2p.crypto.keys.PrivateKey

Bases: Key

A PrivateKey represents a cryptographic private key.

classmethod deserialize_from_protobuf(protobuf_data: bytes) PrivateKey
abstract get_public_key() PublicKey
serialize() bytes

Return the canonical serialization of this Key.

abstract sign(data: bytes) bytes
class libp2p.crypto.keys.PublicKey

Bases: Key

A PublicKey represents a cryptographic public key.

classmethod deserialize_from_protobuf(protobuf_data: bytes) PublicKey
serialize() bytes

Return the canonical serialization of this Key.

abstract verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.rsa module

class libp2p.crypto.rsa.RSAPrivateKey(impl: RsaKey)

Bases: PrivateKey

get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(bits: int = 2048, e: int = 65537) RSAPrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.rsa.RSAPublicKey(impl: RsaKey)

Bases: PublicKey

classmethod from_bytes(key_bytes: bytes) RSAPublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.rsa.create_new_key_pair(bits: int = 2048, e: int = 65537) KeyPair

Returns a new RSA keypair with the requested key size (bits) and the given public exponent e.

Sane defaults are provided for both values.

libp2p.crypto.rsa.validate_rsa_key_length(key_length: int) None

Validate that the RSA key length is positive and within the allowed maximum.

Parameters:

key_length – RSA key size in bits.

Raises:

CryptographyError – If the key size is not positive or exceeds MAX_RSA_KEY_SIZE.

libp2p.crypto.rsa.validate_rsa_key_size(key: RsaKey) None

Validate that an RSA key’s size is within acceptable bounds.

Parameters:

key – The RSA key to validate.

Raises:

CryptographyError – If the key size is invalid.

libp2p.crypto.secp256k1 module

class libp2p.crypto.secp256k1.Secp256k1PrivateKey(impl: PrivateKey)

Bases: PrivateKey

classmethod deserialize(data: bytes) Secp256k1PrivateKey
classmethod from_bytes(data: bytes) Secp256k1PrivateKey
get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(secret: bytes | None = None) Secp256k1PrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.secp256k1.Secp256k1PublicKey(impl: PublicKey)

Bases: PublicKey

classmethod deserialize(data: bytes) Secp256k1PublicKey
classmethod from_bytes(data: bytes) Secp256k1PublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.secp256k1.create_new_key_pair(secret: bytes | None = None) KeyPair

Returns a new Secp256k1 keypair derived from the provided secret, a sequence of bytes corresponding to some integer between 0 and the group order.

A valid secret is created if None is passed.

libp2p.crypto.serialization module

libp2p.crypto.serialization.deserialize_private_key(data: bytes) PrivateKey
libp2p.crypto.serialization.deserialize_public_key(data: bytes) PublicKey

Module contents