From 50c476877e2fdbf6e97259e7790b0c42e0ddd487 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Fri, 2 Dec 2016 09:43:04 +0100 Subject: migrate to GNUNET_assert --- crypto.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) (limited to 'crypto.c') diff --git a/crypto.c b/crypto.c index 6c1b501..dea1617 100644 --- a/crypto.c +++ b/crypto.c @@ -32,6 +32,25 @@ #define CURVE "Ed25519" +/** + * Log an error message at log-level 'error' that indicates a failure of the + * command 'cmd' with the message given by gcry_strerror(rc) and abort the + * programm. + */ +#define ASSERT_GCRY(cmd, rc) do { \ + if (0 != rc) { \ + GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, \ + "libbrandt", \ + "`%s' failed at %s:%d with error: %s\n", \ + cmd, \ + __FILE__, \ + __LINE__, \ + gcry_strerror (rc)); \ + GNUNET_abort_ (); \ + } \ +} while (0) + + struct zkp_challenge_dl { struct ec_mpi g; struct ec_mpi v; @@ -79,7 +98,7 @@ brandt_crypto_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx) ec_dlogctx = dlogctx; rc = gcry_mpi_ec_new (&ec_ctx, NULL, CURVE); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_mpi_ec_new", rc); ec_gen = gcry_mpi_ec_get_point ("g", ec_ctx, 0); brandt_assert (NULL != ec_gen); @@ -113,10 +132,10 @@ ec_skey_create (gcry_mpi_t skey) rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")" "(flags)))"); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_sexp_build", rc); rc = gcry_pk_genkey (&priv_sexp, s_keyparam); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_pk_genkey", rc); gcry_sexp_release (s_keyparam); priv_key = gcry_sexp_find_token (priv_sexp, "private-key", 11); @@ -148,7 +167,11 @@ ec_keypair_create (gcry_mpi_point_t pkey, gcry_mpi_t skey) { gcry_mpi_t sk; - brandt_assert (NULL != pkey); + if (NULL == pkey) + { + GNUNET_break (NULL != pkey); + return; + } sk = (NULL == skey) ? gcry_mpi_new (256) : skey; ec_skey_create (sk); @@ -271,7 +294,7 @@ mpi_serialize (struct ec_mpi *dst, gcry_mpi_t src) rc = gcry_mpi_print (GCRYMPI_FMT_USG, (void *)dst, sizeof (struct ec_mpi), &rsize, src); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_mpi_print", rc); /* Shift the output to the right, if shorter than available space */ if (rsize && rsize < sizeof (struct ec_mpi)) @@ -301,7 +324,7 @@ mpi_parse (gcry_mpi_t dst, const struct ec_mpi *src) src, sizeof (struct ec_mpi), NULL); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_mpi_scan", rc); gcry_mpi_snatch (dst, ret); } @@ -324,15 +347,15 @@ ec_point_serialize (struct ec_mpi *dst, const gcry_mpi_point_t src) brandt_assert (dst); rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")))"); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_sexp_build", rc); brandt_assert (NULL != s); rc = gcry_mpi_ec_new (&ctx, s, NULL); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_mpi_ec_new", rc); gcry_sexp_release (s); rc = gcry_mpi_ec_set_point ("q", src, ctx); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_mpi_ec_set_point", rc); q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0); brandt_assert (NULL != q); @@ -359,10 +382,10 @@ ec_point_parse (gcry_mpi_point_t dst, const struct ec_mpi *src) rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", sizeof (struct ec_mpi), src); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_sexp_build", rc); rc = gcry_mpi_ec_new (&ctx, s, NULL); - brandt_assert_gpgerr (rc); + ASSERT_GCRY ("gcry_mpi_ec_new", rc); gcry_sexp_release (s); ret = gcry_mpi_ec_get_point ("q", ctx, 0); -- cgit v1.2.3