Files CCore/inc/crypton/BlockCipher.h CCore/src/crypton/BlockCipher.cpp
BlockCipher is an abstract block cipher algorithm. It is a template and the template parameter provides all necessary parts to implement the required functionality. An abstract block cipher algorithm is an invertible function, which maps an input block of bytes of the defined length (the cipher block length) to another such block. The function has a parameter — the key, which is a block of bytes of the defined length (the cipher key length).
template <class T>
class BlockCipher : NoCopy
{
....
public:
// length in octets
static const ulen BlockLen = T::BlockLen ;
static const ulen KeyLen = T::KeyLen ;
static const char * GetName() { return T::GetName(); }
// constructors
BlockCipher();
~BlockCipher();
explicit BlockCipher(const uint8 key[KeyLen]);
// methods
void key(const uint8 key[KeyLen]);
void unkey();
void apply(const uint8 src[BlockLen],uint8 *restrict dst/* [BlockLen] */) const;
void apply(uint8 src_dst[BlockLen]) const;
};
BlockLen is the cipher block length.
KeyLen is the key length.
GetName() is the cipher common name, like "AES128".
Default constructor creates a null object. It cannot be used to cipher.
The second constructor creates the object and assigns the given key to it. This object can be used to cipher data.
Destructor performs the unkey cipher operation. This operation cleans all internal buffers with sensitive data. If the cipher implementation uses some hardware support, the correspondent hardware entities also must be cleaned.
key() assigns a key to the object. After call of this method the object can be used to cipher data with the given key.
unkey() erases the key information from the object.
apply() performs the cipher operation on the given block of data in-place or out-of-place. The first variant operates on two distinct data buffers. The second ciphers data in-place in the single data buffer.
An attempt to use the method apply() without a previously assigned key cause an exception. This exception is thrown with the following guard function:
void GuardNoCipherKey();
The real job is performed by the class T. This class must comply with the following pattern:
struct CipherAlgo { // global properties static const ulen KeyLen = .... ; static const ulen BlockLen = .... ; static const char * GetName() { return "...."; } // internal state .... // methods void key(const uint8 src[KeyLen]); void unkey(); void apply(const uint8 src[BlockLen],uint8 *restrict dst/* [BlockLen] */); void apply(uint8 src_dst[BlockLen]); };