From a3152233a76d3fb957f6157bbe00743311f179c7 Mon Sep 17 00:00:00 2001 From: ogtomi Date: Sat, 13 May 2023 12:06:19 +0200 Subject: [PATCH 1/8] gcm.h: Initial commit --- meta/gcm.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 meta/gcm.h diff --git a/meta/gcm.h b/meta/gcm.h new file mode 100644 index 0000000..a4f6664 --- /dev/null +++ b/meta/gcm.h @@ -0,0 +1,32 @@ +#include "../conversion/conversion.h" +#include "../random_gen/rng.h" +#include "../generic_functions/generic_functions.h" +#include +#include + +template +class GCM +{ +private: + AlgorithmType& alg; + RNG rng; + uint8_t iv_len = 16; + std::string iv; + +public: + GCM(AlgorithmType& alg) + :alg(alg) + { + iv = rng.generate_iv(iv_len); + }; + + void encrypt(std::string &message) + { + + }; + + void decrypt(std::string &cipher) + { + + }; +}; From 34243abd973bd5cf2b2f286a876aa8dc6850e936 Mon Sep 17 00:00:00 2001 From: ogtomi Date: Sat, 13 May 2023 16:31:22 +0200 Subject: [PATCH 2/8] gcm.h: Encrption with auth tag --- meta/gcm.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/meta/gcm.h b/meta/gcm.h index a4f6664..e9a654c 100644 --- a/meta/gcm.h +++ b/meta/gcm.h @@ -20,9 +20,58 @@ class GCM iv = rng.generate_iv(iv_len); }; - void encrypt(std::string &message) + void encrypt(std::string &message, std::string &auth_tag) { + std::string init_vec = iv; + std::string counter = init_vec; + std::string iv_auth = iv; + uint8_t counter_arr[16]; + std::vector message_vec; + std::string message_len = std::to_string(message.size()); + alg.encrypt(auth_tag); + + split_message(message, message_vec); + + int j = 15; + + for(size_t i = 0; i < message_vec.size(); i++) + { + if(i > 0) + { + init_vec = counter; + } + + alg.encrypt(init_vec); + xor_iv(message_vec[i], init_vec); + xor_iv(auth_tag, message_vec[i]); + + hexstr_to_uint8t(counter, counter_arr); + counter_arr[j]++; + + if(counter_arr[j] == 0xFF) + { + j--; + } + uint8t_to_hexstr(counter, counter_arr, 16); + + for(size_t i = 0; i < message_vec.size(); i++) + { + message += message_vec[i]; + } + } + + + xor_iv(auth_tag, message_len); + alg.encrypt(iv_auth); + xor_iv(auth_tag, iv_auth); + + message = ""; + + for(size_t i = 0; i < message_vec.size(); i++) + { + message += message_vec[i]; + } }; void decrypt(std::string &cipher) From eaab130f786c4777f8bbe0a2b8d7d1c52107bb7f Mon Sep 17 00:00:00 2001 From: ogtomi Date: Sat, 13 May 2023 17:13:41 +0200 Subject: [PATCH 3/8] gcm.h: Decryption + fixes in encryption --- meta/gcm.h | 65 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/meta/gcm.h b/meta/gcm.h index e9a654c..27bddee 100644 --- a/meta/gcm.h +++ b/meta/gcm.h @@ -20,16 +20,13 @@ class GCM iv = rng.generate_iv(iv_len); }; - void encrypt(std::string &message, std::string &auth_tag) + void encrypt(std::string &message, std::string auth_data, std::string &auth_tag) { std::string init_vec = iv; std::string counter = init_vec; - std::string iv_auth = iv; uint8_t counter_arr[16]; std::vector message_vec; - std::string message_len = std::to_string(message.size()); - - alg.encrypt(auth_tag); + std::string message_len = std::to_string(message.size() * 8); split_message(message, message_vec); @@ -44,7 +41,7 @@ class GCM alg.encrypt(init_vec); xor_iv(message_vec[i], init_vec); - xor_iv(auth_tag, message_vec[i]); + xor_iv(auth_data, message_vec[i]); hexstr_to_uint8t(counter, counter_arr); counter_arr[j]++; @@ -61,10 +58,11 @@ class GCM } } - - xor_iv(auth_tag, message_len); - alg.encrypt(iv_auth); - xor_iv(auth_tag, iv_auth); + xor_iv(auth_data, message_len); + + auth_tag = iv; + alg.encrypt(auth_tag); + xor_iv(auth_tag, auth_data); message = ""; @@ -74,8 +72,53 @@ class GCM } }; - void decrypt(std::string &cipher) + void decrypt(std::string &cipher, std::string auth_data, const std::string &auth_tag) { + std::string init_vec = iv; + std::string counter = init_vec; + std::string auth_iv = iv; + uint8_t counter_arr[16]; + std::vector cipher_vec; + std::string cipher_len = std::to_string(cipher.size() * 8); + + split_message(cipher, cipher_vec); + + int j = 15; + + for(size_t i = 0; i < cipher_vec.size(); i++) + { + if(i > 0) + { + init_vec = counter; + } + + alg.encrypt(init_vec); + xor_iv(cipher_vec[i], init_vec); + xor_iv(auth_data, cipher_vec[i]); + + hexstr_to_uint8t(counter, counter_arr); + counter_arr[j]++; + + if(counter_arr[j] == 0xFF) + { + j--; + } + uint8t_to_hexstr(counter, counter_arr, 16); + } + + xor_iv(auth_data, cipher_len); + + alg.encrypt(auth_iv); + xor_iv(auth_iv, auth_data); + + cipher = ""; + + for(size_t i = 0; i < cipher_vec.size(); i++) + { + cipher += cipher_vec[i]; + } + std::cout << auth_iv << std::endl; + std::cout << auth_tag << std::endl; }; }; From 1d21f0f42528ae973a93ecd0e40239fafffc26c9 Mon Sep 17 00:00:00 2001 From: ogtomi Date: Sun, 14 May 2023 12:23:36 +0200 Subject: [PATCH 4/8] gcm.h: Fixed counter values (start with 0 for IV encryption) --- meta/gcm.h | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/meta/gcm.h b/meta/gcm.h index 27bddee..811b0eb 100644 --- a/meta/gcm.h +++ b/meta/gcm.h @@ -31,6 +31,14 @@ class GCM split_message(message, message_vec); int j = 15; + + hexstr_to_uint8t(counter, counter_arr); + if(counter_arr[j] == 0xFF) + { + j--; + } + counter_arr[j]++; + uint8t_to_hexstr(counter, counter_arr, 16); for(size_t i = 0; i < message_vec.size(); i++) { @@ -44,12 +52,11 @@ class GCM xor_iv(auth_data, message_vec[i]); hexstr_to_uint8t(counter, counter_arr); - counter_arr[j]++; - if(counter_arr[j] == 0xFF) { j--; } + counter_arr[j]++; uint8t_to_hexstr(counter, counter_arr, 16); for(size_t i = 0; i < message_vec.size(); i++) @@ -80,11 +87,19 @@ class GCM uint8_t counter_arr[16]; std::vector cipher_vec; std::string cipher_len = std::to_string(cipher.size() * 8); - + split_message(cipher, cipher_vec); int j = 15; + hexstr_to_uint8t(counter, counter_arr); + if(counter_arr[j] == 0xFF) + { + j--; + } + counter_arr[j]++; + uint8t_to_hexstr(counter, counter_arr, 16); + for(size_t i = 0; i < cipher_vec.size(); i++) { if(i > 0) @@ -97,12 +112,11 @@ class GCM xor_iv(auth_data, cipher_vec[i]); hexstr_to_uint8t(counter, counter_arr); - counter_arr[j]++; - if(counter_arr[j] == 0xFF) { j--; } + counter_arr[j]++; uint8t_to_hexstr(counter, counter_arr, 16); } From e3f88f8c790a7efe4c4ff20b7792801d720178eb Mon Sep 17 00:00:00 2001 From: ogtomi Date: Sun, 14 May 2023 15:13:11 +0200 Subject: [PATCH 5/8] generic_functions.cpp: Fixed iteration from 64 to 32 --- generic_functions/generic_functions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generic_functions/generic_functions.cpp b/generic_functions/generic_functions.cpp index 2e85c25..187608d 100644 --- a/generic_functions/generic_functions.cpp +++ b/generic_functions/generic_functions.cpp @@ -9,7 +9,7 @@ void xor_iv(std::string &submessage, const std::string &init_vec) hexstr_to_uint8t(init_vec, init_vec_bin); hexstr_to_uint8t(submessage, submessage_bin); - for(int i = 0; i < 64; i++) + for(int i = 0; i < 32; i++) { submessage_bin[i] ^= init_vec_bin[i]; } From 585adf7a9b89e9875c128be0b440980f512397b0 Mon Sep 17 00:00:00 2001 From: ogtomi Date: Thu, 18 May 2023 13:21:37 +0200 Subject: [PATCH 6/8] gcm.h: Added gf_mul function draft --- meta/gcm.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/meta/gcm.h b/meta/gcm.h index 811b0eb..a1ec826 100644 --- a/meta/gcm.h +++ b/meta/gcm.h @@ -50,6 +50,7 @@ class GCM alg.encrypt(init_vec); xor_iv(message_vec[i], init_vec); xor_iv(auth_data, message_vec[i]); + gf_multiplication(auth_data, iv); hexstr_to_uint8t(counter, counter_arr); if(counter_arr[j] == 0xFF) @@ -110,6 +111,7 @@ class GCM alg.encrypt(init_vec); xor_iv(cipher_vec[i], init_vec); xor_iv(auth_data, cipher_vec[i]); + gf_multiplication(auth_data, iv); hexstr_to_uint8t(counter, counter_arr); if(counter_arr[j] == 0xFF) @@ -135,4 +137,23 @@ class GCM std::cout << auth_iv << std::endl; std::cout << auth_tag << std::endl; }; + + void gf_multiplication(std::string &auth_data, const std::string &hash_key) + { + uint8_t auth_data_bin[16]; + uint8_t hash_key_bin[16]; + size_t data_len = sizeof(auth_data_bin) / (2 * sizeof(*auth_data_bin)); + + uint8_t result_poly[32]; + + hexstr_to_uint8t(hash_key, hash_key_bin); + hexstr_to_uint8t(auth_data, auth_data_bin); + + for(int i = 0; i < 16; i++) + { + + } + + uint8t_to_hexstr(auth_data, auth_data_bin, data_len); + } }; From 485a06990ba4fd99b8fa937f2b5dbf7ad04a5428 Mon Sep 17 00:00:00 2001 From: ogtomi Date: Thu, 18 May 2023 13:22:07 +0200 Subject: [PATCH 7/8] main.cpp: Added auth_data and auth_tag for GCM --- main.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 1fa6ce8..19d200c 100644 --- a/main.cpp +++ b/main.cpp @@ -11,6 +11,7 @@ #include "meta/cfb.h" #include "meta/ofb.h" #include "meta/ctr.h" +#include "meta/gcm.h" int main() { @@ -20,15 +21,17 @@ int main() std::string message = "00112233445566778899aabbccddeeff"; std::string message_ll = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"; + std::string auth_data = "00000000000000000000000000000000"; + std::string auth_tag{}; + AES aes(AES_key_length::AES_128); aes.generate_keys(key_128); - CBC aes_cbc(aes); - - std::cout << "META" << std::endl; + GCM aes_gcm(aes); + std::cout << message_ll << std::endl; - aes_cbc.encrypt(message_ll); + aes_gcm.encrypt(message_ll, auth_data, auth_tag); std::cout << message_ll << std::endl; - aes_cbc.decrypt(message_ll); + aes_gcm.decrypt(message_ll, auth_data, auth_tag); std::cout << message_ll << std::endl; return 0; From 586d0db8f69ad603ea5770518481e212afb40384 Mon Sep 17 00:00:00 2001 From: ogtomi Date: Sun, 21 May 2023 15:08:23 +0200 Subject: [PATCH 8/8] gcm.h: Added 8 to 64 bit array conversion --- meta/gcm.h | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/meta/gcm.h b/meta/gcm.h index a1ec826..68503fc 100644 --- a/meta/gcm.h +++ b/meta/gcm.h @@ -141,19 +141,35 @@ class GCM void gf_multiplication(std::string &auth_data, const std::string &hash_key) { uint8_t auth_data_bin[16]; + uint64_t auth_data_bin_expanded[2]; uint8_t hash_key_bin[16]; - size_t data_len = sizeof(auth_data_bin) / (2 * sizeof(*auth_data_bin)); - - uint8_t result_poly[32]; + uint64_t hash_key_bin_expanded[2]; + + size_t auth_data_len = sizeof(auth_data_bin) / (2 * sizeof(*auth_data_bin)); hexstr_to_uint8t(hash_key, hash_key_bin); hexstr_to_uint8t(auth_data, auth_data_bin); + uint8t_to_uint64t_arr(hash_key_bin, hash_key_bin_expanded, 2); - for(int i = 0; i < 16; i++) - { - + uint8t_to_hexstr(auth_data, auth_data_bin, auth_data_len); + } + + void uint8t_to_uint64t_arr(uint8_t *uint8t_arr, uint64_t *uint64t_arr, const size_t &uint64t_arr_size) + { + int j = 0; + + for(size_t i = 0; i < uint64t_arr_size; i++) + { + uint64t_arr[i] = (((uint64_t)uint8t_arr[j] << 56) & 0xFF00000000000000) | + (((uint64_t)uint8t_arr[j + 1] << 48) & 0x00FF000000000000) | + (((uint64_t)uint8t_arr[j + 2] << 40) & 0x0000FF0000000000) | + (((uint64_t)uint8t_arr[j + 3] << 32) & 0x000000FF00000000) | + (((uint64_t)uint8t_arr[j + 4] << 24) & 0x00000000FF000000) | + (((uint64_t)uint8t_arr[j + 5] << 16) & 0x0000000000FF0000) | + (((uint64_t)uint8t_arr[j + 6] << 8) & 0x000000000000FF00) | + (((uint64_t)uint8t_arr[j + 7]) & 0x00000000000000FF); + + j += 8; } - - uint8t_to_hexstr(auth_data, auth_data_bin, data_len); } };