From eebeb27a3b52783aaa5cb3a69ba3e5b250276fe8 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 30 Jun 2015 09:43:59 +0000 Subject: patch from Nicolas Fournier to add some _dup and _cmp functions for RSA signatures and private keys --- src/include/gnunet_crypto_lib.h | 31 +++++++++++++++ src/util/crypto_rsa.c | 86 +++++++++++++++++++++++++++++++++++++++++ src/util/test_crypto_rsa.c | 10 +++++ 3 files changed, 127 insertions(+) (limited to 'src') diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 7439932c9..af039dbf5 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h @@ -1602,6 +1602,16 @@ GNUNET_CRYPTO_rsa_private_key_decode (const char *buf, size_t len); +/** + * 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. * @@ -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. @@ -1813,6 +1834,16 @@ GNUNET_CRYPTO_rsa_signature_decode (const char *buf, size_t len); +/** + * 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 diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c index b8e29146f..d1ca760cf 100644 --- a/src/util/crypto_rsa.c +++ b/src/util/crypto_rsa.c @@ -493,6 +493,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 * @@ -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 20c270583..70b388b8f 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); -- cgit v1.2.3