C12Adapter Opensource C++ Interface
|
AES encryption and decryption class. More...
Public Types | |
enum | { KeySize = 16, BlockSize = 16, KeyWrapEncryptionExtraSize = 8, KeyWrapMinimumSize = KeySize, KeyWrapMaximumSize = 2048 } |
Public Member Functions | |
MAes () | |
Create AES encryption class without setting the key. More... | |
MAes (const MByteString &key) | |
Create AES encryption class with key, given as raw 16 bytes. More... | |
MAes (const MAes &other) | |
Copy constructor, creates a copy of a given object. More... | |
virtual | ~MAes () |
Destructor, reclaims memory allocated by the object. | |
MAes & | operator= (const MAes &other) |
Assignment operator that copies the key from another class. More... | |
MByteString | Encrypt (const MByteString &plainText) |
Encrypt a given chunk of data with AES using plain and simple ECB mode. More... | |
MByteString | Decrypt (const MByteString &cipherText) |
Decrypt a given chunk of data with AES using plain and simple ECB mode. More... | |
MByteString | KeyWrap (const MByteString &keys) |
Wrap the given key material, one or more keys, using RFC 3394 key wrap algorithm. More... | |
MByteString | KeyUnwrap (const MByteString &cipher) |
Unwrap the given cipher and produce the original key material, one or more keys. More... | |
const MByteString & | GetKey () const |
void | SetKey (const MByteString &) |
MStdString | GetHexKey () const |
void | SetHexKey (const MStdString &) |
void | EncryptBuffer (const Muint8 *plainText, Muint8 *cipherText) |
void | EncryptBuffer (const char *plainText, char *cipherText) |
void | DecryptBuffer (const Muint8 *cipherText, Muint8 *plainText) |
void | DecryptBuffer (const char *cipherText, char *plainText) |
unsigned | KeyWrapBuffer (const Muint8 *keyText, unsigned keyTextSize, Muint8 *cipherText) |
unsigned | KeyWrapBuffer (const char *keyText, unsigned keyTextSize, char *cipherText) |
unsigned | KeyUnwrapBuffer (const Muint8 *cipherText, unsigned cipherTextSize, Muint8 *keyText) |
unsigned | KeyUnwrapBuffer (const char *cipherText, unsigned cipherTextSize, char *keyText) |
![]() | |
virtual | ~MObject () |
Object destructor. | |
virtual const MClass * | GetClass () const =0 |
Get the final class of the object. More... | |
virtual unsigned | GetEmbeddedSizeof () const |
For embedded object types, return the size of the class. More... | |
bool | IsEmbeddedObject () const |
Tell if the object is of embedded kind. More... | |
SHOW_INTERNAL MVariant | Call (const MStdString &name, const MVariant ¶ms) |
Call the object service with parameters, given as variant. More... | |
MVariant | Call0 (const MStdString &name) |
Call the object service with no parameters. More... | |
MVariant | Call1 (const MStdString &name, const MVariant &p1) |
Call the object service with one parameter. More... | |
MVariant | Call2 (const MStdString &name, const MVariant &p1, const MVariant &p2) |
Call the object service with two parameter. More... | |
MVariant | Call3 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3) |
Call the object service with three parameter. More... | |
MVariant | Call4 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4) |
Call the object service with four parameter. More... | |
MVariant | Call5 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5) |
Call the object service with five parameter. More... | |
MVariant | Call6 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5, const MVariant &p6) |
Call the object service with six parameter. More... | |
virtual MVariant | CallV (const MStdString &name, const MVariant::VariantVector ¶ms) |
Call the object service with parameters, given as variant vector. More... | |
virtual bool | IsPropertyPresent (const MStdString &name) const |
Tell if the property with the given name exists. | |
virtual bool | IsServicePresent (const MStdString &name) const |
Tell if the service with the given name exists. | |
virtual MVariant | GetProperty (const MStdString &name) const |
Get the property value using name of the property. More... | |
virtual void | SetProperty (const MStdString &name, const MVariant &value) |
Set the property using name of the property, and value. More... | |
virtual MStdStringVector | GetAllPropertyNames () const |
Return the list of publicly available properties, persistent or not. More... | |
virtual MStdStringVector | GetAllPersistentPropertyNames () const |
Return the list of persistent properties. More... | |
virtual void | SetPersistentPropertiesToDefault () |
Set the persistent properties of the object to their default values. More... | |
virtual MVariant | GetPersistentPropertyDefaultValue (const MStdString &name) const |
Get the default value of persistent property with the name given. More... | |
virtual void | SetPersistentPropertyToDefault (const MStdString &name) |
Set the persistent property with the name given to default value. More... | |
virtual const char * | GetType () const |
Get the name of the type for the object (could be the same as class name). | |
virtual void | SetType (const MStdString &) |
Intentionally, it will set the name of the type for the object, but the service will not allow setting the name to anything other than the current name. More... | |
virtual void | Validate () |
Validate internal structures of the object. More... | |
Static Public Member Functions | |
static void | CheckKeySizeValid (const MByteString &key) |
Verify the byte size of a given binary key is exactly 16 bytes. More... | |
static void | CheckHexKeySizeValid (const MStdString &key) |
Verify HEX representation and byte size of a given HEX key. More... | |
static MByteString | StaticEncrypt (const MByteString &key, const MByteString &plainText) |
Static version of Encrypt that accepts key as parameter. More... | |
static MByteString | StaticDecrypt (const MByteString &key, const MByteString &cipherText) |
Static version of Decrypt that accepts key as parameter. More... | |
static MByteString | StaticKeyWrap (const MByteString &key, const MByteString &keys) |
Static variant of KeyWrap. More... | |
static MByteString | StaticKeyUnwrap (const MByteString &key, const MByteString &cipher) |
Static variant of KeyUnwrap. More... | |
static M_NORETURN_FUNC void | ThrowValidationError () |
Throw an error that tells about a validation problem in the encrypted or authenticated message. More... | |
static void | DestroySecureData (MByteString &data) |
static void | DestroySecureData (MByteStringVector &data) |
static void | DestroySecureData (Muint8 *data, unsigned size) |
static void | DestroySecureData (char *data, unsigned size) |
static void | AssignSecureData (MByteString &destination, const MByteString &source) |
static void | AssignSecureData (MByteStringVector &destination, const MByteStringVector &source) |
static void | MoveSecureData (MByteString &destination, MByteString &source) |
static void | MoveSecureData (MByteStringVector &destination, MByteStringVector &source) |
static void | SwapSecureData (MByteString &v1, MByteString &v2) |
static void | SwapSecureData (MByteStringVector &v1, MByteStringVector &v2) |
![]() | |
static const MClass * | GetStaticClass () |
Get the declared class of this particular object. More... | |
static bool | IsClassPresent (const MStdString &name) |
Tells if the given class name is available. More... | |
Additional Inherited Members | |
![]() | |
static const MClass | s_class |
Class of MObject. | |
![]() | |
MObject () | |
Object constructor, protected as the class is abstract. | |
void | DoSetPersistentPropertiesToDefault (const MClass *staticClass) |
Set the persistent properties to their default values for one object provided the class for that object. More... | |
AES encryption and decryption class.
Currently only 128-bit AES key is supported, but the interface is generic to support other sizes. Different from all the other MeteringSDK classes that accept AES key as a sequence of hexadecimal characters, this class is primarily using the key as a raw byte string exactly 16 bytes long. There is a special extra property GetHexKey that does the necessary conversion.
This class implements the simplest possible ECB mode, in which every chunk of 16 bytes gets parameterless translation into cipher using key. ECB mode has known security issues as the 16-byte chunks of data with the same contents will produce the very same 16-byte chunks of cipher. This fact presents to an attacker the unwanted knowledge about the contents of plain text. ECB mode is still usable for cases when the data to encrypt has a good entropy, and never repeats, such as cryptographic hash or a key itself.
For EAX mode refer to MAesEax There is also EAX-mode authentication.
Only one thread shall access this object at a time, however since encryption and decryption are long operations, it is a better design to have a per-thread instance of MAes.
anonymous enum |
MAes::MAes | ( | ) |
Create AES encryption class without setting the key.
Any attempt to use this class prior to setting the key will fail with an exception.
MAes::MAes | ( | const MByteString & | key | ) |
Create AES encryption class with key, given as raw 16 bytes.
After successful initialization, assuming the key has correct size, the result object can be used for data encryption or decryption.
key | Raw 16 bytes, key to use during encryption or decryption. |
MAes::MAes | ( | const MAes & | other | ) |
Copy constructor, creates a copy of a given object.
The key gets copied, if the other object has it set. If the other object does not have key, the result MAes object will have to be assigned a key before using it for encryption.
other | An object from which to create a copy. |
|
static |
Assign one secure data such as key or password to another variable.
As the data size of a new buffer can be different as a safety measure the previous contents of the destination is erased.
destination | Where to put the new secure material. |
source | From which value to copy data. The source value does not change. |
|
static |
Assign one secure data such as key or password to another variable.
As the data size of a new buffer can be different as a safety measure the previous contents of the destination is erased.
destination | Where to put the new secure material. |
source | From which value to copy data. The source value does not change. |
|
static |
Verify HEX representation and byte size of a given HEX key.
For the call to succeed, the given string shall be a valid sequence of hex digits that evaluates into 16-byte raw data. Hex digits can have blanks, however no blank shall split the hex pair.
Examples of valid HEX key:
key | The hex representation of key which size is to be checked. |
|
static |
Verify the byte size of a given binary key is exactly 16 bytes.
An exception is thrown if the given binary key has size other than 16 bytes.
key | The key which size is to be checked. |
MByteString MAes::Decrypt | ( | const MByteString & | cipherText | ) |
Decrypt a given chunk of data with AES using plain and simple ECB mode.
ECB mode has known security issues as the 16-byte chunks of data with the same contents will produce the same 16-byte chunks of cipher. This fact presents to an attacker the unwanted knowledge about the contents of plain text. ECB mode is still usable for cases when the data to encrypt has a good entropy, and never repeats, such as cryptographic hash.
For cases other than encrypting random or pseudorandom data, EAX mode of AES is preferred, see class MAesEax. This mode is also capable of handling data of sizes not divisible by 16.
When decryption is to be done once for a given key, StaticDecrypt() call is more convenient, but when key has to be reused for decryption of many chunks, using MAes::Decrypt() yields better performance when it reuses the same MAes instance. This is because there is a key expansion algorithm that is performed per single key assignment.
cipherText | Raw bytes of size divisible by 16. If the size is not divisible by 16, an exception is thrown. |
void MAes::DecryptBuffer | ( | const Muint8 * | cipherText, |
Muint8 * | plainText | ||
) |
Decrypt buffer of size equal to block size
Cipher text and plain text can be the same buffer in which case the plain text will be decrypted in-place.
cipherText | Buffer of size 16 bytes, previously encrypted with this key. |
plainText | Result buffer of size 16 bytes. |
|
inline |
Decrypt buffer of size equal to block size
Cipher text and plain text can be the same buffer in which case the plain text will be decrypted in-place.
cipherText | Buffer of size 16 bytes, previously encrypted with this key. |
plainText | Result buffer of size 16 bytes. |
|
static |
Destroy secure data such as key, password and so on.
The memory area denoted by the data will be filled with zeros. Only the data are erased, while the array sizes are unchanged.
|
static |
Destroy secure data such as key, password and so on.
The memory area denoted by the data will be filled with zeros. Only the data are erased, while the array sizes are unchanged.
|
inlinestatic |
Destroy secure data such as key, password and so on.
The memory area denoted by the data will be filled with zeros. Only the data are erased, while the array sizes are unchanged.
|
inlinestatic |
Destroy secure data such as key, password and so on.
The memory area denoted by the data will be filled with zeros. Only the data are erased, while the array sizes are unchanged.
MByteString MAes::Encrypt | ( | const MByteString & | plainText | ) |
Encrypt a given chunk of data with AES using plain and simple ECB mode.
ECB mode has known security issues as the 16-byte chunks of data with the same contents will produce the same 16-byte chunks of cipher. This fact presents to an attacker the unwanted knowledge about the contents of plain text. ECB mode is still usable for cases when the data to encrypt has a good entropy, and never repeats, such as cryptographic hash.
For cases other than encrypting random or pseudorandom data, EAX mode of AES is preferred, see class MAesEax. This mode is also capable of handling data of sizes not divisible by 16.
When decryption is to be done once for a given key, StaticDecrypt() call is more convenient, but when key has to be reused for decryption of many chunks, using MAes::Decrypt() yields better performance when it reuses the same MAes instance. This is because there is a key expansion algorithm that is performed per single key assignment.
plainText | Raw bytes of size divisible by 16. If the size is not divisible by 16, an exception is thrown. |
void MAes::EncryptBuffer | ( | const Muint8 * | plainText, |
Muint8 * | cipherText | ||
) |
Encrypt buffer of size equal to block size.
Plain text and cipher text can be the same buffer in which case the plain text will be encrypted in-place.
plainText | Buffer of size 16 bytes. |
cipherText | Result buffer of size 16 bytes. |
|
inline |
Encrypt buffer of size equal to block size.
Plain text and cipher text can be the same buffer in which case the plain text will be encrypted in-place.
plainText | Buffer of size 16 bytes. |
cipherText | Result buffer of size 16 bytes. |
MStdString MAes::GetHexKey | ( | ) | const |
AES Key to use by the class, hexadecimal representation.
The key has binary form, not hex, which is different from all the other MeteringSDK classes that accept AES key as a sequence of hexadecimal characters.
|
inline |
AES Key to use by the class, binary representation.
The key has binary form, not hex, which is different from all the other MeteringSDK classes that accept AES key as a sequence of hexadecimal characters.
MByteString MAes::KeyUnwrap | ( | const MByteString & | cipher | ) |
Unwrap the given cipher and produce the original key material, one or more keys.
The key unwrap function takes a pointer to the full encrypted data including the encrypted header.
The return value is true if there was no error (i.e. the prefix matches the fixed prefix that we know we started with) or false if there was an error. In the case of error, the data is overwritten with zeros to avoid leaking information which might be used to guess the key (such as a known ciphertext attack).
cipher | Input that contains wrapped key material. |
unsigned MAes::KeyUnwrapBuffer | ( | const Muint8 * | cipherText, |
unsigned | cipherTextSize, | ||
Muint8 * | keyText | ||
) |
Decrypt key material with AES key wrap algorithm.
If the key is not correct an exception will be thrown as the verification will not be successful. Plain text and cipher text can be the same buffer in which case the plain text will be decrypted in-place. However remember the size of cipher text buffer should be bigger by 8 bytes.
cipherText | Encrypted buffer with key material that has to be decrypted and verified. |
cipherTextSize | Size of the encrypted buffer. |
keyText | Result cipher text, the buffer should be 8 bytes bigger than keyTextSize. |
|
inline |
Decrypt key material with AES key wrap algorithm.
If the key is not correct an exception will be thrown as the verification will not be successful. Plain text and cipher text can be the same buffer in which case the plain text will be decrypted in-place. However remember the size of cipher text buffer should be bigger by 8 bytes.
cipherText | Encrypted buffer with key material that has to be decrypted and verified. |
cipherTextSize | Size of the encrypted buffer. |
keyText | Result cipher text, the buffer should be 8 bytes bigger than keyTextSize. |
MByteString MAes::KeyWrap | ( | const MByteString & | keys | ) |
Wrap the given key material, one or more keys, using RFC 3394 key wrap algorithm.
The key wrapping algorithm is described in a NIST document dated 16 November 2001 and titled "AES Key Wrap Specification" It is later accepted as RFC 3394.
The algorithm, as implemented here, is intended to be able to wrap an arbitrary number of 128-byte blocks (the specification allows for 64-bit blocks, but this is a simplification).
Key wrap:
Note that passed data pointer must start with 8 bytes of padding in front of the actual data, and that the data length must be an integral multiple of 8 bytes. In other words, if you have two 128-bit (16-byte) keys you need to wrap, the data pointer must point to a 24-byte buffer (16+8) with the actual data starting at offset 8. The code will then process the data in place (so it cannot be ROM) and uses the key encryption key (kek) which can be in ROM. When the algorithm completes, the entire buffer must be transmitted since the 8 prepended bytes are required to assure that the data has arrived intact.
keys | One or more keys, should be 16 bytes or more, size divisible by 8. |
unsigned MAes::KeyWrapBuffer | ( | const Muint8 * | keyText, |
unsigned | keyTextSize, | ||
Muint8 * | cipherText | ||
) |
Encrypt key material with AES key wrap algorithm.
Cipher text and plain text can be the same buffer in which case the plain text will be decrypted in-place. However remember the size of cipher text buffer should be bigger by 8 bytes.
keyText | Buffer with key material that has to be wrapped. |
keyTextSize | Size of the buffer with key material. |
cipherText | Result cipher text, the buffer should be 8 bytes bigger than keyTextSize. |
|
inline |
Encrypt key material with AES key wrap algorithm.
Cipher text and plain text can be the same buffer in which case the plain text will be decrypted in-place. However remember the size of cipher text buffer should be bigger by 8 bytes.
keyText | Buffer with key material that has to be wrapped. |
keyTextSize | Size of the buffer with key material. |
cipherText | Result cipher text, the buffer should be 8 bytes bigger than keyTextSize. |
|
static |
Move one secure data such as key or password into another variable, destroy source value.
As the data size of a new buffer can be different as a safety measure the previous contents of the destination is erased. The contents of the source is destroyed after assignment.
destination | Where to put the new secure material. |
source | From which value to copy data. The source value is destroyed after assignment. |
|
static |
Move one secure data such as key or password into another variable, destroy source value.
As the data size of a new buffer can be different as a safety measure the previous contents of the destination is erased. The contents of the source is destroyed after assignment.
destination | Where to put the new secure material. |
source | From which value to copy data. The source value is destroyed after assignment. |
Assignment operator that copies the key from another class.
other | Other object from which to copy. |
void MAes::SetHexKey | ( | const MStdString & | ) |
AES Key to use by the class, hexadecimal representation.
The key has binary form, not hex, which is different from all the other MeteringSDK classes that accept AES key as a sequence of hexadecimal characters.
void MAes::SetKey | ( | const MByteString & | ) |
AES Key to use by the class, binary representation.
The key has binary form, not hex, which is different from all the other MeteringSDK classes that accept AES key as a sequence of hexadecimal characters.
|
static |
Static version of Decrypt that accepts key as parameter.
ECB mode has known security issues as the 16-byte chunks of data with the same contents will produce the same 16-byte chunks of cipher. This fact presents to an attacker the unwanted knowledge about the contents of plain text. ECB mode is still usable for cases when the data to encrypt has a good entropy, and never repeats, such as cryptographic hash.
For cases other than encrypting random or pseudorandom data, EAX mode of AES is preferred, see class MAesEax. This mode is also capable of handling data of sizes not divisible by 16.
When encryption is to be done once for a given key, StaticDecrypt() call is more convenient, but when key has to be reused for encryption of many chunks, using MAes::Decrypt() yields better performance when it reuses the same MAes instance. This is because there is a key expansion algorithm that is performed per single key assignment.
key | Shall be exactly 16 bytes, binary. Notice, the difference from all other MeteringSDK classes is that Key property of MAes is binary, not a hex string. |
cipherText | Raw bytes of size divisible by 16. If the size is not divisible by 16, an exception is thrown. |
|
static |
Static version of Encrypt that accepts key as parameter.
ECB mode has known security issues as the 16-byte chunks of data with the same contents will produce the same 16-byte chunks of cipher. This fact presents to an attacker the unwanted knowledge about the contents of plain text. ECB mode is still usable for cases when the data to encrypt has a good entropy, and never repeats, such as cryptographic hash.
For cases other than encrypting random or pseudorandom data, EAX mode of AES is preferred, see class MAesEax. This mode is also capable of handling data of sizes not divisible by 16.
When encryption is to be done once for a given key, StaticEncrypt() call is more convenient, but when key has to be reused for encryption of many chunks, using MAes::Encrypt() yields better performance when it reuses the same MAes instance. This is because there is a key expansion algorithm that is performed per single key assignment.
key | Shall be exactly 16 bytes, binary. Notice, the difference from all other MeteringSDK classes is that Key property of MAes is binary, not a hex string. |
plainText | Raw bytes of size divisible by 16. If the size is not divisible by 16, an exception is thrown. |
|
static |
Static variant of KeyUnwrap.
key | Encryption key to use in key unwrap, should be the same as the one in the wrap call. |
cipher | Cipher text as produced by keys wrap call. |
|
static |
Static variant of KeyWrap.
key | Encryption key to use in key wrap, should be the same as the one in the unwrap call. |
keys | One or more 16-byte keys to wrap. |
|
static |
Swap secure data such as key or password with another data.
As the data sizes of these can be different, the data gets erased to make sure nothing stays in memory after the operation.
v1 | Data to be swapped with v2. |
v2 | Data to be swapped with v1. |
|
static |
Swap secure data such as key or password with another data.
As the data sizes of these can be different, the data gets erased to make sure nothing stays in memory after the operation.
v1 | Data to be swapped with v2. |
v2 | Data to be swapped with v1. |
|
static |
Throw an error that tells about a validation problem in the encrypted or authenticated message.
Validation is supported in EAX mode of AES, as supported by child class MAesEax, and by key unwrap procedures. The user might decide to throw this error if there is a separate way of validating the encrypted buffer, such as MD5 checksum.
The message thrown mentions that the problem can result from tampering.