diff options
Diffstat (limited to 'src/tests/unit_ecdsa.cpp')
-rw-r--r-- | src/tests/unit_ecdsa.cpp | 52 |
1 files changed, 22 insertions, 30 deletions
diff --git a/src/tests/unit_ecdsa.cpp b/src/tests/unit_ecdsa.cpp index ecafb3c7f..421323fda 100644 --- a/src/tests/unit_ecdsa.cpp +++ b/src/tests/unit_ecdsa.cpp @@ -17,6 +17,7 @@ #include <botan/ec_group.h> #include <botan/oids.h> #include <botan/pkcs8.h> + #include <botan/hash.h> #endif #if defined(BOTAN_HAS_X509_CERTIFICATES) @@ -41,7 +42,6 @@ Test::Result test_hash_larger_than_n() Botan::EC_Group dom_pars(Botan::OIDS::lookup("1.3.132.0.8")); // secp160r1 // n = 0x0100000000000000000001f4c8f927aed3ca752257 (21 bytes) - // -> shouldn't work with SHA224 which outputs 28 bytes Botan::ECDSA_PrivateKey priv_key(Test::rng(), dom_pars); @@ -49,35 +49,27 @@ Test::Result test_hash_larger_than_n() for(size_t i = 0; i != message.size(); ++i) message[i] = i; - Botan::PK_Signer pk_signer_160(priv_key, "EMSA1_BSI(SHA-1)"); - Botan::PK_Verifier pk_verifier_160(priv_key, "EMSA1_BSI(SHA-1)"); + auto sha1 = Botan::HashFunction::create("SHA-1"); + auto sha224 = Botan::HashFunction::create("SHA-224"); - Botan::PK_Signer pk_signer_224(priv_key, "EMSA1_BSI(SHA-224)"); + if(!sha1 || !sha224) + { + result.test_note("Skipping due to missing SHA-1 or SHA-224"); + return result; + } + + Botan::PK_Signer pk_signer_160(priv_key, "EMSA1(SHA-1)"); + Botan::PK_Verifier pk_verifier_160(priv_key, "EMSA1(SHA-1)"); // Verify we can sign and verify with SHA-160 std::vector<byte> signature_160 = pk_signer_160.sign_message(message, Test::rng()); - result.test_eq("message verifies", pk_verifier_160.verify_message(message, signature_160), true); - try - { - pk_signer_224.sign_message(message, Test::rng()); - result.test_failure("bad key/hash combination not rejected"); - } - catch(Botan::Encoding_Error) - { - result.test_note("bad key/hash combination rejected"); - } - - // now check that verification alone fails - - // sign it with the normal EMSA1 + // Verify we can sign and verify with SHA-224 Botan::PK_Signer pk_signer(priv_key, "EMSA1(SHA-224)"); std::vector<byte> signature = pk_signer.sign_message(message, Test::rng()); - - Botan::PK_Verifier pk_verifier(priv_key, "EMSA1_BSI(SHA-224)"); - - result.test_eq("corrupt message does not verify", pk_verifier.verify_message(message, signature), false); + Botan::PK_Verifier pk_verifier(priv_key, "EMSA1(SHA-224)"); + result.test_eq("message verifies", pk_verifier.verify_message(message, signature), true); return result; } @@ -130,12 +122,12 @@ Test::Result test_sign_then_ver() Botan::EC_Group dom_pars(Botan::OID("1.3.132.0.8")); Botan::ECDSA_PrivateKey ecdsa(Test::rng(), dom_pars); - Botan::PK_Signer signer(ecdsa, "EMSA1(SHA-1)"); + Botan::PK_Signer signer(ecdsa, "EMSA1(SHA-256)"); auto msg = Botan::hex_decode("12345678901234567890abcdef12"); std::vector<byte> sig = signer.sign_message(msg, Test::rng()); - Botan::PK_Verifier verifier(ecdsa, "EMSA1(SHA-1)"); + Botan::PK_Verifier verifier(ecdsa, "EMSA1(SHA-256)"); result.confirm("signature verifies", verifier.verify_message(msg, sig)); @@ -205,7 +197,7 @@ Test::Result test_ecdsa_create_save_load() Botan::EC_Group dom_pars(Botan::OID("1.3.132.0.8")); Botan::ECDSA_PrivateKey key(Test::rng(), dom_pars); - Botan::PK_Signer signer(key, "EMSA1(SHA-1)"); + Botan::PK_Signer signer(key, "EMSA1(SHA-256)"); msg_signature = signer.sign_message(msg, Test::rng()); ecc_private_key_pem = Botan::PKCS8::PEM_encode(key); @@ -220,7 +212,7 @@ Test::Result test_ecdsa_create_save_load() Botan::ECDSA_PrivateKey* loaded_ec_key = dynamic_cast<Botan::ECDSA_PrivateKey*>(loaded_key.get()); result.confirm("the loaded key could be converted into an ECDSA_PrivateKey", loaded_ec_key); - Botan::PK_Verifier verifier(*loaded_ec_key, "EMSA1(SHA-1)"); + Botan::PK_Verifier verifier(*loaded_ec_key, "EMSA1(SHA-256)"); result.confirm("generated signature valid", verifier.verify_message(msg, msg_signature)); @@ -268,8 +260,8 @@ Test::Result test_read_pkcs8() Botan::ECDSA_PrivateKey* ecdsa_nodp = dynamic_cast<Botan::ECDSA_PrivateKey*>(loaded_key_nodp.get()); result.confirm("key loaded", ecdsa_nodp); - Botan::PK_Signer signer(*ecdsa_nodp, "EMSA1(SHA-1)"); - Botan::PK_Verifier verifier(*ecdsa_nodp, "EMSA1(SHA-1)"); + Botan::PK_Signer signer(*ecdsa_nodp, "EMSA1(SHA-256)"); + Botan::PK_Verifier verifier(*ecdsa_nodp, "EMSA1(SHA-256)"); std::vector<byte> signature_nodp = signer.sign_message(msg, Test::rng()); @@ -335,8 +327,8 @@ Test::Result test_curve_registry() Botan::EC_Group dom_pars(oid); Botan::ECDSA_PrivateKey ecdsa(Test::rng(), dom_pars); - Botan::PK_Signer signer(ecdsa, "EMSA1(SHA-1)"); - Botan::PK_Verifier verifier(ecdsa, "EMSA1(SHA-1)"); + Botan::PK_Signer signer(ecdsa, "EMSA1(SHA-256)"); + Botan::PK_Verifier verifier(ecdsa, "EMSA1(SHA-256)"); auto msg = Botan::hex_decode("12345678901234567890abcdef12"); std::vector<byte> sig = signer.sign_message(msg, Test::rng()); |