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