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:
Tomasz Sowa 2022-08-14 12:53:19 +02:00
parent 536ba37e33
commit a3438f4ae0
5 changed files with 236 additions and 76 deletions

View File

@ -1,10 +1,33 @@
# DO NOT DELETE # 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 ./base32.o: base32.h
./base64.o: base64.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/textstream/stream.h
./crypto.o: ../../pikotools/src/utf8/utf8_templates.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 ./misc.o: misc.h

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2012-2021, Tomasz Sowa * Copyright (c) 2012-2022, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -32,7 +32,6 @@
* *
*/ */
#include <iostream>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include "aes.h" #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) void AES::CopyToData(const AES::uint8 * state)
{ {
@ -252,15 +256,38 @@ void AES::PrintData()
int x, y; int x, y;
char buf[20]; 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]); for(x=0 ; x<4 ; ++x)
std::cout << buf; {
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; int x, y;
char buf[20]; 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]); for(x=xs ; x<xs+key_col ; ++x)
std::cout << buf; {
} 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; int x, y;
char buf[20]; char buf[20];
for(x=xs ; x<xs+key_col ; ++x) if( plog )
{ {
for(y=0 ; y<4 ; ++y) (*plog) << pt::Log::log4 << "PrintKey2, xs=" << xs << pt::Log::logend;
{
sprintf(buf, "%02x", key[y][x]);
std::cout << buf;
}
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; int y;
char buf[20]; char buf[20];
std::cout << x << " "; if( plog )
for(y=0 ; y<4 ; ++y)
{ {
sprintf(buf, "%02x", key[y][x]); (*plog) << pt::Log::log4 << "PrintKey3" << pt::Log::logend;
std::cout << buf; (*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; int x, y, index = 0;
PrintKey(key_src, key_length);
key_len = key_length; key_len = key_length;
if( key_len == 16 ) if( key_len == 16 )
@ -485,6 +525,12 @@ int x, y, index = 0;
} }
else 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; return false;
} }
@ -521,6 +567,11 @@ void AES::KeyExpansion()
int i, y; int i, y;
int rcon_index = 0; 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(i=key_col ; i < 4*(round+1) ; ++i)
{ {
for(y=0 ; y<4 ; ++y) for(y=0 ; y<4 ; ++y)
@ -542,20 +593,23 @@ int rcon_index = 0;
key[y][i] ^= key[y][i-key_col]; key[y][i] ^= key[y][i-key_col];
if( plog )
//PrintKey3(i);
/*
if( i % key_col == 0 )
{ {
std::cout << " ---- " << (rcon_index-1) << std::endl; PrintKey3(i);
PrintKey2(i-key_col);
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; if( plog )
//PrintKey2(i-key_col); {
(*plog) << pt::Log::log4 << " ---- " << rcon_index << pt::Log::logend;
PrintKey2(i-key_col);
}
} }
@ -575,39 +629,56 @@ void AES::EncodeData()
{ {
int i; int i;
// std::cout << " input data " << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " input data " << pt::Log::logend;
PrintData();
}
AddRoundKey(0); AddRoundKey(0);
// std::cout << " first round key " << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " first round key " << pt::Log::logend;
PrintData();
}
for(i=0 ; i<round ; ++i) for(i=0 ; i<round ; ++i)
{ {
SubBytes(); SubBytes();
// std::cout << " after sub bytes, i= " << i+1 << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after sub bytes, i= " << i+1 << pt::Log::logend;
PrintData();
}
ShiftRows(); ShiftRows();
// std::cout << " after shift rows, i= " << i+1 << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after shift rows, i= " << i+1 << pt::Log::logend;
PrintData();
}
if( i != round-1 ) if( i != round-1 )
{ {
MixColumns(); MixColumns();
// std::cout << " after mix columns, i= " << i+1 << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after mix columns, i= " << i+1 << pt::Log::logend;
PrintData();
}
} }
AddRoundKey((i+1)*4); AddRoundKey((i+1)*4);
// std::cout << " after add round key, i= " << i+1 << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after add round key, i= " << i+1 << pt::Log::logend;
PrintData();
}
} }
} }
@ -728,37 +799,55 @@ void AES::DecodeData()
{ {
int i; int i;
// std::cout << " input data " << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " input data " << pt::Log::logend;
PrintData();
}
AddRoundKey(round*4); AddRoundKey(round*4);
// std::cout << " first round key " << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " first round key " << pt::Log::logend;
PrintData();
}
for(i=round-1 ; i>=0 ; --i) for(i=round-1 ; i>=0 ; --i)
{ {
InvShiftRows(); InvShiftRows();
// std::cout << " after invshiftrows, i= " << i << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after invshiftrows, i= " << i << pt::Log::logend;
PrintData();
}
InvSubBytes(); InvSubBytes();
// std::cout << " after invsubbutes, i= " << i << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after invsubbutes, i= " << i << pt::Log::logend;
PrintData();
}
AddRoundKey(i*4); AddRoundKey(i*4);
// std::cout << " after add round key, i= " << i << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after add round key, i= " << i << pt::Log::logend;
PrintData();
}
if( i != 0 ) if( i != 0 )
{ {
InvMixColumns(); InvMixColumns();
// std::cout << " after inv mix columns, i= " << i << std::endl; if( plog )
// PrintData(); {
(*plog) << pt::Log::log4 << " after inv mix columns, i= " << i << pt::Log::logend;
PrintData();
}
} }
} }
} }
@ -1418,7 +1507,11 @@ bool ok = true;
if( v1 != v2 ) 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; ok = false;
} }
} }
@ -1440,12 +1533,22 @@ bool ok9, ok11, ok13, ok14;
bool ok = ok9 && ok11 && ok13 && ok14; bool ok = ok9 && ok11 && ok13 && ok14;
if( ok ) 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; return ok;
} }
void AES::set_logger(pt::Log * plog)
{
this->plog = plog;
}
} // namespace Tito } // namespace Tito

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2012, Tomasz Sowa * Copyright (c) 2012-2022, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -37,6 +37,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "log/log.h"
namespace Tito namespace Tito
@ -52,6 +53,7 @@ typedef unsigned short int uint16;
typedef unsigned int uint32; typedef unsigned int uint32;
AES();
// 'state' // 'state'
// 128 bits data block (16 bytes) // 128 bits data block (16 bytes)
@ -111,12 +113,19 @@ typedef unsigned int uint32;
// printing 'data' tab // printing 'data' tab
// for debug purposes // for debug purposes
void PrintData(); void PrintData();
void PrintKey(AES::uint8 * key_src, int key_length);
void PrintKey(int xs); void PrintKey(int xs);
void PrintKey2(int xs); void PrintKey2(int xs);
void PrintKey3(int xs); void PrintKey3(int xs);
bool CheckGFTables(); bool CheckGFTables();
/*
* pikotools logger
* default: null (no logging)
*/
void set_logger(pt::Log * plog);
private: private:
@ -160,6 +169,8 @@ private:
static uint8 gf_mul13[16][16]; static uint8 gf_mul13[16][16];
static uint8 gf_mul14[16][16]; static uint8 gf_mul14[16][16];
pt::Log * plog;
// returning a coresponding value from sbox table // returning a coresponding value from sbox table
uint8 SubBytes(AES::uint8 v); uint8 SubBytes(AES::uint8 v);

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2012-2021, Tomasz Sowa * Copyright (c) 2012-2022, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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() Crypto::Crypto()
{ {
dummy_char = 0; 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; return false;
} }
@ -252,6 +257,12 @@ void Crypto::Clear()
Clear(aes_str); Clear(aes_str);
} }
void Crypto::set_logger(pt::Log * plog)
{
this->plog = plog;
aes.set_logger(plog);
}
} // namespace Tito } // namespace Tito

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2012, Tomasz Sowa * Copyright (c) 2012-2022, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -37,6 +37,7 @@
#include "aes.h" #include "aes.h"
#include "base64.h" #include "base64.h"
#include "log/log.h"
namespace Tito namespace Tito
@ -51,10 +52,9 @@ public:
Crypto(); Crypto();
/* /*
setting AES256 key setting AES key (key length can be: 16, 24 or 32 bytes)
the table should consists of 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); void Clear(BufferType & str);
/*
* pikotools logger
* default: null (no logging)
*/
void set_logger(pt::Log * plog);
private: private:
// AES cryptography // AES cryptography
@ -107,6 +115,10 @@ private:
// a dummy character used when an input string is empty // a dummy character used when an input string is empty
unsigned char dummy_char; unsigned char dummy_char;
// logger, default null
pt::Log * plog;
void Clear(); void Clear();