Table of Contents

Class CryptoKeyExchange

Namespace
LibSodium
Assembly
LibSodium.Net.dll

Provides methods for secure, fast, and simple key exchange using libsodium's crypto_kx API. Allows two parties to derive shared session keys securely.

public static class CryptoKeyExchange
Inheritance
CryptoKeyExchange
Inherited Members

Remarks

🧂 Based on libsodium's crypto_kx API: https://doc.libsodium.org/key_exchange

Fields

PublicKeyLen

Length of the public key in bytes (32).

public const int PublicKeyLen = 32

Field Value

int

SecretKeyLen

Length of the secret (private) key in bytes (32).

public const int SecretKeyLen = 32

Field Value

int

SeedLen

Length of the seed used for deterministic key pair generation (32 bytes).

public const int SeedLen = 32

Field Value

int

SessionKeyLen

Length of derived session keys in bytes (32).

public const int SessionKeyLen = 32

Field Value

int

Methods

DeriveClientSessionKeys(SecureMemory<byte>, SecureMemory<byte>, ReadOnlySpan<byte>, SecureMemory<byte>, ReadOnlySpan<byte>)

Derives client-side session keys for secure communication with a server. The generated keys allow secure and authenticated data exchange.

public static void DeriveClientSessionKeys(SecureMemory<byte> rx, SecureMemory<byte> tx, ReadOnlySpan<byte> clientPk, SecureMemory<byte> clientSk, ReadOnlySpan<byte> serverPk)

Parameters

rx SecureMemory<byte>

Buffer to receive the client's receiving key (used to decrypt data from server). Must be exactly 32 bytes.

tx SecureMemory<byte>

Buffer to receive the client's transmitting key (used to encrypt data sent to server). Must be exactly 32 bytes.

clientPk ReadOnlySpan<byte>

Client's public key (32 bytes).

clientSk SecureMemory<byte>

Client's secret key (32 bytes).

serverPk ReadOnlySpan<byte>

Server's public key (32 bytes).

Exceptions

ArgumentException

Thrown if any provided buffer (rx, tx, clientPk, clientSk, serverPk) is not exactly 32 bytes.

LibSodiumException

Thrown if client-side session key derivation fails internally.

