aboutsummaryrefslogtreecommitdiff
path: root/crypto.c
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-06-12 20:52:22 +0200
committerMarkus Teich <markus.teich@stusta.mhn.de>2016-06-12 20:52:22 +0200
commit62b87e57a7f7042d27fe0a80b9194aeae0c14a50 (patch)
tree961a43363dbca413e4b1e65b367c0ffd553cfaf0 /crypto.c
parent5957a777076d014b17aada25afe0991397edbacc (diff)
add tests for key generation
Diffstat (limited to 'crypto.c')
-rw-r--r--crypto.c190
1 files changed, 149 insertions, 41 deletions
diff --git a/crypto.c b/crypto.c
index 399cd21..e78032e 100644
--- a/crypto.c
+++ b/crypto.c
@@ -26,6 +26,28 @@
#define CURVE "Ed25519"
+struct brandt_ec_skey {
+ unsigned char d[256 / 8];
+};
+
+struct brandt_ec_pkey {
+ unsigned char q_y[256 / 8];
+};
+
+gcry_mpi_point_t ec_gen;
+gcry_ctx_t ec_ctx;
+
+void
+brandt_crypto_init ()
+{
+ gcry_error_t rc;
+
+ rc = gcry_mpi_ec_new (&ec_ctx, NULL, CURVE);
+ brandt_assert_gpgerr (rc);
+ ec_gen = gcry_mpi_ec_get_point ("g", ec_ctx, 0);
+ brandt_assert (NULL != ec_gen);
+}
+
/* --- RANDOM --- */
void
@@ -132,28 +154,31 @@ brandt_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size)
brandt_assert_gpgerr (rc);
}
-/* --- ECDHE --- */
-
-/**
- * Convert the given private key from the network format to the
- * S-expression that can be used by libgcrypt.
- *
- * @param priv private key to decode
- * @return NULL on error
- */
-static gcry_sexp_t
-decode_private_ecdhe_key (const struct brandt_dhe_skey *priv)
+/*
+gcry_mpi_point_t
+deserialize_point(const struct brandt_point* data, const int len)
{
- gcry_sexp_t result;
+ gcry_sexp_t s;
+ gcry_ctx_t ctx;
+ gcry_mpi_point_t ret;
gcry_error_t rc;
- rc = gcry_sexp_build (&result, NULL,
- "(private-key(ecc(curve \"" CURVE "\")"
- "(d %b)))",
- (int)sizeof (priv->d), priv->d);
- brandt_assert_gpgerr (rc);
- return result;
+ 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 --- */
/**
* Extract values from an S-expression.
@@ -213,30 +238,81 @@ key_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, const char *topname,
return 0;
}
-/**
- * Create a new private key.
- *
- * @param priv where to write the private key
- */
void
-brandt_ecdhe_key_create (struct brandt_dhe_skey *priv)
+brandt_ec_skey_create (gcry_mpi_t* skey)
{
- gcry_sexp_t priv_sexp;
gcry_sexp_t s_keyparam;
+ gcry_sexp_t priv_sexp;
gcry_mpi_t d;
gcry_error_t rc;
rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")"
- "(flags)))")
- brandt_assert_gpgerr (rc);
- rc = gcry_pk_genkey (&priv_sexp, s_keyparam)
- brandt_assert_gpgerr (rc);
+ "(flags)))");
+ brandt_assert_gpgerr (rc);
+ rc = gcry_pk_genkey (&priv_sexp, s_keyparam);
+ brandt_assert_gpgerr (rc);
gcry_sexp_release (s_keyparam);
- rc = key_from_sexp (&d, priv_sexp, "private-key", "d")
- brandt_assert_gpgerr (rc);
+ rc = key_from_sexp (skey, priv_sexp, "private-key", "d");
+ brandt_assert_gpgerr (rc);
gcry_sexp_release (priv_sexp);
- brandt_mpi_print_unsigned (priv->d, sizeof (priv->d), d);
- gcry_mpi_release (d);
+}
+
+
+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)
+{
+ gcry_error_t rc;
+ gcry_sexp_t s_keyparam;
+ gcry_sexp_t priv_sexp;
+ gcry_ctx_t ctx;
+
+ rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")"
+ "(flags)))");
+ brandt_assert_gpgerr (rc);
+
+ rc = gcry_pk_genkey (&priv_sexp, s_keyparam);
+ brandt_assert_gpgerr (rc);
+ gcry_sexp_release (s_keyparam);
+
+ rc = key_from_sexp (skey, priv_sexp, "private-key", "d");
+ brandt_assert_gpgerr (rc);
+
+ rc = gcry_mpi_ec_new (&ctx, priv_sexp, NULL);
+ brandt_assert_gpgerr (rc);
+ gcry_sexp_release (priv_sexp);
+
+ *pkey = gcry_mpi_ec_get_point("q", ctx, 0);
+ brandt_assert (NULL != *pkey);
+ gcry_ctx_release (ctx);
+}
+
+
+/**
+ * Convert the given private key from the network format to the
+ * S-expression that can be used by libgcrypt.
+ *
+ * @param priv private key to decode
+ * @return NULL on error
+ */
+static gcry_sexp_t
+decode_private_ecdhe_key (const struct brandt_ec_skey *priv)
+{
+ gcry_sexp_t result;
+ gcry_error_t rc;
+
+ rc = gcry_sexp_build (&result, NULL,
+ "(private-key(ecc(curve \"" CURVE "\")"
+ "(d %b)))",
+ (int)sizeof (priv->d), priv->d);
+ brandt_assert_gpgerr (rc);
+ return result;
}
/**
@@ -246,8 +322,8 @@ brandt_ecdhe_key_create (struct brandt_dhe_skey *priv)
* @param pub where to write the public key
*/
void
-brandt_ecdhe_key_get_public (const struct brandt_dhe_skey *priv,
- struct brandt_dhe_pkey *pub)
+brandt_ecdhe_key_get_public (const struct brandt_ec_skey *priv,
+ struct brandt_ec_pkey *pub)
{
gcry_sexp_t sexp;
gcry_ctx_t ctx;
@@ -275,8 +351,8 @@ brandt_ecdhe_key_get_public (const struct brandt_dhe_skey *priv,
* @return 0 on error, 1 on success
*/
int
-brandt_ecdhe (const struct brandt_dhe_skey *priv,
- const struct brandt_dhe_pkey *pub,
+brandt_ecdhe (const struct brandt_ec_skey *priv,
+ const struct brandt_ec_pkey *pub,
struct brandt_hash_code *key_material)
{
gcry_error_t rc;
@@ -331,13 +407,45 @@ brandt_ecdhe (const struct brandt_dhe_skey *priv,
}
/**
- * @ingroup crypto
* Clear memory that was used to store a private key.
*
- * @param pk location of the key
+ * @param skey location of the key
*/
void
-brandt_ecdhe_key_clear (struct brandt_dhe_skey *pk)
+brandt_ec_key_clear (struct brandt_ec_skey *skey)
{
- memset (pk, 0, sizeof (struct brandt_dhe_skey));
+ memset (skey, 0, sizeof (struct brandt_ec_skey));
}
+
+/**
+ * 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;
+//}