diff options
Diffstat (limited to 'src/lib/util/test_crypto_rsa.c')
-rw-r--r-- | src/lib/util/test_crypto_rsa.c | 149 |
1 files changed, 149 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..9f2ddb66e --- /dev/null +++ b/src/lib/util/test_crypto_rsa.c | |||
@@ -0,0 +1,149 @@ | |||
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 | |||
28 | #include "platform.h" | ||
29 | #include <gcrypt.h> | ||
30 | #include "gnunet_util_lib.h" | ||
31 | |||
32 | #define KEY_SIZE 1024 | ||
33 | |||
34 | |||
35 | int | ||
36 | main (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 | void *blind_buf; | ||
51 | size_t bsize; | ||
52 | |||
53 | GNUNET_log_setup ("test-rsa", "WARNING", 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, priv_copy)); | ||
64 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | ||
65 | |||
66 | /* Encoding */ | ||
67 | size_t size; | ||
68 | void *enc; | ||
69 | enc = NULL; | ||
70 | size = GNUNET_CRYPTO_rsa_private_key_encode (priv, &enc); | ||
71 | |||
72 | /* Decoding */ | ||
73 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
74 | priv = NULL; | ||
75 | priv = GNUNET_CRYPTO_rsa_private_key_decode (enc, size); | ||
76 | GNUNET_assert (NULL != priv); | ||
77 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, | ||
78 | enc, size); | ||
79 | GNUNET_assert (NULL == GNUNET_CRYPTO_rsa_private_key_decode (enc, size)); | ||
80 | (void) fprintf (stderr, "The above warning is expected.\n"); | ||
81 | GNUNET_free (enc); | ||
82 | |||
83 | /* try ordinary sig first */ | ||
84 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, | ||
85 | &hash); | ||
86 | sig_copy = GNUNET_CRYPTO_rsa_signature_dup (sig); | ||
87 | GNUNET_assert (NULL != sig); | ||
88 | GNUNET_assert (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig_copy)); | ||
89 | pub_copy = GNUNET_CRYPTO_rsa_public_key_dup (pub); | ||
90 | GNUNET_assert (NULL != pub_copy); | ||
91 | GNUNET_assert (GNUNET_OK == | ||
92 | GNUNET_CRYPTO_rsa_verify (&hash, sig, pub_copy)); | ||
93 | { | ||
94 | void *buf; | ||
95 | size_t buf_size; | ||
96 | struct GNUNET_CRYPTO_RsaPublicKey *pub2; | ||
97 | struct GNUNET_CRYPTO_RsaSignature *sig2; | ||
98 | |||
99 | buf_size = GNUNET_CRYPTO_rsa_public_key_encode (pub, | ||
100 | &buf); | ||
101 | pub2 = GNUNET_CRYPTO_rsa_public_key_decode (buf, | ||
102 | buf_size); | ||
103 | GNUNET_free (buf); | ||
104 | buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig, | ||
105 | &buf); | ||
106 | sig2 = GNUNET_CRYPTO_rsa_signature_decode (buf, | ||
107 | buf_size); | ||
108 | GNUNET_free (buf); | ||
109 | GNUNET_assert (GNUNET_OK == | ||
110 | GNUNET_CRYPTO_rsa_verify (&hash, sig2, pub2)); | ||
111 | GNUNET_CRYPTO_rsa_public_key_free (pub2); | ||
112 | GNUNET_CRYPTO_rsa_signature_free (sig2); | ||
113 | } | ||
114 | /* corrupt our hash and see if the signature is still valid */ | ||
115 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &hash, | ||
116 | sizeof(struct GNUNET_HashCode)); | ||
117 | GNUNET_assert (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&hash, | ||
118 | sig, | ||
119 | pub)); | ||
120 | (void) fprintf (stderr, "The above warning is expected.\n"); | ||
121 | GNUNET_CRYPTO_rsa_signature_free (sig); | ||
122 | |||
123 | /* test blind signing */ | ||
124 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, | ||
125 | &bsec, | ||
126 | sizeof(bsec)); | ||
127 | GNUNET_CRYPTO_rsa_blind (&hash, | ||
128 | &bsec, | ||
129 | pub, | ||
130 | &blind_buf, &bsize); | ||
131 | GNUNET_assert (0 != bsize); | ||
132 | bsig = GNUNET_CRYPTO_rsa_sign_blinded (priv, | ||
133 | blind_buf, | ||
134 | bsize); | ||
135 | GNUNET_free (blind_buf); | ||
136 | sig = GNUNET_CRYPTO_rsa_unblind (bsig, | ||
137 | &bsec, | ||
138 | pub); | ||
139 | GNUNET_CRYPTO_rsa_signature_free (bsig); | ||
140 | GNUNET_assert (GNUNET_OK == | ||
141 | GNUNET_CRYPTO_rsa_verify (&hash, sig, pub)); | ||
142 | GNUNET_CRYPTO_rsa_signature_free (sig); | ||
143 | GNUNET_CRYPTO_rsa_signature_free (sig_copy); | ||
144 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
145 | GNUNET_CRYPTO_rsa_private_key_free (priv_copy); | ||
146 | GNUNET_CRYPTO_rsa_public_key_free (pub); | ||
147 | GNUNET_CRYPTO_rsa_public_key_free (pub_copy); | ||
148 | return 0; | ||
149 | } | ||