32 #include <gtest/gtest.h>
64 using charv = std::vector<char>;
66 TEST(rsa_key, private_zero)
69 cout <<
"zero key:" << k.
str();
71 ASSERT_EQ(k.
width(), 0);
74 TEST(rsa_key, private_generate)
78 cout <<
"256 bit key:" << k.
str();
79 ASSERT_EQ(k.
width(), 256);
82 TEST(rsa_key, private_validate)
86 cout <<
"validate key:" << k.
str();
87 ASSERT_EQ(k.
width(), 256);
91 TEST(rsa_key, zero_construct)
94 ASSERT_EQ(a.
width(), 0);
102 ASSERT_EQ(a.
width(), 512);
105 ASSERT_EQ(a.
width(), 0);
109 TEST(rsa_key, copy_construct)
113 ASSERT_EQ(a.
width(), 512);
117 ASSERT_EQ(a.
width(), 512);
119 ASSERT_EQ(b.width(), 512);
120 ASSERT_TRUE(b.validate());
123 TEST(rsa_key, copy_op)
127 ASSERT_EQ(a.
width(), 512);
132 ASSERT_EQ(a.
width(), 512);
134 ASSERT_EQ(b.
width(), 512);
138 TEST(rsa_key, move_construct)
142 ASSERT_EQ(a.
width(), 512);
146 ASSERT_EQ(a.
width(), 0);
148 ASSERT_EQ(b.
width(), 512);
152 TEST(rsa_key, move_op)
156 ASSERT_EQ(a.
width(), 512);
161 ASSERT_EQ(a.
width(), 0);
163 ASSERT_EQ(b.
width(), 512);
167 static string plaintext =
168 "To be, or not to be, that is the question:\n"
169 "Whether 'tis nobler in the mind to suffer\n"
170 "The slings and arrows of outrageous fortune,\n"
171 "Or to take Arms against a Sea of troubles,\n"
172 "And by opposing end them: to die, to sleep;\n"
173 "No more; and by a sleep, to say we end\n"
174 "The heart-ache, and the thousand natural shocks\n"
175 "That Flesh is heir to? 'Tis a consummation\n"
176 "Devoutly to be wished. To die, to sleep,\n"
177 "perchance to Dream; aye, there's the rub...\n"
180 TEST(rsa_encryption, pkcs_signature)
182 cout <<
"*** Pkcs signature test" << endl;
187 cout <<
"*** Key:\n" << key.
str() << endl;
189 cout <<
"*** Orig:\n" << plaintext << endl;
192 PkcsSignature::sign(plaintext.data(), plaintext.size(), sig, key, PkcsSignature::HashType::sha256);
194 cout <<
"*** Signature (" << sig.size() <<
" bytes):\n" << Hex::bin_to_hexstr(sig,
":", 16) << endl;
196 ASSERT_EQ(sig.size(), PkcsSignature::size(key));
200 bool ver = PkcsSignature::verify(plaintext.data(), plaintext.size(), sig, pubkey, PkcsSignature::HashType::sha256);
201 cout <<
"*** Verify: " << (ver ?
"OK" :
"FAIL") << endl;
205 ver = PkcsSignature::verify(plaintext.data(), 1, sig, pubkey, PkcsSignature::HashType::sha256);
207 ver = PkcsSignature::verify(plaintext.data(), plaintext.size(), sig.data(), 1, pubkey, PkcsSignature::HashType::sha256);
211 explicit_bzero(t.data(), t.size());
212 ver = PkcsSignature::verify(plaintext.data(), plaintext.size(), t.data(), t.size(), pubkey, PkcsSignature::HashType::sha256);
216 TEST(rsa_encryption, oaep_encrypt)
218 cout <<
"*** Oaep encryption test" << endl;
226 cout <<
"plaintext size=" << plaintext.size() << endl;
227 cout <<
"enc max_msg_len=" << enc.max_msg_size() << endl;
228 cout <<
"enc cipher_len=" << enc.cipher_size() << endl;
230 charv plain(plaintext.begin(), plaintext.end());
231 plain.resize(enc.max_msg_size());
234 cipher.resize(enc.cipher_size());
236 string label(
"this is a label, which will be signed but not encrypted");
238 enc.encrypt(plain.data(), plain.size(), cipher.data(), cipher.size(), label.data(), label.size());
240 cout <<
"encrypted plain size=" << plain.size() << endl;
245 decplain.resize(dec.max_msg_size());
247 int r = dec.decrypt(decplain.data(), decplain.size(), cipher.data(), cipher.size(), label.data(), label.size());
249 cout <<
"decrypt returned " << r << endl;
251 ASSERT_EQ(r, plain.size());
252 ASSERT_EQ(plain, decplain);
254 r = dec.decrypt(decplain.data(), decplain.size(), cipher.data(), cipher.size());
256 cout <<
"decrypt without label returned " << r << endl;
261 enc.encrypt(plain.data(), plain.size(), cipher.data(), cipher.size());
262 r = dec.decrypt(decplain.data(), decplain.size(), cipher.data(), cipher.size());
263 ASSERT_EQ(plain, decplain);
266 plain.resize(dec.max_msg_size()/2);
267 enc.encrypt(plain.data(), plain.size(), cipher.data(), cipher.size());
268 r = dec.decrypt(decplain.data(), decplain.size(), cipher.data(), cipher.size());
269 ASSERT_EQ(r, dec.max_msg_size()/2);
271 ASSERT_EQ(plain, decplain);
274 TEST(rsa_encryption, pss_signature)
276 cout <<
"*** Pss signature test" << endl;
281 cout <<
"*** Key:\n" << key.
str() << endl;
283 cout <<
"*** Orig:\n" << plaintext << endl;
286 PssSignature::sign(plaintext.data(), plaintext.size(), sig, key, PssSignature::HashType::sha256);
288 cout <<
"*** Signature (" << sig.size() <<
" bytes):\n" << Hex::bin_to_hexstr(sig,
":", 16) << endl;
290 ASSERT_EQ(sig.size(), PssSignature::size(key));
294 bool ver = PssSignature::verify(plaintext.data(), plaintext.size(), sig, pubkey, PssSignature::HashType::sha256);
295 cout <<
"*** Verify: " << (ver ?
"OK" :
"FAIL") << endl;
299 ver = PssSignature::verify(plaintext.data(), 1, sig, pubkey, PssSignature::HashType::sha256);
301 ver = PssSignature::verify(plaintext.data(), plaintext.size(), sig.data(), 1, pubkey, PssSignature::HashType::sha256);
305 explicit_bzero(t.data(), t.size());
306 ver = PssSignature::verify(plaintext.data(), plaintext.size(), t.data(), t.size(), pubkey, PssSignature::HashType::sha256);
General one-way hashing algorithms.
PKCS #1 version 1.5 digital signature.
RSASSA-PSS https://tools.ietf.org/html/rfc8017#section-8.1 Notes on use in x.509: https://tools....
bool validate(const RsaPublicKey &) const
Validate a public key with the private key.
void clear()
Clear and erase all data.
std::string str(unsigned=8) const
Output with formatted values.
void generate(int)
Generate a private key.
RsaPublicKey pub_key() const
Export the public key.
int width() const
Bit width of the key.
One-way hashing and message digests.
Binary to hex string converter.
RSA public key cryptography.
TEST(inet_example, client_server_stream_test)
[Inet client server]