71 #include <gtest/gtest.h>
80 #include "../test/file_test.h"
81 #include "../test/test_util.h"
82 #include "../test/wycheproof_util.h"
93 }
else if (
name ==
"SHA1") {
95 }
else if (
name ==
"SHA224") {
97 }
else if (
name ==
"SHA256") {
99 }
else if (
name ==
"SHA384") {
101 }
else if (
name ==
"SHA512") {
118 if (
name ==
"Ed25519") {
121 if (
name ==
"X25519") {
129 if (
name ==
"PKCS1") {
137 if (
name ==
"OAEP") {
145 using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>;
150 std::vector<uint8_t>
input;
151 if (!t->GetBytes(&
input,
"Input")) {
157 bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&
cbs));
163 if (!t->GetAttribute(&
key_type,
"Type")) {
173 !marshal_func(cbb.get(), pkey.get()) ||
177 bssl::UniquePtr<uint8_t> free_der(der);
180 if (t->HasAttribute(
"Output") &&
181 !t->GetBytes(&
output,
"Output")) {
185 <<
"Re-encoding the key did not match.";
187 if (t->HasAttribute(
"ExpectNoRawPrivate")) {
190 }
else if (t->HasAttribute(
"ExpectRawPrivate")) {
191 std::vector<uint8_t> expected;
192 if (!t->GetBytes(&expected,
"ExpectRawPrivate")) {
196 std::vector<uint8_t> raw;
214 if (t->HasAttribute(
"ExpectNoRawPublic")) {
217 }
else if (t->HasAttribute(
"ExpectRawPublic")) {
218 std::vector<uint8_t> expected;
219 if (!t->GetBytes(&expected,
"ExpectRawPublic")) {
223 std::vector<uint8_t> raw;
251 if (t->HasAttribute(
"RSAPadding")) {
253 if (!
GetRSAPadding(t, &padding, t->GetAttributeOrDie(
"RSAPadding")) ||
258 if (t->HasAttribute(
"PSSSaltLength") &&
260 ctx, atoi(t->GetAttributeOrDie(
"PSSSaltLength").c_str()))) {
263 if (t->HasAttribute(
"MGF1Digest")) {
264 const EVP_MD *digest =
GetDigest(t, t->GetAttributeOrDie(
"MGF1Digest"));
269 if (t->HasAttribute(
"OAEPDigest")) {
270 const EVP_MD *digest =
GetDigest(t, t->GetAttributeOrDie(
"OAEPDigest"));
275 if (t->HasAttribute(
"OAEPLabel")) {
276 std::vector<uint8_t>
label;
277 if (!t->GetBytes(&
label,
"OAEPLabel")) {
282 bssl::UniquePtr<uint8_t>
buf(
reinterpret_cast<uint8_t *
>(
290 if (t->HasAttribute(
"DerivePeer")) {
291 std::string derive_peer = t->GetAttributeOrDie(
"DerivePeer");
292 if (
key_map->count(derive_peer) == 0) {
293 ADD_FAILURE() <<
"Could not find key " << derive_peer;
296 EVP_PKEY *derive_peer_key = (*key_map)[derive_peer].get();
319 std::vector<uint8_t> actual,
output;
330 if (!t->GetBytes(&
output,
"Output")) {
336 actual.resize(
len + 1);
345 actual.resize(
len - 1);
347 if (t->HasAttribute(
"SmallBufferTruncates")) {
362 if (t->GetType() ==
"PrivateKey") {
367 if (t->GetType() ==
"PublicKey") {
373 if (
key_map->count(key_name) == 0) {
374 ADD_FAILURE() <<
"Could not find key " << key_name;
381 const uint8_t *
in,
size_t in_len) =
nullptr;
384 bool is_verify =
false;
385 if (t->GetType() ==
"Decrypt") {
388 }
else if (t->GetType() ==
"Sign") {
391 }
else if (t->GetType() ==
"Verify") {
394 }
else if (t->GetType() ==
"SignMessage") {
396 }
else if (t->GetType() ==
"VerifyMessage") {
399 }
else if (t->GetType() ==
"Encrypt") {
402 }
else if (t->GetType() ==
"Derive") {
409 const EVP_MD *digest =
nullptr;
410 if (t->HasAttribute(
"Digest")) {
411 digest =
GetDigest(t, t->GetAttributeOrDie(
"Digest"));
412 if (digest ==
nullptr) {
421 if (!t->GetBytes(&
input,
"Input") ||
422 (is_verify && !t->GetBytes(&
output,
"Output"))) {
427 bssl::ScopedEVP_MD_CTX
ctx,
copy;
429 if (!md_op_init(
ctx.get(), &pctx, digest,
nullptr,
key) ||
449 !t->GetBytes(&
output,
"Output")) {
464 !t->GetBytes(&
output,
"Output")) {
474 !key_op_init(
ctx.get()) ||
475 (digest !=
nullptr &&
495 if (!key_op(pctx,
nullptr, &
len,
input.data(),
input.size())) {
499 if (!key_op(pctx, actual.data(), &
len,
input.data(),
input.size())) {
503 if (t->HasAttribute(
"CheckDecrypt")) {
505 size_t plaintext_len;
509 (digest !=
nullptr &&
513 actual.data(), actual.size())) {
516 output.resize(plaintext_len);
518 actual.data(), actual.size())) {
522 output.resize(plaintext_len);
524 }
else if (t->HasAttribute(
"CheckVerify")) {
529 (digest !=
nullptr &&
534 if (t->HasAttribute(
"VerifyPSSSaltLength")) {
537 atoi(t->GetAttributeOrDie(
"VerifyPSSSaltLength").c_str()))) {
543 <<
"Could not verify result.";
546 if (!t->GetBytes(&
output,
"Output")) {
560 if (t->HasAttribute(
"Error")) {
573 t->IgnoreAllUnusedInstructions();
575 std::vector<uint8_t> der;
576 ASSERT_TRUE(t->GetInstructionBytes(&der,
"keyDer"));
583 if (t->HasInstruction(
"sha")) {
588 bool is_pss = t->HasInstruction(
"mgf");
589 const EVP_MD *mgf1_md =
nullptr;
590 int pss_salt_len = -1;
592 ASSERT_EQ(
"MGF1", t->GetInstructionOrDie(
"mgf"));
597 pss_salt_len = atoi(s_len.c_str());
600 std::vector<uint8_t>
msg;
602 std::vector<uint8_t> sig;
620 bssl::ScopedEVP_MD_CTX
ctx;
634 result.IsValid({
"SmallModulus",
"SmallPublicKey",
"WeakHash"})
645 TEST(EVPTest, WycheproofECDSAP224) {
647 "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha224_test.txt");
649 "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha256_test.txt");
651 "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha512_test.txt");
654 TEST(EVPTest, WycheproofECDSAP256) {
656 "third_party/wycheproof_testvectors/ecdsa_secp256r1_sha256_test.txt");
658 "third_party/wycheproof_testvectors/ecdsa_secp256r1_sha512_test.txt");
661 TEST(EVPTest, WycheproofECDSAP384) {
663 "third_party/wycheproof_testvectors/ecdsa_secp384r1_sha384_test.txt");
666 TEST(EVPTest, WycheproofECDSAP521) {
668 "third_party/wycheproof_testvectors/ecdsa_secp384r1_sha512_test.txt");
670 "third_party/wycheproof_testvectors/ecdsa_secp521r1_sha512_test.txt");
673 TEST(EVPTest, WycheproofEdDSA) {
677 TEST(EVPTest, WycheproofRSAPKCS1) {
679 "third_party/wycheproof_testvectors/rsa_signature_2048_sha224_test.txt");
681 "third_party/wycheproof_testvectors/rsa_signature_2048_sha256_test.txt");
683 "third_party/wycheproof_testvectors/rsa_signature_2048_sha384_test.txt");
685 "third_party/wycheproof_testvectors/rsa_signature_2048_sha512_test.txt");
687 "third_party/wycheproof_testvectors/rsa_signature_3072_sha256_test.txt");
689 "third_party/wycheproof_testvectors/rsa_signature_3072_sha384_test.txt");
691 "third_party/wycheproof_testvectors/rsa_signature_3072_sha512_test.txt");
693 "third_party/wycheproof_testvectors/rsa_signature_4096_sha384_test.txt");
695 "third_party/wycheproof_testvectors/rsa_signature_4096_sha512_test.txt");
698 "third_party/wycheproof_testvectors/rsa_signature_test.txt");
701 TEST(EVPTest, WycheproofRSAPKCS1Sign) {
703 "third_party/wycheproof_testvectors/rsa_sig_gen_misc_test.txt",
705 t->IgnoreAllUnusedInstructions();
707 std::vector<uint8_t> pkcs8;
708 ASSERT_TRUE(t->GetInstructionBytes(&pkcs8,
"privateKeyPkcs8"));
717 std::vector<uint8_t>
msg, sig;
723 bssl::ScopedEVP_MD_CTX
ctx;
734 result.IsValid({
"SmallModulus",
"SmallPublicKey",
"WeakHash"});
743 TEST(EVPTest, WycheproofRSAPSS) {
745 "third_party/wycheproof_testvectors/rsa_pss_2048_sha1_mgf1_20_test.txt");
747 "third_party/wycheproof_testvectors/rsa_pss_2048_sha256_mgf1_0_test.txt");
749 "third_party/wycheproof_testvectors/"
750 "rsa_pss_2048_sha256_mgf1_32_test.txt");
752 "third_party/wycheproof_testvectors/"
753 "rsa_pss_3072_sha256_mgf1_32_test.txt");
755 "third_party/wycheproof_testvectors/"
756 "rsa_pss_4096_sha256_mgf1_32_test.txt");
758 "third_party/wycheproof_testvectors/"
759 "rsa_pss_4096_sha512_mgf1_32_test.txt");
761 "third_party/wycheproof_testvectors/rsa_pss_misc_test.txt");
768 t->IgnoreAllUnusedInstructions();
770 std::vector<uint8_t> pkcs8;
771 ASSERT_TRUE(t->GetInstructionBytes(&pkcs8,
"privateKeyPkcs8"));
777 std::vector<uint8_t> ct,
msg;
793 bool is_valid =
result.IsValid({
"SmallModulus"});
808 std::vector<uint8_t>
label;
814 bssl::UniquePtr<uint8_t> label_copy(
820 label_copy.release();
824 TEST(EVPTest, WycheproofRSAOAEP2048) {
826 "third_party/wycheproof_testvectors/"
827 "rsa_oaep_2048_sha1_mgf1sha1_test.txt");
829 "third_party/wycheproof_testvectors/"
830 "rsa_oaep_2048_sha224_mgf1sha1_test.txt");
832 "third_party/wycheproof_testvectors/"
833 "rsa_oaep_2048_sha224_mgf1sha224_test.txt");
835 "third_party/wycheproof_testvectors/"
836 "rsa_oaep_2048_sha256_mgf1sha1_test.txt");
838 "third_party/wycheproof_testvectors/"
839 "rsa_oaep_2048_sha256_mgf1sha256_test.txt");
841 "third_party/wycheproof_testvectors/"
842 "rsa_oaep_2048_sha384_mgf1sha1_test.txt");
844 "third_party/wycheproof_testvectors/"
845 "rsa_oaep_2048_sha384_mgf1sha384_test.txt");
847 "third_party/wycheproof_testvectors/"
848 "rsa_oaep_2048_sha512_mgf1sha1_test.txt");
850 "third_party/wycheproof_testvectors/"
851 "rsa_oaep_2048_sha512_mgf1sha512_test.txt");
854 TEST(EVPTest, WycheproofRSAOAEP3072) {
856 "third_party/wycheproof_testvectors/"
857 "rsa_oaep_3072_sha256_mgf1sha1_test.txt");
859 "third_party/wycheproof_testvectors/"
860 "rsa_oaep_3072_sha256_mgf1sha256_test.txt");
862 "third_party/wycheproof_testvectors/"
863 "rsa_oaep_3072_sha512_mgf1sha1_test.txt");
865 "third_party/wycheproof_testvectors/"
866 "rsa_oaep_3072_sha512_mgf1sha512_test.txt");
869 TEST(EVPTest, WycheproofRSAOAEP4096) {
871 "third_party/wycheproof_testvectors/"
872 "rsa_oaep_4096_sha256_mgf1sha1_test.txt");
874 "third_party/wycheproof_testvectors/"
875 "rsa_oaep_4096_sha256_mgf1sha256_test.txt");
877 "third_party/wycheproof_testvectors/"
878 "rsa_oaep_4096_sha512_mgf1sha1_test.txt");
880 "third_party/wycheproof_testvectors/"
881 "rsa_oaep_4096_sha512_mgf1sha512_test.txt");
884 TEST(EVPTest, WycheproofRSAOAEPMisc) {
886 "third_party/wycheproof_testvectors/rsa_oaep_misc_test.txt");
895 TEST(EVPTest, WycheproofRSAPKCS1Decrypt) {
897 "third_party/wycheproof_testvectors/rsa_pkcs1_2048_test.txt");
899 "third_party/wycheproof_testvectors/rsa_pkcs1_3072_test.txt");
901 "third_party/wycheproof_testvectors/rsa_pkcs1_4096_test.txt");