coding style

This commit is contained in:
Markus Teich 2016-06-13 21:09:41 +02:00
parent 557fbe2cc3
commit 338c95de41
7 changed files with 252 additions and 138 deletions

135
crypto.c
View File

@ -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;

View File

@ -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 */

104
smc.c
View File

@ -22,63 +22,85 @@
#include <gcrypt.h>
#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);
//}

22
smc.h
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/**
* @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

26
test.h
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/**
* @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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/**
* @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;

View File

@ -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