aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-06-30 09:43:59 +0000
committerChristian Grothoff <christian@grothoff.org>2015-06-30 09:43:59 +0000
commiteebeb27a3b52783aaa5cb3a69ba3e5b250276fe8 (patch)
tree1fbaf0967447b79574fd7f3a4870aef7dc974c93
parent18078ccb0adbcad40b9bae16f4934e2e0e686e0c (diff)
patch from Nicolas Fournier to add some _dup and _cmp functions for RSA signatures and private keys
-rw-r--r--src/include/gnunet_crypto_lib.h31
-rw-r--r--src/util/crypto_rsa.c86
-rw-r--r--src/util/test_crypto_rsa.c10
3 files changed, 127 insertions, 0 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 7439932c91..af039dbf5f 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -1603,6 +1603,16 @@ GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
/**
+ * Duplicate the given private key
+ *
+ * @param key the private key to duplicate
+ * @return the duplicate key; NULL upon error
+ */
+struct GNUNET_CRYPTO_rsa_PrivateKey *
+GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_rsa_PrivateKey *key);
+
+
+/**
* Extract the public key of the given private key.
*
* @param priv the private key
@@ -1701,6 +1711,17 @@ int
GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_rsa_Signature *s1,
struct GNUNET_CRYPTO_rsa_Signature *s2);
+/**
+ * Compare the values of two private keys.
+ *
+ * @param p1 one private key
+ * @param p2 the other private key
+ * @return 0 if the two are equal
+ */
+int
+GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_rsa_PrivateKey *p1,
+ struct GNUNET_CRYPTO_rsa_PrivateKey *p2);
+
/**
* Compare the values of two public keys.
@@ -1814,6 +1835,16 @@ GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
/**
+ * Duplicate the given rsa signature
+ *
+ * @param sig the signature to duplicate
+ * @return the duplicate key; NULL upon error
+ */
+struct GNUNET_CRYPTO_rsa_Signature *
+GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_rsa_Signature *sig);
+
+
+/**
* Unblind a blind-signed signature. The signature should have been generated
* with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
* #GNUNET_CRYPTO_rsa_blind().
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index b8e29146f0..d1ca760cfa 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -494,6 +494,39 @@ GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_rsa_PublicKey *p1,
/**
+ * Compare the values of two private keys.
+ *
+ * @param p1 one private key
+ * @param p2 the other private key
+ * @return 0 if the two are equal
+ */
+int
+GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_rsa_PrivateKey *p1,
+ struct GNUNET_CRYPTO_rsa_PrivateKey *p2)
+{
+ char *b1;
+ char *b2;
+ size_t z1;
+ size_t z2;
+ int ret;
+
+ z1 = GNUNET_CRYPTO_rsa_private_key_encode (p1,
+ &b1);
+ z2 = GNUNET_CRYPTO_rsa_private_key_encode (p2,
+ &b2);
+ if (z1 != z2)
+ ret = 1;
+ else
+ ret = memcmp (b1,
+ b2,
+ z1);
+ GNUNET_free (b1);
+ GNUNET_free (b2);
+ return ret;
+}
+
+
+/**
* Destroy a blinding key
*
* @param bkey the blinding key to destroy
@@ -929,4 +962,57 @@ GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
}
+/**
+ * Duplicate the given private key
+ *
+ * @param key the private key to duplicate
+ * @return the duplicate key; NULL upon error
+ */
+struct GNUNET_CRYPTO_rsa_PrivateKey *
+GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_rsa_PrivateKey *key)
+{
+ struct GNUNET_CRYPTO_rsa_PrivateKey *dup;
+ gcry_sexp_t dup_sexp;
+ size_t erroff;
+
+ /* check if we really are exporting a private key */
+ dup_sexp = gcry_sexp_find_token (key->sexp, "private-key", 0);
+ GNUNET_assert (NULL != dup_sexp);
+ gcry_sexp_release (dup_sexp);
+ /* copy the sexp */
+ GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", key->sexp));
+ dup = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey);
+ dup->sexp = dup_sexp;
+ return dup;
+}
+
+
+/**
+ * Duplicate the given private key
+ *
+ * @param key the private key to duplicate
+ * @return the duplicate key; NULL upon error
+ */
+struct GNUNET_CRYPTO_rsa_Signature *
+GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_rsa_Signature *sig)
+{
+ struct GNUNET_CRYPTO_rsa_Signature *dup;
+ gcry_sexp_t dup_sexp;
+ size_t erroff;
+ gcry_mpi_t s;
+ int ret;
+
+ /* verify that this is an RSA signature */
+ ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
+ GNUNET_assert (0 == ret);
+ ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
+ GNUNET_assert (0==ret);
+ /* copy the sexp */
+ GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", sig->sexp));
+ dup = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
+ dup->sexp = dup_sexp;
+ return dup;
+}
+
+
/* end of util/rsa.c */
diff --git a/src/util/test_crypto_rsa.c b/src/util/test_crypto_rsa.c
index 20c270583e..70b388b8f7 100644
--- a/src/util/test_crypto_rsa.c
+++ b/src/util/test_crypto_rsa.c
@@ -32,10 +32,12 @@ main (int argc,
#define RND_BLK_SIZE 4096
unsigned char rnd_blk[RND_BLK_SIZE];
struct GNUNET_CRYPTO_rsa_PrivateKey *priv;
+ struct GNUNET_CRYPTO_rsa_PrivateKey *priv_copy;
struct GNUNET_CRYPTO_rsa_PublicKey *pub;
struct GNUNET_CRYPTO_rsa_PublicKey *pub_copy;
struct GNUNET_CRYPTO_rsa_BlindingKey *bkey;
struct GNUNET_CRYPTO_rsa_Signature *sig;
+ struct GNUNET_CRYPTO_rsa_Signature *sig_copy;
struct GNUNET_CRYPTO_rsa_Signature *bsig;
struct GNUNET_HashCode hash;
char *blind_buf;
@@ -49,6 +51,9 @@ main (int argc,
RND_BLK_SIZE,
&hash);
priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
+ priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
+ GNUNET_assert (NULL != priv_copy);
+ GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
/* Encoding */
size_t size;
@@ -70,6 +75,9 @@ main (int argc,
sig = GNUNET_CRYPTO_rsa_sign (priv,
&hash,
sizeof (hash));
+ sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
+ GNUNET_assert (NULL != sig);
+ GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
GNUNET_assert (NULL != pub_copy);
GNUNET_assert (GNUNET_OK ==
@@ -102,7 +110,9 @@ main (int argc,
GNUNET_assert (GNUNET_OK ==
GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
GNUNET_CRYPTO_rsa_signature_free (sig);
+ GNUNET_CRYPTO_rsa_signature_free (sig_copy);
GNUNET_CRYPTO_rsa_private_key_free (priv);
+ GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
GNUNET_CRYPTO_rsa_public_key_free (pub);
GNUNET_CRYPTO_rsa_public_key_free (pub_copy);
GNUNET_CRYPTO_rsa_blinding_key_free (bkey);