From 338c95de4190065149ddc995e5404d335c05f432 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Mon, 13 Jun 2016 21:09:41 +0200 Subject: [PATCH] coding style --- crypto.c | 135 ++++++++++++++++++++++++++++++------------------ crypto.h | 8 +-- smc.c | 104 ++++++++++++++++++++++--------------- smc.h | 22 +++++++- test.h | 26 ++++++++-- test_crypto.c | 91 +++++++++++++++++++------------- uncrustify/conf | 4 +- 7 files changed, 252 insertions(+), 138 deletions(-) diff --git a/crypto.c b/crypto.c index 7f2940a..55ab26a 100644 --- a/crypto.c +++ b/crypto.c @@ -35,8 +35,13 @@ struct brandt_ec_pkey { }; gcry_mpi_point_t ec_gen; -gcry_ctx_t ec_ctx; +gcry_ctx_t ec_ctx; +/** + * brandt_crypto_init + * + * + */ void brandt_crypto_init () { @@ -154,29 +159,28 @@ brandt_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size) brandt_assert_gpgerr (rc); } -/* -gcry_mpi_point_t -deserialize_point(const struct brandt_point* data, const int len) -{ - gcry_sexp_t s; - gcry_ctx_t ctx; - gcry_mpi_point_t ret; - gcry_error_t rc; +//gcry_mpi_point_t +//deserialize_point(const struct brandt_point* data, const int len) +//{ +// gcry_sexp_t s; +// gcry_ctx_t ctx; +// gcry_mpi_point_t ret; +// gcry_error_t rc; +// +// rc = gcry_sexp_build(&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", +// len, data); +// brandt_assert_gpgerr(rc); +// +// rc = gcry_mpi_ec_new(&ctx, s, NULL); +// brandt_assert_gpgerr(rc); +// gcry_sexp_release(s); +// +// ret = gcry_mpi_ec_get_point("q", ctx, 0); +// brandt_assert(ret); +// gcry_ctx_release(ctx); +// return ret; +//} - rc = gcry_sexp_build(&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", - len, data); - brandt_assert_gpgerr(rc); - - rc = gcry_mpi_ec_new(&ctx, s, NULL); - brandt_assert_gpgerr(rc); - gcry_sexp_release(s); - - ret = gcry_mpi_ec_get_point("q", ctx, 0); - brandt_assert(ret); - gcry_ctx_release(ctx); - return ret; -} -*/ /* --- EC --- */ @@ -238,8 +242,13 @@ key_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, const char *topname, return 0; } +/** + * brandt_ec_skey_create + * + * @param[out] skey where to store the generated secret key + */ void -brandt_ec_skey_create (gcry_mpi_t* skey) +brandt_ec_skey_create (gcry_mpi_t *skey) { gcry_sexp_t s_keyparam; gcry_sexp_t priv_sexp; @@ -259,16 +268,26 @@ brandt_ec_skey_create (gcry_mpi_t* skey) gcry_sexp_release (priv_sexp); } - +/** + * brandt_ec_pkey_compute + * + * @param pkey TODO + * @param skey TODO + */ void -brandt_ec_pkey_compute (gcry_mpi_point_t* pkey, const gcry_mpi_t skey) +brandt_ec_pkey_compute (gcry_mpi_point_t *pkey, const gcry_mpi_t skey) { } - +/** + * brandt_ec_keypair_create + * + * @param[out] pkey where to store the generated public key + * @param[out] skey where to store the generated secret key + */ void -brandt_ec_keypair_create (gcry_mpi_point_t* pkey, gcry_mpi_t* skey) +brandt_ec_keypair_create (gcry_mpi_point_t *pkey, gcry_mpi_t *skey) { gcry_ctx_t ctx; gcry_sexp_t s_keyparam; @@ -290,52 +309,66 @@ brandt_ec_keypair_create (gcry_mpi_point_t* pkey, gcry_mpi_t* skey) brandt_assert_gpgerr (rc); gcry_sexp_release (priv_sexp); - *pkey = gcry_mpi_ec_get_point("q", ctx, 0); + *pkey = gcry_mpi_ec_get_point ("q", ctx, 0); brandt_assert (NULL != *pkey); gcry_ctx_release (ctx); } - +/** + * brandt_ec_keypair_create_base + * + * @param[out] pkey where to store the generated public key + * @param[out] skey where to store the generated secret key + * @param[in] base which base point should be used to calculate the public key + */ void -brandt_ec_keypair_create_base (gcry_mpi_point_t* pkey, gcry_mpi_t* skey, const gcry_mpi_point_t base) +brandt_ec_keypair_create_base (gcry_mpi_point_t *pkey, gcry_mpi_t *skey, + const gcry_mpi_point_t base) { - brandt_ec_skey_create(skey); - brandt_assert(*skey); - *pkey = gcry_mpi_point_new(0); - brandt_assert(*pkey); - gcry_mpi_ec_mul(*pkey, *skey, base, ec_ctx); + brandt_ec_skey_create (skey); + brandt_assert (*skey); + *pkey = gcry_mpi_point_new (0); + brandt_assert (*pkey); + gcry_mpi_ec_mul (*pkey, *skey, base, ec_ctx); } - +/** + * brandt_ec_point_cmp compares two curve points + * + * @param[in] a the first point + * @param[in] b the second point + * @return 0 if @a a and @a b represent the same point on the curve, something + * else otherwise + */ int brandt_ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b) { - int ret = 1; - gcry_mpi_t ax = gcry_mpi_new(0); - gcry_mpi_t bx = gcry_mpi_new(0); - gcry_mpi_t ay = gcry_mpi_new(0); - gcry_mpi_t by = gcry_mpi_new(0); + int ret = 1; + gcry_mpi_t ax = gcry_mpi_new (0); + gcry_mpi_t bx = gcry_mpi_new (0); + gcry_mpi_t ay = gcry_mpi_new (0); + gcry_mpi_t by = gcry_mpi_new (0); brandt_assert (a && b); if (!ax || !bx || !ay || !by) { - weprintf("could not init point in point_cmp"); + weprintf ("could not init point in point_cmp"); return 1; } - if (!gcry_mpi_ec_get_affine(ax, ay, a, ec_ctx) && !gcry_mpi_ec_get_affine(bx, by, b, ec_ctx)) + if (!gcry_mpi_ec_get_affine (ax, ay, a, ec_ctx) + && !gcry_mpi_ec_get_affine (bx, by, b, ec_ctx)) { - ret = gcry_mpi_cmp(ax, bx) || gcry_mpi_cmp(ay, by); + ret = gcry_mpi_cmp (ax, bx) || gcry_mpi_cmp (ay, by); } - gcry_mpi_release(ax); - gcry_mpi_release(bx); - gcry_mpi_release(ay); - gcry_mpi_release(by); + gcry_mpi_release (ax); + gcry_mpi_release (bx); + gcry_mpi_release (ay); + gcry_mpi_release (by); return ret; } - /** * Convert the given private key from the network format to the * S-expression that can be used by libgcrypt. @@ -395,7 +428,7 @@ brandt_ecdhe_key_get_public (const struct brandt_ec_skey *priv, int brandt_ecdhe (const struct brandt_ec_skey *priv, const struct brandt_ec_pkey *pub, - struct brandt_hash_code *key_material) + struct brandt_hash_code *key_material) { gcry_error_t rc; int rc2; diff --git a/crypto.h b/crypto.h index 0df73e4..acdce11 100644 --- a/crypto.h +++ b/crypto.h @@ -50,10 +50,10 @@ void brandt_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size /* --- EC --- */ -void brandt_ec_skey_create (gcry_mpi_t* skey); -void brandt_ec_pkey_compute (gcry_mpi_point_t* pkey, const gcry_mpi_t skey); -void brandt_ec_keypair_create (gcry_mpi_point_t* pkey, gcry_mpi_t* skey); -void brandt_ec_keypair_create_base (gcry_mpi_point_t* pkey, gcry_mpi_t* skey, const gcry_mpi_point_t base); +void brandt_ec_skey_create (gcry_mpi_t *skey); +void brandt_ec_pkey_compute (gcry_mpi_point_t *pkey, const gcry_mpi_t skey); +void brandt_ec_keypair_create (gcry_mpi_point_t *pkey, gcry_mpi_t *skey); +void brandt_ec_keypair_create_base (gcry_mpi_point_t *pkey, gcry_mpi_t *skey, const gcry_mpi_point_t base); int brandt_ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b); #endif /* ifndef _BRANDT_CRYPTO_H */ diff --git a/smc.c b/smc.c index 52afa35..52c1b3b 100644 --- a/smc.c +++ b/smc.c @@ -22,63 +22,85 @@ #include #include "crypto.h" -#include "util.h" #include "smc.h" +#include "util.h" extern gcry_ctx_t ec_ctx; +/** + * smc_zkp_dl + * + * @param v TODO + * @param g TODO + * @param x TODO + * @param a TODO + * @param c TODO + * @param r TODO + */ void -smc_zkp_dl (gcry_mpi_point_t v, gcry_mpi_point_t g, gcry_mpi_t x, gcry_mpi_point_t *a, gcry_mpi_t *c, gcry_mpi_t *r) +smc_zkp_dl (gcry_mpi_point_t v, gcry_mpi_point_t g, gcry_mpi_t x, + gcry_mpi_point_t *a, gcry_mpi_t *c, + gcry_mpi_t *r) { - gcry_mpi_t z = gcry_mpi_new(0); + gcry_mpi_t z = gcry_mpi_new (0); - brandt_ec_keypair_create_base(a, &z, g); + brandt_ec_keypair_create_base (a, &z, g); /**TODO: generate c from HASH(g,v,a) and don't output it */ - brandt_ec_skey_create(c); - *r = gcry_mpi_new(0); - gcry_mpi_mul(*r, *c, x); - gcry_mpi_add(*r, *r, z); + brandt_ec_skey_create (c); + *r = gcry_mpi_new (0); + gcry_mpi_mul (*r, *c, x); + gcry_mpi_add (*r, *r, z); - gcry_mpi_release(z); + gcry_mpi_release (z); } +/** + * smc_zkp_dl_check + * + * @param v TODO + * @param g TODO + * @param a TODO + * @param c TODO + * @param r TODO + * @return 0 if the proof is correct, something else otherwise + */ int -smc_zkp_dl_check (gcry_mpi_point_t v, gcry_mpi_point_t g, gcry_mpi_point_t a, gcry_mpi_t c, gcry_mpi_t r) +smc_zkp_dl_check (gcry_mpi_point_t v, gcry_mpi_point_t g, gcry_mpi_point_t a, + gcry_mpi_t c, + gcry_mpi_t r) { - int ret; - gcry_mpi_point_t left = gcry_mpi_point_new(0); - gcry_mpi_point_t right = gcry_mpi_point_new(0); + int ret; + gcry_mpi_point_t left = gcry_mpi_point_new (0); + gcry_mpi_point_t right = gcry_mpi_point_new (0); - gcry_mpi_ec_mul(left, r, g, ec_ctx); - gcry_mpi_ec_mul(right, c, v, ec_ctx); - gcry_mpi_ec_add(right, a, right, ec_ctx); + gcry_mpi_ec_mul (left, r, g, ec_ctx); + gcry_mpi_ec_mul (right, c, v, ec_ctx); + gcry_mpi_ec_add (right, a, right, ec_ctx); - ret = brandt_ec_point_cmp(left, right); - gcry_mpi_point_release(left); - gcry_mpi_point_release(right); + ret = brandt_ec_point_cmp (left, right); + gcry_mpi_point_release (left); + gcry_mpi_point_release (right); return ret; } -/* -GEN -smc_hextodec (const char *s) -{ - size_t i; - char c; - pari_sp ltop = avma; - GEN ret = gen_0; - - for (i = 0; i < strlen (s); i++) - { - errno = 0; - if (1 != sscanf (&s[i], "%1hhx", &c)) - { - brandt_eprintf ("failed to parse hex (\"%s\") to decimal:", s); - return NULL; - } - ret = addis (shifti (ret, 4), c); - } - return gerepilecopy (ltop, ret); -} -*/ +//GEN +//smc_hextodec (const char *s) +//{ +// size_t i; +// char c; +// pari_sp ltop = avma; +// GEN ret = gen_0; +// +// for (i = 0; i < strlen (s); i++) +// { +// errno = 0; +// if (1 != sscanf (&s[i], "%1hhx", &c)) +// { +// brandt_eprintf ("failed to parse hex (\"%s\") to decimal:", s); +// return NULL; +// } +// ret = addis (shifti (ret, 4), c); +// } +// return gerepilecopy (ltop, ret); +//} diff --git a/smc.h b/smc.h index d5a13cf..5ea81e8 100644 --- a/smc.h +++ b/smc.h @@ -1,3 +1,23 @@ +/* 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 . + */ + +/** + * @file smc.h + * @brief describes the secure multiparty computation interface. + */ #ifndef _BRANDT_SMC_H #define _BRANDT_SMC_H @@ -7,4 +27,4 @@ void smc_zkp_dl (gcry_mpi_point_t v, gcry_mpi_point_t g, gcry_mpi_t x, gcry_mpi_point_t *a, gcry_mpi_t *c, gcry_mpi_t *r); int smc_zkp_dl_check (gcry_mpi_point_t v, gcry_mpi_point_t g, gcry_mpi_point_t a, gcry_mpi_t c, gcry_mpi_t r); -#endif +#endif // ifndef _BRANDT_SMC_H diff --git a/test.h b/test.h index 45ace82..dd1ead0 100644 --- a/test.h +++ b/test.h @@ -1,3 +1,23 @@ +/* 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 . + */ + +/** + * @file test.h + * @brief Some helpers for running tests + */ #ifndef _BRANDT_TEST_H #define _BRANDT_TEST_H @@ -6,7 +26,7 @@ int tests_run = 0; int ret = 0; -#define check(cond, message) do { if (!(cond)) { fputs(message, stderr); fputc('\n', stderr); return 0; } } while (0) -#define run(test) do { tests_run++; if (!test()) ret = 1; } while (0) +#define check(cond, message) do { if (!(cond)) { fputs (message, stderr); fputc ('\n', stderr); return 0; } } while (0) +#define run(test) do { tests_run++; if (!test ()) { ret = 1; } } while (0) -#endif +#endif // ifndef _BRANDT_TEST_H diff --git a/test_crypto.c b/test_crypto.c index 1bfc321..d692055 100644 --- a/test_crypto.c +++ b/test_crypto.c @@ -1,69 +1,88 @@ +/* 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 . + */ +/** + * @file test_crypto.c + * @brief testing crypto and smc functions. + */ #include "brandt.h" #include "crypto.h" #include "smc.h" #include "test.h" extern gcry_mpi_point_t ec_gen; -extern gcry_ctx_t ec_ctx; +extern gcry_ctx_t ec_ctx; int test_brandt_ec_keypair_create () { - gcry_mpi_t skey; + gcry_mpi_t skey; gcry_mpi_point_t pkey1; - gcry_mpi_point_t pkey2 = gcry_mpi_point_new(0); + gcry_mpi_point_t pkey2 = gcry_mpi_point_new (0); brandt_ec_keypair_create (&pkey1, &skey); - check(skey, "no sec key created"); - check(pkey1, "no pub key created"); - check(pkey2, "could not init pkey2"); + check (skey, "no sec key created"); + check (pkey1, "no pub key created"); + check (pkey2, "could not init pkey2"); - gcry_mpi_ec_mul(pkey2, skey, ec_gen, ec_ctx); - check(!brandt_ec_point_cmp(pkey1, pkey2), "pkeys do not match"); + gcry_mpi_ec_mul (pkey2, skey, ec_gen, ec_ctx); + check (!brandt_ec_point_cmp (pkey1, pkey2), "pkeys do not match"); - gcry_mpi_release(skey); - gcry_mpi_point_release(pkey1); - gcry_mpi_point_release(pkey2); + gcry_mpi_release (skey); + gcry_mpi_point_release (pkey1); + gcry_mpi_point_release (pkey2); } int test_smc_zkp_dl () { - static int first = 1; - gcry_mpi_t c; - gcry_mpi_t r; - gcry_mpi_t s; - gcry_mpi_t x; + static int first = 1; + gcry_mpi_t c; + gcry_mpi_t r; + gcry_mpi_t s; + gcry_mpi_t x; gcry_mpi_point_t a; gcry_mpi_point_t g; - gcry_mpi_point_t v = gcry_mpi_point_new(0); + gcry_mpi_point_t v = gcry_mpi_point_new (0); - check(v, "no pub key initialized"); + check (v, "no pub key initialized"); brandt_ec_keypair_create (&g, &s); - check(g, "no gen created"); + check (g, "no gen created"); if (first) { - gcry_mpi_ec_mul(g, GCRYMPI_CONST_ONE, ec_gen, ec_ctx); + gcry_mpi_ec_mul (g, GCRYMPI_CONST_ONE, ec_gen, ec_ctx); first = 0; } - brandt_ec_skey_create(&x); - check(x, "no sec key created"); - gcry_mpi_ec_mul(v, x, g, ec_ctx); - check(v, "no pub key created"); + brandt_ec_skey_create (&x); + check (x, "no sec key created"); + gcry_mpi_ec_mul (v, x, g, ec_ctx); + check (v, "no pub key created"); - smc_zkp_dl(v, g, x, &a, &c, &r); - check(!smc_zkp_dl_check(v, g, a, c, r), "zkp was false, should be true"); + smc_zkp_dl (v, g, x, &a, &c, &r); + check (!smc_zkp_dl_check (v, g, a, c, r), "zkp was false, should be true"); - gcry_mpi_release(c); - gcry_mpi_release(r); - gcry_mpi_release(s); - gcry_mpi_release(x); - gcry_mpi_point_release(a); - gcry_mpi_point_release(g); - gcry_mpi_point_release(v); + gcry_mpi_release (c); + gcry_mpi_release (r); + gcry_mpi_release (s); + gcry_mpi_release (x); + gcry_mpi_point_release (a); + gcry_mpi_point_release (g); + gcry_mpi_point_release (v); } int @@ -72,12 +91,12 @@ main (int argc, char *argv[]) int repeat = 50; int i; - BRANDT_init(); + BRANDT_init (); for (i = 0; i < repeat; i++) { - run(test_brandt_ec_keypair_create); - run(test_smc_zkp_dl); + run (test_brandt_ec_keypair_create); + run (test_smc_zkp_dl); } return ret; diff --git a/uncrustify/conf b/uncrustify/conf index 2a2b39a..8a04ef8 100644 --- a/uncrustify/conf +++ b/uncrustify/conf @@ -1154,7 +1154,7 @@ nl_after_func_body_one_liner = 0 # number # The minimum number of newlines before a multi-line comment. # Doesn't apply if after a brace open or another multi-line comment. -nl_before_block_comment = 0 # number +nl_before_block_comment = 1 # number # The minimum number of newlines before a single-line C comment. # Doesn't apply if after a brace open or other single-line C comments. @@ -1320,7 +1320,7 @@ cmt_cpp_nl_start = false # false/true cmt_cpp_nl_end = false # false/true # Whether to change cpp-comments into c-comments -cmt_cpp_to_c = true # false/true +cmt_cpp_to_c = false # false/true # Whether to put a star on subsequent comment lines cmt_star_cont = true # false/true