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));
95 struct GNUNET_CRYPTO_RsaPublicKey *pub2;
96 struct GNUNET_CRYPTO_RsaSignature *sig2;
98 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (pub,
100 pub2 = GNUNET_CRYPTO_rsa_public_key_decode (buf,
103 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
105 sig2 = GNUNET_CRYPTO_rsa_signature_decode (buf,
108 GNUNET_assert (GNUNET_OK ==
109 GNUNET_CRYPTO_rsa_verify (&hash, sig2, pub2));
110 GNUNET_CRYPTO_rsa_public_key_free (pub2);
111 GNUNET_CRYPTO_rsa_signature_free (sig2);
113 /* corrupt our hash and see if the signature is still valid */
114 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
115 sizeof(struct GNUNET_HashCode));
116 GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
119 (void) fprintf (stderr, "The above warning is expected.\n");
120 GNUNET_CRYPTO_rsa_signature_free (sig);
122 /* test blind signing */
123 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
126 GNUNET_CRYPTO_rsa_blind (&hash,
130 GNUNET_assert (0 != bsize);
131 bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
134 GNUNET_free (blind_buf);
135 sig = GNUNET_CRYPTO_rsa_unblind (bsig,
138 GNUNET_CRYPTO_rsa_signature_free (bsig);
139 GNUNET_assert (GNUNET_OK ==
140 GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
141 GNUNET_CRYPTO_rsa_signature_free (sig);
142 GNUNET_CRYPTO_rsa_signature_free (sig_copy);
143 GNUNET_CRYPTO_rsa_private_key_free (priv);
144 GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
145 GNUNET_CRYPTO_rsa_public_key_free (pub);
146 GNUNET_CRYPTO_rsa_public_key_free (pub_copy);