inline static const int ivSize{16}; // AES block size
+ /// The size used for initialization of the iv for this cipher.
+ /// In CTR mode, there should be zero padding in the lower part of the iv, so the counter can increase from 0 to (the number of blocks - 1).
+ /// We use 8 bytes for random data, and the lower 8 bytes for the counter. This allows us to encrypt 2**(8*8) blocks, which is 2**(8*8) * 16 bytes = 268,435,456 TiB data
+ inline static const int ivSizeInit{8};
/// Random size to generate a new cipher, provided in fromRandom().
- inline static const int randomSize{keySize + ivSize};
+ inline static const int randomSize{keySize + ivSizeInit};
using DataT = std::string;
private:
struct RawTag {};
/**
* Constructs an AES-256-CTR cipher using `key` and `iv`.
*
* `key` and `iv` are provided as-is.
*
* `key` must be of size `keySize` and `iv` must be of size `ivSize`.
* Otherwise, the constructed cipher is invalid.
*/
AES256CTRDesc(RawTag, DataT key, DataT iv);
public:
/**
* Constructs an AES-256-CTR cipher using `key` and `iv`.
*
* `key` is provided as urlsafe unpadded base64.
* `iv` is provided as unpadded base64.
*
* One must check with `valid()` before actual processing with
* the object if the key and iv comes from a remote Matrix event.
*/
AES256CTRDesc(std::string key, std::string iv);
/**
* Generate a new AES-256-CTR cipher from random data.
*
* @param random The random data provided as-is. `random.size()`
* must be at least `randomSize`.
*
* The first keySize bytes will be used as the key, and the
* next ivSize bytes will be used as the iv.
*
* @return An AES-256-CTR cipher generated with the random data.