ddn.crypto.cipher.modes

Block Cipher Modes of Operation.

Types 8

classCBC(BlockCipherT) : Cipher

Cipher Block Chaining (CBC) Mode.

Note

this mode invokes the underlying block cipher with the same

buffer as input and output (encrypt(block, block)), so BlockCipherT must support in-place operation.

Parameters

BlockCipherTThe block cipher implementation (e.g., AES).
Fields
private BlockCipherT _cipher
private Padding _padding
private CipherOperation _op
private ubyte[] _buffer
private ubyte[] _iv
private ubyte[] _lastBlock
private bool _initialized
Methods
void initialize(CipherOperation op, SecretKey key, const(ubyte)[] iv)
void init(CipherOperation op, Key key, const(ubyte)[] iv = null)
ubyte[] update(const(ubyte)[] input)
ubyte[] finish(const(ubyte)[] input = null)
Constructors
this(Padding padding = new PKCS7Padding())Constructs a CBC mode cipher.
classCTR(BlockCipherT) : Cipher

Counter (CTR) Mode.

Turns a block cipher into a stream cipher.

Fields
private BlockCipherT _cipher
private ubyte[] _counter
private ubyte[] _keystream
private size_t _keystreamIdx
private bool _initialized
Methods
void initialize(CipherOperation op, SecretKey key, const(ubyte)[] iv)
void init(CipherOperation op, Key key, const(ubyte)[] iv = null)
ubyte[] update(const(ubyte)[] input)
ubyte[] finish(const(ubyte)[] input = null)
classECB(BlockCipherT) : Cipher

Electronic Codebook (ECB) Mode.

Fields
private BlockCipherT _cipher
private Padding _padding
private CipherOperation _op
private ubyte[] _buffer
private bool _initialized
Methods
void initialize(CipherOperation op, SecretKey key, const(ubyte)[] iv)
void init(CipherOperation op, Key key, const(ubyte)[] iv = null)
ubyte[] update(const(ubyte)[] input)
ubyte[] finish(const(ubyte)[] input = null)
Constructors
this(Padding padding = new PKCS7Padding())
classCFB(BlockCipherT) : Cipher

Cipher Feedback (CFB) Mode (CFB-BlockSize).

Fields
private BlockCipherT _cipher
private CipherOperation _op
private ubyte[] _iv
private ubyte[] _feedback
private ubyte[] _buffer
private bool _initialized
private size_t _blockSize
Methods
void initialize(CipherOperation op, SecretKey key, const(ubyte)[] iv)
void init(CipherOperation op, Key key, const(ubyte)[] iv = null)
ubyte[] update(const(ubyte)[] input)
ubyte[] finish(const(ubyte)[] input = null)
classOFB(BlockCipherT) : Cipher

Output Feedback (OFB) Mode.

Note

this mode feeds the cipher output back as the next input using

the same buffer (encrypt(_iv, _iv)), so BlockCipherT must support in-place operation.

Fields
private BlockCipherT _cipher
private ubyte[] _iv
private ubyte[] _keystream
private size_t _keystreamIdx
private bool _initialized
private size_t _blockSize
Methods
void initialize(CipherOperation op, SecretKey key, const(ubyte)[] iv)
void init(CipherOperation op, Key key, const(ubyte)[] iv = null)
ubyte[] update(const(ubyte)[] input)
ubyte[] finish(const(ubyte)[] input = null)
classXTS(BlockCipherT) : Cipher

XEX-based Tweaked-codebook mode with ciphertext Stealing (XTS).

Used for disk encryption.

Fields
private BlockCipherT _cipher1
private BlockCipherT _cipher2
private CipherOperation _op
private ubyte[] _tweak
private ubyte[] _buffer
private bool _initialized
private size_t _blockSize
Methods
void initialize(CipherOperation op, SecretKey key, const(ubyte)[] iv)
void init(CipherOperation op, Key key, const(ubyte)[] iv = null)
private void multiplyAlpha(ubyte[] tweak)
private bool distinctHalves(const(ubyte)[] k) static @safe pure
ubyte[] update(const(ubyte)[] input)
private ubyte[] processBlock(ubyte[] block)
ubyte[] finish(const(ubyte)[] input = null)
classGCM(BlockCipherT) : AEADCipher

Galois/Counter Mode (GCM).

Fields
private BlockCipherT _cipher
private SecretKey _key
private size_t _keySize
private ubyte[16] _H
private ubyte[16] _J0
private ulong MAX_DATA_LENMaximum per-message data length: (2^^32 - 2) blocks of 16 bytes (NIST SP 800-38D 5.2.1.1). Longer messages would wrap the 32-bit counter field of inc32 and reuse keystream blocks.
Methods
size_t tagSize() @property const
size_t nonceSize() @property const
size_t keySize() @property const
ubyte[] seal(const(ubyte)[] plaintext, const(ubyte)[] aad, const(ubyte)[] nonce, ubyte[] output = null)
ubyte[] open(const(ubyte)[] ciphertext, const(ubyte)[] aad, const(ubyte)[] nonce, ubyte[] output = null)
private void inc32(ref ubyte[16] counter)
private void validateDataLength(ulong len) staticValidates a plaintext or ciphertext length against the GCM block bound.
private void prepareJ0(const(ubyte)[] nonce)
private ubyte[16] calculateTag(const(ubyte)[] aad, const(ubyte)[] ciphertext)
private void processData(ref ubyte[16] y, const(ubyte)[] data)
private void ghashBlock(ref ubyte[16] y, const(ubyte)[] block)
private void gmult(ref ubyte[16] x, const(ubyte)[] y)
Constructors
this(Key key)
classCCM(BlockCipherT) : AEADCipher

Counter with CBC-MAC (CCM).

Note

the CBC-MAC half of this mode invokes the underlying block

cipher with the same buffer as input and output (encrypt(x, x)), so BlockCipherT must support in-place operation.

Fields
private BlockCipherT _cipher
private SecretKey _key
private size_t _keySize
private size_t _tagSize
Methods
size_t tagSize() @property const
size_t nonceSize() @property const
size_t keySize() @property const
ubyte[] seal(const(ubyte)[] plaintext, const(ubyte)[] aad, const(ubyte)[] nonce, ubyte[] output = null)
ubyte[] open(const(ubyte)[] ciphertext, const(ubyte)[] aad, const(ubyte)[] nonce, ubyte[] output = null)
private void processCTR(const(ubyte)[] input, const(ubyte)[] nonce, ubyte[] output)
private void validateLengths(const(ubyte)[] message, const(ubyte)[] aad, const(ubyte)[] nonce) staticValidates that the message and AAD lengths are encodable for this nonce: the q = 15 - nonce.length length field must hold the message length, the block counter must not wrap past 2^(8q) blocks (whi...
private ubyte[16] formatCounter(const(ubyte)[] nonce, size_t counterVal)
private ubyte[16] calculateMac(const(ubyte)[] plaintext, const(ubyte)[] aad, const(ubyte)[] nonce)
Constructors
this(Key key, size_t tagSize = 16)

Functions 2

private fnvoid incrementCounter(ubyte[] counter)Increment a counter block (big-endian).
private fnvoid init(Cipher cipher, CipherOperation op, Key key, const(ubyte)[] iv = null)Backward-compatible initializer alias for code that still calls `init(...)` through the `Cipher` interface type.