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 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 GNUnet; 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>
21 * @author Jeffrey Burdges <burdges@gnunet.org>
25 #include "gnunet_util_lib.h"
34 #define RND_BLK_SIZE 4096
35 unsigned char rnd_blk[RND_BLK_SIZE];
36 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
37 struct GNUNET_CRYPTO_RsaPrivateKey *priv_copy;
38 struct GNUNET_CRYPTO_RsaPublicKey *pub;
39 struct GNUNET_CRYPTO_RsaPublicKey *pub_copy;
40 struct GNUNET_CRYPTO_RsaSignature *sig;
41 struct GNUNET_CRYPTO_RsaSignature *sig_copy;
42 struct GNUNET_CRYPTO_RsaSignature *bsig;
43 struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec;
44 struct GNUNET_HashCode hash;
48 GNUNET_log_setup ("test-rsa", "WARNING", NULL);
49 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
52 GNUNET_CRYPTO_hash (rnd_blk,
55 priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
56 priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
57 GNUNET_assert (NULL != priv_copy);
58 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
59 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
65 size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
68 GNUNET_CRYPTO_rsa_private_key_free (priv);
70 priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
71 GNUNET_assert (NULL != priv);
72 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
74 GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
75 (void) fprintf (stderr, "The above warning is expected.\n");
78 /* try ordinary sig first */
79 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
81 sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
82 GNUNET_assert (NULL != sig);
83 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
84 pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
85 GNUNET_assert (NULL != pub_copy);
86 GNUNET_assert (GNUNET_OK ==
87 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
88 /* corrupt our hash and see if the signature is still valid */
89 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
90 sizeof (struct GNUNET_HashCode));
91 GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
94 (void) fprintf (stderr, "The above warning is expected.\n");
95 GNUNET_CRYPTO_rsa_signature_free (sig);
97 /* test blind signing */
98 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
101 GNUNET_CRYPTO_rsa_blind (&hash,
105 GNUNET_assert (0 != bsize);
106 bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
109 GNUNET_free (blind_buf);
110 sig = GNUNET_CRYPTO_rsa_unblind (bsig,
113 GNUNET_CRYPTO_rsa_signature_free (bsig);
114 GNUNET_assert (GNUNET_OK ==
115 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
116 GNUNET_CRYPTO_rsa_signature_free (sig);
117 GNUNET_CRYPTO_rsa_signature_free (sig_copy);
118 GNUNET_CRYPTO_rsa_private_key_free (priv);
119 GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
120 GNUNET_CRYPTO_rsa_public_key_free (pub);
121 GNUNET_CRYPTO_rsa_public_key_free (pub_copy);