diff options
author | Fournier Nicolas <nicolas.fournier@ensta-paristech.fr> | 2015-06-10 14:59:14 +0200 |
---|---|---|
committer | Fournier Nicolas <nicolas.fournier@ensta-paristech.fr> | 2015-06-10 14:59:14 +0200 |
commit | 7e990741dfbeadbf13a5cea7423ae088a39494d4 (patch) | |
tree | fc350b8a770fcb0f83fe07a0ec2da67789d3a6f8 /src/mintdb/perf_taler_mintdb_init.c | |
parent | e40cbd96dde9ee6b103db165d1b7ccc5e304efd2 (diff) | |
download | exchange-7e990741dfbeadbf13a5cea7423ae088a39494d4.tar.xz |
Commenting, cleaning and checking for return values
Diffstat (limited to 'src/mintdb/perf_taler_mintdb_init.c')
-rw-r--r-- | src/mintdb/perf_taler_mintdb_init.c | 433 |
1 files changed, 278 insertions, 155 deletions
diff --git a/src/mintdb/perf_taler_mintdb_init.c b/src/mintdb/perf_taler_mintdb_init.c index 801d928a5..227d5de6a 100644 --- a/src/mintdb/perf_taler_mintdb_init.c +++ b/src/mintdb/perf_taler_mintdb_init.c @@ -1,86 +1,114 @@ /* - This file is part of TALER - Copyright (C) 2014, 2015 Christian Grothoff (and other contributing authors) + This file is part of TALER + Copyright (C) 2014, 2015 Christian Grothoff (and other contributing authors) - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. - TALER is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along with - TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> -*/ + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> + */ /** * @file mintdb/perf_taler_mintdb_init.c * @brief Interpreter library for mint database performance analysis * @author Nicolas Fournier */ -#include "platform.h" +#include "../include/platform.h" #include <gnunet/gnunet_crypto_lib.h> #include <gnunet/gnunet_signatures.h> -#include "taler_mintdb_plugin.h" -#include "taler_signatures.h" -#include "taler_amount_lib.h" +#include "../include/taler_mintdb_plugin.h" +#include "../include/taler_signatures.h" +#include "../include/taler_amount_lib.h" #define CURRENCY "EUR" + + struct TALER_MINTDB_CollectableBlindcoin * collectable_blindcoin_init () { // indent by 2 spaces - struct TALER_MINTDB_CollectableBlindcoin *coin = GNUNET_new (*coin); + struct TALER_MINTDB_CollectableBlindcoin *coin = + GNUNET_new (struct TALER_MINTDB_CollectableBlindcoin); - struct GNUNET_CRYPTO_EddsaPrivateKey *reserve_sig_key = GNUNET_CRYPTO_eddsa_key_create (); - struct GNUNET_CRYPTO_rsa_PrivateKey *denomination_key = GNUNET_CRYPTO_rsa_private_key_create (512); + struct GNUNET_CRYPTO_EddsaPrivateKey *reserve_sig_key; + GNUNET_assert (NULL != + (reserve_sig_key = GNUNET_CRYPTO_eddsa_key_create ()) + ); + struct GNUNET_CRYPTO_rsa_PrivateKey *denomination_key ; + GNUNET_assert (NULL != + (denomination_key = GNUNET_CRYPTO_rsa_private_key_create (512)) + ); - coin->denom_pub.rsa_public_key = GNUNET_CRYPTO_rsa_private_key_get_public (denomination_key); - GNUNET_CRYPTO_eddsa_key_get_public (reserve_sig_key, - &coin->reserve_pub.eddsa_pub); + GNUNET_assert (NULL == + (coin->denom_pub.rsa_public_key = + GNUNET_CRYPTO_rsa_private_key_get_public (denomination_key)) + ); + GNUNET_CRYPTO_eddsa_key_get_public (reserve_sig_key, + &coin->reserve_pub.eddsa_pub); - //TODO Randomise the amount that is deposited and apply a fee subsequently + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":1.1", + &coin->amount_with_fee)); - // GNUNET_assert (GNUNET_OK == - // TALER_string_to_amount (CURRENCY ":1.1", - // &coin->amount_with_fee)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":1.1", + &coin->withdraw_fee)); - coin->amount_with_fee = (struct TALER_Amount) {1, 1, CURRENCY}; - coin->withdraw_fee = (struct TALER_Amount) {0, 1, CURRENCY}; - int random_int = rand(); // GNUNET_CRYPTO_random_u32 - coin->sig.rsa_signature = GNUNET_CRYPTO_rsa_sign(denomination_key, &random_int, sizeof(random_int)); - GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &coin->h_coin_envelope); + uint32_t random_int = + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); + GNUNET_assert (NULL != + (coin->sig.rsa_signature = + GNUNET_CRYPTO_rsa_sign (denomination_key, + &random_int, + sizeof (random_int) + )) + ); - void *purpose = GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof(int)); - ((struct GNUNET_CRYPTO_EccSignaturePurpose *)purpose)->size = sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof(int); - ((struct GNUNET_CRYPTO_EccSignaturePurpose *)purpose)->purpose = GNUNET_SIGNATURE_PURPOSE_TEST; - *((int *)(purpose + sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose))) = random_int; + GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, + &coin->h_coin_envelope); - GNUNET_CRYPTO_eddsa_sign(reserve_sig_key, purpose, &coin->reserve_sig.eddsa_signature); + struct { + struct GNUNET_CRYPTO_EccSignaturePurpose purpose; + uint32_t data; + } unsigned_data; + unsigned_data.purpose.size = sizeof (unsigned_data); + unsigned_data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_TEST; + unsigned_data.data = random_int; + + GNUNET_assert (GNUNET_OK == + GNUNET_CRYPTO_eddsa_sign (reserve_sig_key, + (struct GNUNET_CRYPTO_EccSignaturePurpose *) &unsigned_data, + &coin->reserve_sig.eddsa_signature) + ); - GNUNET_free(reserve_sig_key); - GNUNET_CRYPTO_rsa_private_key_free(denomination_key); - return coin; + GNUNET_CRYPTO_eddsa_key_clear (reserve_sig_key); + GNUNET_CRYPTO_rsa_private_key_free (denomination_key); + return coin; } /** - * @return a randomly generated blindcoin + * Liberate memory of @a coin */ int -collectable_blindcoin_free(struct TALER_MINTDB_CollectableBlindcoin *coin) +collectable_blindcoin_free (struct TALER_MINTDB_CollectableBlindcoin *coin) { - GNUNET_free(coin->sig.rsa_signature); - GNUNET_free(coin->denom_pub.rsa_public_key); + GNUNET_CRYPTO_rsa_signature_free (coin->sig.rsa_signature); + GNUNET_CRYPTO_rsa_public_key_free (coin->denom_pub.rsa_public_key); - GNUNET_free(coin); + GNUNET_free (coin); return GNUNET_OK; } @@ -90,195 +118,290 @@ collectable_blindcoin_free(struct TALER_MINTDB_CollectableBlindcoin *coin) * @return a randomly generated reserve */ struct TALER_MINTDB_Reserve * -reserve_init() +reserve_init () { - struct TALER_MINTDB_Reserve *reserve = GNUNET_malloc(sizeof(*reserve)); - struct GNUNET_CRYPTO_EddsaPrivateKey *reserve_priv = GNUNET_CRYPTO_eddsa_key_create(); + struct TALER_MINTDB_Reserve *reserve ; + + GNUNET_assert (NULL != + (reserve = GNUNET_new (struct TALER_MINTDB_Reserve) + )); - GNUNET_CRYPTO_eddsa_key_get_public(reserve_priv , &(reserve->pub.eddsa_pub)); + struct GNUNET_CRYPTO_EddsaPrivateKey *reserve_priv ; + GNUNET_assert (NULL != + (reserve_priv = GNUNET_CRYPTO_eddsa_key_create () + )); - reserve->balance = (struct TALER_Amount){1, 1, CURRENCY}; - reserve->expiry = GNUNET_TIME_absolute_get_forever_(); + GNUNET_CRYPTO_eddsa_key_get_public (reserve_priv , + &reserve->pub.eddsa_pub); + + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":1.1", &reserve->balance) + ); - GNUNET_free(reserve_priv); - return reserve; -} + reserve->expiry = GNUNET_TIME_absolute_get_forever_ (); + GNUNET_CRYPTO_eddsa_key_clear (reserve_priv); + return reserve; +} -struct TALER_MINTDB_RefreshSession * -refresh_session_init() +int +reserve_free (struct TALER_MINTDB_Reserve *reserve) { - struct TALER_MINTDB_RefreshSession *refresh_session = GNUNET_malloc(sizeof(*refresh_session)); - - refresh_session->noreveal_index = 1; - refresh_session->num_oldcoins = 1; - refresh_session->num_newcoins = 1; + GNUNET_free (reserve); - return refresh_session; + return GNUNET_OK; } -struct TALER_MINTDB_Deposit * -deposit_init() +/** + * @return a randomly generated refresh session + */ +struct TALER_MINTDB_RefreshSession * +refresh_session_init () { - static int transaction_id = 0; + struct TALER_MINTDB_RefreshSession *refresh_session; + GNUNET_assert (NULL != + (refresh_session = GNUNET_new (struct TALER_MINTDB_RefreshSession) + )); - struct TALER_MINTDB_Deposit *deposit = GNUNET_malloc(sizeof(*deposit)); + refresh_session->noreveal_index = 1; + refresh_session->num_oldcoins = 1; + refresh_session->num_newcoins = 1; - deposit-> transaction_id = transaction_id; - transaction_id++; + return refresh_session; +} - //TODO Randomize the amount that is deposited +/** + * Free a refresh session + */ +int +refresh_session_free (struct TALER_MINTDB_RefreshSession *refresh_session) +{ + GNUNET_free (refresh_session); - deposit->amount_with_fee = (struct TALER_Amount) {1, 1, CURRENCY}; - deposit->deposit_fee = (struct TALER_Amount) {0, 1, CURRENCY}; + return GNUNET_OK; +} - deposit->timestamp = GNUNET_TIME_absolute_get(); - deposit->refund_deadline = GNUNET_TIME_absolute_get(); - GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &deposit->h_contract); - GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &deposit->h_wire); +/** + * Create a randomly generated deposit + */ +struct TALER_MINTDB_Deposit * +deposit_init () +{ + static int transaction_id = 0; - // Coin Spend Signature - { - struct GNUNET_CRYPTO_EddsaSignature sig; + struct TALER_MINTDB_Deposit *deposit ; + GNUNET_assert (NULL != + (deposit = GNUNET_new (struct TALER_MINTDB_Deposit) + )); - struct GNUNET_CRYPTO_EddsaPrivateKey *p_eddsa_prvt = GNUNET_CRYPTO_eddsa_key_create(); - void *prp = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)+sizeof(int)); - *((struct GNUNET_CRYPTO_EccSignaturePurpose *)prp) =(struct GNUNET_CRYPTO_EccSignaturePurpose) {sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)+sizeof(int), GNUNET_SIGNATURE_PURPOSE_TEST}; + deposit-> transaction_id = transaction_id; + transaction_id++; + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":1.1", &deposit->amount_with_fee) + ); - GNUNET_CRYPTO_eddsa_sign(p_eddsa_prvt, (struct GNUNET_CRYPTO_EccSignaturePurpose *) prp, &sig); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":0.1", &deposit->deposit_fee) + ); - deposit->csig.eddsa_signature = sig; - GNUNET_free(p_eddsa_prvt); - } + deposit->timestamp = GNUNET_TIME_absolute_get (); + deposit->refund_deadline = GNUNET_TIME_absolute_get (); + + GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, + &deposit->h_contract); + GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, + &deposit->h_wire); - // Merchant Key + // Coin Spend Signature + { + struct u32_presign { - struct GNUNET_CRYPTO_EddsaPublicKey eddsa_pub; - struct GNUNET_CRYPTO_EddsaPrivateKey *p_eddsa_prv = GNUNET_CRYPTO_eddsa_key_create(); + struct GNUNET_CRYPTO_EccSignaturePurpose purpose; + uint32_t data; + } unsigned_data; + + struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa_prvt; + GNUNET_assert (NULL != + (eddsa_prvt = GNUNET_CRYPTO_eddsa_key_create () + )); + + uint32_t random_int = + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); + + + unsigned_data.purpose.size = sizeof (unsigned_data); + unsigned_data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_TEST; + unsigned_data.data = random_int; + + GNUNET_assert (GNUNET_OK == + GNUNET_CRYPTO_eddsa_sign (eddsa_prvt, + (struct GNUNET_CRYPTO_EccSignaturePurpose *) &unsigned_data, + &deposit->csig.eddsa_signature) + ); + + GNUNET_CRYPTO_eddsa_key_clear (eddsa_prvt); + } + + // Merchant Key + { + struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa_prv; + GNUNET_assert(NULL != + (eddsa_prv = GNUNET_CRYPTO_eddsa_key_create () + )); + + GNUNET_CRYPTO_eddsa_key_get_public ( + eddsa_prv, + &deposit->merchant_pub.eddsa_pub); + + + GNUNET_CRYPTO_eddsa_key_clear (eddsa_prv); + } + + // Coin + { + { + struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa_prvt; + GNUNET_assert (NULL != + (eddsa_prvt = GNUNET_CRYPTO_eddsa_key_create () + )); - GNUNET_CRYPTO_eddsa_key_get_public(p_eddsa_prv, &eddsa_pub); + GNUNET_CRYPTO_eddsa_key_get_public ( + eddsa_prvt, + &deposit->coin.coin_pub.eddsa_pub + ); - deposit->merchant_pub.eddsa_pub = eddsa_pub; - GNUNET_free(p_eddsa_prv); + GNUNET_CRYPTO_eddsa_key_clear (eddsa_prvt); } - // Coin { - - - { - struct GNUNET_CRYPTO_EddsaPublicKey eddsa_pub; - struct GNUNET_CRYPTO_EddsaPrivateKey *p_eddsa_prvt = GNUNET_CRYPTO_eddsa_key_create(); - - GNUNET_CRYPTO_eddsa_key_get_public(p_eddsa_prvt, &eddsa_pub); - - deposit->coin.coin_pub.eddsa_pub = eddsa_pub; - - GNUNET_free(p_eddsa_prvt); - } - - { - struct GNUNET_CRYPTO_rsa_PrivateKey *p_rsa_prv = GNUNET_CRYPTO_rsa_private_key_create(128); - struct GNUNET_CRYPTO_rsa_PublicKey *p_rsa_pub = GNUNET_CRYPTO_rsa_private_key_get_public(p_rsa_prv); - - deposit->coin.denom_pub.rsa_public_key = p_rsa_pub; - - - - deposit->coin.denom_sig.rsa_signature = GNUNET_CRYPTO_rsa_sign(p_rsa_prv, - (void *) &(deposit->coin.coin_pub.eddsa_pub), - sizeof(&(deposit->coin.coin_pub.eddsa_pub))); - - GNUNET_CRYPTO_rsa_private_key_free(p_rsa_prv); - } - + struct GNUNET_CRYPTO_rsa_PrivateKey *rsa_prv; + GNUNET_assert (NULL != + (rsa_prv = GNUNET_CRYPTO_rsa_private_key_create (128) + )); + + GNUNET_assert (NULL != + (deposit->coin.denom_pub.rsa_public_key = + GNUNET_CRYPTO_rsa_private_key_get_public (rsa_prv) + )); + + GNUNET_assert (NULL != + (deposit->coin.denom_sig.rsa_signature = + GNUNET_CRYPTO_rsa_sign (rsa_prv, + (void *) &deposit->coin.coin_pub.eddsa_pub, + sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + ) + )); + + GNUNET_CRYPTO_rsa_private_key_free (rsa_prv); } - - - return deposit; + } + return deposit; } +/** + * Free memory of a deposit + */ int -deposit_free(struct TALER_MINTDB_Deposit *deposit) +deposit_free (struct TALER_MINTDB_Deposit *deposit) { - GNUNET_free(deposit->coin.denom_pub.rsa_public_key); - GNUNET_free(deposit->coin.denom_sig.rsa_signature); + GNUNET_CRYPTO_rsa_public_key_free (deposit->coin.denom_pub.rsa_public_key); + GNUNET_CRYPTO_rsa_signature_free (deposit->coin.denom_sig.rsa_signature); - GNUNET_free(deposit); + GNUNET_free (deposit); return GNUNET_OK; } +/** + * Generate a randomly generate DenominationKeyInformation + */ struct TALER_MINTDB_DenominationKeyIssueInformation * -denomination_init() +denomination_init () { - struct TALER_MINTDB_DenominationKeyIssueInformation *dki = GNUNET_malloc(sizeof(&dki)); + struct TALER_MINTDB_DenominationKeyIssueInformation *dki; + struct GNUNET_CRYPTO_EddsaPrivateKey *master_prvt; + struct GNUNET_TIME_Absolute anchor; + struct TALER_Amount amount; + + GNUNET_assert (NULL != + (dki = GNUNET_new (struct TALER_MINTDB_DenominationKeyIssueInformation) + )); + + GNUNET_assert (NULL != + (dki->denom_priv.rsa_private_key + = GNUNET_CRYPTO_rsa_private_key_create (128) + )); + GNUNET_assert (NULL != + (dki->denom_pub.rsa_public_key = + GNUNET_CRYPTO_rsa_private_key_get_public (dki->denom_priv.rsa_private_key) + )); - - dki->denom_priv.rsa_private_key - = GNUNET_CRYPTO_rsa_private_key_create (128); - GNUNET_assert (NULL != dki->denom_priv.rsa_private_key); - dki->denom_pub.rsa_public_key - = GNUNET_CRYPTO_rsa_private_key_get_public (dki->denom_priv.rsa_private_key); GNUNET_CRYPTO_rsa_public_key_hash (dki->denom_pub.rsa_public_key, - &dki->issue.denom_hash); + &dki->issue.denom_hash); - struct GNUNET_CRYPTO_EddsaPrivateKey *master_prvt = - GNUNET_CRYPTO_eddsa_key_create(); + GNUNET_assert (NULL != + (master_prvt = GNUNET_CRYPTO_eddsa_key_create () + )); - struct GNUNET_CRYPTO_EddsaPublicKey master_pub; - GNUNET_CRYPTO_eddsa_key_get_public(master_prvt, &master_pub); - dki->issue.master.eddsa_pub = master_pub; + GNUNET_CRYPTO_eddsa_key_get_public (master_prvt, + &dki->issue.master.eddsa_pub); - struct GNUNET_TIME_Absolute anchor = GNUNET_TIME_absolute_get(); + anchor = GNUNET_TIME_absolute_get (); dki->issue.start = GNUNET_TIME_absolute_hton (anchor); dki->issue.expire_withdraw = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add (anchor, - GNUNET_TIME_relative_get_hour_())); + GNUNET_TIME_relative_get_hour_ ())); dki->issue.expire_spend = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add (anchor, - GNUNET_TIME_relative_get_hour_())); + GNUNET_TIME_relative_get_hour_ ())); dki->issue.expire_legal = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add (anchor, - GNUNET_TIME_relative_get_hour_())); + GNUNET_TIME_relative_get_hour_ ())); - struct TALER_Amount amount = {.value = 1, .fraction = 1, .currency = CURRENCY}; + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (CURRENCY ":1.1", &amount) + ); TALER_amount_hton (&dki->issue.value, &amount); TALER_amount_hton (&dki->issue.fee_withdraw, &amount); TALER_amount_hton (&dki->issue.fee_deposit, &amount); TALER_amount_hton (&dki->issue.fee_refresh, &amount); dki->issue.purpose.purpose = htonl (TALER_SIGNATURE_MASTER_DENOMINATION_KEY_VALIDITY); - dki->issue.purpose.size = htonl (sizeof (struct TALER_MINTDB_DenominationKeyIssueInformation) - - offsetof (struct TALER_MINTDB_DenominationKeyIssueInformation, - issue.purpose)); + dki->issue.purpose.size = + htonl (sizeof (struct TALER_MINTDB_DenominationKeyIssueInformation) - + offsetof (struct TALER_MINTDB_DenominationKeyIssueInformation, + issue.purpose)); GNUNET_assert (GNUNET_OK == - GNUNET_CRYPTO_eddsa_sign (master_prvt, - &dki->issue.purpose, - &dki->issue.signature.eddsa_signature)); + GNUNET_CRYPTO_eddsa_sign (master_prvt, + &dki->issue.purpose, + &dki->issue.signature.eddsa_signature)); + + GNUNET_CRYPTO_eddsa_key_clear (master_prvt); return dki; } - +/** + * Free memory for a DenominationKeyIssueInformation + */ int -denomination_free(struct TALER_MINTDB_DenominationKeyIssueInformation *dki) +denomination_free (struct TALER_MINTDB_DenominationKeyIssueInformation *dki) { - GNUNET_free(dki->denom_priv.rsa_private_key); - GNUNET_free(dki->denom_pub.rsa_public_key); + GNUNET_CRYPTO_rsa_private_key_free (dki->denom_priv.rsa_private_key); + GNUNET_CRYPTO_rsa_public_key_free (dki->denom_pub.rsa_public_key); - GNUNET_free(dki); + GNUNET_free (dki); return GNUNET_OK; } |