aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-04-02 16:01:40 -0400
committerJack Lloyd <[email protected]>2017-04-02 16:01:40 -0400
commitafbf3dc9e0e1169dce42a72275d95128c6d4e4da (patch)
tree987b3a5be31eb25b5e80d5d49e34b00b47729550 /src/tests
parentf6723299eb3a05b0e3482ed88650680a43be1cd8 (diff)
Add generic getters for PK parameters in C interface
Supporting RSA, DSA, and ECC. Add also DSA specific loading functions.
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/test_ffi.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp
index bd5c38c06..abc069c97 100644
--- a/src/tests/test_ffi.cpp
+++ b/src/tests/test_ffi.cpp
@@ -388,6 +388,7 @@ class FFI_Unit_Tests : public Test
std::vector<Test::Result> results;
results.push_back(ffi_test_mp(rng));
results.push_back(ffi_test_rsa(rng));
+ results.push_back(ffi_test_dsa(rng));
results.push_back(ffi_test_ecdsa(rng));
results.push_back(ffi_test_ecdh(rng));
results.push_back(ffi_test_mceliece(rng));
@@ -748,6 +749,111 @@ class FFI_Unit_Tests : public Test
return result;
}
+ Test::Result ffi_test_dsa(botan_rng_t rng)
+ {
+ Test::Result result("FFI DSA");
+
+ botan_privkey_t priv;
+ if(TEST_FFI_OK(botan_privkey_create, (&priv, "DSA", "dsa/jce/1024", rng)))
+ {
+ TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
+
+ botan_pubkey_t pub;
+ TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
+ TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
+
+ ffi_test_pubkey_export(result, pub, priv, rng);
+
+ botan_mp_t p, q, g, x, y;
+ botan_mp_init(&p);
+ botan_mp_init(&q);
+ botan_mp_init(&g);
+ botan_mp_init(&x);
+ botan_mp_init(&y);
+
+ TEST_FFI_OK(botan_privkey_dsa_get_x, (x, priv));
+ TEST_FFI_OK(botan_pubkey_dsa_get_g, (g, pub));
+ TEST_FFI_OK(botan_pubkey_dsa_get_p, (p, pub));
+ TEST_FFI_OK(botan_pubkey_dsa_get_q, (q, pub));
+ TEST_FFI_OK(botan_pubkey_dsa_get_y, (y, pub));
+
+ botan_mp_t cmp;
+ botan_mp_init(&cmp);
+ TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "x"));
+ TEST_FFI_RC(1, botan_mp_equal, (cmp, x));
+ TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "y"));
+ TEST_FFI_RC(1, botan_mp_equal, (cmp, y));
+ TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "p"));
+ TEST_FFI_RC(1, botan_mp_equal, (cmp, p));
+ botan_mp_destroy(cmp);
+
+ botan_privkey_t loaded_privkey;
+ TEST_FFI_OK(botan_privkey_load_dsa, (&loaded_privkey, p, q, g, x));
+ TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
+
+ botan_pubkey_t loaded_pubkey;
+ TEST_FFI_OK(botan_pubkey_load_dsa, (&loaded_pubkey, p, q, g, y));
+ TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
+
+ botan_mp_destroy(p);
+ botan_mp_destroy(q);
+ botan_mp_destroy(g);
+ botan_mp_destroy(y);
+ botan_mp_destroy(x);
+
+ botan_pk_op_sign_t signer;
+
+ std::vector<uint8_t> message(6, 6);
+ std::vector<uint8_t> signature(20*2);
+
+ if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, "EMSA1(SHA-256)", 0)))
+ {
+ // TODO: break input into multiple calls to update
+ TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
+
+ signature.resize(20*2); // TODO: no way to derive this from API
+ size_t sig_len = signature.size();
+ TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
+ signature.resize(sig_len);
+
+ TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
+ }
+
+ botan_pk_op_verify_t verifier;
+
+ if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "EMSA1(SHA-256)", 0)))
+ {
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ // TODO: randomize this
+ signature[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_FAIL("bad signature", botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ message[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_FAIL("bad signature", botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ signature[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_FAIL("bad signature", botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ message[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
+ }
+
+ TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
+ TEST_FFI_OK(botan_pubkey_destroy, (pub));
+ TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
+ TEST_FFI_OK(botan_privkey_destroy, (priv));
+ }
+
+ return result;
+ }
Test::Result ffi_test_ecdsa(botan_rng_t rng)
{
Test::Result result("FFI ECDSA");