error handling
[oweals/gnunet.git] / src / util / test_crypto_rsa.c
1 /*
2    This file is part of GNUnet
3    Copyright (C) 2014,2015 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
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>
26  */
27 #include "platform.h"
28 #include <gcrypt.h>
29 #include "gnunet_util_lib.h"
30
31 #define KEY_SIZE 1024
32
33
34 int
35 main (int argc,
36       char *argv[])
37 {
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;
49   char *blind_buf;
50   size_t bsize;
51
52   GNUNET_log_setup ("test-rsa", "WARNING", NULL);
53   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
54                               rnd_blk,
55                               RND_BLK_SIZE);
56   GNUNET_CRYPTO_hash (rnd_blk,
57                       RND_BLK_SIZE,
58                       &hash);
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);
64
65   /* Encoding */
66   size_t size;
67   char *enc;
68   enc = NULL;
69   size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
70
71   /* Decoding */
72   GNUNET_CRYPTO_rsa_private_key_free (priv);
73   priv = NULL;
74   priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
75   GNUNET_assert (NULL != priv);
76   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
77                               enc, size);
78   GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
79   (void) fprintf (stderr, "The above warning is expected.\n");
80   GNUNET_free (enc);
81
82   /* try ordinary sig first */
83   sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
84                                     &hash);
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   {
93     char *buf;
94     size_t buf_size;
95     struct GNUNET_CRYPTO_RsaPublicKey *pub2;
96     struct GNUNET_CRYPTO_RsaSignature *sig2;
97
98     buf_size = GNUNET_CRYPTO_rsa_public_key_encode (pub,
99                                                     &buf);
100     pub2 = GNUNET_CRYPTO_rsa_public_key_decode (buf,
101                                                 buf_size);
102     GNUNET_free (buf);
103     buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
104                                                    &buf);
105     sig2 = GNUNET_CRYPTO_rsa_signature_decode (buf,
106                                                buf_size);
107     GNUNET_free (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);
112   }
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,
117                                                         sig,
118                                                         pub));
119   (void) fprintf (stderr, "The above warning is expected.\n");
120   GNUNET_CRYPTO_rsa_signature_free (sig);
121
122   /* test blind signing */
123   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
124                               &bsec,
125                               sizeof(bsec));
126   GNUNET_CRYPTO_rsa_blind (&hash,
127                            &bsec,
128                            pub,
129                            &blind_buf, &bsize);
130   GNUNET_assert (0 != bsize);
131   bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
132                                          blind_buf,
133                                          bsize);
134   GNUNET_free (blind_buf);
135   sig = GNUNET_CRYPTO_rsa_unblind (bsig,
136                                    &bsec,
137                                    pub);
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);
147   return 0;
148 }