-remove trailing whitespace
[oweals/gnunet.git] / src / util / test_crypto_symmetric.c
1 /*
2      This file is part of GNUnet.
3      (C) 2002, 2003, 2004, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
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                                  INITVALUE, result);
46   if (size == -1)
47   {
48     printf ("symciphertest failed: encryptBlock returned %d\n", size);
49     return 1;
50   }
51   size =
52       GNUNET_CRYPTO_symmetric_decrypt (result, size, &key,
53                                  (const struct
54                                   GNUNET_CRYPTO_SymmetricInitializationVector *)
55                                  INITVALUE, res);
56   if (strlen (TESTSTRING) + 1 != size)
57   {
58     printf ("symciphertest failed: decryptBlock returned %d\n", size);
59     return 1;
60   }
61   if (0 != strcmp (res, TESTSTRING))
62   {
63     printf ("symciphertest failed: %s != %s\n", res, TESTSTRING);
64     return 1;
65   }
66   else
67     return 0;
68 }
69
70
71 static int
72 verifyCrypto ()
73 {
74   struct GNUNET_CRYPTO_SymmetricSessionKey key;
75   char result[GNUNET_CRYPTO_AES_KEY_LENGTH];
76   char *res;
77   int ret;
78
79   unsigned char plain[] =
80   {
81     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   };
84   unsigned char raw_key_aes[] =
85   {
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   {
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   {
98     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,
100     206, 204, 1
101   };
102
103   res = NULL;
104   ret = 0;
105
106   memcpy (key.aes_key, raw_key_aes, GNUNET_CRYPTO_AES_KEY_LENGTH);
107   memcpy (key.twofish_key, raw_key_twofish, GNUNET_CRYPTO_AES_KEY_LENGTH);
108   if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
109       GNUNET_CRYPTO_symmetric_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH, &key,
110                                        (const struct
111                                         GNUNET_CRYPTO_SymmetricInitializationVector *)
112                                        "testtesttesttesttesttesttesttest",
113                                        result))
114   {
115     printf ("Wrong return value from encrypt block.\n");
116     ret = 1;
117     goto error;
118   }
119
120   if (0 != memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH))
121   {
122     int i;
123     printf ("Encrypted result wrong.\n");
124     for (i=0;i<GNUNET_CRYPTO_AES_KEY_LENGTH;i++)
125       printf ("%u, ", (uint8_t) result[i]);
126     ret = 1;
127     goto error;
128   }
129
130   res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH);
131   if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
132       GNUNET_CRYPTO_symmetric_decrypt (result, GNUNET_CRYPTO_AES_KEY_LENGTH, &key,
133                                  (const struct
134                                   GNUNET_CRYPTO_SymmetricInitializationVector *)
135                                  "testtesttesttesttesttesttesttest", res))
136   {
137     printf ("Wrong return value from decrypt block.\n");
138     ret = 1;
139     goto error;
140   }
141   if (0 != memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH))
142   {
143     printf ("Decrypted result does not match input.\n");
144     ret = 1;
145   }
146 error:
147   GNUNET_free_non_null (res);
148   return ret;
149 }
150
151
152 int
153 main (int argc, char *argv[])
154 {
155   int failureCount = 0;
156
157   GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL);
158   GNUNET_assert (strlen (INITVALUE) >
159                  sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector));
160   failureCount += testSymcipher ();
161   failureCount += verifyCrypto ();
162
163   if (failureCount != 0)
164   {
165     printf ("%d TESTS FAILED!\n", failureCount);
166     return -1;
167   }
168   return 0;
169 }
170
171 /* end of test_crypto_aes.c */