Fix compilation of AES implementation, document that is is not actually used
This commit is contained in:
parent
d1b7187085
commit
5ca417069a
|
@ -36,6 +36,12 @@ set(TEST_SRC_FILES tests/utils.h tests/passwordfiletests.cpp tests/entrytests.cp
|
|||
|
||||
set(DOC_FILES README.md)
|
||||
|
||||
option(COMPILE_AES_SOURCES "compile AES sources" OFF)
|
||||
if (COMPILE_AES_SOURCES)
|
||||
list(APPEND HEADER_FILES aes/aes.h)
|
||||
list(APPEND SRC_FILES aes/aes.cpp)
|
||||
endif ()
|
||||
|
||||
# find c++utilities
|
||||
set(CONFIGURATION_PACKAGE_SUFFIX
|
||||
""
|
||||
|
|
152
aes/aes.cpp
152
aes/aes.cpp
|
@ -4,6 +4,15 @@
|
|||
|
||||
namespace Crypto {
|
||||
|
||||
/*!
|
||||
* \class Aes
|
||||
* \brief The Aes class implements AES encryption/decryption.
|
||||
* \deprecated This class is not used by the rest of the library and not compiled by default.
|
||||
* OpenSSL's crypto library is used instead. This class only serves educational
|
||||
* purposes.
|
||||
*/
|
||||
|
||||
// clang-format off
|
||||
Aes::byte Aes::sbox[16][16] = {
|
||||
{ 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76 },
|
||||
{ 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0 },
|
||||
|
@ -51,11 +60,12 @@ namespace Crypto {
|
|||
0x4A000000, 0x94000000, 0x33000000, 0x66000000, 0xCC000000, 0x83000000, 0x1D000000, 0x3A000000,
|
||||
0x74000000, 0xE8000000, 0xCB000000, 0x8D000000
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
Aes::Aes() :
|
||||
key_length(0),
|
||||
num_rounds(0),
|
||||
w(0)
|
||||
Aes::Aes()
|
||||
: keyLength(0)
|
||||
, numRounds(0)
|
||||
, w(0)
|
||||
{
|
||||
memset(state, 0, 16);
|
||||
}
|
||||
|
@ -112,26 +122,26 @@ void Aes::expandKey( byte *key )
|
|||
if (w)
|
||||
delete[] w;
|
||||
|
||||
w = new word[ 4 * ( num_rounds + 1 ) ];
|
||||
w = new word[4 * (numRounds + 1)];
|
||||
|
||||
for( byte iw = 0, ib = 0; iw < key_length; ++iw, ib += 4 )
|
||||
w[ iw ] = ( key[ ib ] << 24 ) + ( key[ ib + 1 ] << 16 ) + ( key[ ib + 2 ] << 8 ) + key[ ib + 3 ];
|
||||
for (byte iw = 0, ib = 0; iw < keyLength; ++iw, ib += 4)
|
||||
w[iw] = static_cast<word>((key[ib] << 24) + (key[ib + 1] << 16) + (key[ib + 2] << 8) + key[ib + 3]);
|
||||
|
||||
word tmp;
|
||||
|
||||
for( byte i = key_length; i < ( 4 * ( num_rounds + 1 ) ); ++i ) {
|
||||
for (byte i = keyLength; i < (4 * (numRounds + 1)); ++i) {
|
||||
|
||||
tmp = w[i - 1];
|
||||
|
||||
if( ( i % key_length ) == 0 ) {
|
||||
if ((i % keyLength) == 0) {
|
||||
rotWord(&tmp);
|
||||
subWord(&tmp);
|
||||
tmp ^= rcon[ i / key_length ];
|
||||
} else if( ( key_length > 6 ) && ( ( i % key_length ) == 4 ) ) {
|
||||
tmp ^= rcon[i / keyLength];
|
||||
} else if ((keyLength > 6) && ((i % keyLength) == 4)) {
|
||||
subWord(&tmp);
|
||||
}
|
||||
|
||||
w[ i ] = w[ i - key_length ] ^ tmp;
|
||||
w[i] = w[i - keyLength] ^ tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -139,7 +149,7 @@ void Aes::addRoundKey( byte round )
|
|||
{
|
||||
for (byte col = 0; col < 4; ++col)
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
state[ row ][ col ] ^= ( w[ round * 4 + col ] >> ( 24 - row * 8 ) ) & 0x000000ff;
|
||||
state[row][col] ^= static_cast<byte>((w[round * 4 + col] >> (24 - row * 8)) & 0x000000ff);
|
||||
}
|
||||
|
||||
void Aes::shiftRows(void)
|
||||
|
@ -195,17 +205,13 @@ void Aes::mixColumns( void )
|
|||
unsigned char h = 0;
|
||||
|
||||
for (byte col = 0; col < 4; ++col) {
|
||||
|
||||
for (byte row = 0; row < 4; ++row) {
|
||||
|
||||
a[row] = state[row][col];
|
||||
h = state[row][col] & 0x80;
|
||||
b[ row ] = state[ row ][ col ] << 1;
|
||||
|
||||
b[row] = static_cast<byte>(state[row][col] << 1);
|
||||
if (h == 0x80)
|
||||
b[row] ^= 0x1b;
|
||||
}
|
||||
|
||||
state[0][col] = b[0] ^ a[3] ^ a[2] ^ b[1] ^ a[1];
|
||||
state[1][col] = b[1] ^ a[0] ^ a[3] ^ b[2] ^ a[2];
|
||||
state[2][col] = b[2] ^ a[1] ^ a[0] ^ b[3] ^ a[3];
|
||||
|
@ -218,58 +224,47 @@ void Aes::invMixColumns( void )
|
|||
unsigned char a[4];
|
||||
|
||||
for (byte col = 0; col < 4; ++col) {
|
||||
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
a[row] = state[row][col];
|
||||
|
||||
state[0][col] = gmul(a[0], 14) ^ gmul(a[3], 9) ^ gmul(a[2], 13) ^ gmul(a[1], 11);
|
||||
state[1][col] = gmul(a[1], 14) ^ gmul(a[0], 9) ^ gmul(a[3], 13) ^ gmul(a[2], 11);
|
||||
state[2][col] = gmul(a[2], 14) ^ gmul(a[1], 9) ^ gmul(a[0], 13) ^ gmul(a[3], 11);
|
||||
state[3][col] = gmul(a[3], 14) ^ gmul(a[2], 9) ^ gmul(a[1], 13) ^ gmul(a[0], 11);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool Aes::setKey(char *key)
|
||||
{
|
||||
size_t keyLen = strlen(key);
|
||||
|
||||
size_t key_len = strlen( key );
|
||||
byte *wKey = 0;
|
||||
byte desKeyLen = 0;
|
||||
|
||||
byte *w_key = 0;
|
||||
byte des_key_len = 0;
|
||||
if (keyLen <= 16) {
|
||||
keyLength = 4;
|
||||
numRounds = 10;
|
||||
desKeyLen = 16;
|
||||
|
||||
if( key_len <= 16 ) {
|
||||
|
||||
key_length = 4;
|
||||
num_rounds = 10;
|
||||
des_key_len = 16;
|
||||
|
||||
} else if( key_len <= 24 ) {
|
||||
|
||||
key_length = 6;
|
||||
num_rounds = 12;
|
||||
des_key_len = 24;
|
||||
|
||||
} else if( key_len <= 32 ) {
|
||||
|
||||
key_length = 8;
|
||||
num_rounds = 14;
|
||||
des_key_len = 32;
|
||||
} else if (keyLen <= 24) {
|
||||
keyLength = 6;
|
||||
numRounds = 12;
|
||||
desKeyLen = 24;
|
||||
|
||||
} else if (keyLen <= 32) {
|
||||
keyLength = 8;
|
||||
numRounds = 14;
|
||||
desKeyLen = 32;
|
||||
}
|
||||
|
||||
w_key = new byte[ des_key_len ];
|
||||
|
||||
for( byte i = 0, t = 0; t < des_key_len; ++i, ++t ) {
|
||||
if( i == key_len )
|
||||
wKey = new byte[desKeyLen];
|
||||
for (byte i = 0, t = 0; t < desKeyLen; ++i, ++t) {
|
||||
if (i == keyLen)
|
||||
i = 0;
|
||||
*( w_key + t ) = *( key + i );
|
||||
*(wKey + t) = static_cast<byte>(*(key + i));
|
||||
}
|
||||
|
||||
expandKey( w_key );
|
||||
|
||||
delete [] w_key;
|
||||
|
||||
expandKey(wKey);
|
||||
delete[] wKey;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -277,12 +272,10 @@ void Aes::cipher( void )
|
|||
{
|
||||
addRoundKey(0);
|
||||
|
||||
for( byte round = 1; round < num_rounds; ++round ) {
|
||||
|
||||
for (byte round = 1; round < numRounds; ++round) {
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
for (byte col = 0; col < 4; ++col)
|
||||
state[row][col] = sbox[state[row][col] >> 4][state[row][col] & 0x0f];
|
||||
|
||||
shiftRows();
|
||||
mixColumns();
|
||||
addRoundKey(round);
|
||||
|
@ -293,21 +286,18 @@ void Aes::cipher( void )
|
|||
state[row][col] = sbox[state[row][col] >> 4][state[row][col] & 0x0f];
|
||||
|
||||
shiftRows();
|
||||
addRoundKey( num_rounds );
|
||||
addRoundKey(numRounds);
|
||||
}
|
||||
|
||||
void Aes::invCipher(void)
|
||||
{
|
||||
addRoundKey( num_rounds );
|
||||
|
||||
for( byte round = ( num_rounds - 1 ); round > 0; --round ) {
|
||||
addRoundKey(numRounds);
|
||||
|
||||
for (byte round = (numRounds - 1); round > 0; --round) {
|
||||
invShiftRows();
|
||||
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
for (byte col = 0; col < 4; ++col)
|
||||
state[row][col] = inv_sbox[state[row][col] >> 4][state[row][col] & 0x0f];
|
||||
|
||||
addRoundKey(round);
|
||||
invMixColumns();
|
||||
}
|
||||
|
@ -321,7 +311,7 @@ void Aes::invCipher( void )
|
|||
addRoundKey(0);
|
||||
}
|
||||
|
||||
size_t Aes::encrypt( char **data, size_t length, char *key )
|
||||
size_t Aes::encrypt(char **data, std::size_t length, char *key)
|
||||
{
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
@ -329,39 +319,34 @@ void Aes::invCipher( void )
|
|||
if (!setKey(key))
|
||||
return 0;
|
||||
|
||||
size_t old_length = length;
|
||||
size_t oldLength = length;
|
||||
while (length % 16)
|
||||
++length;
|
||||
|
||||
char *buffer = new char[length];
|
||||
memset(buffer, 0, length);
|
||||
memcpy( buffer, *data, old_length );
|
||||
memcpy(buffer, *data, oldLength);
|
||||
|
||||
delete[] * data;
|
||||
*data = buffer;
|
||||
|
||||
size_t cur_block = 0;
|
||||
size_t curBlock = 0;
|
||||
|
||||
do {
|
||||
|
||||
for (byte col = 0; col < 4; ++col)
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
state[ row ][ col ] = static_cast< byte >( ( *data )[ cur_block * 16 + row + 4 * col ] );
|
||||
|
||||
state[row][col] = static_cast<byte>((*data)[curBlock * 16 + row + 4 * col]);
|
||||
cipher();
|
||||
|
||||
for (byte col = 0; col < 4; ++col)
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
( *data )[ cur_block * 16 + row + 4 * col ] = static_cast< char >( state[ row ][ col ] );
|
||||
(*data)[curBlock * 16 + row + 4 * col] = static_cast<char>(state[row][col]);
|
||||
++curBlock;
|
||||
} while (curBlock * 16 != length);
|
||||
|
||||
++cur_block;
|
||||
|
||||
} while( cur_block * 16 != length );
|
||||
|
||||
return cur_block;
|
||||
return curBlock;
|
||||
}
|
||||
|
||||
size_t Aes::decrypt( char **data, size_t length, char *key )
|
||||
size_t Aes::decrypt(char **data, std::size_t length, char *key)
|
||||
{
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
@ -369,38 +354,37 @@ void Aes::invCipher( void )
|
|||
if (!setKey(key))
|
||||
return 0;
|
||||
|
||||
size_t old_length = length;
|
||||
std::size_t oldLength = length;
|
||||
while (length % 16)
|
||||
++length;
|
||||
|
||||
char *buffer = new char[length];
|
||||
memset(buffer, 0, length);
|
||||
memcpy( buffer, *data, old_length );
|
||||
memcpy(buffer, *data, oldLength);
|
||||
|
||||
delete[] * data;
|
||||
*data = buffer;
|
||||
|
||||
size_t cur_block = 0;
|
||||
std::size_t curBlock = 0;
|
||||
|
||||
do {
|
||||
|
||||
for (byte col = 0; col < 4; ++col)
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
state[ row ][ col ] = static_cast< byte >( ( *data )[ cur_block * 16 + row + 4 * col ] );
|
||||
state[row][col] = static_cast<byte>((*data)[curBlock * 16 + row + 4 * col]);
|
||||
|
||||
invCipher();
|
||||
|
||||
for (byte col = 0; col < 4; ++col)
|
||||
for (byte row = 0; row < 4; ++row)
|
||||
( *data )[ cur_block * 16 + row + 4 * col ] = static_cast< char >( state[ row ][ col ] );
|
||||
(*data)[curBlock * 16 + row + 4 * col] = static_cast<char>(state[row][col]);
|
||||
|
||||
++cur_block;
|
||||
++curBlock;
|
||||
|
||||
} while( cur_block * 16 != length );
|
||||
} while (curBlock * 16 != length);
|
||||
|
||||
--length;
|
||||
|
||||
while( ! ( ( *buffer ) + length ) )
|
||||
while (!(static_cast<decltype(length)>(*buffer) + length))
|
||||
--length;
|
||||
|
||||
++length;
|
||||
|
@ -408,4 +392,4 @@ void Aes::invCipher( void )
|
|||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace Crypto
|
||||
|
|
18
aes/aes.h
18
aes/aes.h
|
@ -1,23 +1,23 @@
|
|||
#ifndef AES_INCLUDED
|
||||
#define AES_INCLUDED AES_INCLUDED
|
||||
|
||||
#include <c++utilities/application/global.h>
|
||||
#include "../global.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace Crypto {
|
||||
|
||||
class LIB_EXPORT Aes {
|
||||
class PASSWORD_FILE_EXPORT Aes {
|
||||
|
||||
public:
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned long word;
|
||||
using byte = unsigned char;
|
||||
using word = unsigned long;
|
||||
|
||||
Aes();
|
||||
~Aes();
|
||||
|
||||
size_t encrypt(char **data, size_t length, char *key);
|
||||
size_t decrypt(char **data, size_t length, char *key);
|
||||
std::size_t encrypt(char **data, std::size_t length, char *key);
|
||||
std::size_t decrypt(char **data, std::size_t length, char *key);
|
||||
|
||||
private:
|
||||
static byte gmul(byte a, byte b);
|
||||
|
@ -42,13 +42,13 @@ private:
|
|||
static byte inv_sbox[16][16];
|
||||
static word rcon[52];
|
||||
|
||||
byte key_length;
|
||||
byte num_rounds;
|
||||
byte keyLength;
|
||||
byte numRounds;
|
||||
|
||||
word *w;
|
||||
byte state[4][4];
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Crypto
|
||||
|
||||
#endif /* AES_INCLUDED */
|
||||
|
|
Loading…
Reference in New Issue