GitHub

key_agreement

API reference for key_agreement.

Key agreement as a service (Effect's effect/crypto/KeyAgreement)::

agreement = yield from KeyAgreement()
pair = yield from agreement.generate_key_pair(x25519)
secret = yield from agreement.derive_shared_secret(pair.private_key, peer_public_key)

X25519 and ECDH on P-256 / P-384 / P-521. A peer key off the curve, of another algorithm, or a low-order X25519 key giving an all-zero secret fails with InvalidKey. Feed the shared secret to kdf before using it as a key.

Re-exports

Values

AttributeTypeDescription
x25519X25519
ecdh_p256Ecdh[Literal['P-256']]
ecdh_p384Ecdh[Literal['P-384']]
ecdh_p521Ecdh[Literal['P-521']]
familyFamily
layer_web_cryptoLayer[KeyAgreement, Never, SecureRandom]

Key agreement on the WebCrypto reference; X25519 keys from SecureRandom.

Types

TypeDefinitionDescription
Algorithmtype Algorithm = X25519 | Ecdh[Any]

Classes

Functions

is_private_key

is_private_key(u) -> bool
ParameterTypeDescription
uobject

Returns bool

def is_private_key(u: object) -> bool:
    return isinstance(u, PrivateKey)

is_public_key

is_public_key(u) -> bool
ParameterTypeDescription
uobject

Returns bool

def is_public_key(u: object) -> bool:
    return isinstance(u, PublicKey)

private_key

private_key[A: X25519](algorithm, data) -> Effect[PrivateKey[A], CryptoError]

An X25519 private key from its 32 bytes.

ParameterTypeDescription
algorithmA
dataBinary

Returns Effect[PrivateKey[A], CryptoError]

def private_key[A: X25519](algorithm: A, data: Binary) -> Effect[PrivateKey[A], CryptoError]:
    """An X25519 private key from its 32 bytes."""
    return _key.from_bytes(
        lambda copied: PrivateKey(algorithm, copied),
        "KeyAgreement",
        "privateKey",
        data,
        _asymmetric.check_seed_length("X25519", "private"),
    )

public_key

public_key[A: Algorithm](algorithm, data) -> Effect[PublicKey[A], CryptoError]

An X25519 public key (32 bytes) or an uncompressed EC point.

ParameterTypeDescription
algorithmA
dataBinary

Returns Effect[PublicKey[A], CryptoError]

def public_key[A: Algorithm](algorithm: A, data: Binary) -> Effect[PublicKey[A], CryptoError]:
    """An X25519 public key (32 bytes) or an uncompressed EC point."""
    check = (
        _asymmetric.check_point(algorithm.named_curve)
        if isinstance(algorithm, Ecdh)
        else _asymmetric.check_seed_length("X25519", "public")
    )
    return _key.from_bytes(
        lambda copied: PublicKey(algorithm, copied), "KeyAgreement", "publicKey", data, check
    )

private_key_from_crypto_key

private_key_from_crypto_key[A: Algorithm](algorithm, crypto_key) -> Effect[PrivateKey[A], CryptoError]
ParameterTypeDescription
algorithmA
crypto_keyCryptoKey

Returns Effect[PrivateKey[A], CryptoError]

def private_key_from_crypto_key[A: Algorithm](
    algorithm: A, crypto_key: CryptoKey
) -> Effect[PrivateKey[A], CryptoError]:
    return _asymmetric.from_crypto_key(
        family, "privateKeyFromCryptoKey", algorithm, "private", crypto_key
    )

public_key_from_crypto_key

public_key_from_crypto_key[A: Algorithm](algorithm, crypto_key) -> Effect[PublicKey[A], CryptoError]
ParameterTypeDescription
algorithmA
crypto_keyCryptoKey

Returns Effect[PublicKey[A], CryptoError]

def public_key_from_crypto_key[A: Algorithm](
    algorithm: A, crypto_key: CryptoKey
) -> Effect[PublicKey[A], CryptoError]:
    return _asymmetric.from_crypto_key(
        family, "publicKeyFromCryptoKey", algorithm, "public", crypto_key
    )

to_bytes

to_bytes(key) -> Effect[bytes, CryptoError]
ParameterTypeDescription
keyPrivateKey[Any] | PublicKey[Any]

Returns Effect[bytes, CryptoError]

def to_bytes(key: PrivateKey[Any] | PublicKey[Any]) -> Effect[bytes, CryptoError]:
    return _asymmetric.to_bytes(family, key)