From 904cd4fe1ce7f88873891e5cb8a0ed32cec8925b Mon Sep 17 00:00:00 2001 From: Krzysztof Kwiatkowski Date: Sat, 10 Jun 2017 16:01:20 +0100 Subject: FFI: Posibility to load EC privkey --- src/tests/test_ffi.cpp | 100 ++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 47 deletions(-) (limited to 'src/tests/test_ffi.cpp') diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp index 75fe337f3..8411af941 100644 --- a/src/tests/test_ffi.cpp +++ b/src/tests/test_ffi.cpp @@ -1196,71 +1196,77 @@ class FFI_Unit_Tests : public Test Test::Result ffi_test_ecdsa(botan_rng_t rng) { Test::Result result("FFI ECDSA"); - + static const char* kCurve = "secp384r1"; botan_privkey_t priv; + botan_privkey_t loaded_privkey; + botan_pubkey_t pub; - if(TEST_FFI_OK(botan_privkey_create_ecdsa, (&priv, rng, "secp384r1"))) - { - botan_pubkey_t pub; - TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv)); - - ffi_test_pubkey_export(result, pub, priv, rng); + REQUIRE_FFI_OK(botan_privkey_create_ecdsa, (&priv, rng, kCurve)); + TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv)); + ffi_test_pubkey_export(result, pub, priv, rng); - char namebuf[32] = { 0 }; - size_t name_len = sizeof(namebuf); - TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len)); + // Check key load functions + botan_mp_t x; + botan_mp_init(&x); - result.test_eq(namebuf, namebuf, "ECDSA"); + TEST_FFI_OK(botan_privkey_get_field, (x, priv, "x")); + TEST_FFI_OK(botan_privkey_load_ec, (&loaded_privkey, x, kCurve)); + TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0)); - std::vector message(1280), signature; - TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size())); + char namebuf[32] = { 0 }; + size_t name_len = sizeof(namebuf); - botan_pk_op_sign_t signer; + TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len)); + result.test_eq(namebuf, namebuf, "ECDSA"); - if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "EMSA1(SHA-384)", 0))) - { - // TODO: break input into multiple calls to update - TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size())); - - signature.resize(96); // 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); + std::vector message(1280), signature; + TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size())); + botan_pk_op_sign_t signer; + if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, "EMSA1(SHA-384)", 0))) + { + // TODO: break input into multiple calls to update + TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size())); - TEST_FFI_OK(botan_pk_op_sign_destroy, (signer)); - } + signature.resize(96); // 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); - botan_pk_op_verify_t verifier; + TEST_FFI_OK(botan_pk_op_sign_destroy, (signer)); + } - if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "EMSA1(SHA-384)", 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())); + botan_pk_op_verify_t verifier; - // 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())); + if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "EMSA1(SHA-384)", 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())); - 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())); + // 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())); - 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())); - 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())); + 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())); - TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier)); - } + 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_pubkey_destroy, (pub)); - TEST_FFI_OK(botan_privkey_destroy, (priv)); + TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier)); } + botan_mp_destroy(x); + TEST_FFI_OK(botan_pubkey_destroy, (pub)); + TEST_FFI_OK(botan_privkey_destroy, (priv)); + TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey)); + return result; } -- cgit v1.2.3