Key exchange

Purpose

It is generally difficult to share a symmetric key with another party because it needs to remain secret. Fortunately, a sender and recipient can exchange public keys, which do not need to be secret.

Both parties can then compute the same shared secret using their private key and the other party's public key. This shared secret can be turned into a symmetric key (e.g. for encryption).

For post-quantum securityarrow-up-right, a pre-shared key can also be shared between the two parties via a secure (e.g. encrypted) channel.

triangle-exclamation
circle-info

You often want to include ephemeral (one-time) key pairs in a key exchange as well. See the Notes for examples of how to do this.

Usage

GenerateKeyPair

Fills a span with a randomly generated private key and another span with the associated public key.

X25519.GenerateKeyPair(Span<byte> publicKey, Span<byte> privateKey)

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

publicKey has a length not equal to PublicKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

privateKey has a length not equal to PrivateKeySize.

CryptographicExceptionarrow-up-right

Unable to generate key pair.

GenerateKeyPair

Fills a span with a private key generated using a random seed and another span with the associated public key.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

publicKey has a length not equal to PublicKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

privateKey has a length not equal to PrivateKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

seed has a length not equal to SeedSize.

CryptographicExceptionarrow-up-right

Unable to generate key pair from seed.

ComputePublicKey

Fills a span with the public key computed from a private key.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

publicKey has a length not equal to PublicKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

privateKey has a length not equal to PrivateKeySize.

CryptographicExceptionarrow-up-right

Unable to compute public key from private key.

DeriveSenderSharedKey

Fills a span with the shared key for the sender using their private key, a recipient public key, and an optional pre-shared key.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

sharedKey has a length not equal to SharedKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

senderPrivateKey has a length not equal to PrivateKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

recipientPublicKey has a length not equal to PublicKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

If specified, preSharedKey has a length less than MinPreSharedKeySize or greater than MaxPreSharedKeySize.

CryptographicExceptionarrow-up-right

Invalid recipient public key or unable to compute hash.

DeriveRecipientSharedKey

Fills a span with the shared key for the recipient using their private key, the sender's public key, and an optional pre-shared key.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

sharedKey has a length not equal to SharedKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

recipientPrivateKey has a length not equal to PrivateKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

senderPublicKey has a length not equal to PublicKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

If specified, preSharedKey has a length less than MinPreSharedKeySize or greater than MaxPreSharedKeySize.

CryptographicExceptionarrow-up-right

Invalid sender public key or unable to compute hash.

ComputeSharedSecret

Fills a span with the unhashed X25519 shared secret for a given sender private key and recipient public key.

triangle-exclamation

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

sharedSecret has a length not equal to SharedSecretSize.

ArgumentOutOfRangeExceptionarrow-up-right

senderPrivateKey has a length not equal to PrivateKeySize.

ArgumentOutOfRangeExceptionarrow-up-right

recipientPublicKey has a length not equal to PublicKeySize.

CryptographicExceptionarrow-up-right

Invalid public key.

Constants

These are used for validation and/or save you defining your own constants.

Notes

circle-check
circle-exclamation
circle-exclamation
triangle-exclamation

Non-Interactive Patterns

These are one-way patterns, so no back and forth between the sender and recipient is required. They are appropriate for offline applications (e.g. a file encryption program).

Authenticated Key Exchange

The recipient knows who sent the message, and only the recipient can decrypt the message. This can be achieved using the K patternarrow-up-right. The security properties are discussed herearrow-up-right.

  1. The sender generates an ephemeral key pair.

  2. The sender computes an ephemeral shared secret using their ephemeral private key and the recipient's public key. The ephemeral private key is then erased from memory.

  3. The sender also computes a long-term shared secret using their long-term private key and the recipient's public key.

  4. The sender concatenates the ephemeral shared secret and long-term shared secret to form the input keying material for a KDF. The output keying material is used as the key to encrypt a message using an AEAD.

  5. The sender's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the recipient.

  6. The recipient reads the ephemeral public key, computes the ephemeral shared secret using their private key and the sender's ephemeral public key, computes the long-term shared secret using their private key and the sender's long-term public key, derives the encryption key using the same KDF, and decrypts the ciphertext.

The X patternarrow-up-right can also be used, which is identical except that the sender's long-term public key is sent with the message under encryption instead of already being known to the recipient. The security properties are discussed herearrow-up-right.

Deniable Authenticated Key Exchange

The recipient knows who sent the message, but both parties can decrypt the message. This means either party could have encrypted the message.

