2 This file is part of GNUnet
3 Copyright (C) 2014,2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
18 * @file util/test_crypto_rsa.c
19 * @brief testcase for utility functions for RSA cryptography
20 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23 #include "gnunet_util_lib.h"
32 #define RND_BLK_SIZE 4096
33 unsigned char rnd_blk[RND_BLK_SIZE];
34 struct GNUNET_CRYPTO_rsa_PrivateKey *priv;
35 struct GNUNET_CRYPTO_rsa_PublicKey *pub;
36 struct GNUNET_CRYPTO_rsa_PublicKey *pub_copy;
37 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey;
38 struct GNUNET_CRYPTO_rsa_Signature *sig;
39 struct GNUNET_CRYPTO_rsa_Signature *bsig;
40 struct GNUNET_HashCode hash;
44 GNUNET_log_setup ("test-rsa", "WARNING", NULL);
45 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
48 GNUNET_CRYPTO_hash (rnd_blk,
51 priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
52 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
57 size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
59 GNUNET_CRYPTO_rsa_private_key_free (priv);
61 priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
62 GNUNET_assert (NULL != priv);
63 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
65 GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
66 (void) fprintf (stderr, "The above warning is expected.\n");
69 /* try ordinary sig first */
70 sig = GNUNET_CRYPTO_rsa_sign (priv,
73 pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
74 GNUNET_assert (NULL != pub_copy);
75 GNUNET_assert (GNUNET_OK ==
76 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
77 /* corrupt our hash and see if the signature is still valid */
78 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
79 sizeof (struct GNUNET_HashCode));
80 GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
83 (void) fprintf (stderr, "The above warning is expected.\n");
84 GNUNET_CRYPTO_rsa_signature_free (sig);
87 /* test blind signing */
88 bkey = GNUNET_CRYPTO_rsa_blinding_key_create (KEY_SIZE);
89 bsize = GNUNET_CRYPTO_rsa_blind (&hash,
93 GNUNET_assert (0 != bsize);
94 bsig = GNUNET_CRYPTO_rsa_sign (priv,
97 GNUNET_free (blind_buf);
98 sig = GNUNET_CRYPTO_rsa_unblind (bsig,
101 GNUNET_CRYPTO_rsa_signature_free (bsig);
102 GNUNET_assert (GNUNET_OK ==
103 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
104 GNUNET_CRYPTO_rsa_signature_free (sig);
105 GNUNET_CRYPTO_rsa_private_key_free (priv);
106 GNUNET_CRYPTO_rsa_public_key_free (pub);
107 GNUNET_CRYPTO_rsa_public_key_free (pub_copy);
108 GNUNET_CRYPTO_rsa_blinding_key_free (bkey);