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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/test_crypto_rsa.c
23 * @brief testcase for utility functions for RSA cryptography
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25 * @author Jeffrey Burdges <burdges@gnunet.org>
29 #include "gnunet_util_lib.h"
38 #define RND_BLK_SIZE 4096
39 unsigned char rnd_blk[RND_BLK_SIZE];
40 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
41 struct GNUNET_CRYPTO_RsaPrivateKey *priv_copy;
42 struct GNUNET_CRYPTO_RsaPublicKey *pub;
43 struct GNUNET_CRYPTO_RsaPublicKey *pub_copy;
44 struct GNUNET_CRYPTO_RsaSignature *sig;
45 struct GNUNET_CRYPTO_RsaSignature *sig_copy;
46 struct GNUNET_CRYPTO_RsaSignature *bsig;
47 struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec;
48 struct GNUNET_HashCode hash;
52 GNUNET_log_setup ("test-rsa", "WARNING", NULL);
53 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
56 GNUNET_CRYPTO_hash (rnd_blk,
59 priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
60 priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
61 GNUNET_assert (NULL != priv_copy);
62 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
63 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
69 size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
72 GNUNET_CRYPTO_rsa_private_key_free (priv);
74 priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
75 GNUNET_assert (NULL != priv);
76 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
78 GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
79 (void) fprintf (stderr, "The above warning is expected.\n");
82 /* try ordinary sig first */
83 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
85 sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
86 GNUNET_assert (NULL != sig);
87 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
88 pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
89 GNUNET_assert (NULL != pub_copy);
90 GNUNET_assert (GNUNET_OK ==
91 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
92 /* corrupt our hash and see if the signature is still valid */
93 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
94 sizeof (struct GNUNET_HashCode));
95 GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
98 (void) fprintf (stderr, "The above warning is expected.\n");
99 GNUNET_CRYPTO_rsa_signature_free (sig);
101 /* test blind signing */
102 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
105 GNUNET_CRYPTO_rsa_blind (&hash,
109 GNUNET_assert (0 != bsize);
110 bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
113 GNUNET_free (blind_buf);
114 sig = GNUNET_CRYPTO_rsa_unblind (bsig,
117 GNUNET_CRYPTO_rsa_signature_free (bsig);
118 GNUNET_assert (GNUNET_OK ==
119 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
120 GNUNET_CRYPTO_rsa_signature_free (sig);
121 GNUNET_CRYPTO_rsa_signature_free (sig_copy);
122 GNUNET_CRYPTO_rsa_private_key_free (priv);
123 GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
124 GNUNET_CRYPTO_rsa_public_key_free (pub);
125 GNUNET_CRYPTO_rsa_public_key_free (pub_copy);