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