DeriveClientSessionKeys(Span<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Derives client-side session keys for secure communication with a server. The generated keys allow secure and authenticated data exchange.

public static void DeriveClientSessionKeys(Span<byte> rx, Span<byte> tx, ReadOnlySpan<byte> clientPk, ReadOnlySpan<byte> clientSk, ReadOnlySpan<byte> serverPk)

Parameters

rx Span<byte>

Buffer to receive the client's receiving key (used to decrypt data from server). Must be exactly 32 bytes.

tx Span<byte>

Buffer to receive the client's transmitting key (used to encrypt data sent to server). Must be exactly 32 bytes.

clientPk ReadOnlySpan<byte>

Client's public key (32 bytes).

clientSk ReadOnlySpan<byte>

Client's secret key (32 bytes).

serverPk ReadOnlySpan<byte>

Server's public key (32 bytes).

Exceptions

ArgumentException

Thrown if any provided buffer (rx, tx, clientPk, clientSk, serverPk) is not exactly 32 bytes.

LibSodiumException

Thrown if client-side session key derivation fails internally.

DeriveServerSessionKeys(SecureMemory<byte>, SecureMemory<byte>, ReadOnlySpan<byte>, SecureMemory<byte>, ReadOnlySpan<byte>)

Derives server-side session keys for secure communication with a client. The generated keys allow secure and authenticated data exchange.

public static void DeriveServerSessionKeys(SecureMemory<byte> rx, SecureMemory<byte> tx, ReadOnlySpan<byte> serverPk, SecureMemory<byte> serverSk, ReadOnlySpan<byte> clientPk)

Parameters

rx SecureMemory<byte>

Buffer to receive the server's receiving key (used to decrypt data from client). Must be exactly 32 bytes.

tx SecureMemory<byte>

Buffer to receive the server's transmitting key (used to encrypt data sent to client). Must be exactly 32 bytes.

serverPk ReadOnlySpan<byte>

Server's public key (32 bytes).

serverSk SecureMemory<byte>

Server's secret key (32 bytes).

clientPk ReadOnlySpan<byte>

Client's public key (32 bytes).

Exceptions

ArgumentException

Thrown if any provided buffer (rx, tx, serverPk, serverSk, clientPk) is not exactly 32 bytes.

LibSodiumException

Thrown if server-side session key derivation fails internally.

DeriveServerSessionKeys(Span<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Derives server-side session keys for secure communication with a client. The generated keys allow secure and authenticated data exchange.

public static void DeriveServerSessionKeys(Span<byte> rx, Span<byte> tx, ReadOnlySpan<byte> serverPk, ReadOnlySpan<byte> serverSk, ReadOnlySpan<byte> clientPk)

Parameters

rx Span<byte>

Buffer to receive the server's receiving key (used to decrypt data from client). Must be exactly 32 bytes.

tx Span<byte>

Buffer to receive the server's transmitting key (used to encrypt data sent to client). Must be exactly 32 bytes.

serverPk ReadOnlySpan<byte>

Server's public key (32 bytes).

serverSk ReadOnlySpan<byte>

Server's secret key (32 bytes).

clientPk ReadOnlySpan<byte>

Client's public key (32 bytes).

Exceptions

ArgumentException

Thrown if any provided buffer (rx, tx, serverPk, serverSk, clientPk) is not exactly 32 bytes.

LibSodiumException

Thrown if server-side session key derivation fails internally.

GenerateKeyPair(Span<byte>, SecureMemory<byte>)

Generates a new random key pair suitable for key exchange (crypto_kx).

public static void GenerateKeyPair(Span<byte> publicKey, SecureMemory<byte> secretKey)

Parameters

publicKey Span<byte>

Buffer to receive the generated public key. Must be exactly 32 bytes.

secretKey SecureMemory<byte>

Buffer to receive the generated secret key. Must be exactly 32 bytes.

Exceptions

ArgumentException

Thrown if publicKey or secretKey are not exactly 32 bytes.

LibSodiumException

Thrown if key pair generation fails internally.

GenerateKeyPair(Span<byte>, Span<byte>)

Generates a new random key pair suitable for key exchange (crypto_kx).

public static void GenerateKeyPair(Span<byte> publicKey, Span<byte> secretKey)

Parameters

publicKey Span<byte>

Buffer to receive the generated public key. Must be exactly 32 bytes.

secretKey Span<byte>

Buffer to receive the generated secret key. Must be exactly 32 bytes.

Exceptions

ArgumentException

Thrown if publicKey or secretKey are not exactly 32 bytes.

LibSodiumException

Thrown if key pair generation fails internally.

GenerateKeyPairDeterministically(Span<byte>, SecureMemory<byte>, SecureMemory<byte>)

Deterministically generates a key pair from a provided seed. This method always produces the same key pair for the same seed.

public static void GenerateKeyPairDeterministically(Span<byte> publicKey, SecureMemory<byte> secretKey, SecureMemory<byte> seed)

Parameters

publicKey Span<byte>

Buffer to receive the derived public key. Must be exactly 32 bytes.

secretKey SecureMemory<byte>

Buffer to receive the derived secret key. Must be exactly 32 bytes.

seed SecureMemory<byte>

Seed used for deterministic generation. Must be exactly 32 bytes.

Exceptions

ArgumentException

Thrown if publicKey, secretKey, or seed are not exactly 32 bytes.

LibSodiumException

Thrown if deterministic key pair generation fails internally.

GenerateKeyPairDeterministically(Span<byte>, Span<byte>, ReadOnlySpan<byte>)

Deterministically generates a key pair from a provided seed. This method always produces the same key pair for the same seed.

public static void GenerateKeyPairDeterministically(Span<byte> publicKey, Span<byte> secretKey, ReadOnlySpan<byte> seed)

Parameters

publicKey Span<byte>

Buffer to receive the derived public key. Must be exactly 32 bytes.

secretKey Span<byte>

Buffer to receive the derived secret key. Must be exactly 32 bytes.

seed ReadOnlySpan<byte>

Seed used for deterministic generation. Must be exactly 32 bytes.

Exceptions

ArgumentException

Thrown if publicKey, secretKey, or seed are not exactly 32 bytes.

LibSodiumException

Thrown if deterministic key pair generation fails internally.