diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-06-30 09:43:59 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-06-30 09:43:59 +0000 |
commit | eebeb27a3b52783aaa5cb3a69ba3e5b250276fe8 (patch) | |
tree | 1fbaf0967447b79574fd7f3a4870aef7dc974c93 | |
parent | 18078ccb0adbcad40b9bae16f4934e2e0e686e0c (diff) | |
download | gnunet-eebeb27a3b52783aaa5cb3a69ba3e5b250276fe8.tar.gz gnunet-eebeb27a3b52783aaa5cb3a69ba3e5b250276fe8.zip |
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.h | 31 | ||||
-rw-r--r-- | src/util/crypto_rsa.c | 86 | ||||
-rw-r--r-- | src/util/test_crypto_rsa.c | 10 |
3 files changed, 127 insertions, 0 deletions
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 | |||
@@ -1603,6 +1603,16 @@ GNUNET_CRYPTO_rsa_private_key_decode (const char *buf, | |||
1603 | 1603 | ||
1604 | 1604 | ||
1605 | /** | 1605 | /** |
1606 | * Duplicate the given private key | ||
1607 | * | ||
1608 | * @param key the private key to duplicate | ||
1609 | * @return the duplicate key; NULL upon error | ||
1610 | */ | ||
1611 | struct GNUNET_CRYPTO_rsa_PrivateKey * | ||
1612 | GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_rsa_PrivateKey *key); | ||
1613 | |||
1614 | |||
1615 | /** | ||
1606 | * Extract the public key of the given private key. | 1616 | * Extract the public key of the given private key. |
1607 | * | 1617 | * |
1608 | * @param priv the private key | 1618 | * @param priv the private key |
@@ -1701,6 +1711,17 @@ int | |||
1701 | GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_rsa_Signature *s1, | 1711 | GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_rsa_Signature *s1, |
1702 | struct GNUNET_CRYPTO_rsa_Signature *s2); | 1712 | struct GNUNET_CRYPTO_rsa_Signature *s2); |
1703 | 1713 | ||
1714 | /** | ||
1715 | * Compare the values of two private keys. | ||
1716 | * | ||
1717 | * @param p1 one private key | ||
1718 | * @param p2 the other private key | ||
1719 | * @return 0 if the two are equal | ||
1720 | */ | ||
1721 | int | ||
1722 | GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_rsa_PrivateKey *p1, | ||
1723 | struct GNUNET_CRYPTO_rsa_PrivateKey *p2); | ||
1724 | |||
1704 | 1725 | ||
1705 | /** | 1726 | /** |
1706 | * Compare the values of two public keys. | 1727 | * Compare the values of two public keys. |
@@ -1814,6 +1835,16 @@ GNUNET_CRYPTO_rsa_signature_decode (const char *buf, | |||
1814 | 1835 | ||
1815 | 1836 | ||
1816 | /** | 1837 | /** |
1838 | * Duplicate the given rsa signature | ||
1839 | * | ||
1840 | * @param sig the signature to duplicate | ||
1841 | * @return the duplicate key; NULL upon error | ||
1842 | */ | ||
1843 | struct GNUNET_CRYPTO_rsa_Signature * | ||
1844 | GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_rsa_Signature *sig); | ||
1845 | |||
1846 | |||
1847 | /** | ||
1817 | * Unblind a blind-signed signature. The signature should have been generated | 1848 | * Unblind a blind-signed signature. The signature should have been generated |
1818 | * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with | 1849 | * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with |
1819 | * #GNUNET_CRYPTO_rsa_blind(). | 1850 | * #GNUNET_CRYPTO_rsa_blind(). |
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 | |||
@@ -494,6 +494,39 @@ GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_rsa_PublicKey *p1, | |||
494 | 494 | ||
495 | 495 | ||
496 | /** | 496 | /** |
497 | * Compare the values of two private keys. | ||
498 | * | ||
499 | * @param p1 one private key | ||
500 | * @param p2 the other private key | ||
501 | * @return 0 if the two are equal | ||
502 | */ | ||
503 | int | ||
504 | GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_rsa_PrivateKey *p1, | ||
505 | struct GNUNET_CRYPTO_rsa_PrivateKey *p2) | ||
506 | { | ||
507 | char *b1; | ||
508 | char *b2; | ||
509 | size_t z1; | ||
510 | size_t z2; | ||
511 | int ret; | ||
512 | |||
513 | z1 = GNUNET_CRYPTO_rsa_private_key_encode (p1, | ||
514 | &b1); | ||
515 | z2 = GNUNET_CRYPTO_rsa_private_key_encode (p2, | ||
516 | &b2); | ||
517 | if (z1 != z2) | ||
518 | ret = 1; | ||
519 | else | ||
520 | ret = memcmp (b1, | ||
521 | b2, | ||
522 | z1); | ||
523 | GNUNET_free (b1); | ||
524 | GNUNET_free (b2); | ||
525 | return ret; | ||
526 | } | ||
527 | |||
528 | |||
529 | /** | ||
497 | * Destroy a blinding key | 530 | * Destroy a blinding key |
498 | * | 531 | * |
499 | * @param bkey the blinding key to destroy | 532 | * @param bkey the blinding key to destroy |
@@ -929,4 +962,57 @@ GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash, | |||
929 | } | 962 | } |
930 | 963 | ||
931 | 964 | ||
965 | /** | ||
966 | * Duplicate the given private key | ||
967 | * | ||
968 | * @param key the private key to duplicate | ||
969 | * @return the duplicate key; NULL upon error | ||
970 | */ | ||
971 | struct GNUNET_CRYPTO_rsa_PrivateKey * | ||
972 | GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_rsa_PrivateKey *key) | ||
973 | { | ||
974 | struct GNUNET_CRYPTO_rsa_PrivateKey *dup; | ||
975 | gcry_sexp_t dup_sexp; | ||
976 | size_t erroff; | ||
977 | |||
978 | /* check if we really are exporting a private key */ | ||
979 | dup_sexp = gcry_sexp_find_token (key->sexp, "private-key", 0); | ||
980 | GNUNET_assert (NULL != dup_sexp); | ||
981 | gcry_sexp_release (dup_sexp); | ||
982 | /* copy the sexp */ | ||
983 | GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", key->sexp)); | ||
984 | dup = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey); | ||
985 | dup->sexp = dup_sexp; | ||
986 | return dup; | ||
987 | } | ||
988 | |||
989 | |||
990 | /** | ||
991 | * Duplicate the given private key | ||
992 | * | ||
993 | * @param key the private key to duplicate | ||
994 | * @return the duplicate key; NULL upon error | ||
995 | */ | ||
996 | struct GNUNET_CRYPTO_rsa_Signature * | ||
997 | GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_rsa_Signature *sig) | ||
998 | { | ||
999 | struct GNUNET_CRYPTO_rsa_Signature *dup; | ||
1000 | gcry_sexp_t dup_sexp; | ||
1001 | size_t erroff; | ||
1002 | gcry_mpi_t s; | ||
1003 | int ret; | ||
1004 | |||
1005 | /* verify that this is an RSA signature */ | ||
1006 | ret = key_from_sexp (&s, sig->sexp, "sig-val", "s"); | ||
1007 | GNUNET_assert (0 == ret); | ||
1008 | ret = key_from_sexp (&s, sig->sexp, "rsa", "s"); | ||
1009 | GNUNET_assert (0==ret); | ||
1010 | /* copy the sexp */ | ||
1011 | GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", sig->sexp)); | ||
1012 | dup = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature); | ||
1013 | dup->sexp = dup_sexp; | ||
1014 | return dup; | ||
1015 | } | ||
1016 | |||
1017 | |||
932 | /* end of util/rsa.c */ | 1018 | /* 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, | |||
32 | #define RND_BLK_SIZE 4096 | 32 | #define RND_BLK_SIZE 4096 |
33 | unsigned char rnd_blk[RND_BLK_SIZE]; | 33 | unsigned char rnd_blk[RND_BLK_SIZE]; |
34 | struct GNUNET_CRYPTO_rsa_PrivateKey *priv; | 34 | struct GNUNET_CRYPTO_rsa_PrivateKey *priv; |
35 | struct GNUNET_CRYPTO_rsa_PrivateKey *priv_copy; | ||
35 | struct GNUNET_CRYPTO_rsa_PublicKey *pub; | 36 | struct GNUNET_CRYPTO_rsa_PublicKey *pub; |
36 | struct GNUNET_CRYPTO_rsa_PublicKey *pub_copy; | 37 | struct GNUNET_CRYPTO_rsa_PublicKey *pub_copy; |
37 | struct GNUNET_CRYPTO_rsa_BlindingKey *bkey; | 38 | struct GNUNET_CRYPTO_rsa_BlindingKey *bkey; |
38 | struct GNUNET_CRYPTO_rsa_Signature *sig; | 39 | struct GNUNET_CRYPTO_rsa_Signature *sig; |
40 | struct GNUNET_CRYPTO_rsa_Signature *sig_copy; | ||
39 | struct GNUNET_CRYPTO_rsa_Signature *bsig; | 41 | struct GNUNET_CRYPTO_rsa_Signature *bsig; |
40 | struct GNUNET_HashCode hash; | 42 | struct GNUNET_HashCode hash; |
41 | char *blind_buf; | 43 | char *blind_buf; |
@@ -49,6 +51,9 @@ main (int argc, | |||
49 | RND_BLK_SIZE, | 51 | RND_BLK_SIZE, |
50 | &hash); | 52 | &hash); |
51 | priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE); | 53 | priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE); |
54 | priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv); | ||
55 | GNUNET_assert (NULL != priv_copy); | ||
56 | GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy)); | ||
52 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | 57 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); |
53 | /* Encoding */ | 58 | /* Encoding */ |
54 | size_t size; | 59 | size_t size; |
@@ -70,6 +75,9 @@ main (int argc, | |||
70 | sig = GNUNET_CRYPTO_rsa_sign (priv, | 75 | sig = GNUNET_CRYPTO_rsa_sign (priv, |
71 | &hash, | 76 | &hash, |
72 | sizeof (hash)); | 77 | sizeof (hash)); |
78 | sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig); | ||
79 | GNUNET_assert (NULL != sig); | ||
80 | GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy)); | ||
73 | pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub); | 81 | pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub); |
74 | GNUNET_assert (NULL != pub_copy); | 82 | GNUNET_assert (NULL != pub_copy); |
75 | GNUNET_assert (GNUNET_OK == | 83 | GNUNET_assert (GNUNET_OK == |
@@ -102,7 +110,9 @@ main (int argc, | |||
102 | GNUNET_assert (GNUNET_OK == | 110 | GNUNET_assert (GNUNET_OK == |
103 | GNUNET_CRYPTO_rsa_verify (&hash, sig, pub)); | 111 | GNUNET_CRYPTO_rsa_verify (&hash, sig, pub)); |
104 | GNUNET_CRYPTO_rsa_signature_free (sig); | 112 | GNUNET_CRYPTO_rsa_signature_free (sig); |
113 | GNUNET_CRYPTO_rsa_signature_free (sig_copy); | ||
105 | GNUNET_CRYPTO_rsa_private_key_free (priv); | 114 | GNUNET_CRYPTO_rsa_private_key_free (priv); |
115 | GNUNET_CRYPTO_rsa_private_key_free (priv_copy); | ||
106 | GNUNET_CRYPTO_rsa_public_key_free (pub); | 116 | GNUNET_CRYPTO_rsa_public_key_free (pub); |
107 | GNUNET_CRYPTO_rsa_public_key_free (pub_copy); | 117 | GNUNET_CRYPTO_rsa_public_key_free (pub_copy); |
108 | GNUNET_CRYPTO_rsa_blinding_key_free (bkey); | 118 | GNUNET_CRYPTO_rsa_blinding_key_free (bkey); |