diff options
Diffstat (limited to 'src/util/test_crypto_aes_weak.c')
-rw-r--r-- | src/util/test_crypto_aes_weak.c | 127 |
1 files changed, 65 insertions, 62 deletions
diff --git a/src/util/test_crypto_aes_weak.c b/src/util/test_crypto_aes_weak.c index a0161b575..25931b625 100644 --- a/src/util/test_crypto_aes_weak.c +++ b/src/util/test_crypto_aes_weak.c | |||
@@ -40,9 +40,9 @@ printWeakKey (struct GNUNET_CRYPTO_AesSessionKey *key) | |||
40 | int i; | 40 | int i; |
41 | 41 | ||
42 | for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) | 42 | for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) |
43 | { | 43 | { |
44 | printf ("%x ", (int) (key->key[i])); | 44 | printf ("%x ", (int) (key->key[i])); |
45 | } | 45 | } |
46 | } | 46 | } |
47 | 47 | ||
48 | static int | 48 | static int |
@@ -55,7 +55,7 @@ testWeakKey () | |||
55 | struct GNUNET_CRYPTO_AesInitializationVector INITVALUE; | 55 | struct GNUNET_CRYPTO_AesInitializationVector INITVALUE; |
56 | 56 | ||
57 | memset (&INITVALUE, 42, | 57 | memset (&INITVALUE, 42, |
58 | sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); | 58 | sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); |
59 | /* sorry, this is not a weak key -- I don't have | 59 | /* sorry, this is not a weak key -- I don't have |
60 | * any at the moment! */ | 60 | * any at the moment! */ |
61 | weak_key.key[0] = (char) (0x4c); | 61 | weak_key.key[0] = (char) (0x4c); |
@@ -92,31 +92,31 @@ testWeakKey () | |||
92 | weak_key.key[31] = (char) (0xaa); | 92 | weak_key.key[31] = (char) (0xaa); |
93 | /* memset(&weak_key, 0, 32); */ | 93 | /* memset(&weak_key, 0, 32); */ |
94 | weak_key.crc32 = | 94 | weak_key.crc32 = |
95 | htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH)); | 95 | htonl (GNUNET_CRYPTO_crc32_n (&weak_key, GNUNET_CRYPTO_AES_KEY_LENGTH)); |
96 | 96 | ||
97 | size = | 97 | size = |
98 | GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING, | 98 | GNUNET_CRYPTO_aes_encrypt (WEAK_KEY_TESTSTRING, |
99 | strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key, | 99 | strlen (WEAK_KEY_TESTSTRING) + 1, &weak_key, |
100 | &INITVALUE, result); | 100 | &INITVALUE, result); |
101 | 101 | ||
102 | if (size == -1) | 102 | if (size == -1) |
103 | { | 103 | { |
104 | GNUNET_break (0); | 104 | GNUNET_break (0); |
105 | return 1; | 105 | return 1; |
106 | } | 106 | } |
107 | 107 | ||
108 | size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res); | 108 | size = GNUNET_CRYPTO_aes_decrypt (result, size, &weak_key, &INITVALUE, res); |
109 | 109 | ||
110 | if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size) | 110 | if ((strlen (WEAK_KEY_TESTSTRING) + 1) != size) |
111 | { | 111 | { |
112 | GNUNET_break (0); | 112 | GNUNET_break (0); |
113 | return 1; | 113 | return 1; |
114 | } | 114 | } |
115 | if (0 != strcmp (res, WEAK_KEY_TESTSTRING)) | 115 | if (0 != strcmp (res, WEAK_KEY_TESTSTRING)) |
116 | { | 116 | { |
117 | GNUNET_break (0); | 117 | GNUNET_break (0); |
118 | return 1; | 118 | return 1; |
119 | } | 119 | } |
120 | else | 120 | else |
121 | return 0; | 121 | return 0; |
122 | } | 122 | } |
@@ -133,42 +133,45 @@ getWeakKeys () | |||
133 | 133 | ||
134 | for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS; | 134 | for (number_of_runs = 0; number_of_runs < MAX_WEAK_KEY_TRIALS; |
135 | number_of_runs++) | 135 | number_of_runs++) |
136 | { | ||
137 | |||
138 | if (number_of_runs % 1000 == 0) | ||
139 | fprintf (stderr, "."); | ||
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 | { | 136 | { |
148 | printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n", | ||
149 | number_of_runs, gcry_strerror (rc)); | ||
150 | continue; | ||
151 | } | ||
152 | 137 | ||
153 | rc = gcry_cipher_setkey (handle, &sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH); | 138 | if (number_of_runs % 1000 == 0) |
139 | fprintf (stderr, "."); | ||
140 | /*printf("Got to run number %d.\n", number_of_runs); */ | ||
141 | GNUNET_CRYPTO_aes_create_session_key (&sessionkey); | ||
142 | |||
143 | rc = | ||
144 | gcry_cipher_open (&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, | ||
145 | 0); | ||
146 | |||
147 | if (rc) | ||
148 | { | ||
149 | printf ("testweakkey: gcry_cipher_open failed on trial %d. %s\n", | ||
150 | number_of_runs, gcry_strerror (rc)); | ||
151 | continue; | ||
152 | } | ||
153 | |||
154 | rc = | ||
155 | gcry_cipher_setkey (handle, &sessionkey, | ||
156 | GNUNET_CRYPTO_AES_KEY_LENGTH); | ||
157 | |||
158 | if ((char) rc == GPG_ERR_WEAK_KEY) | ||
159 | { | ||
160 | printf ("\nWeak key (in hex): "); | ||
161 | printWeakKey (&sessionkey); | ||
162 | printf ("\n"); | ||
163 | number_of_weak_keys++; | ||
164 | } | ||
165 | else if (rc) | ||
166 | { | ||
167 | printf ("\nUnexpected error generating keys. Error is %s\n", | ||
168 | gcry_strerror (rc)); | ||
169 | } | ||
170 | |||
171 | gcry_cipher_close (handle); | ||
154 | 172 | ||
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 | } | 173 | } |
167 | 174 | ||
168 | gcry_cipher_close (handle); | ||
169 | |||
170 | } | ||
171 | |||
172 | return number_of_weak_keys; | 175 | return number_of_weak_keys; |
173 | } | 176 | } |
174 | 177 | ||
@@ -180,19 +183,19 @@ main (int argc, char *argv[]) | |||
180 | GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL); | 183 | GNUNET_log_setup ("test-crypto-aes-weak", "WARNING", NULL); |
181 | GNUNET_CRYPTO_random_disable_entropy_gathering (); | 184 | GNUNET_CRYPTO_random_disable_entropy_gathering (); |
182 | if (GENERATE_WEAK_KEYS) | 185 | 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 | { | 186 | { |
192 | printf ("\n%d weak keys found in %d runs.\n", weak_keys, | 187 | weak_keys = getWeakKeys (); |
193 | MAX_WEAK_KEY_TRIALS); | 188 | |
189 | if (weak_keys == 0) | ||
190 | { | ||
191 | printf ("\nNo weak keys found in %d runs.\n", MAX_WEAK_KEY_TRIALS); | ||
192 | } | ||
193 | else | ||
194 | { | ||
195 | printf ("\n%d weak keys found in %d runs.\n", weak_keys, | ||
196 | MAX_WEAK_KEY_TRIALS); | ||
197 | } | ||
194 | } | 198 | } |
195 | } | ||
196 | 199 | ||
197 | if (testWeakKey () != 0) | 200 | if (testWeakKey () != 0) |
198 | return -1; | 201 | return -1; |