-dce
[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   weak_key.crc32 =
95       htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH));
96
97   size =
98       GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING,
99                                  strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key,
100                                  &INITVALUE, result);
101
102   if (size == -1)
103   {
104     GNUNET_break (0);
105     return 1;
106   }
107
108   size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res);
109
110   if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size)
111   {
112     GNUNET_break (0);
113     return 1;
114   }
115   if (0 != strcmp (res, WEAK_KEY_TESTSTRING))
116   {
117     GNUNET_break (0);
118     return 1;
119   }
120   else
121     return 0;
122 }
123
124 static int
125 getWeakKeys ()
126 {
127   struct GNUNET_CRYPTO_AesSessionKey sessionkey;
128   int number_of_weak_keys = 0;
129   int number_of_runs;
130
131   gcry_cipher_hd_t handle;
132   int rc;
133
134   for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS;
135        number_of_runs++)
136   {
137
138     if (number_of_runs % 1000 == 0)
139       FPRINTF (stderr, "%s",  ".");
140     /*printf("Got to run number %d.\n", number_of_runs); */
141     GNUNET_CRYPTO_aes_create_session_key (&sessionkey);
142
143     rc = gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB,
144                            0);
145
146     if (rc)
147     {
148       printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n",
149               number_of_runs, gcry_strerror (rc));
150       continue;
151     }
152
153     rc = gcry_cipher_setkey (handle, &sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
154
155     if ((char) rc == GPG_ERR_WEAK_KEY)
156     {
157       printf ("\nWeak key (in hex): ");
158       printWeakKey (&sessionkey);
159       printf ("\n");
160       number_of_weak_keys++;
161     }
162     else if (rc)
163     {
164       printf ("\nUnexpected error generating keys. Error is %s\n",
165               gcry_strerror (rc));
166     }
167
168     gcry_cipher_close (handle);
169
170   }
171
172   return number_of_weak_keys;
173 }
174
175 int
176 main (int argc, char *argv[])
177 {
178   int weak_keys;
179
180   GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL);
181   GNUNET_CRYPTO_random_disable_entropy_gathering ();
182   if (GENERATE_WEAK_KEYS)
183   {
184     weak_keys = getWeakKeys ();
185
186     if (weak_keys == 0)
187     {
188       printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS);
189     }
190     else
191     {
192       printf ("\n%d weak keys found in %d runs.\n", weak_keys,
193               MAX_WEAK_KEY_TRIALS);
194     }
195   }
196
197   if (testWeakKey () != 0)
198     return -1;
199   return 0;
200 }
201
202 /* end of weakkeytest.c */