Module: XStream::X25519HKDF

Defined in:
lib/xstream/x25519hkdf.rb

Overview

XSTREAM with X25519 key agreement and HKDF as the key derivation function

Defined Under Namespace

Classes: Decryptor, Encryptor

Constant Summary collapse

HKDF_INFO =

Domain separation string passed as HKDF info

"XSTREAM_X25519_HKDF".freeze
SYMMETRIC_KEY_SIZE =

Size of an AES-128 key * 2 (for SIV mode)

32

Class Method Summary collapse

Class Method Details

.kdf(private_key, public_key, output_size:, salt: nil, digest_alg: "SHA-256") ⇒ Object

Derive a symmetric encryption key from the combination of a public and private key and salt using X25519 D-H and HKDF

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/xstream/x25519hkdf.rb', line 79

def self.kdf(private_key, public_key, output_size:, salt: nil, digest_alg: "SHA-256")
  raise ArgumentError, "invalid digest_alg: #{digest_alg}" unless digest_alg == "SHA-256"

  # Use X25519 to compute a shared secret
  shared_secret = X25519.diffie_hellman(private_key, public_key)

  # Use HKDF to derive a symmetric encryption key from the shared secret
  ::HKDF.new(
    shared_secret,
    salt: salt,
    info: HKDF_INFO,
    algorithm: "SHA256"
  ).next_bytes(output_size)
end