32 #include <gtest/gtest.h>
59 using CharVec = std::vector<char>;
61 string key16 =
"use a 16 b key!!";
62 string key24 =
"use a 24 b key!!!!!!!!!!";
63 string key32 =
"use a 32 b key!!!!!!!!!!!!!!!!!!";
64 string nonce =
"the nonce!!!";
65 string adddata =
"this is the additional data";
67 To be, or not to be, that is the question: \n\
68 Whether 'tis nobler in the mind to suffer \n\
69 The slings and arrows of outrageous fortune, \n\
70 Or to take Arms against a Sea of troubles, \n\
71 And by opposing end them: to die, to sleep; \n\
72 No more; and by a sleep, to say we end \n\
73 The heart-ache, and the thousand natural shocks \n\
74 That Flesh is heir to? 'Tis a consummation \n\
75 Devoutly to be wished. To die, to sleep, \n\
76 perchance to Dream; aye, there's the rub...";
78 using mappair = std::pair<const string,Cipher>;
80 struct Ciphertest :
public testing::Test
82 map<string, Cipher> ciphers;
86 ciphers.insert(std::make_pair(std::string(
"gcm16"),
Cipher(Cipher::aes_gcm_type, key16)));
87 ciphers.insert(std::make_pair(std::string(
"gcm24"),
Cipher(Cipher::aes_gcm_type, key24)));
88 ciphers.insert(std::make_pair(std::string(
"gcm32"),
Cipher(Cipher::aes_gcm_type, key32)));
89 ciphers.insert(std::make_pair(std::string(
"ccm16"),
Cipher(Cipher::aes_ccm_type, key16)));
90 ciphers.insert(std::make_pair(std::string(
"ccm24"),
Cipher(Cipher::aes_ccm_type, key24)));
91 ciphers.insert(std::make_pair(std::string(
"ccm32"),
Cipher(Cipher::aes_ccm_type, key32)));
93 virtual ~Ciphertest() {}
94 void reset(mappair& c,
const string& nonce,
const string& aad)
96 cout <<
"reset for: " << c.first <<
" nonce " << nonce.size() <<
" bytes, aad " << aad.size() <<
" bytes" << endl;
97 c.second.reset(nonce.data(), nonce.size(), aad.data(), aad.size());
99 void reset(mappair& c,
const string& nonce)
101 cout <<
"reset for: " << c.first <<
" nonce " << nonce.size() <<
" bytes" << endl;
102 c.second.reset(nonce.data(), nonce.size());
104 string auth_tag(mappair& c)
106 cout <<
"auth tag for: " << c.first << endl;
109 c.second.auth_tag(tag.data(), tag.size());
110 return Hex::bin_to_hex(tag, 16);
112 CharVec enc(mappair& c,
const string& plain)
114 cout <<
"encrypt " << plain.size() <<
" bytes for: " << c.first << endl;
116 cipher.resize(plain.size(),
'\0');
117 c.second.encrypt(plain.data(), plain.size(), cipher.data(), cipher.size());
120 string dec(mappair& c,
const CharVec& cipher)
122 cout <<
"decrypt " << cipher.size() <<
" bytes for: " << c.first << endl;
124 plain.resize(cipher.size(),
'\0');
125 c.second.decrypt(cipher.data(), cipher.size(), plain.data(), plain.size());
132 TEST_F(Ciphertest, nonce_only)
134 for (
auto& c: ciphers)
136 string tag0 = auth_tag(c);
137 cout <<
"tag0 for " << c.first <<
": " << tag0 << endl;
140 string tag1 = auth_tag(c);
141 cout <<
"tag1 for " << c.first <<
": " << tag1 << endl;
142 ASSERT_EQ(tag0, tag1);
145 string tag2 = auth_tag(c);
146 cout <<
"tag2 for " << c.first <<
": " << tag2 << endl;
147 ASSERT_EQ(tag1, tag2);
152 TEST_F(Ciphertest, authdata_only)
154 for (
auto& c: ciphers)
156 string tag0 = auth_tag(c);
157 cout <<
"tag0 for " << c.first <<
": " << tag0 << endl;
159 reset(c, nonce, adddata);
160 string tag1 = auth_tag(c);
161 cout <<
"tag1 for " << c.first <<
": " << tag1 << endl;
162 ASSERT_NE(tag0, tag1);
164 reset(c, nonce, adddata);
165 string tag2 = auth_tag(c);
166 cout <<
"tag2 for " << c.first <<
": " << tag1 << endl;
167 ASSERT_EQ(tag1, tag2);
171 TEST_F(Ciphertest, enc_reset)
173 for (
auto& c: ciphers)
176 CharVec cip0 = enc(c, plain);
177 string tag0 = auth_tag(c);
180 CharVec cip1 = enc(c, plain);
181 string tag1 = auth_tag(c);
183 ASSERT_EQ(cip0, cip1);
184 ASSERT_EQ(tag0, tag1);
188 TEST_F(Ciphertest, enc_reset_adddata)
190 for (
auto& c: ciphers)
192 reset(c, nonce, adddata);
193 CharVec cip0 = enc(c, plain);
194 string tag0 = auth_tag(c);
196 reset(c, nonce, adddata);
197 CharVec cip1 = enc(c, plain);
198 string tag1 = auth_tag(c);
200 ASSERT_EQ(cip0, cip1);
201 ASSERT_EQ(tag0, tag1);
205 TEST_F(Ciphertest, enc_dec)
207 for (
auto& c: ciphers)
210 CharVec cip = enc(c, plain);
211 string tag = auth_tag(c);
214 string plain1 = dec(c, cip);
215 string tag1 = auth_tag(c);
217 ASSERT_EQ(plain, plain1);
218 ASSERT_EQ(tag, tag1);
222 TEST_F(Ciphertest, enc_dec_adddata)
224 for (
auto& c: ciphers)
226 reset(c, nonce, adddata);
227 CharVec cip = enc(c, plain);
228 string tag = auth_tag(c);
230 reset(c, nonce, adddata);
231 string plain1 = dec(c, cip);
232 string tag1 = auth_tag(c);
234 ASSERT_EQ(plain, plain1);
235 ASSERT_EQ(tag, tag1);
239 TEST_F(Ciphertest, enc_dec_samebuf)
241 for (
auto& c: ciphers)
243 reset(c, nonce, adddata);
245 CharVec buf(plain.begin(), plain.end());
246 buf.resize(plain.size()+1024,
'\0');
248 CharVec verify(plain.begin(), plain.end());
249 verify.resize(plain.size()+1024,
'\0');
251 auto size = plain.size();
253 cout <<
"encrypt in place " << size <<
"bytes for: " << c.first << endl;
255 c.second.encrypt(buf.data(), size, buf.data(), buf.size());
257 CharVec enctag(16,
'\0');
258 c.second.auth_tag(enctag.data(), enctag.size());
260 reset(c, nonce, adddata);
262 cout <<
"decrypt in place " << size <<
"bytes for: " << c.first << endl;
264 c.second.decrypt(buf.data(), size, buf.data(), buf.size());
266 CharVec dectag(16,
'\0');
267 c.second.auth_tag(dectag.data(), dectag.size());
269 ASSERT_EQ(buf, verify);
270 ASSERT_EQ(enctag, dectag);
Binary to hex string converter.