module

Beryl::Crypto

Cryptography written in Crystal.

The calls on this module always use the algorithm Beryl recommends today:

Beryl::Crypto.digest("contents of a file")                             # SHA-256
tag = Beryl::Crypto.authenticate("message", key: secret_key)           # HMAC-SHA256
Beryl::Crypto.verify_authentication("message", tag, key: secret_key)   # => true
Beryl::Crypto.derive_key(shared_secret, info: "cookies", bytesize: 32) # HKDF-SHA256
sealed = Beryl::Crypto.encrypt("a private note", key: secret_key)      # XChaCha20-Poly1305
Beryl::Crypto.decrypt(sealed, key: secret_key)                         # => the note, or nil
signing_key = Beryl::Crypto.generate_signing_key                       # Ed25519
signature = Beryl::Crypto.sign("a message", signing_key: signing_key)
Beryl::Crypto.verify_signature("a message", signature, verification_key: signing_key.verification_key) # => true
mine = Beryl::Crypto.generate_key_agreement_key                                                        # X25519
Beryl::Crypto.agree_on_shared_secret(mine, peer_public_key)                                            # => 32 bytes, or nil

Beryl::Crypto::V1 names each algorithm exactly, for code that must keep producing the same bytes. Beryl::Crypto::Compatibility holds MD5, SHA-1, PBKDF2 and AES-CBC for formats that require them.

Constants

ENCRYPTED_MESSAGE_NONCE_BYTESIZE = V1::XChaCha20Poly1305::NONCE_BYTESIZE

The size of the random nonce encrypt places in front of each message.

Class methods

agree_on_shared_secret(own_key : V1::X25519::PrivateKey, peer_public_key : V1::X25519::PublicKey) : Bytes | Nil

Returns the 32-byte X25519 shared secret of own_key and peer_public_key, or nil when the peer sent a point of small order. Derive keys from it with derive_key; never use it as a key directly.

Source
authenticate(message : Bytes | String, *, key : Bytes | String) : Bytes

Returns the HMAC-SHA256 authentication tag of message under key (32 bytes).

Source
decrypt(encrypted_message : Bytes, *, key : Bytes, associated_data : Bytes | String = Bytes.empty) : Bytes | Nil

Returns the message inside encrypted_message (the result of encrypt), or nil when it was altered, was encrypted under another key, or goes with other associated data.

Source
derive_key(input_key_material : Bytes | String, *, salt : Bytes | String = Bytes.empty, info : Bytes | String = Bytes.empty, bytesize : Int32 = 32) : Bytes

Returns bytesize bytes of key derived from input_key_material with HKDF-SHA256. info names the purpose, so that different purposes get unrelated keys; salt is optional and need not be secret.

Source
digest(data : Bytes | String) : Bytes

Returns the SHA-256 digest of data (32 bytes).

Source
encrypt(message : Bytes | String, *, key : Bytes, associated_data : Bytes | String = Bytes.empty) : Bytes

Returns message encrypted and authenticated under key (32 bytes) with XChaCha20-Poly1305: a fresh random nonce (24 bytes), then the ciphertext, then the tag (16 bytes). associated_data is authenticated and left out of the result; decrypt must be given the same bytes.

Source
generate_key_agreement_key

Returns a new X25519 key agreement key (RFC 7748). Its public_key is the half to send to the peer.

Source
generate_signing_key

Returns a new Ed25519 signing key (RFC 8032). Its verification_key is the public half to hand out.

Source
sign(message : Bytes | String, *, signing_key : V1::Ed25519::SigningKey) : Bytes

Returns the Ed25519 signature of message (64 bytes).

Source
verify_authentication(message : Bytes | String, authentication_tag : Bytes, *, key : Bytes | String) : Bool

Returns whether authentication_tag is the HMAC-SHA256 tag of message under key. The comparison runs in constant time.

Source
verify_signature(message : Bytes | String, signature : Bytes, *, verification_key : V1::Ed25519::VerificationKey) : Bool

Returns whether signature is the Ed25519 signature of message under verification_key.

Source

Nested types