ng
[oweals/gnunet.git] / src / util / test_crypto_aes_weak.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19
20 */
21
22 /**
23  * @author Krista Bennett
24  * @author Christian Grothoff
25  * @file util/test_crypto_aes_weak.c
26  * @brief AES weak key test.
27  */
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_crypto_lib.h"
31 #include <gcrypt.h>
32
33 #define MAX_WEAK_KEY_TRIALS 100000
34 #define GENERATE_WEAK_KEYS GNUNET_NO
35 #define WEAK_KEY_TESTSTRING "I hate weak keys."
36
37 static void
38 printWeakKey (struct GNUNET_CRYPTO_AesSessionKey *key)
39 {
40   int i;
41   for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++)
42     {
43       printf ("%x ", (int) (key->key[i]));
44     }
45 }
46
47 static int
48 testWeakKey ()
49 {
50   char result[100];
51   char res[100];
52   int size;
53   struct GNUNET_CRYPTO_AesSessionKey weak_key;
54   struct GNUNET_CRYPTO_AesInitializationVector INITVALUE;
55
56   memset (&INITVALUE, 42,
57           sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
58   /* sorry, this is not a weak key -- I don't have
59      any at the moment! */
60   weak_key.key[0] = (char) (0x4c);
61   weak_key.key[1] = (char) (0x31);
62   weak_key.key[2] = (char) (0xc6);
63   weak_key.key[3] = (char) (0x2b);
64   weak_key.key[4] = (char) (0xc1);
65   weak_key.key[5] = (char) (0x5f);
66   weak_key.key[6] = (char) (0x4d);
67   weak_key.key[7] = (char) (0x1f);
68   weak_key.key[8] = (char) (0x31);
69   weak_key.key[9] = (char) (0xaa);
70   weak_key.key[10] = (char) (0x12);
71   weak_key.key[11] = (char) (0x2e);
72   weak_key.key[12] = (char) (0xb7);
73   weak_key.key[13] = (char) (0x82);
74   weak_key.key[14] = (char) (0xc0);
75   weak_key.key[15] = (char) (0xb6);
76   weak_key.key[16] = (char) (0x4d);
77   weak_key.key[17] = (char) (0x1f);
78   weak_key.key[18] = (char) (0x31);
79   weak_key.key[19] = (char) (0xaa);
80   weak_key.key[20] = (char) (0x4c);
81   weak_key.key[21] = (char) (0x31);
82   weak_key.key[22] = (char) (0xc6);
83   weak_key.key[23] = (char) (0x2b);
84   weak_key.key[24] = (char) (0xc1);
85   weak_key.key[25] = (char) (0x5f);
86   weak_key.key[26] = (char) (0x4d);
87   weak_key.key[27] = (char) (0x1f);
88   weak_key.key[28] = (char) (0x31);
89   weak_key.key[29] = (char) (0xaa);
90   weak_key.key[30] = (char) (0xaa);
91   weak_key.key[31] = (char) (0xaa);
92   /* memset(&weak_key, 0, 32); */
93   weak_key.crc32 =
94     htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH));
95
96   size = GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING,
97                                     strlen (WEAK_KEY_TESTSTRING) + 1,
98                                     &weak_key, &INITVALUE, result);
99
100   if (size == -1)
101     {
102       GNUNET_break (0);
103       return 1;
104     }
105
106   size = GNUNET_CRYPTO_aes_decrypt (&weak_key, result, size, &INITVALUE, res);
107
108   if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size)
109     {
110       GNUNET_break (0);
111       return 1;
112     }
113   if (0 != strcmp (res, WEAK_KEY_TESTSTRING))
114     {
115       GNUNET_break (0);
116       return 1;
117     }
118   else
119     return 0;
120 }
121
122 static int
123 getWeakKeys ()
124 {
125   struct GNUNET_CRYPTO_AesSessionKey sessionkey;
126   int number_of_weak_keys = 0;
127   int number_of_runs;
128
129   gcry_cipher_hd_t handle;
130   int rc;
131
132   for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS;
133        number_of_runs++)
134     {
135
136       if (number_of_runs % 1000 == 0)
137         fprintf (stderr, ".");
138       /*printf("Got to run number %d.\n", number_of_runs); */
139       GNUNET_CRYPTO_aes_create_session_key (&sessionkey);
140
141       rc = gcry_cipher_open (&handle,
142                              GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, 0);
143
144       if (rc)
145         {
146           printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n",
147                   number_of_runs, gcry_strerror (rc));
148           rc = 0;
149           continue;
150         }
151
152       rc =
153         gcry_cipher_setkey (handle, &sessionkey,
154                             GNUNET_CRYPTO_AES_KEY_LENGTH);
155
156       if ((char) rc == GPG_ERR_WEAK_KEY)
157         {
158           printf ("\nWeak key (in hex): ");
159           printWeakKey (&sessionkey);
160           printf ("\n");
161           number_of_weak_keys++;
162         }
163       else if (rc)
164         {
165           printf ("\nUnexpected error generating keys. Error is %s\n",
166                   gcry_strerror (rc));
167         }
168
169       gcry_cipher_close (handle);
170
171     }
172
173   return number_of_weak_keys;
174 }
175
176 int
177 main (int argc, char *argv[])
178 {
179   int weak_keys;
180
181   GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL);
182   GNUNET_CRYPTO_random_disable_entropy_gathering ();
183   if (GENERATE_WEAK_KEYS)
184     {
185       weak_keys = getWeakKeys ();
186
187       if (weak_keys == 0)
188         {
189           printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS);
190         }
191       else
192         {
193           printf ("\n%d weak keys found in %d runs.\n",
194                   weak_keys, MAX_WEAK_KEY_TRIALS);
195         }
196     }
197
198   if (testWeakKey () != 0)
199     return -1;
200   return 0;
201 }
202
203 /* end of weakkeytest.c */