-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaead.cpp
174 lines (129 loc) · 5.1 KB
/
aead.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "schwaemm.hpp"
#include <cassert>
#include <iostream>
// Compile it with
//
// g++ -std=c++20 -Wall -O3 -I ./include example/aead.cpp
int
main()
{
constexpr size_t d_len = 32ul; // associate data byte length
constexpr size_t ct_len = 32ul; // plain/ cipher text byte length
uint8_t data[d_len];
uint8_t txt[ct_len];
uint8_t enc[ct_len];
uint8_t dec[ct_len];
sparkle_utils::random_data(data, d_len); // generate random associated data
sparkle_utils::random_data(txt, d_len); // generate random plain text
std::cout << "data = " << sparkle_utils::to_hex(data, sizeof(data)) << "\n";
std::cout << "text = " << sparkle_utils::to_hex(txt, sizeof(txt)) << "\n";
{
std::cout << "\nSchwaemm128-128 AEAD\n"
<< "\n";
uint8_t key[schwaemm128_128::C];
uint8_t nonce[schwaemm128_128::R];
uint8_t tag[schwaemm128_128::C];
sparkle_utils::random_data(key, sizeof(key));
sparkle_utils::random_data(nonce, sizeof(nonce));
std::memset(enc, 0, sizeof(enc));
std::memset(dec, 0, sizeof(dec));
using namespace schwaemm128_128;
// authenticated encryption with Schwaemm128-128 AEAD
encrypt(key, nonce, data, d_len, txt, enc, ct_len, tag);
// verified decryption with Schwaemm128-128 AEAD
const bool f = decrypt(key, nonce, tag, data, d_len, enc, dec, ct_len);
assert(f);
bool cmp = false;
for (size_t i = 0; i < ct_len; i++) {
cmp |= dec[i] ^ txt[i];
}
assert(!cmp);
using namespace sparkle_utils;
std::cout << "key = " << to_hex(key, sizeof(key)) << "\n";
std::cout << "nonce = " << to_hex(nonce, sizeof(nonce)) << "\n";
std::cout << "cipher = " << to_hex(enc, sizeof(enc)) << "\n";
std::cout << "decrypted = " << to_hex(dec, sizeof(dec)) << "\n";
}
{
std::cout << "\nSchwaemm192-192 AEAD\n"
<< "\n";
uint8_t key[schwaemm192_192::C];
uint8_t nonce[schwaemm192_192::R];
uint8_t tag[schwaemm192_192::C];
sparkle_utils::random_data(key, sizeof(key));
sparkle_utils::random_data(nonce, sizeof(nonce));
std::memset(enc, 0, sizeof(enc));
std::memset(dec, 0, sizeof(dec));
using namespace schwaemm192_192;
// authenticated encryption with Schwaemm192-192 AEAD
encrypt(key, nonce, data, d_len, txt, enc, ct_len, tag);
// verified decryption with Schwaemm192-192 AEAD
const bool f = decrypt(key, nonce, tag, data, d_len, enc, dec, ct_len);
assert(f);
bool cmp = false;
for (size_t i = 0; i < ct_len; i++) {
cmp |= dec[i] ^ txt[i];
}
assert(!cmp);
using namespace sparkle_utils;
std::cout << "key = " << to_hex(key, sizeof(key)) << "\n";
std::cout << "nonce = " << to_hex(nonce, sizeof(nonce)) << "\n";
std::cout << "cipher = " << to_hex(enc, sizeof(enc)) << "\n";
std::cout << "decrypted = " << to_hex(dec, sizeof(dec)) << "\n";
}
{
std::cout << "\nSchwaemm256-128 AEAD\n"
<< "\n";
uint8_t key[schwaemm256_128::C];
uint8_t nonce[schwaemm256_128::R];
uint8_t tag[schwaemm256_128::C];
sparkle_utils::random_data(key, sizeof(key));
sparkle_utils::random_data(nonce, sizeof(nonce));
std::memset(enc, 0, sizeof(enc));
std::memset(dec, 0, sizeof(dec));
using namespace schwaemm256_128;
// authenticated encryption with Schwaemm256-128 AEAD
encrypt(key, nonce, data, d_len, txt, enc, ct_len, tag);
// verified decryption with Schwaemm256-128 AEAD
const bool f = decrypt(key, nonce, tag, data, d_len, enc, dec, ct_len);
assert(f);
bool cmp = false;
for (size_t i = 0; i < ct_len; i++) {
cmp |= dec[i] ^ txt[i];
}
assert(!cmp);
using namespace sparkle_utils;
std::cout << "key = " << to_hex(key, sizeof(key)) << "\n";
std::cout << "nonce = " << to_hex(nonce, sizeof(nonce)) << "\n";
std::cout << "cipher = " << to_hex(enc, sizeof(enc)) << "\n";
std::cout << "decrypted = " << to_hex(dec, sizeof(dec)) << "\n";
}
{
std::cout << "\nSchwaemm256-256 AEAD\n"
<< "\n";
uint8_t key[schwaemm256_256::C];
uint8_t nonce[schwaemm256_256::R];
uint8_t tag[schwaemm256_256::C];
sparkle_utils::random_data(key, sizeof(key));
sparkle_utils::random_data(nonce, sizeof(nonce));
std::memset(enc, 0, sizeof(enc));
std::memset(dec, 0, sizeof(dec));
using namespace schwaemm256_256;
// authenticated encryption with Schwaemm256-256 AEAD
encrypt(key, nonce, data, d_len, txt, enc, ct_len, tag);
// verified decryption with Schwaemm256-256 AEAD
const bool f = decrypt(key, nonce, tag, data, d_len, enc, dec, ct_len);
assert(f);
bool cmp = false;
for (size_t i = 0; i < ct_len; i++) {
cmp |= dec[i] ^ txt[i];
}
assert(!cmp);
using namespace sparkle_utils;
std::cout << "key = " << to_hex(key, sizeof(key)) << "\n";
std::cout << "nonce = " << to_hex(nonce, sizeof(nonce)) << "\n";
std::cout << "cipher = " << to_hex(enc, sizeof(enc)) << "\n";
std::cout << "decrypted = " << to_hex(dec, sizeof(dec)) << "\n";
}
return EXIT_SUCCESS;
}