class

Beryl::Crypto::Compatibility::AdvancedEncryptionStandardInCipherBlockChainingMode

Inherits Reference < Object

AES in cipher block chaining mode with PKCS#7 padding (FIPS 197, NIST SP 800-38A §6.2, RFC 5652 §6.3), under a 16, 24 or 32-byte key.

CBC keeps a message private and does nothing to prove it is whole: anyone can alter the ciphertext, and a receiver that reports bad padding helps an attacker read it. It is here to read and write formats that already use it (Amber's version 1 cookies, Grant's version 1 encrypted attributes), which authenticate the ciphertext with HMAC before they decrypt. New designs use Beryl::Crypto.encrypt.

cipher = Beryl::Crypto::Compatibility::AESCBC.new(key)
ciphertext = cipher.encrypt("a private note", initialization_vector: initialization_vector)
cipher.decrypt(ciphertext, initialization_vector: initialization_vector) # => the note's bytes, or nil

Constants

BLOCK_BYTESIZE = 16

Constructors

new(key : Bytes)

Creates the cipher under key (16, 24 or 32 bytes).

Source

Instance methods

decrypt(ciphertext : Bytes, *, initialization_vector : Bytes) : Bytes | Nil

Returns the plaintext of ciphertext, or nil when the ciphertext is empty, is not a whole number of blocks, or decrypts to malformed padding. The padding is checked in constant time; whether it was well formed, and how long it was, is what this method returns.

Source
decrypt(ciphertext : Bytes, *, into padded_plaintext : Bytes, initialization_vector : Bytes) : Int32 | Nil

Decrypts ciphertext into padded_plaintext, which must be as long, and returns how many of its first bytes are the plaintext; or returns nil, with padded_plaintext wiped, when the ciphertext is empty, is not a whole number of blocks, or decrypts to malformed padding.

Source
encrypt(plaintext : Bytes | String, *, initialization_vector : Bytes) : Bytes

Returns plaintext, padded to a whole number of blocks, encrypted under initialization_vector (16 bytes, fresh and unpredictable for every message).

Source
encrypt(plaintext : Bytes | String, *, into ciphertext : Bytes, initialization_vector : Bytes) : Nil

Encrypts plaintext into ciphertext, which must hold the padded length: padded_bytesize(plaintext.size) bytes.

Source
padded_bytesize(plaintext_bytesize : Int32) : Int32

The length of the ciphertext of a plaintext_bytesize-byte message: padding takes it to the next whole block, adding a block when it is whole already.

Source