diff options
Diffstat (limited to 'src/util/test_crypto_symmetric.c')
-rw-r--r-- | src/util/test_crypto_symmetric.c | 167 |
1 files changed, 85 insertions, 82 deletions
diff --git a/src/util/test_crypto_symmetric.c b/src/util/test_crypto_symmetric.c index c6854711f..0d83d5aa1 100644 --- a/src/util/test_crypto_symmetric.c +++ b/src/util/test_crypto_symmetric.c | |||
@@ -30,71 +30,69 @@ | |||
30 | #define INITVALUE "InitializationVectorValueinitializationvectorvalue" | 30 | #define INITVALUE "InitializationVectorValueinitializationvectorvalue" |
31 | 31 | ||
32 | static int | 32 | static int |
33 | testSymcipher() | 33 | testSymcipher () |
34 | { | 34 | { |
35 | struct GNUNET_CRYPTO_SymmetricSessionKey key; | 35 | struct GNUNET_CRYPTO_SymmetricSessionKey key; |
36 | char result[100]; | 36 | char result[100]; |
37 | int size; | 37 | int size; |
38 | char res[100]; | 38 | char res[100]; |
39 | 39 | ||
40 | GNUNET_CRYPTO_symmetric_create_session_key(&key); | 40 | GNUNET_CRYPTO_symmetric_create_session_key (&key); |
41 | size = | 41 | size = |
42 | GNUNET_CRYPTO_symmetric_encrypt(TESTSTRING, strlen(TESTSTRING) + 1, &key, | 42 | GNUNET_CRYPTO_symmetric_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key, |
43 | (const struct | 43 | (const struct |
44 | GNUNET_CRYPTO_SymmetricInitializationVector *) | 44 | GNUNET_CRYPTO_SymmetricInitializationVector |
45 | INITVALUE, result); | 45 | *) |
46 | INITVALUE, result); | ||
46 | if (size == -1) | 47 | if (size == -1) |
47 | { | 48 | { |
48 | printf("symciphertest failed: encryptBlock returned %d\n", size); | 49 | printf ("symciphertest failed: encryptBlock returned %d\n", size); |
49 | return 1; | 50 | return 1; |
50 | } | 51 | } |
51 | size = | 52 | size = |
52 | GNUNET_CRYPTO_symmetric_decrypt(result, size, &key, | 53 | GNUNET_CRYPTO_symmetric_decrypt (result, size, &key, |
53 | (const struct | 54 | (const struct |
54 | GNUNET_CRYPTO_SymmetricInitializationVector *) | 55 | GNUNET_CRYPTO_SymmetricInitializationVector |
55 | INITVALUE, res); | 56 | *) |
56 | if (strlen(TESTSTRING) + 1 != size) | 57 | INITVALUE, res); |
57 | { | 58 | if (strlen (TESTSTRING) + 1 != size) |
58 | printf("symciphertest failed: decryptBlock returned %d\n", size); | 59 | { |
59 | return 1; | 60 | printf ("symciphertest failed: decryptBlock returned %d\n", size); |
60 | } | 61 | return 1; |
61 | if (0 != strcmp(res, TESTSTRING)) | 62 | } |
62 | { | 63 | if (0 != strcmp (res, TESTSTRING)) |
63 | printf("symciphertest failed: %s != %s\n", res, TESTSTRING); | 64 | { |
64 | return 1; | 65 | printf ("symciphertest failed: %s != %s\n", res, TESTSTRING); |
65 | } | 66 | return 1; |
67 | } | ||
66 | else | 68 | else |
67 | return 0; | 69 | return 0; |
68 | } | 70 | } |
69 | 71 | ||
70 | 72 | ||
71 | static int | 73 | static int |
72 | verifyCrypto() | 74 | verifyCrypto () |
73 | { | 75 | { |
74 | struct GNUNET_CRYPTO_SymmetricSessionKey key; | 76 | struct GNUNET_CRYPTO_SymmetricSessionKey key; |
75 | char result[GNUNET_CRYPTO_AES_KEY_LENGTH]; | 77 | char result[GNUNET_CRYPTO_AES_KEY_LENGTH]; |
76 | char *res; | 78 | char *res; |
77 | int ret; | 79 | int ret; |
78 | 80 | ||
79 | unsigned char plain[] = | 81 | unsigned char plain[] = { |
80 | { | ||
81 | 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249, | 82 | 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249, |
82 | 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164 | 83 | 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164 |
83 | }; | 84 | }; |
84 | unsigned char raw_key_aes[] = | 85 | unsigned char raw_key_aes[] = { |
85 | { | ||
86 | 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, | 86 | 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, |
87 | 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, | 87 | 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, |
88 | 34, 191 | 88 | 34, 191 |
89 | }; | 89 | }; |
90 | unsigned char raw_key_twofish[] = | 90 | unsigned char raw_key_twofish[] = { |
91 | { | ||
92 | 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, | 91 | 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, |
93 | 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, | 92 | 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, |
94 | 34, 191, 106, 74, 209, 88 | 93 | 34, 191, 106, 74, 209, 88 |
95 | }; | 94 | }; |
96 | unsigned char encrresult[] = | 95 | unsigned char encrresult[] = { |
97 | { | ||
98 | 155, 88, 106, 174, 124, 172, 47, 149, 85, 15, 208, 176, 65, 124, 155, | 96 | 155, 88, 106, 174, 124, 172, 47, 149, 85, 15, 208, 176, 65, 124, 155, |
99 | 74, 215, 25, 177, 231, 162, 109, 165, 4, 133, 165, 93, 44, 213, 77, | 97 | 74, 215, 25, 177, 231, 162, 109, 165, 4, 133, 165, 93, 44, 213, 77, |
100 | 206, 204, 1 | 98 | 206, 204, 1 |
@@ -103,68 +101,73 @@ verifyCrypto() | |||
103 | res = NULL; | 101 | res = NULL; |
104 | ret = 0; | 102 | ret = 0; |
105 | 103 | ||
106 | GNUNET_memcpy(key.aes_key, raw_key_aes, GNUNET_CRYPTO_AES_KEY_LENGTH); | 104 | GNUNET_memcpy (key.aes_key, raw_key_aes, GNUNET_CRYPTO_AES_KEY_LENGTH); |
107 | GNUNET_memcpy(key.twofish_key, raw_key_twofish, GNUNET_CRYPTO_AES_KEY_LENGTH); | 105 | GNUNET_memcpy (key.twofish_key, raw_key_twofish, |
106 | GNUNET_CRYPTO_AES_KEY_LENGTH); | ||
108 | if (GNUNET_CRYPTO_AES_KEY_LENGTH != | 107 | if (GNUNET_CRYPTO_AES_KEY_LENGTH != |
109 | GNUNET_CRYPTO_symmetric_encrypt(plain, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, | 108 | GNUNET_CRYPTO_symmetric_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH, |
110 | (const struct | 109 | &key, |
111 | GNUNET_CRYPTO_SymmetricInitializationVector *) | 110 | (const struct |
112 | "testtesttesttesttesttesttesttest", | 111 | GNUNET_CRYPTO_SymmetricInitializationVector |
113 | result)) | 112 | *) |
114 | { | 113 | "testtesttesttesttesttesttesttest", |
115 | printf("Wrong return value from encrypt block.\n"); | 114 | result)) |
116 | ret = 1; | 115 | { |
117 | goto error; | 116 | printf ("Wrong return value from encrypt block.\n"); |
118 | } | 117 | ret = 1; |
119 | 118 | goto error; | |
120 | if (0 != memcmp(encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH)) | 119 | } |
121 | { | 120 | |
122 | int i; | 121 | if (0 != memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH)) |
123 | printf("Encrypted result wrong.\n"); | 122 | { |
124 | for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) | 123 | int i; |
125 | printf("%u, ", (uint8_t)result[i]); | 124 | printf ("Encrypted result wrong.\n"); |
126 | ret = 1; | 125 | for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) |
127 | goto error; | 126 | printf ("%u, ", (uint8_t) result[i]); |
128 | } | 127 | ret = 1; |
129 | 128 | goto error; | |
130 | res = GNUNET_malloc(GNUNET_CRYPTO_AES_KEY_LENGTH); | 129 | } |
130 | |||
131 | res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH); | ||
131 | if (GNUNET_CRYPTO_AES_KEY_LENGTH != | 132 | if (GNUNET_CRYPTO_AES_KEY_LENGTH != |
132 | GNUNET_CRYPTO_symmetric_decrypt(result, GNUNET_CRYPTO_AES_KEY_LENGTH, &key, | 133 | GNUNET_CRYPTO_symmetric_decrypt (result, GNUNET_CRYPTO_AES_KEY_LENGTH, |
133 | (const struct | 134 | &key, |
134 | GNUNET_CRYPTO_SymmetricInitializationVector *) | 135 | (const struct |
135 | "testtesttesttesttesttesttesttest", res)) | 136 | GNUNET_CRYPTO_SymmetricInitializationVector |
136 | { | 137 | *) |
137 | printf("Wrong return value from decrypt block.\n"); | 138 | "testtesttesttesttesttesttesttest", res)) |
138 | ret = 1; | 139 | { |
139 | goto error; | 140 | printf ("Wrong return value from decrypt block.\n"); |
140 | } | 141 | ret = 1; |
141 | if (0 != memcmp(res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH)) | 142 | goto error; |
142 | { | 143 | } |
143 | printf("Decrypted result does not match input.\n"); | 144 | if (0 != memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH)) |
144 | ret = 1; | 145 | { |
145 | } | 146 | printf ("Decrypted result does not match input.\n"); |
147 | ret = 1; | ||
148 | } | ||
146 | error: | 149 | error: |
147 | GNUNET_free_non_null(res); | 150 | GNUNET_free_non_null (res); |
148 | return ret; | 151 | return ret; |
149 | } | 152 | } |
150 | 153 | ||
151 | 154 | ||
152 | int | 155 | int |
153 | main(int argc, char *argv[]) | 156 | main (int argc, char *argv[]) |
154 | { | 157 | { |
155 | int failureCount = 0; | 158 | int failureCount = 0; |
156 | 159 | ||
157 | GNUNET_log_setup("test-crypto-aes", "WARNING", NULL); | 160 | GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL); |
158 | GNUNET_assert(strlen(INITVALUE) > | 161 | GNUNET_assert (strlen (INITVALUE) > |
159 | sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector)); | 162 | sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector)); |
160 | failureCount += testSymcipher(); | 163 | failureCount += testSymcipher (); |
161 | failureCount += verifyCrypto(); | 164 | failureCount += verifyCrypto (); |
162 | 165 | ||
163 | if (failureCount != 0) | 166 | if (failureCount != 0) |
164 | { | 167 | { |
165 | printf("%d TESTS FAILED!\n", failureCount); | 168 | printf ("%d TESTS FAILED!\n", failureCount); |
166 | return -1; | 169 | return -1; |
167 | } | 170 | } |
168 | return 0; | 171 | return 0; |
169 | } | 172 | } |
170 | 173 | ||