diff options
Diffstat (limited to 'src/lib/util/test_crypto_symmetric.c')
-rw-r--r-- | src/lib/util/test_crypto_symmetric.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/lib/util/test_crypto_symmetric.c b/src/lib/util/test_crypto_symmetric.c new file mode 100644 index 000000000..4c8c2f0c2 --- /dev/null +++ b/src/lib/util/test_crypto_symmetric.c | |||
@@ -0,0 +1,176 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2002, 2003, 2004, 2006 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | |||
20 | */ | ||
21 | /** | ||
22 | * @author Christian Grothoff | ||
23 | * @file util/test_crypto_symmetric.c | ||
24 | * @brief test for AES ciphers | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | |||
30 | #define TESTSTRING "Hello World!" | ||
31 | #define INITVALUE "InitializationVectorValueinitializationvectorvalue" | ||
32 | |||
33 | static int | ||
34 | testSymcipher () | ||
35 | { | ||
36 | struct GNUNET_CRYPTO_SymmetricSessionKey key; | ||
37 | char result[100]; | ||
38 | int size; | ||
39 | char res[100]; | ||
40 | |||
41 | GNUNET_CRYPTO_symmetric_create_session_key (&key); | ||
42 | size = | ||
43 | GNUNET_CRYPTO_symmetric_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key, | ||
44 | (const struct | ||
45 | GNUNET_CRYPTO_SymmetricInitializationVector | ||
46 | *) | ||
47 | INITVALUE, result); | ||
48 | if (size == -1) | ||
49 | { | ||
50 | printf ("symciphertest failed: encryptBlock returned %d\n", size); | ||
51 | return 1; | ||
52 | } | ||
53 | size = | ||
54 | GNUNET_CRYPTO_symmetric_decrypt (result, size, &key, | ||
55 | (const struct | ||
56 | GNUNET_CRYPTO_SymmetricInitializationVector | ||
57 | *) | ||
58 | INITVALUE, res); | ||
59 | if (strlen (TESTSTRING) + 1 != size) | ||
60 | { | ||
61 | printf ("symciphertest failed: decryptBlock returned %d\n", size); | ||
62 | return 1; | ||
63 | } | ||
64 | if (0 != strcmp (res, TESTSTRING)) | ||
65 | { | ||
66 | printf ("symciphertest failed: %s != %s\n", res, TESTSTRING); | ||
67 | return 1; | ||
68 | } | ||
69 | else | ||
70 | return 0; | ||
71 | } | ||
72 | |||
73 | |||
74 | static int | ||
75 | verifyCrypto () | ||
76 | { | ||
77 | struct GNUNET_CRYPTO_SymmetricSessionKey key; | ||
78 | char result[GNUNET_CRYPTO_AES_KEY_LENGTH]; | ||
79 | char *res; | ||
80 | int ret; | ||
81 | |||
82 | unsigned char plain[] = { | ||
83 | 29, 128, 192, 253, 74, 171, 38, 187, 84, 219, 76, 76, 209, 118, 33, 249, | ||
84 | 172, 124, 96, 9, 157, 110, 8, 215, 200, 63, 69, 230, 157, 104, 247, 164 | ||
85 | }; | ||
86 | unsigned char raw_key_aes[] = { | ||
87 | 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25, | ||
88 | 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184, | ||
89 | 34, 191 | ||
90 | }; | ||
91 | unsigned char raw_key_twofish[] = { | ||
92 | 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, | ||
94 | 34, 191, 106, 74, 209, 88 | ||
95 | }; | ||
96 | unsigned char encrresult[] = { | ||
97 | 155, 88, 106, 174, 124, 172, 47, 149, 85, 15, 208, 176, 65, 124, 155, | ||
98 | 74, 215, 25, 177, 231, 162, 109, 165, 4, 133, 165, 93, 44, 213, 77, | ||
99 | 206, 204, 1 | ||
100 | }; | ||
101 | |||
102 | res = NULL; | ||
103 | ret = 0; | ||
104 | |||
105 | GNUNET_memcpy (key.aes_key, raw_key_aes, GNUNET_CRYPTO_AES_KEY_LENGTH); | ||
106 | GNUNET_memcpy (key.twofish_key, raw_key_twofish, | ||
107 | GNUNET_CRYPTO_AES_KEY_LENGTH); | ||
108 | if (GNUNET_CRYPTO_AES_KEY_LENGTH != | ||
109 | GNUNET_CRYPTO_symmetric_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH, | ||
110 | &key, | ||
111 | (const struct | ||
112 | GNUNET_CRYPTO_SymmetricInitializationVector | ||
113 | *) | ||
114 | "testtesttesttesttesttesttesttest", | ||
115 | result)) | ||
116 | { | ||
117 | printf ("Wrong return value from encrypt block.\n"); | ||
118 | ret = 1; | ||
119 | goto error; | ||
120 | } | ||
121 | |||
122 | if (0 != memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH)) | ||
123 | { | ||
124 | int i; | ||
125 | printf ("Encrypted result wrong.\n"); | ||
126 | for (i = 0; i < GNUNET_CRYPTO_AES_KEY_LENGTH; i++) | ||
127 | printf ("%u, ", (uint8_t) result[i]); | ||
128 | ret = 1; | ||
129 | goto error; | ||
130 | } | ||
131 | |||
132 | res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH); | ||
133 | if (GNUNET_CRYPTO_AES_KEY_LENGTH != | ||
134 | GNUNET_CRYPTO_symmetric_decrypt (result, GNUNET_CRYPTO_AES_KEY_LENGTH, | ||
135 | &key, | ||
136 | (const struct | ||
137 | GNUNET_CRYPTO_SymmetricInitializationVector | ||
138 | *) | ||
139 | "testtesttesttesttesttesttesttest", res)) | ||
140 | { | ||
141 | printf ("Wrong return value from decrypt block.\n"); | ||
142 | ret = 1; | ||
143 | goto error; | ||
144 | } | ||
145 | if (0 != memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH)) | ||
146 | { | ||
147 | printf ("Decrypted result does not match input.\n"); | ||
148 | ret = 1; | ||
149 | } | ||
150 | error: | ||
151 | GNUNET_free (res); | ||
152 | return ret; | ||
153 | } | ||
154 | |||
155 | |||
156 | int | ||
157 | main (int argc, char *argv[]) | ||
158 | { | ||
159 | int failureCount = 0; | ||
160 | |||
161 | GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL); | ||
162 | GNUNET_assert (strlen (INITVALUE) > | ||
163 | sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector)); | ||
164 | failureCount += testSymcipher (); | ||
165 | failureCount += verifyCrypto (); | ||
166 | |||
167 | if (failureCount != 0) | ||
168 | { | ||
169 | printf ("%d TESTS FAILED!\n", failureCount); | ||
170 | return -1; | ||
171 | } | ||
172 | return 0; | ||
173 | } | ||
174 | |||
175 | |||
176 | /* end of test_crypto_aes.c */ | ||