2 This file is part of GNUnet
3 Copyright (C) 2014,2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file util/test_crypto_rsa.c
21 * @brief testcase for utility functions for RSA cryptography
22 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23 * @author Jeffrey Burdges <burdges@gnunet.org>
27 #include "gnunet_util_lib.h"
36 #define RND_BLK_SIZE 4096
37 unsigned char rnd_blk[RND_BLK_SIZE];
38 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
39 struct GNUNET_CRYPTO_RsaPrivateKey *priv_copy;
40 struct GNUNET_CRYPTO_RsaPublicKey *pub;
41 struct GNUNET_CRYPTO_RsaPublicKey *pub_copy;
42 struct GNUNET_CRYPTO_RsaSignature *sig;
43 struct GNUNET_CRYPTO_RsaSignature *sig_copy;
44 struct GNUNET_CRYPTO_RsaSignature *bsig;
45 struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec;
46 struct GNUNET_HashCode hash;
50 GNUNET_log_setup ("test-rsa", "WARNING", NULL);
51 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
54 GNUNET_CRYPTO_hash (rnd_blk,
57 priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
58 priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
59 GNUNET_assert (NULL != priv_copy);
60 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
61 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
67 size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
70 GNUNET_CRYPTO_rsa_private_key_free (priv);
72 priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
73 GNUNET_assert (NULL != priv);
74 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
76 GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
77 (void) fprintf (stderr, "The above warning is expected.\n");
80 /* try ordinary sig first */
81 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
83 sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
84 GNUNET_assert (NULL != sig);
85 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
86 pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
87 GNUNET_assert (NULL != pub_copy);
88 GNUNET_assert (GNUNET_OK ==
89 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
90 /* corrupt our hash and see if the signature is still valid */
91 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
92 sizeof (struct GNUNET_HashCode));
93 GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
96 (void) fprintf (stderr, "The above warning is expected.\n");
97 GNUNET_CRYPTO_rsa_signature_free (sig);
99 /* test blind signing */
100 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
103 GNUNET_CRYPTO_rsa_blind (&hash,
107 GNUNET_assert (0 != bsize);
108 bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
111 GNUNET_free (blind_buf);
112 sig = GNUNET_CRYPTO_rsa_unblind (bsig,
115 GNUNET_CRYPTO_rsa_signature_free (bsig);
116 GNUNET_assert (GNUNET_OK ==
117 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
118 GNUNET_CRYPTO_rsa_signature_free (sig);
119 GNUNET_CRYPTO_rsa_signature_free (sig_copy);
120 GNUNET_CRYPTO_rsa_private_key_free (priv);
121 GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
122 GNUNET_CRYPTO_rsa_public_key_free (pub);
123 GNUNET_CRYPTO_rsa_public_key_free (pub_copy);