migrate to GNUNET_assert

This commit is contained in:
Markus Teich 2016-12-02 09:43:04 +01:00
parent 56b43dab7f
commit 50c476877e
5 changed files with 55 additions and 161 deletions

View File

@ -10,8 +10,7 @@ libbrandt_la_SOURCES = \
fp_priv.c \ fp_priv.c \
fp_pub.c \ fp_pub.c \
mp_priv.c \ mp_priv.c \
mp_pub.c \ mp_pub.c
util.c
libbrandt_la_LIBADD = \ libbrandt_la_LIBADD = \
-lgcrypt -lgpg-error -lgnunetutil -lgcrypt -lgpg-error -lgnunetutil

View File

@ -33,7 +33,12 @@ BRANDT_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx)
gcry_error_t err = 0; gcry_error_t err = 0;
if (!gcry_check_version ("1.7.0")) if (!gcry_check_version ("1.7.0"))
eprintf ("libgcrypt version mismatch"); {
GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
"libbrandt",
"libgcrypt version mismatch\n");
GNUNET_assert (0);
}
/* SECMEM cannot be resized dynamically. We do not know how much we need */ /* SECMEM cannot be resized dynamically. We do not know how much we need */
if ((err = gcry_control (GCRYCTL_DISABLE_SECMEM, 0))) if ((err = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))

View File

@ -32,6 +32,25 @@
#define CURVE "Ed25519" #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 zkp_challenge_dl {
struct ec_mpi g; struct ec_mpi g;
struct ec_mpi v; struct ec_mpi v;
@ -79,7 +98,7 @@ brandt_crypto_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx)
ec_dlogctx = dlogctx; ec_dlogctx = dlogctx;
rc = gcry_mpi_ec_new (&ec_ctx, NULL, CURVE); 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); ec_gen = gcry_mpi_ec_get_point ("g", ec_ctx, 0);
brandt_assert (NULL != ec_gen); 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 "\")" rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")"
"(flags)))"); "(flags)))");
brandt_assert_gpgerr (rc); ASSERT_GCRY ("gcry_sexp_build", rc);
rc = gcry_pk_genkey (&priv_sexp, s_keyparam); rc = gcry_pk_genkey (&priv_sexp, s_keyparam);
brandt_assert_gpgerr (rc); ASSERT_GCRY ("gcry_pk_genkey", rc);
gcry_sexp_release (s_keyparam); gcry_sexp_release (s_keyparam);
priv_key = gcry_sexp_find_token (priv_sexp, "private-key", 11); 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; gcry_mpi_t sk;
brandt_assert (NULL != pkey); if (NULL == pkey)
{
GNUNET_break (NULL != pkey);
return;
}
sk = (NULL == skey) ? gcry_mpi_new (256) : skey; sk = (NULL == skey) ? gcry_mpi_new (256) : skey;
ec_skey_create (sk); 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, rc = gcry_mpi_print (GCRYMPI_FMT_USG, (void *)dst,
sizeof (struct ec_mpi), &rsize, src); 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 */ /* Shift the output to the right, if shorter than available space */
if (rsize && rsize < sizeof (struct ec_mpi)) if (rsize && rsize < sizeof (struct ec_mpi))
@ -301,7 +324,7 @@ mpi_parse (gcry_mpi_t dst, const struct ec_mpi *src)
src, src,
sizeof (struct ec_mpi), sizeof (struct ec_mpi),
NULL); NULL);
brandt_assert_gpgerr (rc); ASSERT_GCRY ("gcry_mpi_scan", rc);
gcry_mpi_snatch (dst, ret); 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); brandt_assert (dst);
rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")))"); 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); brandt_assert (NULL != s);
rc = gcry_mpi_ec_new (&ctx, s, NULL); rc = gcry_mpi_ec_new (&ctx, s, NULL);
brandt_assert_gpgerr (rc); ASSERT_GCRY ("gcry_mpi_ec_new", rc);
gcry_sexp_release (s); gcry_sexp_release (s);
rc = gcry_mpi_ec_set_point ("q", src, ctx); 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); q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
brandt_assert (NULL != q); 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)))", rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))",
sizeof (struct ec_mpi), src); sizeof (struct ec_mpi), src);
brandt_assert_gpgerr (rc); ASSERT_GCRY ("gcry_sexp_build", rc);
rc = gcry_mpi_ec_new (&ctx, s, NULL); rc = gcry_mpi_ec_new (&ctx, s, NULL);
brandt_assert_gpgerr (rc); ASSERT_GCRY ("gcry_mpi_ec_new", rc);
gcry_sexp_release (s); gcry_sexp_release (s);
ret = gcry_mpi_ec_get_point ("q", ctx, 0); ret = gcry_mpi_ec_get_point ("q", ctx, 0);

