libkazv
Kazv::AES256CTRDesc Class Reference

#include <aes-256-ctr.hpp>

Classes

struct  Private
 

Public Types

using DataT = std::string
 
template<class RangeT >
using ResultBase = std::pair< AES256CTRDesc, RangeT >
 
using Result = ResultBase< DataT >
 

Public Member Functions

 AES256CTRDesc (std::string key, std::string iv)
 Constructs an AES-256-CTR cipher using key and iv. More...
 
 ~AES256CTRDesc ()
 
bool valid () const
 
std::string key () const
 
std::string iv () const
 
Result process (DataT data) const &
 Encrypts or decrypts data and derives the next cipher state. More...
 
template<class RangeT , class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
ResultBase< std::decay_t< RangeT > > process (RangeT data) const &
 Encrypts or decrypts data and derives the next cipher state. More...
 
Result process (DataT data) &&
 Encrypts or decrypts data and derives the next cipher state. More...
 
template<class RangeT , class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
ResultBase< std::decay_t< RangeT > > process (RangeT data) &&
 Encrypts or decrypts data and derives the next cipher state. More...
 
DataT processInPlace (DataT data)
 Encrypt or decrypt data and modify the state of the cipher in-place. More...
 
template<class RangeT , class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
std::decay_t< RangeTprocessInPlace (RangeT data)
 Encrypt or decrypt data and modify the state of the cipher in-place. More...
 

Static Public Member Functions

template<class RangeT >
static AES256CTRDesc fromRandom (RangeT random)
 Generate a new AES-256-CTR cipher from random data. More...
 

Static Public Attributes

static const int optimalBlockSize {16}
 The optimal block size for this cipher. More...
 
static const int keySize {32}
 The key size for this cipher. More...
 
static const int ivSize {16}
 The iv size for this cipher. More...
 
static const int ivSizeInit {8}
 The size used for initialization of the iv for this cipher. More...
 
static const int randomSize {keySize + ivSizeInit}
 Random size to generate a new cipher, provided in fromRandom(). More...
 

Member Typedef Documentation

◆ DataT

using Kazv::AES256CTRDesc::DataT = std::string

◆ Result

◆ ResultBase

template<class RangeT >
using Kazv::AES256CTRDesc::ResultBase = std::pair<AES256CTRDesc, RangeT>

Constructor & Destructor Documentation

◆ AES256CTRDesc()

Kazv::AES256CTRDesc::AES256CTRDesc ( std::string  key,
std::string  iv 
)

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()

Kazv::AES256CTRDesc::~AES256CTRDesc ( )
default

Member Function Documentation

◆ fromRandom()

template<class RangeT >
static AES256CTRDesc Kazv::AES256CTRDesc::fromRandom ( RangeT  random)
inlinestatic

Generate a new AES-256-CTR cipher from random data.

Parameters
randomThe 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.

Returns
An AES-256-CTR cipher generated with the random data.

◆ iv()

std::string Kazv::AES256CTRDesc::iv ( ) const
Returns
The iv encoded as unpadded base64.

◆ key()

std::string Kazv::AES256CTRDesc::key ( ) const
Returns
The key encoded as urlsafe unpadded base64.

◆ process() [1/4]

auto Kazv::AES256CTRDesc::process ( DataT  data) &&

Encrypts or decrypts data and derives the next cipher state.

This function differs from the above two only in that its calling object is an rvalue reference (a.k.a casted using std::move()).

◆ process() [2/4]

auto Kazv::AES256CTRDesc::process ( DataT  data) const &

Encrypts or decrypts data and derives the next cipher state.

Parameters
dataThe data to encrypt or decrypt.

Example:

auto cipher = Kazv::AES256CTRDesc(
    Kazv::genRandom(Kazv::AES256CTRDesc::keySize),
    Kazv::genRandom(Kazv::AES256CTRDesc::ivSize)
);
std::string original = "Some test here";
auto [next, encrypted] = cipher.process(original);
auto [next2, encrypted2] = cipher.process(original);
REQUIRE(encrypted == encrypted2);
Returns
an object that can be destructured into the next cipher state (as AES256CTRDesc) and the encrypted or decrypted data (as DataT).

◆ process() [3/4]

template<class RangeT , class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
ResultBase<std::decay_t<RangeT> > Kazv::AES256CTRDesc::process ( RangeT  data) &&
inline

Encrypts or decrypts data and derives the next cipher state.

This function differs from the above only in that it takes any range type and the second component of returned object is of the same type as the argument.

◆ process() [4/4]

template<class RangeT , class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
ResultBase<std::decay_t<RangeT> > Kazv::AES256CTRDesc::process ( RangeT  data) const &
inline

Encrypts or decrypts data and derives the next cipher state.

This function only differs from the above in that it takes any range type and the second component of returned object is of the same type as the argument.

◆ processInPlace() [1/2]

auto Kazv::AES256CTRDesc::processInPlace ( DataT  data)

Encrypt or decrypt data and modify the state of the cipher in-place.

Parameters
dataThe data to encrypt or decrypt.

◆ processInPlace() [2/2]

template<class RangeT , class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
std::decay_t<RangeT> Kazv::AES256CTRDesc::processInPlace ( RangeT  data)
inline

Encrypt or decrypt data and modify the state of the cipher in-place.

This function differs from the above only in that it takes any range type and returns the processed data as the same range type.

◆ valid()

bool Kazv::AES256CTRDesc::valid ( ) const
Returns
whether this cipher is valid. If the cipher is invalid, you must not use any method except destructor or operator=().

Member Data Documentation

◆ ivSize

const int Kazv::AES256CTRDesc::ivSize {16}
inlinestatic

The iv size for this cipher.

◆ ivSizeInit

const int Kazv::AES256CTRDesc::ivSizeInit {8}
inlinestatic

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

◆ keySize

const int Kazv::AES256CTRDesc::keySize {32}
inlinestatic

The key size for this cipher.

◆ optimalBlockSize

const int Kazv::AES256CTRDesc::optimalBlockSize {16}
inlinestatic

The optimal block size for this cipher.

◆ randomSize

const int Kazv::AES256CTRDesc::randomSize {keySize + ivSizeInit}
inlinestatic

Random size to generate a new cipher, provided in fromRandom().


The documentation for this class was generated from the following files: