libbrandt/crypto.c

363 lines
8.2 KiB
C
Raw Normal View History

2016-06-12 01:14:56 +02:00
/* 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 crypto.c
* @brief Implementation of the crypto primitives.
*/
#include <arpa/inet.h>
#include "crypto.h"
#include "util.h"
#define CURVE "Ed25519"
2016-06-12 20:52:22 +02:00
struct brandt_ec_skey {
unsigned char d[256 / 8];
};
struct brandt_ec_pkey {
unsigned char q_y[256 / 8];
};
2016-06-13 21:09:41 +02:00
gcry_ctx_t ec_ctx;
gcry_mpi_point_t ec_gen;
gcry_mpi_point_t ec_zero;
gcry_mpi_t ec_n;
2016-06-12 20:52:22 +02:00
2016-06-13 21:09:41 +02:00
/**
* brandt_crypto_init
*
*
*/
2016-06-12 20:52:22 +02:00
void
brandt_crypto_init ()
{
gcry_error_t rc;
rc = gcry_mpi_ec_new (&ec_ctx, NULL, CURVE);
brandt_assert_gpgerr (rc);
2016-06-12 20:52:22 +02:00
ec_gen = gcry_mpi_ec_get_point ("g", ec_ctx, 0);
brandt_assert (NULL != ec_gen);
ec_zero = gcry_mpi_point_new (0);
brandt_assert (NULL != ec_zero);
gcry_mpi_ec_sub (ec_zero, ec_gen, ec_gen, ec_ctx);
ec_n = gcry_mpi_ec_get_mpi ("n", ec_ctx, 1);
brandt_assert (NULL != ec_n);
2016-06-12 20:52:22 +02:00
}
2016-06-12 01:14:56 +02:00
/* --- RANDOM --- */
void
brandt_rand_poll ()
{
static unsigned char rand_amount = 255;
if (!(rand_amount--))
gcry_fast_random_poll ();
}
2016-06-12 01:14:56 +02:00
/* --- HASHING --- */
/**
* Hash block of given size.
*
* @param block the data to #brandt_hash, length is given as a second argument
* @param size the length of the data to #brandt_hash in @a block
* @param ret pointer to where to write the hashcode
*/
void
brandt_hash (const void *block, size_t size, struct brandt_hash_code *ret)
{
gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
}
2016-06-12 01:14:56 +02:00
/* --- MPI --- */
/**
* If target != size, move @a target bytes to the end of the size-sized
* buffer and zero out the first @a target - @a size bytes.
*
* @param buf original buffer
* @param size number of bytes in @a buf
* @param target target size of the buffer
*/
static void
adjust (void *buf, size_t size, size_t target)
{
char *p = buf;
if (size < target)
{
memmove (&p[target - size], buf, size);
memset (buf, 0, target - size);
}
}
2016-06-12 01:14:56 +02:00
/**
* Output the given MPI value to the given buffer in
* network byte order.
* The MPI @a val may not be negative.
*
* @param buf where to output to
* @param size number of bytes in @a buf
* @param val value to write to @a buf
*/
void
brandt_mpi_print_unsigned (void *buf, size_t size, gcry_mpi_t val)
{
size_t rsize;
gcry_error_t rc;
if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE))
{
/* Store opaque MPIs left aligned into the buffer. */
unsigned int nbits;
const void *p;
p = gcry_mpi_get_opaque (val, &nbits);
brandt_assert (NULL != p);
rsize = (nbits + 7) / 8;
if (rsize > size)
rsize = size;
memcpy (buf, p, rsize);
if (rsize < size)
memset (((char *)buf) + rsize, 0, size - rsize);
}
else
{
/* Store regular MPIs as unsigned integers right aligned into the buffer. */
rsize = size;
rc = gcry_mpi_print (GCRYMPI_FMT_USG, buf, rsize, &rsize, val);
brandt_assert_gpgerr (rc);
adjust (buf, rsize, size);
}
}
2016-06-12 01:14:56 +02:00
/**
* Convert data buffer into MPI value.
* The buffer is interpreted as network
* byte order, unsigned integer.
*
* @param result where to store MPI value (allocated)
* @param data raw data (GCRYMPI_FMT_USG)
* @param size number of bytes in @a data
*/
void
brandt_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size)
{
gcry_error_t rc;
rc = gcry_mpi_scan (result, GCRYMPI_FMT_USG, data, size, &size);
brandt_assert_gpgerr (rc);
}
2016-06-13 21:09:41 +02:00
//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;
//}
2016-06-12 01:14:56 +02:00
2016-06-12 20:52:22 +02:00
/* --- EC --- */
2016-06-12 01:14:56 +02:00
2016-06-13 21:09:41 +02:00
/**
* brandt_ec_skey_create
*
2016-06-19 18:42:19 +02:00
* @param[out] skey where to store the generated secret key. This has to be an
* already initialized mpi.
2016-06-13 21:09:41 +02:00
*/
2016-06-12 01:14:56 +02:00
void
brandt_ec_skey_create (gcry_mpi_t skey)
2016-06-12 01:14:56 +02:00
{
gcry_mpi_t ret;
2016-06-12 01:14:56 +02:00
gcry_sexp_t s_keyparam;
2016-06-12 20:52:22 +02:00
gcry_sexp_t priv_sexp;
gcry_sexp_t priv_key;
gcry_sexp_t priv_key2;
2016-06-12 01:14:56 +02:00
gcry_error_t rc;
rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")"
2016-06-12 20:52:22 +02:00
"(flags)))");
brandt_assert_gpgerr (rc);
2016-06-13 21:01:14 +02:00
2016-06-12 20:52:22 +02:00
rc = gcry_pk_genkey (&priv_sexp, s_keyparam);
brandt_assert_gpgerr (rc);
2016-06-12 01:14:56 +02:00
gcry_sexp_release (s_keyparam);
2016-06-13 21:01:14 +02:00
priv_key = gcry_sexp_find_token (priv_sexp, "private-key", 11);
brandt_assert (NULL != priv_key);
2016-06-12 01:14:56 +02:00
gcry_sexp_release (priv_sexp);
2016-06-12 20:52:22 +02:00
priv_key2 = gcry_sexp_find_token (priv_key, "d", 1);
brandt_assert (NULL != priv_key2);
gcry_sexp_release (priv_key);
2016-06-12 20:52:22 +02:00
ret = gcry_sexp_nth_mpi (priv_key2, 1, GCRYMPI_FMT_USG);
brandt_assert (NULL != ret);
gcry_sexp_release (priv_key2);
gcry_mpi_snatch (skey, ret);
2016-06-12 20:52:22 +02:00
}
2016-06-13 21:09:41 +02:00
/**
* brandt_ec_keypair_create
*
* @param[out] pkey where to store the generated public key
* @param[out] skey where to store the generated secret key
*/
2016-06-12 20:52:22 +02:00
void
brandt_ec_keypair_create (gcry_mpi_point_t pkey, gcry_mpi_t skey)
2016-06-12 20:52:22 +02:00
{
brandt_assert (NULL != pkey);
brandt_assert (NULL != skey);
2016-06-12 20:52:22 +02:00
brandt_ec_skey_create (skey);
gcry_mpi_ec_mul (pkey, skey, ec_gen, ec_ctx);
2016-06-12 20:52:22 +02:00
}
2016-06-13 21:09:41 +02:00
/**
* 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
*/
2016-06-13 21:01:14 +02:00
void
brandt_ec_keypair_create_base (gcry_mpi_point_t pkey,
gcry_mpi_t skey,
2016-06-13 21:09:41 +02:00
const gcry_mpi_point_t base)
2016-06-13 21:01:14 +02:00
{
brandt_assert (NULL != pkey);
brandt_assert (NULL != skey);
brandt_assert (NULL != base);
2016-06-13 21:09:41 +02:00
brandt_ec_skey_create (skey);
gcry_mpi_ec_mul (pkey, skey, base, ec_ctx);
2016-06-13 21:01:14 +02:00
}
2016-06-13 21:09:41 +02:00
/**
* 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
*/
2016-06-13 21:01:14 +02:00
int
brandt_ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b)
{
2016-06-13 21:09:41 +02:00
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);
2016-06-13 21:01:14 +02:00
brandt_assert (a && b);
if (!ax || !bx || !ay || !by)
{
2016-06-13 21:09:41 +02:00
weprintf ("could not init point in point_cmp");
2016-06-13 21:01:14 +02:00
return 1;
}
if (!gcry_mpi_ec_get_affine (ax, ay, a, ec_ctx) &&
!gcry_mpi_ec_get_affine (bx, by, b, ec_ctx))
2016-06-13 21:01:14 +02:00
{
2016-06-13 21:09:41 +02:00
ret = gcry_mpi_cmp (ax, bx) || gcry_mpi_cmp (ay, by);
2016-06-13 21:01:14 +02:00
}
2016-06-13 21:09:41 +02:00
gcry_mpi_release (ax);
gcry_mpi_release (bx);
gcry_mpi_release (ay);
gcry_mpi_release (by);
2016-06-13 21:01:14 +02:00
return ret;
}
2016-06-12 01:14:56 +02:00
/**
* Clear memory that was used to store a private key.
*
2016-06-12 20:52:22 +02:00
* @param skey location of the key
2016-06-12 01:14:56 +02:00
*/
void
2016-06-12 20:52:22 +02:00
brandt_ec_key_clear (struct brandt_ec_skey *skey)
2016-06-12 01:14:56 +02:00
{
2016-06-12 20:52:22 +02:00
memset (skey, 0, sizeof (struct brandt_ec_skey));
2016-06-12 01:14:56 +02:00
}
2016-06-12 20:52:22 +02:00
2016-06-12 20:52:22 +02:00
/**
* Generate a random value mod n.
*
* @param edc ECC context
* @return random value mod n.
*/
//gcry_mpi_t
//GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc)
//{
// gcry_mpi_t n;
// unsigned int highbit;
// gcry_mpi_t r;
//
// n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
//
// /* check public key for number of bits, bail out if key is all zeros */
// highbit = 256; /* Curve25519 */
// while ( (! gcry_mpi_test_bit (n, highbit)) &&
// (0 != highbit) )
// highbit--;
// GNUNET_assert (0 != highbit);
// /* generate fact < n (without bias) */
// GNUNET_assert (NULL != (r = gcry_mpi_new (0)));
// do {
// gcry_mpi_randomize (r,
// highbit + 1,
// GCRY_STRONG_RANDOM);
// }
// while (gcry_mpi_cmp (r, n) >= 0);
// gcry_mpi_release (n);
// return r;
//}