From 0a0feeea8679d96cf2b5679562654512d467f0c2 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 15 Apr 2015 18:34:14 +0200 Subject: [PATCH] finish #3777 including testing --- src/include/taler_crypto_lib.h | 18 ++++++++++ src/util/crypto.c | 40 ++++++++++++++++++++- src/util/test_crypto.c | 66 +++++++++++++++++++++++++++++++--- 3 files changed, 119 insertions(+), 5 deletions(-) diff --git a/src/include/taler_crypto_lib.h b/src/include/taler_crypto_lib.h index dedeef5b0..752d2bfd8 100644 --- a/src/include/taler_crypto_lib.h +++ b/src/include/taler_crypto_lib.h @@ -441,6 +441,24 @@ TALER_link_decrypt_secret (const struct TALER_EncryptedLinkSecretP *secret_enc, struct TALER_LinkSecretP *secret); +/** + * Decrypt the shared @a secret from the information in the + * encrypted link secret @e secret_enc using the transfer + * public key and the coin's private key. + * + * @param secret_enc encrypted link secret + * @param transfer_pub transfer public key + * @param coin_priv coin private key + * @param[out] secret set to the shared secret + * @return #GNUNET_OK on success, #GNUNET_SYSERR on error + */ +int +TALER_link_decrypt_secret2 (const struct TALER_EncryptedLinkSecretP *secret_enc, + const struct TALER_TransferPublicKeyP *trans_pub, + const union TALER_CoinSpendPrivateKeyP *coin_priv, + struct TALER_LinkSecretP *secret); + + /** * Encrypt the shared @a secret to generate the encrypted link secret. * Also creates the transfer key. diff --git a/src/util/crypto.c b/src/util/crypto.c index 158bb9511..14c14ebce 100644 --- a/src/util/crypto.c +++ b/src/util/crypto.c @@ -333,7 +333,6 @@ TALER_test_coin_valid (const struct TALER_CoinPublicInfo *coin_public_info) } - /** * Decrypt the shared @a secret from the information in the * encrypted link secret @e secret_enc using the transfer @@ -373,6 +372,45 @@ TALER_link_decrypt_secret (const struct TALER_EncryptedLinkSecretP *secret_enc, } +/** + * Decrypt the shared @a secret from the information in the + * encrypted link secret @e secret_enc using the transfer + * public key and the coin's private key. + * + * @param secret_enc encrypted link secret + * @param transfer_pub transfer public key + * @param coin_priv coin private key + * @param[out] secret set to the shared secret + * @return #GNUNET_OK on success, #GNUNET_SYSERR on error + */ +int +TALER_link_decrypt_secret2 (const struct TALER_EncryptedLinkSecretP *secret_enc, + const struct TALER_TransferPublicKeyP *trans_pub, + const union TALER_CoinSpendPrivateKeyP *coin_priv, + struct TALER_LinkSecretP *secret) +{ + struct TALER_TransferSecretP transfer_secret; + + if (GNUNET_OK != + GNUNET_CRYPTO_ecc_ecdh (&coin_priv->ecdhe_priv, + &trans_pub->ecdhe_pub, + &transfer_secret.key)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (GNUNET_OK != + TALER_transfer_decrypt (secret_enc, + &transfer_secret, + secret)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + + /** * Encrypt the shared @a secret to generate the encrypted link secret. * Also creates the transfer key. diff --git a/src/util/test_crypto.c b/src/util/test_crypto.c index 032127706..e95f25e90 100644 --- a/src/util/test_crypto.c +++ b/src/util/test_crypto.c @@ -24,9 +24,8 @@ #include "taler_crypto_lib.h" -int -main(int argc, - const char *const argv[]) +static int +test_basics () { struct TALER_EncryptedLinkSecretP secret_enc; struct TALER_TransferSecretP trans_sec; @@ -39,7 +38,6 @@ main(int argc, GNUNET_log_setup ("test-crypto", "WARNING", NULL); - /* FIXME: implement test... */ GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &secret, sizeof (secret)); @@ -78,4 +76,64 @@ main(int argc, return 0; } + +static int +test_high_level () +{ + struct GNUNET_CRYPTO_EcdsaPrivateKey *pk; + struct TALER_LinkSecretP secret; + struct TALER_LinkSecretP secret2; + union TALER_CoinSpendPublicKeyP coin_pub; + union TALER_CoinSpendPrivateKeyP coin_priv; + struct TALER_TransferPrivateKeyP trans_priv; + struct TALER_TransferPublicKeyP trans_pub; + struct TALER_EncryptedLinkSecretP secret_enc; + + pk = GNUNET_CRYPTO_ecdsa_key_create (); + GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, + &secret, + sizeof (secret)); + GNUNET_CRYPTO_ecdsa_key_get_public (pk, + &coin_pub.ecdsa_pub); + GNUNET_assert (GNUNET_OK == + TALER_link_encrypt_secret (&secret, + &coin_pub, + &trans_priv, + &trans_pub, + &secret_enc)); + GNUNET_assert (GNUNET_OK == + TALER_link_decrypt_secret (&secret_enc, + &trans_priv, + &coin_pub, + &secret2)); + GNUNET_assert (0 == + memcmp (&secret, + &secret2, + sizeof (secret))); + coin_priv.ecdsa_priv = *pk; + GNUNET_assert (GNUNET_OK == + TALER_link_decrypt_secret2 (&secret_enc, + &trans_pub, + &coin_priv, + &secret2)); + GNUNET_assert (0 == + memcmp (&secret, + &secret2, + sizeof (secret))); + GNUNET_free (pk); + return 0; +} + + +int +main(int argc, + const char *const argv[]) +{ + if (0 != test_basics ()) + return 1; + if (0 != test_high_level ()) + return 1; + return 0; +} + /* end of test_crypto.c */