let Crypto::SetAESKey(...) takes the key length as an argument
while here: - use the pikotools logger to print some debug messages
This commit is contained in:
parent
536ba37e33
commit
a3438f4ae0
|
@ -1,10 +1,33 @@
|
|||
# DO NOT DELETE
|
||||
|
||||
./aes.o: aes.h
|
||||
./aes.o: aes.h ../../pikotools/src/log/log.h
|
||||
./aes.o: ../../pikotools/src/textstream/textstream.h
|
||||
./aes.o: ../../pikotools/src/textstream/stream.h
|
||||
./aes.o: ../../pikotools/src/space/space.h
|
||||
./aes.o: ../../pikotools/src/textstream/types.h
|
||||
./aes.o: ../../pikotools/src/convert/inttostr.h
|
||||
./aes.o: ../../pikotools/src/utf8/utf8.h
|
||||
./aes.o: ../../pikotools/src/textstream/stream.h
|
||||
./aes.o: ../../pikotools/src/utf8/utf8_templates.h
|
||||
./aes.o: ../../pikotools/src/utf8/utf8_private.h
|
||||
./aes.o: ../../pikotools/src/date/date.h
|
||||
./aes.o: ../../pikotools/src/membuffer/membuffer.h
|
||||
./aes.o: ../../pikotools/src/textstream/types.h
|
||||
./aes.o: ../../pikotools/src/log/filelog.h
|
||||
./base32.o: base32.h
|
||||
./base64.o: base64.h
|
||||
./crypto.o: crypto.h aes.h base64.h ../../pikotools/src/utf8/utf8.h
|
||||
./crypto.o: crypto.h aes.h ../../pikotools/src/log/log.h
|
||||
./crypto.o: ../../pikotools/src/textstream/textstream.h
|
||||
./crypto.o: ../../pikotools/src/textstream/stream.h
|
||||
./crypto.o: ../../pikotools/src/space/space.h
|
||||
./crypto.o: ../../pikotools/src/textstream/types.h
|
||||
./crypto.o: ../../pikotools/src/convert/inttostr.h
|
||||
./crypto.o: ../../pikotools/src/utf8/utf8.h
|
||||
./crypto.o: ../../pikotools/src/textstream/stream.h
|
||||
./crypto.o: ../../pikotools/src/utf8/utf8_templates.h
|
||||
./crypto.o: ../../pikotools/src/utf8/utf8_private.h misc.h
|
||||
./crypto.o: ../../pikotools/src/utf8/utf8_private.h
|
||||
./crypto.o: ../../pikotools/src/date/date.h
|
||||
./crypto.o: ../../pikotools/src/membuffer/membuffer.h
|
||||
./crypto.o: ../../pikotools/src/textstream/types.h
|
||||
./crypto.o: ../../pikotools/src/log/filelog.h base64.h misc.h
|
||||
./misc.o: misc.h
|
||||
|
|
231
src/aes.cpp
231
src/aes.cpp
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2021, Tomasz Sowa
|
||||
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -32,7 +32,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "aes.h"
|
||||
|
@ -178,6 +177,11 @@ AES::uint8 AES::gf_mul14[16][16] = {
|
|||
|
||||
|
||||
|
||||
AES::AES()
|
||||
{
|
||||
plog = nullptr;
|
||||
}
|
||||
|
||||
|
||||
void AES::CopyToData(const AES::uint8 * state)
|
||||
{
|
||||
|
@ -252,15 +256,38 @@ void AES::PrintData()
|
|||
int x, y;
|
||||
char buf[20];
|
||||
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
if( plog )
|
||||
{
|
||||
for(x=0 ; x<4 ; ++x)
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
{
|
||||
sprintf(buf, "%02x ", data[y][x]);
|
||||
std::cout << buf;
|
||||
for(x=0 ; x<4 ; ++x)
|
||||
{
|
||||
sprintf(buf, "%02x ", data[y][x]);
|
||||
(*plog) << pt::Log::log4 << buf;
|
||||
}
|
||||
|
||||
(*plog) << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AES::PrintKey(AES::uint8 * key_src, int key_length)
|
||||
{
|
||||
char buf[20];
|
||||
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << "AES key, key_length: " << key_length << pt::Log::logend;
|
||||
|
||||
for(int i=0 ; i < key_length ; ++i)
|
||||
{
|
||||
sprintf(buf, "%02x", key_src[i]);
|
||||
(*plog) << pt::Log::log4 << buf << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
(*plog) << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -270,15 +297,18 @@ void AES::PrintKey(int xs)
|
|||
int x, y;
|
||||
char buf[20];
|
||||
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
if( plog )
|
||||
{
|
||||
for(x=xs ; x<xs+key_col ; ++x)
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
{
|
||||
sprintf(buf, "%02x ", key[y][x]);
|
||||
std::cout << buf;
|
||||
}
|
||||
for(x=xs ; x<xs+key_col ; ++x)
|
||||
{
|
||||
sprintf(buf, "%02x ", key[y][x]);
|
||||
(*plog) << pt::Log::log4 << buf << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
(*plog) << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -287,15 +317,20 @@ void AES::PrintKey2(int xs)
|
|||
int x, y;
|
||||
char buf[20];
|
||||
|
||||
for(x=xs ; x<xs+key_col ; ++x)
|
||||
if( plog )
|
||||
{
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
{
|
||||
sprintf(buf, "%02x", key[y][x]);
|
||||
std::cout << buf;
|
||||
}
|
||||
(*plog) << pt::Log::log4 << "PrintKey2, xs=" << xs << pt::Log::logend;
|
||||
|
||||
std::cout << std::endl;
|
||||
for(x=xs ; x<xs+key_col ; ++x)
|
||||
{
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
{
|
||||
sprintf(buf, "%02x", key[y][x]);
|
||||
(*plog) << pt::Log::log4 << buf << " ";
|
||||
}
|
||||
|
||||
(*plog) << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -305,15 +340,19 @@ void AES::PrintKey3(int x)
|
|||
int y;
|
||||
char buf[20];
|
||||
|
||||
std::cout << x << " ";
|
||||
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
if( plog )
|
||||
{
|
||||
sprintf(buf, "%02x", key[y][x]);
|
||||
std::cout << buf;
|
||||
}
|
||||
(*plog) << pt::Log::log4 << "PrintKey3" << pt::Log::logend;
|
||||
(*plog) << pt::Log::log4 << x << " ";
|
||||
|
||||
std::cout << std::endl;
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
{
|
||||
sprintf(buf, "%02x", key[y][x]);
|
||||
(*plog) << pt::Log::log4 << buf << " ";
|
||||
}
|
||||
|
||||
(*plog) << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -464,6 +503,7 @@ bool AES::Key(AES::uint8 * key_src, int key_length)
|
|||
{
|
||||
int x, y, index = 0;
|
||||
|
||||
PrintKey(key_src, key_length);
|
||||
key_len = key_length;
|
||||
|
||||
if( key_len == 16 )
|
||||
|
@ -485,6 +525,12 @@ int x, y, index = 0;
|
|||
}
|
||||
else
|
||||
{
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log2 << "AES: I cannot initialize the AES key, incorrect key size: " << key_length
|
||||
<< ", expected: 16, 24 or 32 " << pt::Log::logend;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -521,6 +567,11 @@ void AES::KeyExpansion()
|
|||
int i, y;
|
||||
int rcon_index = 0;
|
||||
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << "AES key expansion" << pt::Log::logend;
|
||||
}
|
||||
|
||||
for(i=key_col ; i < 4*(round+1) ; ++i)
|
||||
{
|
||||
for(y=0 ; y<4 ; ++y)
|
||||
|
@ -542,20 +593,23 @@ int rcon_index = 0;
|
|||
key[y][i] ^= key[y][i-key_col];
|
||||
|
||||
|
||||
|
||||
//PrintKey3(i);
|
||||
|
||||
/*
|
||||
if( i % key_col == 0 )
|
||||
if( plog )
|
||||
{
|
||||
std::cout << " ---- " << (rcon_index-1) << std::endl;
|
||||
PrintKey2(i-key_col);
|
||||
PrintKey3(i);
|
||||
|
||||
if( i % key_col == 0 )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " ---- " << (rcon_index-1) << pt::Log::logend;
|
||||
PrintKey2(i-key_col);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
//std::cout << " ---- " << rcon_index << std::endl;
|
||||
//PrintKey2(i-key_col);
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " ---- " << rcon_index << pt::Log::logend;
|
||||
PrintKey2(i-key_col);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -575,39 +629,56 @@ void AES::EncodeData()
|
|||
{
|
||||
int i;
|
||||
|
||||
// std::cout << " input data " << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " input data " << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
AddRoundKey(0);
|
||||
|
||||
// std::cout << " first round key " << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " first round key " << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
for(i=0 ; i<round ; ++i)
|
||||
{
|
||||
SubBytes();
|
||||
|
||||
// std::cout << " after sub bytes, i= " << i+1 << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after sub bytes, i= " << i+1 << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
ShiftRows();
|
||||
|
||||
// std::cout << " after shift rows, i= " << i+1 << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after shift rows, i= " << i+1 << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
if( i != round-1 )
|
||||
{
|
||||
MixColumns();
|
||||
|
||||
// std::cout << " after mix columns, i= " << i+1 << std::endl;
|
||||
// PrintData();
|
||||
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after mix columns, i= " << i+1 << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
}
|
||||
|
||||
AddRoundKey((i+1)*4);
|
||||
|
||||
// std::cout << " after add round key, i= " << i+1 << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after add round key, i= " << i+1 << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -728,37 +799,55 @@ void AES::DecodeData()
|
|||
{
|
||||
int i;
|
||||
|
||||
// std::cout << " input data " << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " input data " << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
AddRoundKey(round*4);
|
||||
|
||||
// std::cout << " first round key " << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " first round key " << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
for(i=round-1 ; i>=0 ; --i)
|
||||
{
|
||||
InvShiftRows();
|
||||
|
||||
// std::cout << " after invshiftrows, i= " << i << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after invshiftrows, i= " << i << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
InvSubBytes();
|
||||
|
||||
// std::cout << " after invsubbutes, i= " << i << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after invsubbutes, i= " << i << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
AddRoundKey(i*4);
|
||||
|
||||
// std::cout << " after add round key, i= " << i << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after add round key, i= " << i << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
|
||||
if( i != 0 )
|
||||
{
|
||||
InvMixColumns();
|
||||
|
||||
// std::cout << " after inv mix columns, i= " << i << std::endl;
|
||||
// PrintData();
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << " after inv mix columns, i= " << i << pt::Log::logend;
|
||||
PrintData();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1418,7 +1507,11 @@ bool ok = true;
|
|||
|
||||
if( v1 != v2 )
|
||||
{
|
||||
std::cout << "error: y: " << y << ", x: " << x << ", v1: " << v1 << ", v2: " << v2 << std::endl;
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log2 << "error: y: " << y << ", x: " << x << ", v1: " << v1 << ", v2: " << v2 << pt::Log::logend;
|
||||
}
|
||||
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
@ -1440,12 +1533,22 @@ bool ok9, ok11, ok13, ok14;
|
|||
bool ok = ok9 && ok11 && ok13 && ok14;
|
||||
|
||||
if( ok )
|
||||
std::cout << "All GF tables are good" << std::endl;
|
||||
{
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log4 << "All GF tables are good" << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
void AES::set_logger(pt::Log * plog)
|
||||
{
|
||||
this->plog = plog;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Tito
|
||||
|
|
13
src/aes.h
13
src/aes.h
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012, Tomasz Sowa
|
||||
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -37,6 +37,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "log/log.h"
|
||||
|
||||
|
||||
namespace Tito
|
||||
|
@ -52,6 +53,7 @@ typedef unsigned short int uint16;
|
|||
typedef unsigned int uint32;
|
||||
|
||||
|
||||
AES();
|
||||
|
||||
// 'state'
|
||||
// 128 bits data block (16 bytes)
|
||||
|
@ -111,12 +113,19 @@ typedef unsigned int uint32;
|
|||
// printing 'data' tab
|
||||
// for debug purposes
|
||||
void PrintData();
|
||||
void PrintKey(AES::uint8 * key_src, int key_length);
|
||||
void PrintKey(int xs);
|
||||
void PrintKey2(int xs);
|
||||
void PrintKey3(int xs);
|
||||
bool CheckGFTables();
|
||||
|
||||
|
||||
/*
|
||||
* pikotools logger
|
||||
* default: null (no logging)
|
||||
*/
|
||||
void set_logger(pt::Log * plog);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
@ -160,6 +169,8 @@ private:
|
|||
static uint8 gf_mul13[16][16];
|
||||
static uint8 gf_mul14[16][16];
|
||||
|
||||
pt::Log * plog;
|
||||
|
||||
// returning a coresponding value from sbox table
|
||||
uint8 SubBytes(AES::uint8 v);
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2021, Tomasz Sowa
|
||||
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -83,15 +83,20 @@ void Crypto::AssignString(const std::wstring & src, std::string & dst, bool clea
|
|||
Crypto::Crypto()
|
||||
{
|
||||
dummy_char = 0;
|
||||
plog = nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Crypto::SetAESKey(unsigned char aes_key[32])
|
||||
bool Crypto::SetAESKey(unsigned char * aes_key, int key_length)
|
||||
{
|
||||
if( !aes.Key(aes_key, 32) )
|
||||
if( !aes.Key(aes_key, key_length) )
|
||||
{
|
||||
//log << log1 << "problem with AES key" << logend;
|
||||
if( plog )
|
||||
{
|
||||
(*plog) << pt::Log::log2 << "Crypto: I cannot initialize the AES key" << pt::Log::logend;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -252,6 +257,12 @@ void Crypto::Clear()
|
|||
Clear(aes_str);
|
||||
}
|
||||
|
||||
void Crypto::set_logger(pt::Log * plog)
|
||||
{
|
||||
this->plog = plog;
|
||||
aes.set_logger(plog);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Tito
|
||||
|
|
20
src/crypto.h
20
src/crypto.h
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012, Tomasz Sowa
|
||||
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -37,6 +37,7 @@
|
|||
|
||||
#include "aes.h"
|
||||
#include "base64.h"
|
||||
#include "log/log.h"
|
||||
|
||||
|
||||
namespace Tito
|
||||
|
@ -51,10 +52,9 @@ public:
|
|||
Crypto();
|
||||
|
||||
/*
|
||||
setting AES256 key
|
||||
the table should consists of 32 bytes
|
||||
setting AES key (key length can be: 16, 24 or 32 bytes)
|
||||
*/
|
||||
bool SetAESKey(unsigned char aes_key[32]);
|
||||
bool SetAESKey(unsigned char * aes_key, int key_length);
|
||||
|
||||
|
||||
/*
|
||||
|
@ -85,6 +85,14 @@ public:
|
|||
void Clear(BufferType & str);
|
||||
|
||||
|
||||
/*
|
||||
* pikotools logger
|
||||
* default: null (no logging)
|
||||
*/
|
||||
void set_logger(pt::Log * plog);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// AES cryptography
|
||||
|
@ -107,6 +115,10 @@ private:
|
|||
// a dummy character used when an input string is empty
|
||||
unsigned char dummy_char;
|
||||
|
||||
// logger, default null
|
||||
pt::Log * plog;
|
||||
|
||||
|
||||
void Clear();
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue