From a3438f4ae010ebb21403a83ade76980c1053a08b Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 14 Aug 2022 12:53:19 +0200 Subject: [PATCH] let Crypto::SetAESKey(...) takes the key length as an argument while here: - use the pikotools logger to print some debug messages --- src/Makefile.dep | 29 +++++- src/aes.cpp | 231 ++++++++++++++++++++++++++++++++++------------- src/aes.h | 13 ++- src/crypto.cpp | 19 +++- src/crypto.h | 20 +++- 5 files changed, 236 insertions(+), 76 deletions(-) diff --git a/src/Makefile.dep b/src/Makefile.dep index 9a4b611..fb8c007 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -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 diff --git a/src/aes.cpp b/src/aes.cpp index 919c83e..62b4ea5 100644 --- a/src/aes.cpp +++ b/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 #include #include #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=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 diff --git a/src/aes.h b/src/aes.h index 64f3bd7..99072ab 100644 --- a/src/aes.h +++ b/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 #include +#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); diff --git a/src/crypto.cpp b/src/crypto.cpp index def7db5..b5e41e9 100644 --- a/src/crypto.cpp +++ b/src/crypto.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 @@ -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 diff --git a/src/crypto.h b/src/crypto.h index 8feaa53..f276032 100644 --- a/src/crypto.h +++ b/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();