circle-exclamation
  1. The sender computes a shared secret using their long-term private key and the recipient's public key.

  2. The sender uses the shared secret as the input keying material for a KDF. The output keying material is used as the key to encrypt a message using an AEAD.

  3. The recipient computes the shared secret using their private key and the sender's public key, derives the encryption key using the same KDF, and decrypts the ciphertext.

Anonymous Key Exchange

The recipient does not know who sent the message, and only the recipient can decrypt the message. This can be accomplished using the N patternarrow-up-right. The security properties are discussed herearrow-up-right.

circle-exclamation
  1. The sender generates an ephemeral key pair.

  2. The sender computes the shared secret using their ephemeral private key and the recipient's public key. The ephemeral private key is then erased from memory.

  3. The sender uses the shared secret as input keying material to a KDF. The output keying material is used as the key to encrypt a message using an AEAD.

  4. The sender's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the recipient.

  5. The recipient reads the ephemeral public key, computes the shared secret using their private key and the sender's ephemeral public key, derives the encryption key using the same KDF, and decrypts the ciphertext.

Interactive Patterns

These are two-way patterns, so back and forth between the sender and recipient is required. They are appropriate for online applications and can provide betterarrow-up-right security properties than the one-way patterns.

Authenticated Key Exchange

The recipient knows who sent the message, and you eventuallyarrow-up-right benefit from resistance to key compromise impersonation and strong forward secrecy. This can be achieved using the KK patternarrow-up-right.

  1. The sender generates an ephemeral key pair.

  2. The sender computes an ephemeral shared secret using their ephemeral private key and the recipient's long-term public key.

  3. The sender also computes a long-term shared secret using their long-term private key and the recipient's long-term public key.

  4. The sender concatenates the ephemeral shared secret and long-term shared secret to form the input keying material for a KDF. The output keying material is used as the key to encrypt a message using an AEAD.

  5. The sender's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the recipient.

  6. The recipient reads the ephemeral public key, computes the ephemeral shared secret using their private key and the sender's ephemeral public key, computes the long-term shared secret using their private key and the sender's long-term public key, derives the encryption key using the same KDF, and decrypts the ciphertext.

  7. The recipient generates an ephemeral key pair.

  8. The recipient computes an ephemeral shared secret using their ephemeral private key and the sender's ephemeral public key.

  9. The recipient computes another ephemeral shared secret using their ephemeral private key and the sender's long-term public key. The ephemeral private key is then erased from memory.

  10. The recipient concatenates both ephemeral shared secrets to form the input keying material for a KDF. Importantly, the previous derived encryption key is included in this KDF usage (as a salt by the spec, but the info parameter could be used). The output keying material is used as the key to encrypt a message using an AEAD.

  11. The recipient's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the sender.

  12. The sender reads the ephemeral public key, computes the ephemeral shared secret using their ephemeral private key and the sender's ephemeral public key, erases their ephemeral private key, computes the other shared secret using their long-term private key and the recipient's ephemeral public key, and then derives the new encryption key in the same way the recipient did, which allows them to decrypt the ciphertext.

  13. Subsequent messages can be encrypted using the same key and an incremented nonce.

Anonymous Key Exchange

The recipient does not know who sent the message. This can be accomplished using the NK patternarrow-up-right. The security properties are discussed herearrow-up-right.

  1. The sender generates an ephemeral key pair.

  2. The sender computes the shared secret using their ephemeral private key and the recipient's public key.

  3. The sender uses the shared secret as input keying material to a KDF. The output keying material is used as the key to encrypt a message using an AEAD.

  4. The sender's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the recipient.

  5. The recipient reads the ephemeral public key, computes the shared secret using their private key and the sender's ephemeral public key, derives the encryption key using the same KDF, and decrypts the ciphertext.

  6. The recipient generates an ephemeral key pair.

  7. The recipient computes an ephemeral shared secret using their ephemeral private key and the sender's ephemeral public key. The ephemeral private key is then erased from memory.

  8. The recipient uses the ephemeral shared secret as input keying material to a KDF. Importantly, the previous derived encryption key is included in this KDF usage (as a salt by the spec, but the info parameter could be used). The output keying material is used as the key to encrypt a message using an AEAD.

  9. The recipient's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the sender.

  10. The sender reads the ephemeral public key, computes the ephemeral shared secret using their ephemeral private key and the sender's ephemeral public key, erases their ephemeral private key, and then derives the new encryption key in the same way the recipient did, which allows them to decrypt the ciphertext.

  11. Subsequent messages can be encrypted using the same key and an incremented nonce.

Last updated