aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_crypto_rsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_crypto_rsa.c')
-rw-r--r--src/lib/util/test_crypto_rsa.c171
1 files changed, 171 insertions, 0 deletions
diff --git a/src/lib/util/test_crypto_rsa.c b/src/lib/util/test_crypto_rsa.c
new file mode 100644
index 000000000..c513a68fe
--- /dev/null
+++ b/src/lib/util/test_crypto_rsa.c
@@ -0,0 +1,171 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2023 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
28#include "platform.h"
29#include <gcrypt.h>
30#include "gnunet_util_lib.h"
31
32#define KEY_SIZE 1024
33
34
35int
36main (int argc,
37 char *argv[])
38{
39#define RND_BLK_SIZE 4096
40 unsigned char rnd_blk[RND_BLK_SIZE];
41 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
42 struct GNUNET_CRYPTO_RsaPrivateKey *priv_copy;
43 struct GNUNET_CRYPTO_RsaPublicKey *pub;
44 struct GNUNET_CRYPTO_RsaPublicKey *pub_copy;
45 struct GNUNET_CRYPTO_RsaSignature *sig;
46 struct GNUNET_CRYPTO_RsaSignature *sig_copy;
47 struct GNUNET_CRYPTO_RsaSignature *bsig;
48 struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec;
49 struct GNUNET_HashCode hash;
50
51 GNUNET_log_setup ("test-crypto-rsa",
52 "WARNING",
53 NULL);
54 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
55 rnd_blk,
56 RND_BLK_SIZE);
57 GNUNET_CRYPTO_hash (rnd_blk,
58 RND_BLK_SIZE,
59 &hash);
60 priv = GNUNET_CRYPTO_rsa_private_key_create (KEY_SIZE);
61 priv_copy = GNUNET_CRYPTO_rsa_private_key_dup (priv);
62 GNUNET_assert (NULL != priv_copy);
63 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_private_key_cmp (priv,
64 priv_copy));
65 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
66
67 /* Encoding */
68 size_t size;
69 void *enc;
70 enc = NULL;
71 size = GNUNET_CRYPTO_rsa_private_key_encode (priv,
72 &enc);
73
74 /* Decoding */
75 GNUNET_CRYPTO_rsa_private_key_free (priv);
76 priv = NULL;
77 priv = GNUNET_CRYPTO_rsa_private_key_decode (enc,
78 size);
79 GNUNET_assert (NULL != priv);
80 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
81 enc,
82 size);
83 GNUNET_assert (NULL ==
84 GNUNET_CRYPTO_rsa_private_key_decode (enc,
85 size));
86 (void) fprintf (stderr,
87 "The above warning is expected.\n");
88 GNUNET_free (enc);
89
90 /* try ordinary sig first */
91 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
92 &hash,
93 sizeof (hash));
94 sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig);
95 GNUNET_assert (NULL != sig);
96 GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig,
97 sig_copy));
98 pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub);
99 GNUNET_assert (NULL != pub_copy);
100 GNUNET_assert (GNUNET_OK ==
101 GNUNET_CRYPTO_rsa_verify (&hash,
102 sizeof (hash),
103 sig,
104 pub_copy));
105 {
106 void *buf;
107 size_t buf_size;
108 struct GNUNET_CRYPTO_RsaPublicKey *pub2;
109 struct GNUNET_CRYPTO_RsaSignature *sig2;
110
111 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (pub,
112 &buf);
113 pub2 = GNUNET_CRYPTO_rsa_public_key_decode (buf,
114 buf_size);
115 GNUNET_free (buf);
116 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
117 &buf);
118 sig2 = GNUNET_CRYPTO_rsa_signature_decode (buf,
119 buf_size);
120 GNUNET_free (buf);
121 GNUNET_assert (GNUNET_OK ==
122 GNUNET_CRYPTO_rsa_verify (&hash,
123 sizeof (hash),
124 sig2,
125 pub2));
126 GNUNET_CRYPTO_rsa_public_key_free (pub2);
127 GNUNET_CRYPTO_rsa_signature_free (sig2);
128 }
129 /* corrupt our hash and see if the signature is still valid */
130 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
131 &hash,
132 sizeof(hash));
133 GNUNET_assert (GNUNET_OK !=
134 GNUNET_CRYPTO_rsa_verify (&hash,
135 sizeof (hash),
136 sig,
137 pub));
138 (void) fprintf (stderr,
139 "The above warning is expected.\n");
140 GNUNET_CRYPTO_rsa_signature_free (sig);
141
142 /* test blind signing */
143 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
144 &bsec,
145 sizeof(bsec));
146 struct GNUNET_CRYPTO_RsaBlindedMessage bm;
147 GNUNET_CRYPTO_rsa_blind (&hash,
148 sizeof (hash),
149 &bsec,
150 pub,
151 &bm);
152 bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv,
153 &bm);
154 GNUNET_CRYPTO_rsa_blinded_message_free (&bm);
155 sig = GNUNET_CRYPTO_rsa_unblind (bsig,
156 &bsec,
157 pub);
158 GNUNET_CRYPTO_rsa_signature_free (bsig);
159 GNUNET_assert (GNUNET_OK ==
160 GNUNET_CRYPTO_rsa_verify (&hash,
161 sizeof (hash),
162 sig,
163 pub));
164 GNUNET_CRYPTO_rsa_signature_free (sig);
165 GNUNET_CRYPTO_rsa_signature_free (sig_copy);
166 GNUNET_CRYPTO_rsa_private_key_free (priv);
167 GNUNET_CRYPTO_rsa_private_key_free (priv_copy);
168 GNUNET_CRYPTO_rsa_public_key_free (pub);
169 GNUNET_CRYPTO_rsa_public_key_free (pub_copy);
170 return 0;
171}