notes
[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
19 /**
20  * @file util/test_crypto_rsa.c
21  * @brief testcase for utility functions for RSA cryptography
22  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23  * @author Jeffrey Burdges <burdges@gnunet.org>
24  */
25 #include "platform.h"
26 #include <gcrypt.h>
27 #include "gnunet_util_lib.h"
28
29 #define KEY_SIZE 1024
30
31
32 int
33 main (int argc,
34       char *argv[])
35 {
36 #define RND_BLK_SIZE 4096
37   unsigned char rnd_blk[RND_BLK_SIZE];
38   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
39   struct GNUNET_CRYPTO_RsaPrivateKey *priv_copy;
40   struct GNUNET_CRYPTO_RsaPublicKey *pub;
41   struct GNUNET_CRYPTO_RsaPublicKey *pub_copy;
42   struct GNUNET_CRYPTO_RsaSignature *sig;
43   struct GNUNET_CRYPTO_RsaSignature *sig_copy;
44   struct GNUNET_CRYPTO_RsaSignature *bsig;
45   struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec;
46   struct GNUNET_HashCode hash;
47   char *blind_buf;
48   size_t bsize;
49
50   GNUNET_log_setup ("test-rsa", "WARNING", NULL);
51   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
52                               rnd_blk,
53                               RND_BLK_SIZE);
54   GNUNET_CRYPTO_hash (rnd_blk,
55                       RND_BLK_SIZE,
56                       &hash);
57   priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
58   priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
59   GNUNET_assert (NULL != priv_copy);
60   GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
61   pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
62
63   /* Encoding */
64   size_t size;
65   char *enc;
66   enc = NULL;
67   size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
68
69   /* Decoding */
70   GNUNET_CRYPTO_rsa_private_key_free (priv);
71   priv = NULL;
72   priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
73   GNUNET_assert (NULL != priv);
74   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
75                               enc, size);
76   GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
77   (void) fprintf (stderr, "The above warning is expected.\n");
78   GNUNET_free (enc);
79
80   /* try ordinary sig first */
81   sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
82                                     &hash);
83   sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
84   GNUNET_assert (NULL != sig);
85   GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
86   pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
87   GNUNET_assert (NULL != pub_copy);
88   GNUNET_assert (GNUNET_OK ==
89                  GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
90   /* corrupt our hash and see if the signature is still valid */
91   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
92                               sizeof (struct GNUNET_HashCode));
93   GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
94                                                         sig,
95                                                         pub));
96   (void) fprintf (stderr, "The above warning is expected.\n");
97   GNUNET_CRYPTO_rsa_signature_free (sig);
98
99   /* test blind signing */
100   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
101                               &bsec,
102                               sizeof (bsec));
103   GNUNET_CRYPTO_rsa_blind (&hash,
104                            &bsec,
105                            pub,
106                            &blind_buf,&bsize);
107   GNUNET_assert (0 != bsize);
108   bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
109                                          blind_buf,
110                                          bsize);
111   GNUNET_free (blind_buf);
112   sig = GNUNET_CRYPTO_rsa_unblind (bsig,
113                                    &bsec,
114                                    pub);
115   GNUNET_CRYPTO_rsa_signature_free (bsig);
116   GNUNET_assert (GNUNET_OK ==
117                  GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
118   GNUNET_CRYPTO_rsa_signature_free (sig);
119   GNUNET_CRYPTO_rsa_signature_free (sig_copy);
120   GNUNET_CRYPTO_rsa_private_key_free (priv);
121   GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
122   GNUNET_CRYPTO_rsa_public_key_free (pub);
123   GNUNET_CRYPTO_rsa_public_key_free (pub_copy);
124   return 0;
125 }