-fix (C) notices
[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 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.
8
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.
12
13   You should have received a copy of the GNU General Public License along with
14   TALER; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
15 */
16
17 /**
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  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #define KEY_SIZE 1024
26
27
28 int
29 main (int argc,
30       char *argv[])
31 {
32 #define RND_BLK_SIZE 4096
33   unsigned char rnd_blk[RND_BLK_SIZE];
34   struct GNUNET_CRYPTO_rsa_PrivateKey *priv;
35   struct GNUNET_CRYPTO_rsa_PrivateKey *priv_copy;
36   struct GNUNET_CRYPTO_rsa_PublicKey *pub;
37   struct GNUNET_CRYPTO_rsa_PublicKey *pub_copy;
38   struct GNUNET_CRYPTO_rsa_BlindingKey *bkey;
39   struct GNUNET_CRYPTO_rsa_Signature *sig;
40   struct GNUNET_CRYPTO_rsa_Signature *sig_copy;
41   struct GNUNET_CRYPTO_rsa_Signature *bsig;
42   struct GNUNET_HashCode hash;
43   char *blind_buf;
44   size_t bsize;
45
46   GNUNET_log_setup ("test-rsa", "WARNING", NULL);
47   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
48                               rnd_blk,
49                               RND_BLK_SIZE);
50   GNUNET_CRYPTO_hash (rnd_blk,
51                       RND_BLK_SIZE,
52                       &hash);
53   priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
54   priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
55   GNUNET_assert (NULL != priv_copy);
56   GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv, priv_copy));
57   pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
58   /* Encoding */
59   size_t size;
60   char *enc;
61   enc = NULL;
62   size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc);
63   /* Decoding */
64   GNUNET_CRYPTO_rsa_private_key_free (priv);
65   priv = NULL;
66   priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size);
67   GNUNET_assert (NULL != priv);
68   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
69                               enc, size);
70   GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size));
71   (void) fprintf (stderr, "The above warning is expected.\n");
72   GNUNET_free (enc);
73
74   /* try ordinary sig first */
75   sig = GNUNET_CRYPTO_rsa_sign (priv,
76                         &hash,
77                         sizeof (hash));
78   sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
79   GNUNET_assert (NULL != sig);
80   GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy));
81   pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
82   GNUNET_assert (NULL != pub_copy);
83   GNUNET_assert (GNUNET_OK ==
84                  GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy));
85   /* corrupt our hash and see if the signature is still valid */
86   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash,
87                               sizeof (struct GNUNET_HashCode));
88   GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash,
89                                                         sig,
90                                                         pub));
91   (void) fprintf (stderr, "The above warning is expected.\n");
92   GNUNET_CRYPTO_rsa_signature_free (sig);
93
94
95   /* test blind signing */
96   bkey = GNUNET_CRYPTO_rsa_blinding_key_create (KEY_SIZE);
97   bsize = GNUNET_CRYPTO_rsa_blind (&hash,
98                            bkey,
99                            pub,
100                            &blind_buf);
101   GNUNET_assert (0 != bsize);
102   bsig = GNUNET_CRYPTO_rsa_sign (priv,
103                         blind_buf,
104                         bsize);
105   GNUNET_free (blind_buf);
106   sig = GNUNET_CRYPTO_rsa_unblind (bsig,
107                            bkey,
108                            pub);
109   GNUNET_CRYPTO_rsa_signature_free (bsig);
110   GNUNET_assert (GNUNET_OK ==
111                  GNUNET_CRYPTO_rsa_verify (&hash, sig, pub));
112   GNUNET_CRYPTO_rsa_signature_free (sig);
113   GNUNET_CRYPTO_rsa_signature_free (sig_copy);
114   GNUNET_CRYPTO_rsa_private_key_free (priv);
115   GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
116   GNUNET_CRYPTO_rsa_public_key_free (pub);
117   GNUNET_CRYPTO_rsa_public_key_free (pub_copy);
118   GNUNET_CRYPTO_rsa_blinding_key_free (bkey);
119   return 0;
120 }