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