110
util.c
View File

@ -1,110 +0,0 @@
/* This file is part of libbrandt.
* Copyright (C) 2016 GNUnet e.V.
*
* libbrandt 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 of the License, or (at your option) any later
* version.
*
* libbrandt 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
* libbrandt. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file util.c
* @brief Implementation of common utility functions.
* @author Markus Teich
*/
#include "brandt_config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
static FILE *logstream = NULL;
/**
* setlog sets another output for logging.
*
* @param[in] stream The new logging target.
*/
void
setlog (FILE *stream)
{
logstream = stream;
}
/**
* xvprintf prints a formatstring with prefix "libbrandt: ". If the format
* string ends with a ':', the strerror() from errno.h output will be appended.
* The output is always terminated with a newline.
*
* @param[in] fmt The format string
* @param[in] ap The inputs to the format string
*/
static void
xvprintf (const char *fmt, va_list ap)
{
fputs ("libbrandt: ", logstream ? logstream : stderr);
vfprintf (logstream ? logstream : stderr, fmt, ap);
if (fmt[0] && fmt[strlen (fmt) - 1] == ':')
{
fputc (' ', logstream ? logstream : stderr);
fputs (strerror (errno), logstream ? logstream : stderr);
}
else
{
fputc ('\n', logstream ? logstream : stderr);
}
}
/**
* eprintf prints an error message and then calls abort() to terminate the
* process.
*
* @param[in] fmt The format string
* @param[in] ... The inputs to the format string
*/
void
eprintf (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
xvprintf (fmt, ap);
va_end (ap);
abort ();
}
/**
* weprintf prints a warning message
*
* @param[in] fmt The format string
* @param[in] ... The inputs to the format string
*/
void
weprintf (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
xvprintf (fmt, ap);
va_end (ap);
}

47
util.h
View File

@ -23,52 +23,29 @@
#ifndef _BRANDT_UTIL_H #ifndef _BRANDT_UTIL_H
#define _BRANDT_UTIL_H #define _BRANDT_UTIL_H
void setlog (FILE *stream);
void eprintf (const char *fmt, ...);
void weprintf (const char *fmt, ...);
#undef brandt_assert #define brandt_assert(cond) do { if (!(cond)) { GNUNET_log_from ( \
#undef brandt_assert_perror GNUNET_ERROR_TYPE_ERROR, \
#undef brandt_assert_gpgerr "libbrandt", \
"Assertion failed at %s:%d.\n", \
__FILE__, \
__LINE__); GNUNET_abort_ (); \
} } while (0)
#ifdef NDEBUG #ifdef NDEBUG
#define brandt_assert(expr) do { \
(expr) ? (void)(0) : eprintf ( \
"Assertion failed in file %s line %d function %s: %s", \
__FILE__, \
__LINE__, \
__PRETTY_FUNCTION__, \
(# expr)); \
} while (0)
#define brandt_assert_perror(errnum) do { \
!(errnum) ? (void)(0) : eprintf ( \
"Assertion failed in file %s line %d function %s:", \
__FILE__, \
__LINE__, \
__PRETTY_FUNCTION__); \
} while (0)
#define brandt_assert_gpgerr(errnum) do { \
!(errnum) ? (void)(0) : eprintf ( \
"Assertion failed in file %s line %d function %s: %s", \
__FILE__, \
__LINE__, \
__PRETTY_FUNCTION__, \
gcry_strerror ((errnum))); \
} while (0)
#define DP(point) ((void)(gcry_log_debugpnt (# point, point, ec_ctx))) #define DP(point) ((void)(gcry_log_debugpnt (# point, point, ec_ctx)))
#define DM(mpi) ((void)(gcry_log_debugmpi (# mpi, mpi))) #define DM(mpi) ((void)(gcry_log_debugmpi (# mpi, mpi)))
#define DS(sexp) ((void)(gcry_log_debugsxp (# sexp, sexp))) #define DS(sexp) ((void)(gcry_log_debugsxp (# sexp, sexp)))
#else /* ifdef NDEBUG */ #else /* ifdef NDEBUG */
#define brandt_assert(expr) ((void)(expr)) #define DP(point) ((void)(0))
#define brandt_assert_perror(errnum) ((void)(errnum)) #define DM(mpi) ((void)(0))
#define brandt_assert_gpgerr(errnum) ((void)(errnum)) #define DS(sexp) ((void)(0))
#endif /* ifdef NDEBUG */ #endif /* ifdef NDEBUG */
#endif /* ifndef _BRANDT_UTIL_H */ #endif /* ifndef _BRANDT_UTIL_H */