Skip to content

AES Key Container

Overview

Is a container for a cryptographic key and AES cipher parameters.


Constructors

Default constructor

Constructs blank un-initialized instance of AES Key container.
Allows for a manual initialization, when needed.

AES::keyc()

  • Parameters:
Example
using namespace tancrypt

AES::keyc key;
key.setType(AES::Type::CBC256);
key.setKey(buffer);
key.setHashAlg(algorithm);

// Needs to be enabled manually if hashing used
key.setHashEnabled(true); 


Raw key

Warning

Please note that when constructing without key hashing, you're expected to provide a properly padded key of correct length. If you do not want to handle your key padding, use hashing by constructing with tancrypt::hashAlg.

Tip

To get a proper length for AES::Type you can use AES::RefKeylen(AES::Type type)

Constructs instance of AES Key container without key hashing.
When constructing without tancrypt::hashAlg, you must ensure your key data are pre-padded.

AES::keyc(const dutils::dbuffer key,AES::Type type)

  • Parameters:
    • const dutils::dbuffer key - Pre-padded key data
    • AES::Type type - Type of AES algorithm to use

AES::keyc(const char* key,AES::Type type)

  • Parameters:
    • const char* key - Pre-padded key data
    • AES::Type type - Type of AES algorithm to use

AES::keyc(const unsigned char* key,AES::Type type)

  • Parameters:
    • const unsigned char* key - Pre-padded key data
    • AES::Type type - Type of AES algorithm to use

AES::keyc::randomKey(size_t size,AES::Type type)

  • Creates a key container with a random key using OpenSSL's RAND_bytes

  • Parameters:

    • size_t size - Size of the key (bytes)
    • AES::Type type - Type of AES algorithm to use



Hashed key

Note

Bear in mind that to avoid possible race conditions and unnecessary state holding, the hashing occurs on encryption runtime this may cause this key constructor to be less efficient when encrypting more than once.

Constructs instance of AES Key container with key hashing. Key is automatically hashed on encryption using the given hash algorithm.

AES::keyc(const dutils::dbuffer key,AES::Type type,tancrypt::hashAlg alg)

  • Parameters:
    • const dutils::dbuffer key - Key data
    • tancrypt::AES::Type type - AES algorithm type
    • tancrypt::hashAlg alg - Hashing algorithm to use on key during encryption
  • Returns:
    • tancrypt::AES::keyc key

AES::keyc(const char* key,AES::Type type,tancrypt::hashAlg alg)

  • Parameters:
    • const char* key - Key data
    • tancrypt::AES::Type type - AES algorithm type
    • tancrypt::hashAlg alg - Hashing algorithm to use on key during encryption
  • Returns:
    • tancrypt::AES::keyc key

AES::keyc(const unsigned char* key,AES::Type type,tancrypt::hashAlg alg)

  • Parameters:
    • const unsigned char* key - Key data
    • tancrypt::AES::Type type - AES algorithm type
    • tancrypt::hashAlg alg - Hashing algorithm to use on key during encryption
  • Returns:
    • tancrypt::AES::keyc key

AES::keyc::keyc(size_t size,AES::Type type,tancrypt::hashAlg alg)

  • Creates a key container with a random key using OpenSSL's RAND_bytes

  • Parameters:

    • size_t size - Size of the key (bytes)
    • tancrypt::AES::Type type - AES algorithm type
    • tancrypt::hashAlg alg - Hashing algorithm to use on key during encryption
  • Returns:
    • tancrypt::AES::keyc key


AES Type

Enum Constant Cryptographic Algorithm Mode Key Size
AES::Type::CBC128 AES-128-CBC CBC 128-bit
AES::Type::CBC192 AES-192-CBC CBC 192-bit
AES::Type::CBC256 AES-256-CBC CBC 256-bit
AES::Type::GCM128 AES-128-GCM GCM 128-bit
AES::Type::GCM192 AES-192-GCM GCM 192-bit
AES::Type::GCM256 AES-256-GCM GCM 256-bit



Methods

setType

Member method, sets type of AES algorithm.
Does not corrupt key and can be used even past first initialization.

AES::keyc::setType(AES::Type type)

  • Parameters:
    • AES::Type type - AES algorithm type


setHashAlg

Sets hashing algorithm to be used for key.
Does not corrupt key and can be used even past first initialization.

AES::keyc::setHashAlg(tancrypt::hashAlg alg)

  • Parameters:
    • tancrypt::hashAlg alg - Hashing algorithm of your choice


setKey

Member method, sets key data of the AES Key container.

AES::keyc::setKey(const dutils::dbuffer key)

  • Parameters:
    • const dutils::dbuffer key - Key data buffer

AES::keyc::setKey(const char* key)

  • Parameters:
    • const char* key - Key data buffer

AES::keyc::setKey(const unsigned char* key)

  • Parameters:
    • const unsigned char* key - Key data buffer




makeRandomKey

Warning

This does not initialize the key metadata like AES algorithm etc...
Only thing changed are allocated size and key content.

Generates new random key in the current buffer

makeRandomKey(size_t size)

  • Parameters:
    • size_t size


getHashAlg

Retrieves currently set hashing algorithm of the AES Key Container

AES::keyc::getHashAlg()

  • Parameters:
  • Returns:
    • tancrypt::hashAlg alg - Current hashing algorithm


getHashEnabled

Retrieves current bool state - is hasing enabled ? true/false

AES::keyc::getHashEnabled()

  • Parameters:
  • Returns:
    • bool hashingEnabled

getKey

Retrieves current key data buffer

AES::keyc::getKey()

  • Parameters:
  • Returns:
    • const dutils::dbuffer key