uncrustify as demanded.
[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   /* corrupt our hash and see if the signature is still valid */
93   GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, &hash,
94                              sizeof(struct GNUNET_HashCode));
95   GNUNET_assert(GNUNET_OK != GNUNET_CRYPTO_rsa_verify(&hash,
96                                                       sig,
97                                                       pub));
98   (void)fprintf(stderr, "The above warning is expected.\n");
99   GNUNET_CRYPTO_rsa_signature_free(sig);
100
101   /* test blind signing */
102   GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK,
103                              &bsec,
104                              sizeof(bsec));
105   GNUNET_CRYPTO_rsa_blind(&hash,
106                           &bsec,
107                           pub,
108                           &blind_buf, &bsize);
109   GNUNET_assert(0 != bsize);
110   bsig = GNUNET_CRYPTO_rsa_sign_blinded(priv,
111                                         blind_buf,
112                                         bsize);
113   GNUNET_free(blind_buf);
114   sig = GNUNET_CRYPTO_rsa_unblind(bsig,
115                                   &bsec,
116                                   pub);
117   GNUNET_CRYPTO_rsa_signature_free(bsig);
118   GNUNET_assert(GNUNET_OK ==
119                 GNUNET_CRYPTO_rsa_verify(&hash, sig, pub));
120   GNUNET_CRYPTO_rsa_signature_free(sig);
121   GNUNET_CRYPTO_rsa_signature_free(sig_copy);
122   GNUNET_CRYPTO_rsa_private_key_free(priv);
123   GNUNET_CRYPTO_rsa_private_key_free(priv_copy);
124   GNUNET_CRYPTO_rsa_public_key_free(pub);
125   GNUNET_CRYPTO_rsa_public_key_free(pub_copy);
126   return 0;
127 }