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