- test for external iterator
[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 3, 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
42   for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++)
43   {
44     printf ("%x ", (int) (key->key[i]));
45   }
46 }
47
48 static int
49 testWeakKey ()
50 {
51   char result[100];
52   char res[100];
53   int size;
54   struct GNUNET_CRYPTO_AesSessionKey weak_key;
55   struct GNUNET_CRYPTO_AesInitializationVector INITVALUE;
56
57   memset (&INITVALUE, 42,
58           sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
59   /* sorry, this is not a weak key -- I don't have
60    * any at the moment! */
61   weak_key.key[0] = (char) (0x4c);
62   weak_key.key[1] = (char) (0x31);
63   weak_key.key[2] = (char) (0xc6);
64   weak_key.key[3] = (char) (0x2b);
65   weak_key.key[4] = (char) (0xc1);
66   weak_key.key[5] = (char) (0x5f);
67   weak_key.key[6] = (char) (0x4d);
68   weak_key.key[7] = (char) (0x1f);
69   weak_key.key[8] = (char) (0x31);
70   weak_key.key[9] = (char) (0xaa);
71   weak_key.key[10] = (char) (0x12);
72   weak_key.key[11] = (char) (0x2e);
73   weak_key.key[12] = (char) (0xb7);
74   weak_key.key[13] = (char) (0x82);
75   weak_key.key[14] = (char) (0xc0);
76   weak_key.key[15] = (char) (0xb6);
77   weak_key.key[16] = (char) (0x4d);
78   weak_key.key[17] = (char) (0x1f);
79   weak_key.key[18] = (char) (0x31);
80   weak_key.key[19] = (char) (0xaa);
81   weak_key.key[20] = (char) (0x4c);
82   weak_key.key[21] = (char) (0x31);
83   weak_key.key[22] = (char) (0xc6);
84   weak_key.key[23] = (char) (0x2b);
85   weak_key.key[24] = (char) (0xc1);
86   weak_key.key[25] = (char) (0x5f);
87   weak_key.key[26] = (char) (0x4d);
88   weak_key.key[27] = (char) (0x1f);
89   weak_key.key[28] = (char) (0x31);
90   weak_key.key[29] = (char) (0xaa);
91   weak_key.key[30] = (char) (0xaa);
92   weak_key.key[31] = (char) (0xaa);
93   /* memset(&weak_key, 0, 32); */
94   size =
95       GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING,
96                                  strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key,
97                                  &INITVALUE, result);
98
99   if (size == -1)
100   {
101     GNUNET_break (0);
102     return 1;
103   }
104
105   size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res);
106
107   if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size)
108   {
109     GNUNET_break (0);
110     return 1;
111   }
112   if (0 != strcmp (res, WEAK_KEY_TESTSTRING))
113   {
114     GNUNET_break (0);
115     return 1;
116   }
117   else
118     return 0;
119 }
120
121 static int
122 getWeakKeys ()
123 {
124   struct GNUNET_CRYPTO_AesSessionKey sessionkey;
125   int number_of_weak_keys = 0;
126   int number_of_runs;
127
128   gcry_cipher_hd_t handle;
129   int rc;
130
131   for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS;
132        number_of_runs++)
133   {
134
135     if (number_of_runs % 1000 == 0)
136       FPRINTF (stderr, "%s",  ".");
137     /*printf("Got to run number %d.\n", number_of_runs); */
138     GNUNET_CRYPTO_aes_create_session_key (&sessionkey);
139
140     rc = gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB,
141                            0);
142
143     if (rc)
144     {
145       printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n",
146               number_of_runs, gcry_strerror (rc));
147       continue;
148     }
149
150     rc = gcry_cipher_setkey (handle, &sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
151
152     if ((char) rc == GPG_ERR_WEAK_KEY)
153     {
154       printf ("\nWeak key (in hex): ");
155       printWeakKey (&sessionkey);
156       printf ("\n");
157       number_of_weak_keys++;
158     }
159     else if (rc)
160     {
161       printf ("\nUnexpected error generating keys. Error is %s\n",
162               gcry_strerror (rc));
163     }
164
165     gcry_cipher_close (handle);
166
167   }
168
169   return number_of_weak_keys;
170 }
171
172 int
173 main (int argc, char *argv[])
174 {
175   int weak_keys;
176
177   GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL);
178   if (GENERATE_WEAK_KEYS)
179   {
180     weak_keys = getWeakKeys ();
181
182     if (weak_keys == 0)
183     {
184       printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS);
185     }
186     else
187     {
188       printf ("\n%d weak keys found in %d runs.\n", weak_keys,
189               MAX_WEAK_KEY_TRIALS);
190     }
191   }
192
193   if (testWeakKey () != 0)
194     return -1;
195   return 0;
196 }
197
198 /* end of weakkeytest.c */