diff options
author | Markus Teich <markus.teich@stusta.mhn.de> | 2016-12-02 09:43:04 +0100 |
---|---|---|
committer | Markus Teich <markus.teich@stusta.mhn.de> | 2016-12-02 09:43:04 +0100 |
commit | 50c476877e2fdbf6e97259e7790b0c42e0ddd487 (patch) | |
tree | 6ed4e5b26d23d1d03f259bd72f02ce8737e2f4a4 | |
parent | 56b43dab7ff80acc4cd0e7ad3057abd5e6bad680 (diff) | |
download | libbrandt-50c476877e2fdbf6e97259e7790b0c42e0ddd487.tar.gz libbrandt-50c476877e2fdbf6e97259e7790b0c42e0ddd487.zip |
migrate to GNUNET_assert
-rw-r--r-- | Makefile.am | 3 | ||||
-rw-r--r-- | brandt.c | 7 | ||||
-rw-r--r-- | crypto.c | 45 | ||||
-rw-r--r-- | util.c | 110 | ||||
-rw-r--r-- | util.h | 51 |
5 files changed, 55 insertions, 161 deletions
diff --git a/Makefile.am b/Makefile.am index 9b80791..b4f41d1 100644 --- a/Makefile.am +++ b/Makefile.am | |||
@@ -10,8 +10,7 @@ libbrandt_la_SOURCES = \ | |||
10 | fp_priv.c \ | 10 | fp_priv.c \ |
11 | fp_pub.c \ | 11 | fp_pub.c \ |
12 | mp_priv.c \ | 12 | mp_priv.c \ |
13 | mp_pub.c \ | 13 | mp_pub.c |
14 | util.c | ||
15 | 14 | ||
16 | libbrandt_la_LIBADD = \ | 15 | libbrandt_la_LIBADD = \ |
17 | -lgcrypt -lgpg-error -lgnunetutil | 16 | -lgcrypt -lgpg-error -lgnunetutil |
@@ -33,7 +33,12 @@ BRANDT_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx) | |||
33 | gcry_error_t err = 0; | 33 | gcry_error_t err = 0; |
34 | 34 | ||
35 | if (!gcry_check_version ("1.7.0")) | 35 | if (!gcry_check_version ("1.7.0")) |
36 | eprintf ("libgcrypt version mismatch"); | 36 | { |
37 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
38 | "libbrandt", | ||
39 | "libgcrypt version mismatch\n"); | ||
40 | GNUNET_assert (0); | ||
41 | } | ||
37 | 42 | ||
38 | /* SECMEM cannot be resized dynamically. We do not know how much we need */ | 43 | /* SECMEM cannot be resized dynamically. We do not know how much we need */ |
39 | if ((err = gcry_control (GCRYCTL_DISABLE_SECMEM, 0))) | 44 | if ((err = gcry_control (GCRYCTL_DISABLE_SECMEM, 0))) |
@@ -32,6 +32,25 @@ | |||
32 | #define CURVE "Ed25519" | 32 | #define CURVE "Ed25519" |
33 | 33 | ||
34 | 34 | ||
35 | /** | ||
36 | * Log an error message at log-level 'error' that indicates a failure of the | ||
37 | * command 'cmd' with the message given by gcry_strerror(rc) and abort the | ||
38 | * programm. | ||
39 | */ | ||
40 | #define ASSERT_GCRY(cmd, rc) do { \ | ||
41 | if (0 != rc) { \ | ||
42 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, \ | ||
43 | "libbrandt", \ | ||
44 | "`%s' failed at %s:%d with error: %s\n", \ | ||
45 | cmd, \ | ||
46 | __FILE__, \ | ||
47 | __LINE__, \ | ||
48 | gcry_strerror (rc)); \ | ||
49 | GNUNET_abort_ (); \ | ||
50 | } \ | ||
51 | } while (0) | ||
52 | |||
53 | |||
35 | struct zkp_challenge_dl { | 54 | struct zkp_challenge_dl { |
36 | struct ec_mpi g; | 55 | struct ec_mpi g; |
37 | struct ec_mpi v; | 56 | struct ec_mpi v; |
@@ -79,7 +98,7 @@ brandt_crypto_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx) | |||
79 | ec_dlogctx = dlogctx; | 98 | ec_dlogctx = dlogctx; |
80 | 99 | ||
81 | rc = gcry_mpi_ec_new (&ec_ctx, NULL, CURVE); | 100 | rc = gcry_mpi_ec_new (&ec_ctx, NULL, CURVE); |
82 | brandt_assert_gpgerr (rc); | 101 | ASSERT_GCRY ("gcry_mpi_ec_new", rc); |
83 | 102 | ||
84 | ec_gen = gcry_mpi_ec_get_point ("g", ec_ctx, 0); | 103 | ec_gen = gcry_mpi_ec_get_point ("g", ec_ctx, 0); |
85 | brandt_assert (NULL != ec_gen); | 104 | brandt_assert (NULL != ec_gen); |
@@ -113,10 +132,10 @@ ec_skey_create (gcry_mpi_t skey) | |||
113 | 132 | ||
114 | rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")" | 133 | rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")" |
115 | "(flags)))"); | 134 | "(flags)))"); |
116 | brandt_assert_gpgerr (rc); | 135 | ASSERT_GCRY ("gcry_sexp_build", rc); |
117 | 136 | ||
118 | rc = gcry_pk_genkey (&priv_sexp, s_keyparam); | 137 | rc = gcry_pk_genkey (&priv_sexp, s_keyparam); |
119 | brandt_assert_gpgerr (rc); | 138 | ASSERT_GCRY ("gcry_pk_genkey", rc); |
120 | gcry_sexp_release (s_keyparam); | 139 | gcry_sexp_release (s_keyparam); |
121 | 140 | ||
122 | priv_key = gcry_sexp_find_token (priv_sexp, "private-key", 11); | 141 | 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) | |||
148 | { | 167 | { |
149 | gcry_mpi_t sk; | 168 | gcry_mpi_t sk; |
150 | 169 | ||
151 | brandt_assert (NULL != pkey); | 170 | if (NULL == pkey) |
171 | { | ||
172 | GNUNET_break (NULL != pkey); | ||
173 | return; | ||
174 | } | ||
152 | sk = (NULL == skey) ? gcry_mpi_new (256) : skey; | 175 | sk = (NULL == skey) ? gcry_mpi_new (256) : skey; |
153 | 176 | ||
154 | ec_skey_create (sk); | 177 | ec_skey_create (sk); |
@@ -271,7 +294,7 @@ mpi_serialize (struct ec_mpi *dst, gcry_mpi_t src) | |||
271 | 294 | ||
272 | rc = gcry_mpi_print (GCRYMPI_FMT_USG, (void *)dst, | 295 | rc = gcry_mpi_print (GCRYMPI_FMT_USG, (void *)dst, |
273 | sizeof (struct ec_mpi), &rsize, src); | 296 | sizeof (struct ec_mpi), &rsize, src); |
274 | brandt_assert_gpgerr (rc); | 297 | ASSERT_GCRY ("gcry_mpi_print", rc); |
275 | 298 | ||
276 | /* Shift the output to the right, if shorter than available space */ | 299 | /* Shift the output to the right, if shorter than available space */ |
277 | if (rsize && rsize < sizeof (struct ec_mpi)) | 300 | if (rsize && rsize < sizeof (struct ec_mpi)) |
@@ -301,7 +324,7 @@ mpi_parse (gcry_mpi_t dst, const struct ec_mpi *src) | |||
301 | src, | 324 | src, |
302 | sizeof (struct ec_mpi), | 325 | sizeof (struct ec_mpi), |
303 | NULL); | 326 | NULL); |
304 | brandt_assert_gpgerr (rc); | 327 | ASSERT_GCRY ("gcry_mpi_scan", rc); |
305 | 328 | ||
306 | gcry_mpi_snatch (dst, ret); | 329 | gcry_mpi_snatch (dst, ret); |
307 | } | 330 | } |
@@ -324,15 +347,15 @@ ec_point_serialize (struct ec_mpi *dst, const gcry_mpi_point_t src) | |||
324 | brandt_assert (dst); | 347 | brandt_assert (dst); |
325 | 348 | ||
326 | rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")))"); | 349 | rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")))"); |
327 | brandt_assert_gpgerr (rc); | 350 | ASSERT_GCRY ("gcry_sexp_build", rc); |
328 | brandt_assert (NULL != s); | 351 | brandt_assert (NULL != s); |
329 | 352 | ||
330 | rc = gcry_mpi_ec_new (&ctx, s, NULL); | 353 | rc = gcry_mpi_ec_new (&ctx, s, NULL); |
331 | brandt_assert_gpgerr (rc); | 354 | ASSERT_GCRY ("gcry_mpi_ec_new", rc); |
332 | gcry_sexp_release (s); | 355 | gcry_sexp_release (s); |
333 | 356 | ||
334 | rc = gcry_mpi_ec_set_point ("q", src, ctx); | 357 | rc = gcry_mpi_ec_set_point ("q", src, ctx); |
335 | brandt_assert_gpgerr (rc); | 358 | ASSERT_GCRY ("gcry_mpi_ec_set_point", rc); |
336 | 359 | ||
337 | q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0); | 360 | q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0); |
338 | brandt_assert (NULL != q); | 361 | brandt_assert (NULL != q); |
@@ -359,10 +382,10 @@ ec_point_parse (gcry_mpi_point_t dst, const struct ec_mpi *src) | |||
359 | 382 | ||
360 | rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", | 383 | rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", |
361 | sizeof (struct ec_mpi), src); | 384 | sizeof (struct ec_mpi), src); |
362 | brandt_assert_gpgerr (rc); | 385 | ASSERT_GCRY ("gcry_sexp_build", rc); |
363 | 386 | ||
364 | rc = gcry_mpi_ec_new (&ctx, s, NULL); | 387 | rc = gcry_mpi_ec_new (&ctx, s, NULL); |
365 | brandt_assert_gpgerr (rc); | 388 | ASSERT_GCRY ("gcry_mpi_ec_new", rc); |
366 | gcry_sexp_release (s); | 389 | gcry_sexp_release (s); |
367 | 390 | ||
368 | ret = gcry_mpi_ec_get_point ("q", ctx, 0); | 391 | ret = gcry_mpi_ec_get_point ("q", ctx, 0); |
@@ -1,110 +0,0 @@ | |||
1 | /* This file is part of libbrandt. | ||
2 | * Copyright (C) 2016 GNUnet e.V. | ||
3 | * | ||
4 | * libbrandt is free software: you can redistribute it and/or modify it under | ||
5 | * the terms of the GNU General Public License as published by the Free Software | ||
6 | * Foundation, either version 3 of the License, or (at your option) any later | ||
7 | * version. | ||
8 | * | ||
9 | * libbrandt is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License along with | ||
14 | * libbrandt. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file util.c | ||
19 | * @brief Implementation of common utility functions. | ||
20 | * @author Markus Teich | ||
21 | */ | ||
22 | |||
23 | #include "brandt_config.h" | ||
24 | |||
25 | #include <errno.h> | ||
26 | #include <stdarg.h> | ||
27 | #include <stdio.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <string.h> | ||
30 | |||
31 | #include "util.h" | ||
32 | |||
33 | |||
34 | static FILE *logstream = NULL; | ||
35 | |||
36 | |||
37 | /** | ||
38 | * setlog sets another output for logging. | ||
39 | * | ||
40 | * @param[in] stream The new logging target. | ||
41 | */ | ||
42 | void | ||
43 | setlog (FILE *stream) | ||
44 | { | ||
45 | logstream = stream; | ||
46 | } | ||
47 | |||
48 | |||
49 | /** | ||
50 | * xvprintf prints a formatstring with prefix "libbrandt: ". If the format | ||
51 | * string ends with a ':', the strerror() from errno.h output will be appended. | ||
52 | * The output is always terminated with a newline. | ||
53 | * | ||
54 | * @param[in] fmt The format string | ||
55 | * @param[in] ap The inputs to the format string | ||
56 | */ | ||
57 | static void | ||
58 | xvprintf (const char *fmt, va_list ap) | ||
59 | { | ||
60 | fputs ("libbrandt: ", logstream ? logstream : stderr); | ||
61 | |||
62 | vfprintf (logstream ? logstream : stderr, fmt, ap); | ||
63 | |||
64 | if (fmt[0] && fmt[strlen (fmt) - 1] == ':') | ||
65 | { | ||
66 | fputc (' ', logstream ? logstream : stderr); | ||
67 | fputs (strerror (errno), logstream ? logstream : stderr); | ||
68 | } | ||
69 | else | ||
70 | { | ||
71 | fputc ('\n', logstream ? logstream : stderr); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | |||
76 | /** | ||
77 | * eprintf prints an error message and then calls abort() to terminate the | ||
78 | * process. | ||
79 | * | ||
80 | * @param[in] fmt The format string | ||
81 | * @param[in] ... The inputs to the format string | ||
82 | */ | ||
83 | void | ||
84 | eprintf (const char *fmt, ...) | ||
85 | { | ||
86 | va_list ap; | ||
87 | |||
88 | va_start (ap, fmt); | ||
89 | xvprintf (fmt, ap); | ||
90 | va_end (ap); | ||
91 | |||
92 | abort (); | ||
93 | } | ||
94 | |||
95 | |||
96 | /** | ||
97 | * weprintf prints a warning message | ||
98 | * | ||
99 | * @param[in] fmt The format string | ||
100 | * @param[in] ... The inputs to the format string | ||
101 | */ | ||
102 | void | ||
103 | weprintf (const char *fmt, ...) | ||
104 | { | ||
105 | va_list ap; | ||
106 | |||
107 | va_start (ap, fmt); | ||
108 | xvprintf (fmt, ap); | ||
109 | va_end (ap); | ||
110 | } | ||
@@ -23,52 +23,29 @@ | |||
23 | #ifndef _BRANDT_UTIL_H | 23 | #ifndef _BRANDT_UTIL_H |
24 | #define _BRANDT_UTIL_H | 24 | #define _BRANDT_UTIL_H |
25 | 25 | ||
26 | void setlog (FILE *stream); | ||
27 | void eprintf (const char *fmt, ...); | ||
28 | void weprintf (const char *fmt, ...); | ||
29 | 26 | ||
30 | #undef brandt_assert | 27 | #define brandt_assert(cond) do { if (!(cond)) { GNUNET_log_from ( \ |
31 | #undef brandt_assert_perror | 28 | GNUNET_ERROR_TYPE_ERROR, \ |
32 | #undef brandt_assert_gpgerr | 29 | "libbrandt", \ |
30 | "Assertion failed at %s:%d.\n", \ | ||
31 | __FILE__, \ | ||
32 | __LINE__); GNUNET_abort_ (); \ | ||
33 | } } while (0) | ||
33 | 34 | ||
34 | #ifdef NDEBUG | ||
35 | |||
36 | #define brandt_assert(expr) do { \ | ||
37 | (expr) ? (void)(0) : eprintf ( \ | ||
38 | "Assertion failed in file %s line %d function %s: %s", \ | ||
39 | __FILE__, \ | ||
40 | __LINE__, \ | ||
41 | __PRETTY_FUNCTION__, \ | ||
42 | (# expr)); \ | ||
43 | } while (0) | ||
44 | |||
45 | #define brandt_assert_perror(errnum) do { \ | ||
46 | !(errnum) ? (void)(0) : eprintf ( \ | ||
47 | "Assertion failed in file %s line %d function %s:", \ | ||
48 | __FILE__, \ | ||
49 | __LINE__, \ | ||
50 | __PRETTY_FUNCTION__); \ | ||
51 | } while (0) | ||
52 | 35 | ||
53 | #define brandt_assert_gpgerr(errnum) do { \ | 36 | #ifdef NDEBUG |
54 | !(errnum) ? (void)(0) : eprintf ( \ | ||
55 | "Assertion failed in file %s line %d function %s: %s", \ | ||
56 | __FILE__, \ | ||
57 | __LINE__, \ | ||
58 | __PRETTY_FUNCTION__, \ | ||
59 | gcry_strerror ((errnum))); \ | ||
60 | } while (0) | ||
61 | 37 | ||
62 | #define DP(point) ((void)(gcry_log_debugpnt (# point, point, ec_ctx))) | 38 | #define DP(point) ((void)(gcry_log_debugpnt (# point, point, ec_ctx))) |
63 | #define DM(mpi) ((void)(gcry_log_debugmpi (# mpi, mpi))) | 39 | #define DM(mpi) ((void)(gcry_log_debugmpi (# mpi, mpi))) |
64 | #define DS(sexp) ((void)(gcry_log_debugsxp (# sexp, sexp))) | 40 | #define DS(sexp) ((void)(gcry_log_debugsxp (# sexp, sexp))) |
65 | 41 | ||
66 | #else /* ifdef NDEBUG */ | 42 | #else /* ifdef NDEBUG */ |
67 | 43 | ||
68 | #define brandt_assert(expr) ((void)(expr)) | 44 | #define DP(point) ((void)(0)) |
69 | #define brandt_assert_perror(errnum) ((void)(errnum)) | 45 | #define DM(mpi) ((void)(0)) |
70 | #define brandt_assert_gpgerr(errnum) ((void)(errnum)) | 46 | #define DS(sexp) ((void)(0)) |
71 | 47 | ||
72 | #endif /* ifdef NDEBUG */ | 48 | #endif /* ifdef NDEBUG */ |
73 | 49 | ||
50 | |||
74 | #endif /* ifndef _BRANDT_UTIL_H */ | 51 | #endif /* ifndef _BRANDT_UTIL_H */ |