Skip to content

Commit

Permalink
run clang format for all sources
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdcvlsc committed Jun 6, 2024
1 parent 22a70c3 commit 9468b1a
Show file tree
Hide file tree
Showing 33 changed files with 1,591 additions and 1,905 deletions.
316 changes: 141 additions & 175 deletions ChaCha20-Poly1305.cpp

Large diffs are not rendered by default.

78 changes: 35 additions & 43 deletions header-build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,86 @@

#include "Header-Mode-ChaCha20-Poly1305.hpp"

void printChar(const std::string& name, unsigned char* arr, size_t len) {
void printChar(const std::string &name, unsigned char *arr, size_t len) {
std::cout << name << " = ";
for(size_t i=0; i<len; ++i) {
std::cout << (unsigned char)arr[i];
for (size_t i = 0; i < len; ++i) {
std::cout << (unsigned char) arr[i];
}
std::cout << "\n";
}

int main() {
std::string lorem =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
"sed do eiusmod tempor incididunt ut labore et dolore mag"
"na aliqua. Ut enim ad minim veniam, quis nostrud exercit"
"ation ullamco laboris nisi ut aliquip ex ea commodo cons"
"equat. Duis aute irure dolor in reprehenderit in volupta"
"te velit esse cillum dolore eu fugiat nulla pariatur. Ex"
"cepteur sint occaecat cupidatat non proident, sunt in cu"
"lpa qui officia deserunt mollit anim id est laborum.";
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
"sed do eiusmod tempor incididunt ut labore et dolore mag"
"na aliqua. Ut enim ad minim veniam, quis nostrud exercit"
"ation ullamco laboris nisi ut aliquip ex ea commodo cons"
"equat. Duis aute irure dolor in reprehenderit in volupta"
"te velit esse cillum dolore eu fugiat nulla pariatur. Ex"
"cepteur sint occaecat cupidatat non proident, sunt in cu"
"lpa qui officia deserunt mollit anim id est laborum.";

std::string key = "MyRandomKeyThatShouldBe32char...";

unsigned char AAD[12] = {
0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7
};
unsigned char AAD[12] = {0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7};

unsigned char nonce[] = {
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
unsigned char nonce[] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

unsigned char *iv = nonce+4;
unsigned char *iv = nonce + 4;
unsigned char *constant = nonce;

// encryption (with C++ style casting on parameters).
unsigned char *cipher_text = new unsigned char[lorem.size()];
unsigned char *encrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
ChaCha20_Poly1305::aead_encrypt(
cipher_text,encrypt_mac,
reinterpret_cast<const unsigned char*>(lorem.data()),lorem.size(),
AAD,sizeof(AAD),reinterpret_cast<const unsigned char*>(key.data()),iv,constant
cipher_text, encrypt_mac, reinterpret_cast<const unsigned char *>(lorem.data()), lorem.size(), AAD, sizeof(AAD),
reinterpret_cast<const unsigned char *>(key.data()), iv, constant
);

// decryption (with C style casting on parameters).
unsigned char *recover_text = new unsigned char[lorem.size()];
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
ChaCha20_Poly1305::aead_decrypt(
recover_text,decrypt_mac,
cipher_text,lorem.size(),
AAD,sizeof(AAD),(const unsigned char*)key.data(),iv,constant
recover_text, decrypt_mac, cipher_text, lorem.size(), AAD, sizeof(AAD), (const unsigned char *) key.data(), iv,
constant
);

// you can use whatever cast you want C or C++.

// compare plain text and recovered text
bool correct = true;
for(size_t i=0; i<lorem.size(); ++i) {
if(recover_text[i]!=(unsigned char)(lorem[i])) {
for (size_t i = 0; i < lorem.size(); ++i) {
if (recover_text[i] != (unsigned char) (lorem[i])) {
correct = false;
break;
}
}

bool authentic = true;
for(size_t i=0; i<POLY1305_MAC_BYTES; ++i) {
if(encrypt_mac[i]!=decrypt_mac[i]) {
for (size_t i = 0; i < POLY1305_MAC_BYTES; ++i) {
if (encrypt_mac[i] != decrypt_mac[i]) {
authentic = false;
break;
}
}
delete [] cipher_text;
delete [] encrypt_mac;
delete [] recover_text;
delete [] decrypt_mac;
if(correct) {

delete[] cipher_text;
delete[] encrypt_mac;
delete[] recover_text;
delete[] decrypt_mac;

if (correct) {
std::cout << "The Decrypted Message is Correct\n";
}
else {
} else {
std::cout << "The Decrypted Message is Wrong!!!\n";
}

if(authentic) {
if (authentic) {
std::cout << "Message is Authentic\n";
}
else
} else
std::cout << "The Message was Altered\n";

if(correct && authentic)
if (correct && authentic)
return 0;
return 1;
}
70 changes: 31 additions & 39 deletions static-build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,76 @@

int main() {
std::string lorem =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
"sed do eiusmod tempor incididunt ut labore et dolore mag"
"na aliqua. Ut enim ad minim veniam, quis nostrud exercit"
"ation ullamco laboris nisi ut aliquip ex ea commodo cons"
"equat. Duis aute irure dolor in reprehenderit in volupta"
"te velit esse cillum dolore eu fugiat nulla pariatur. Ex"
"cepteur sint occaecat cupidatat non proident, sunt in cu"
"lpa qui officia deserunt mollit anim id est laborum.";
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
"sed do eiusmod tempor incididunt ut labore et dolore mag"
"na aliqua. Ut enim ad minim veniam, quis nostrud exercit"
"ation ullamco laboris nisi ut aliquip ex ea commodo cons"
"equat. Duis aute irure dolor in reprehenderit in volupta"
"te velit esse cillum dolore eu fugiat nulla pariatur. Ex"
"cepteur sint occaecat cupidatat non proident, sunt in cu"
"lpa qui officia deserunt mollit anim id est laborum.";

std::string key = "MyRandomKeyThatShouldBe32char...";

unsigned char AAD[12] = {
0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7
};
unsigned char AAD[12] = {0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7};

unsigned char nonce[] = {
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
unsigned char nonce[] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

unsigned char *iv = nonce+4;
unsigned char *iv = nonce + 4;
unsigned char *constant = nonce;

// encryption (with C++ style casting on parameters).
unsigned char *cipher_text = new unsigned char[lorem.size()];
unsigned char *encrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
ChaCha20_Poly1305::aead_encrypt(
cipher_text,encrypt_mac,
reinterpret_cast<const unsigned char*>(lorem.data()),lorem.size(),
AAD,sizeof(AAD),reinterpret_cast<const unsigned char*>(key.data()),iv,constant
cipher_text, encrypt_mac, reinterpret_cast<const unsigned char *>(lorem.data()), lorem.size(), AAD, sizeof(AAD),
reinterpret_cast<const unsigned char *>(key.data()), iv, constant
);

// decryption (with C style casting on parameters).
unsigned char *recover_text = new unsigned char[lorem.size()];
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
ChaCha20_Poly1305::aead_decrypt(
recover_text,decrypt_mac,
cipher_text,lorem.size(),
AAD,sizeof(AAD),(const unsigned char*)key.data(),iv,constant
recover_text, decrypt_mac, cipher_text, lorem.size(), AAD, sizeof(AAD), (const unsigned char *) key.data(), iv,
constant
);

// you can use whatever cast you want C or C++.

// compare plain text and recovered text
bool correct = true;
for(size_t i=0; i<lorem.size(); ++i) {
if(recover_text[i]!=(unsigned char)(lorem[i])) {
for (size_t i = 0; i < lorem.size(); ++i) {
if (recover_text[i] != (unsigned char) (lorem[i])) {
correct = false;
break;
}
}

bool authentic = true;
for(size_t i=0; i<POLY1305_MAC_BYTES; ++i) {
if(encrypt_mac[i]!=decrypt_mac[i]) {
for (size_t i = 0; i < POLY1305_MAC_BYTES; ++i) {
if (encrypt_mac[i] != decrypt_mac[i]) {
authentic = false;
break;
}
}

delete [] cipher_text;
delete [] encrypt_mac;
delete [] recover_text;
delete [] decrypt_mac;
if(correct) {
delete[] cipher_text;
delete[] encrypt_mac;
delete[] recover_text;
delete[] decrypt_mac;

if (correct) {
std::cout << "The Decrypted Message is Correct\n";
}
else {
} else {
std::cout << "The Decrypted Message is Wrong!!!\n";
}

if(authentic) {
if (authentic) {
std::cout << "Message is Authentic\n";
}
else
} else
std::cout << "The Message was Altered\n";

if(correct && authentic)
if (correct && authentic)
return 0;
return 1;
}
74 changes: 31 additions & 43 deletions tests/BlockFunction_test.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,61 @@
#include <iostream>

#ifndef _MAKE_LIB
#include "../Header-Mode-ChaCha20-Poly1305.hpp"
#include "../Header-Mode-ChaCha20-Poly1305.hpp"
#else
#include <ChaCha20-Poly1305.hpp>
#include <ChaCha20-Poly1305.hpp>
#endif

// ChaCha State Comparison
bool CompareState(unsigned int *A, unsigned int *B){
for(size_t i=0; i<16; ++i)
if(A[i] != B[i])
bool CompareState(unsigned int *A, unsigned int *B) {
for (size_t i = 0; i < 16; ++i)
if (A[i] != B[i])
return false;
return true;
}

int main()
{
unsigned char key[32] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f
};
int main() {
unsigned char key[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};

unsigned char nonce[12] = {
0x00,0x00,0x00,0x09,0x00,0x00,
0x00,0x4a,0x00,0x00,0x00,0x00
};
unsigned char nonce[12] = {0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00};

unsigned int correct_output_state[16] = {
0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3,
0xc7f4d1c7, 0x0368c033, 0x9aaa2204, 0x4e6cd4c3,
0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9,
0xd19c12b5, 0xb94e16de, 0xe883d0cb, 0x4e3c50a2
};
unsigned int correct_output_state[16] = {0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3, 0xc7f4d1c7, 0x0368c033,
0x9aaa2204, 0x4e6cd4c3, 0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9,
0xd19c12b5, 0xb94e16de, 0xe883d0cb, 0x4e3c50a2};

unsigned char correct_serial[64] = {
0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4,
0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03, 0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e,
0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09, 0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2,
0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9, 0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e
};
unsigned char correct_serial[64] = {0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, 0xdd, 0x1f, 0xa3,
0x20, 0x71, 0xc4, 0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03, 0x04, 0x22,
0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e, 0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa,
0x09, 0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2, 0xb5, 0x12, 0x9c, 0xd1,
0xde, 0x16, 0x4e, 0xb9, 0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e};

unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];
unsigned int *output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];

chacha20::init_state(initial_state,(unsigned int*)key,1,(unsigned int*)nonce);
chacha20::apply_20rounds(output_state,initial_state);
unsigned char* serialize = (unsigned char*) output_state;
chacha20::init_state(initial_state, (unsigned int *) key, 1, (unsigned int *) nonce);
chacha20::apply_20rounds(output_state, initial_state);

unsigned char *serialize = (unsigned char *) output_state;

bool serial_passed = true;
for(size_t i=0; i<CHACHA20_BLK_FUNC_OUTPUT_BYTES; ++i) {
if(serialize[i] != correct_serial[i]) {
for (size_t i = 0; i < CHACHA20_BLK_FUNC_OUTPUT_BYTES; ++i) {
if (serialize[i] != correct_serial[i]) {
serial_passed = false;
break;
}
}

bool passed = CompareState(correct_output_state,output_state);

delete [] initial_state;
delete [] output_state;
bool passed = CompareState(correct_output_state, output_state);

delete[] initial_state;
delete[] output_state;

if(passed && serial_passed) {
if (passed && serial_passed) {
std::cout << "ChaChaBlockFunctionTest : PASSED\n";
return 0;
}
else {
} else {
std::cout << "ChaChaBlockFunctionTest : FAILED\n";
return 1;
}
Expand Down
Loading

0 comments on commit 9468b1a

Please sign in to comment.