2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @author Krista Bennett
24 * @author Christian Grothoff
25 * @file util/test_crypto_aes_weak.c
26 * @brief AES weak key test.
29 #include "gnunet_common.h"
30 #include "gnunet_crypto_lib.h"
33 #define MAX_WEAK_KEY_TRIALS 100000
34 #define GENERATE_WEAK_KEYS GNUNET_NO
35 #define WEAK_KEY_TESTSTRING "I hate weak keys."
38 printWeakKey (struct GNUNET_CRYPTO_AesSessionKey *key)
42 for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++)
44 printf ("%x ", (int) (key->key[i]));
54 struct GNUNET_CRYPTO_AesSessionKey weak_key;
55 struct GNUNET_CRYPTO_AesInitializationVector INITVALUE;
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); */
95 GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING,
96 strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key,
105 size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res);
107 if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size)
112 if (0 != strcmp (res, WEAK_KEY_TESTSTRING))
124 struct GNUNET_CRYPTO_AesSessionKey sessionkey;
125 int number_of_weak_keys = 0;
128 gcry_cipher_hd_t handle;
131 for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS;
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);
140 rc = gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB,
145 printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n",
146 number_of_runs, gcry_strerror (rc));
150 rc = gcry_cipher_setkey (handle, &sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
152 if ((char) rc == GPG_ERR_WEAK_KEY)
154 printf ("\nWeak key (in hex): ");
155 printWeakKey (&sessionkey);
157 number_of_weak_keys++;
161 printf ("\nUnexpected error generating keys. Error is %s\n",
165 gcry_cipher_close (handle);
169 return number_of_weak_keys;
173 main (int argc, char *argv[])
177 GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL);
178 if (GENERATE_WEAK_KEYS)
180 weak_keys = getWeakKeys ();
184 printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS);
188 printf ("\n%d weak keys found in %d runs.\n", weak_keys,
189 MAX_WEAK_KEY_TRIALS);
193 if (testWeakKey () != 0)
198 /* end of weakkeytest.c */