Skip to content

Commit 9468b1a

Browse files
committed
run clang format for all sources
1 parent 22a70c3 commit 9468b1a

33 files changed

+1591
-1905
lines changed

ChaCha20-Poly1305.cpp

Lines changed: 141 additions & 175 deletions
Large diffs are not rendered by default.

header-build.cpp

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,86 @@
55

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

8-
void printChar(const std::string& name, unsigned char* arr, size_t len) {
8+
void printChar(const std::string &name, unsigned char *arr, size_t len) {
99
std::cout << name << " = ";
10-
for(size_t i=0; i<len; ++i) {
11-
std::cout << (unsigned char)arr[i];
10+
for (size_t i = 0; i < len; ++i) {
11+
std::cout << (unsigned char) arr[i];
1212
}
1313
std::cout << "\n";
1414
}
1515

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

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

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

33-
unsigned char nonce[] = {
34-
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
35-
};
31+
unsigned char nonce[] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
3632

37-
unsigned char *iv = nonce+4;
33+
unsigned char *iv = nonce + 4;
3834
unsigned char *constant = nonce;
3935

4036
// encryption (with C++ style casting on parameters).
4137
unsigned char *cipher_text = new unsigned char[lorem.size()];
4238
unsigned char *encrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
4339
ChaCha20_Poly1305::aead_encrypt(
44-
cipher_text,encrypt_mac,
45-
reinterpret_cast<const unsigned char*>(lorem.data()),lorem.size(),
46-
AAD,sizeof(AAD),reinterpret_cast<const unsigned char*>(key.data()),iv,constant
40+
cipher_text, encrypt_mac, reinterpret_cast<const unsigned char *>(lorem.data()), lorem.size(), AAD, sizeof(AAD),
41+
reinterpret_cast<const unsigned char *>(key.data()), iv, constant
4742
);
4843

4944
// decryption (with C style casting on parameters).
5045
unsigned char *recover_text = new unsigned char[lorem.size()];
51-
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
46+
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
5247
ChaCha20_Poly1305::aead_decrypt(
53-
recover_text,decrypt_mac,
54-
cipher_text,lorem.size(),
55-
AAD,sizeof(AAD),(const unsigned char*)key.data(),iv,constant
48+
recover_text, decrypt_mac, cipher_text, lorem.size(), AAD, sizeof(AAD), (const unsigned char *) key.data(), iv,
49+
constant
5650
);
5751

5852
// you can use whatever cast you want C or C++.
59-
53+
6054
// compare plain text and recovered text
6155
bool correct = true;
62-
for(size_t i=0; i<lorem.size(); ++i) {
63-
if(recover_text[i]!=(unsigned char)(lorem[i])) {
56+
for (size_t i = 0; i < lorem.size(); ++i) {
57+
if (recover_text[i] != (unsigned char) (lorem[i])) {
6458
correct = false;
6559
break;
6660
}
6761
}
6862

6963
bool authentic = true;
70-
for(size_t i=0; i<POLY1305_MAC_BYTES; ++i) {
71-
if(encrypt_mac[i]!=decrypt_mac[i]) {
64+
for (size_t i = 0; i < POLY1305_MAC_BYTES; ++i) {
65+
if (encrypt_mac[i] != decrypt_mac[i]) {
7266
authentic = false;
7367
break;
7468
}
7569
}
76-
77-
delete [] cipher_text;
78-
delete [] encrypt_mac;
79-
delete [] recover_text;
80-
delete [] decrypt_mac;
81-
82-
if(correct) {
70+
71+
delete[] cipher_text;
72+
delete[] encrypt_mac;
73+
delete[] recover_text;
74+
delete[] decrypt_mac;
75+
76+
if (correct) {
8377
std::cout << "The Decrypted Message is Correct\n";
84-
}
85-
else {
78+
} else {
8679
std::cout << "The Decrypted Message is Wrong!!!\n";
8780
}
8881

89-
if(authentic) {
82+
if (authentic) {
9083
std::cout << "Message is Authentic\n";
91-
}
92-
else
84+
} else
9385
std::cout << "The Message was Altered\n";
9486

95-
if(correct && authentic)
87+
if (correct && authentic)
9688
return 0;
9789
return 1;
9890
}

static-build.cpp

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,76 @@
77

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

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

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

25-
unsigned char nonce[] = {
26-
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
27-
};
23+
unsigned char nonce[] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
2824

29-
unsigned char *iv = nonce+4;
25+
unsigned char *iv = nonce + 4;
3026
unsigned char *constant = nonce;
3127

3228
// encryption (with C++ style casting on parameters).
3329
unsigned char *cipher_text = new unsigned char[lorem.size()];
3430
unsigned char *encrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
3531
ChaCha20_Poly1305::aead_encrypt(
36-
cipher_text,encrypt_mac,
37-
reinterpret_cast<const unsigned char*>(lorem.data()),lorem.size(),
38-
AAD,sizeof(AAD),reinterpret_cast<const unsigned char*>(key.data()),iv,constant
32+
cipher_text, encrypt_mac, reinterpret_cast<const unsigned char *>(lorem.data()), lorem.size(), AAD, sizeof(AAD),
33+
reinterpret_cast<const unsigned char *>(key.data()), iv, constant
3934
);
4035

4136
// decryption (with C style casting on parameters).
4237
unsigned char *recover_text = new unsigned char[lorem.size()];
43-
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
38+
unsigned char *decrypt_mac = new unsigned char[POLY1305_MAC_BYTES];
4439
ChaCha20_Poly1305::aead_decrypt(
45-
recover_text,decrypt_mac,
46-
cipher_text,lorem.size(),
47-
AAD,sizeof(AAD),(const unsigned char*)key.data(),iv,constant
40+
recover_text, decrypt_mac, cipher_text, lorem.size(), AAD, sizeof(AAD), (const unsigned char *) key.data(), iv,
41+
constant
4842
);
4943

5044
// you can use whatever cast you want C or C++.
51-
45+
5246
// compare plain text and recovered text
5347
bool correct = true;
54-
for(size_t i=0; i<lorem.size(); ++i) {
55-
if(recover_text[i]!=(unsigned char)(lorem[i])) {
48+
for (size_t i = 0; i < lorem.size(); ++i) {
49+
if (recover_text[i] != (unsigned char) (lorem[i])) {
5650
correct = false;
5751
break;
5852
}
5953
}
6054

6155
bool authentic = true;
62-
for(size_t i=0; i<POLY1305_MAC_BYTES; ++i) {
63-
if(encrypt_mac[i]!=decrypt_mac[i]) {
56+
for (size_t i = 0; i < POLY1305_MAC_BYTES; ++i) {
57+
if (encrypt_mac[i] != decrypt_mac[i]) {
6458
authentic = false;
6559
break;
6660
}
6761
}
6862

69-
delete [] cipher_text;
70-
delete [] encrypt_mac;
71-
delete [] recover_text;
72-
delete [] decrypt_mac;
73-
74-
if(correct) {
63+
delete[] cipher_text;
64+
delete[] encrypt_mac;
65+
delete[] recover_text;
66+
delete[] decrypt_mac;
67+
68+
if (correct) {
7569
std::cout << "The Decrypted Message is Correct\n";
76-
}
77-
else {
70+
} else {
7871
std::cout << "The Decrypted Message is Wrong!!!\n";
7972
}
8073

81-
if(authentic) {
74+
if (authentic) {
8275
std::cout << "Message is Authentic\n";
83-
}
84-
else
76+
} else
8577
std::cout << "The Message was Altered\n";
8678

87-
if(correct && authentic)
79+
if (correct && authentic)
8880
return 0;
8981
return 1;
9082
}

tests/BlockFunction_test.cpp

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,61 @@
11
#include <iostream>
22

33
#ifndef _MAKE_LIB
4-
#include "../Header-Mode-ChaCha20-Poly1305.hpp"
4+
#include "../Header-Mode-ChaCha20-Poly1305.hpp"
55
#else
6-
#include <ChaCha20-Poly1305.hpp>
6+
#include <ChaCha20-Poly1305.hpp>
77
#endif
88

99
// ChaCha State Comparison
10-
bool CompareState(unsigned int *A, unsigned int *B){
11-
for(size_t i=0; i<16; ++i)
12-
if(A[i] != B[i])
10+
bool CompareState(unsigned int *A, unsigned int *B) {
11+
for (size_t i = 0; i < 16; ++i)
12+
if (A[i] != B[i])
1313
return false;
1414
return true;
1515
}
1616

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

26-
unsigned char nonce[12] = {
27-
0x00,0x00,0x00,0x09,0x00,0x00,
28-
0x00,0x4a,0x00,0x00,0x00,0x00
29-
};
22+
unsigned char nonce[12] = {0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00};
3023

31-
unsigned int correct_output_state[16] = {
32-
0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3,
33-
0xc7f4d1c7, 0x0368c033, 0x9aaa2204, 0x4e6cd4c3,
34-
0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9,
35-
0xd19c12b5, 0xb94e16de, 0xe883d0cb, 0x4e3c50a2
36-
};
24+
unsigned int correct_output_state[16] = {0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3, 0xc7f4d1c7, 0x0368c033,
25+
0x9aaa2204, 0x4e6cd4c3, 0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9,
26+
0xd19c12b5, 0xb94e16de, 0xe883d0cb, 0x4e3c50a2};
3727

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

4534
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
46-
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];
35+
unsigned int *output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];
4736

48-
chacha20::init_state(initial_state,(unsigned int*)key,1,(unsigned int*)nonce);
49-
chacha20::apply_20rounds(output_state,initial_state);
50-
51-
unsigned char* serialize = (unsigned char*) output_state;
37+
chacha20::init_state(initial_state, (unsigned int *) key, 1, (unsigned int *) nonce);
38+
chacha20::apply_20rounds(output_state, initial_state);
39+
40+
unsigned char *serialize = (unsigned char *) output_state;
5241

5342
bool serial_passed = true;
54-
for(size_t i=0; i<CHACHA20_BLK_FUNC_OUTPUT_BYTES; ++i) {
55-
if(serialize[i] != correct_serial[i]) {
43+
for (size_t i = 0; i < CHACHA20_BLK_FUNC_OUTPUT_BYTES; ++i) {
44+
if (serialize[i] != correct_serial[i]) {
5645
serial_passed = false;
5746
break;
5847
}
5948
}
60-
61-
bool passed = CompareState(correct_output_state,output_state);
6249

63-
delete [] initial_state;
64-
delete [] output_state;
50+
bool passed = CompareState(correct_output_state, output_state);
51+
52+
delete[] initial_state;
53+
delete[] output_state;
6554

66-
if(passed && serial_passed) {
55+
if (passed && serial_passed) {
6756
std::cout << "ChaChaBlockFunctionTest : PASSED\n";
6857
return 0;
69-
}
70-
else {
58+
} else {
7159
std::cout << "ChaChaBlockFunctionTest : FAILED\n";
7260
return 1;
7361
}

0 commit comments

Comments
 (0)