8dfaef732394c38a9b0cd6805decebde512b42dd
[oweals/gnunet.git] / src / util / test_crypto_aes.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_aes.c
24  * @brief test for AES ciphers
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29
30 #define TESTSTRING "Hello World!"
31 #define INITVALUE "InitializationVectorValue"
32
33 static int
34 testSymcipher ()
35 {
36   struct GNUNET_CRYPTO_AesSessionKey key;
37   char result[100];
38   int size;
39   char res[100];
40
41   GNUNET_CRYPTO_aes_create_session_key (&key);
42   size = GNUNET_CRYPTO_aes_encrypt (TESTSTRING,
43                                     strlen (TESTSTRING) + 1,
44                                     &key,
45                                     (const struct
46                                      GNUNET_CRYPTO_AesInitializationVector *)
47                                     INITVALUE, result);
48   if (size == -1)
49     {
50       printf ("symciphertest failed: encryptBlock returned %d\n", size);
51       return 1;
52     }
53   size = GNUNET_CRYPTO_aes_decrypt (result, size,
54                                     &key,
55                                     (const struct
56                                      GNUNET_CRYPTO_AesInitializationVector *)
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 int
73 verifyCrypto ()
74 {
75   struct GNUNET_CRYPTO_AesSessionKey key;
76   char result[GNUNET_CRYPTO_AES_KEY_LENGTH];
77   char *res;
78   int ret;
79
80   unsigned char plain[] =
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[] =
85     { 106, 74, 209, 88, 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25,
86     169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184,
87     34, 191
88   };
89   unsigned char encrresult[] =
90     { 167, 102, 230, 233, 127, 195, 176, 107, 17, 91, 199, 127, 96, 113, 75,
91     195, 245, 217, 61, 236, 159, 165, 103, 121, 203, 99, 202, 41, 23, 222, 25,
92     102
93   };
94
95   res = NULL;
96   ret = 0;
97
98   memcpy (key.key, raw_key, GNUNET_CRYPTO_AES_KEY_LENGTH);
99   key.crc32 =
100     htonl (GNUNET_CRYPTO_crc32_n (&key, GNUNET_CRYPTO_AES_KEY_LENGTH));
101
102   if (ntohl (key.crc32) != (unsigned int) 38125195LL)
103     {
104       printf ("Static key has different CRC: %u - %u\n",
105               ntohl (key.crc32), key.crc32);
106
107       ret = 1;
108       goto error;
109     }
110
111   if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
112       GNUNET_CRYPTO_aes_encrypt (plain,
113                                  GNUNET_CRYPTO_AES_KEY_LENGTH,
114                                  &key,
115                                  (const struct
116                                   GNUNET_CRYPTO_AesInitializationVector *)
117                                  "testtesttesttest", result))
118     {
119       printf ("Wrong return value from encrypt block.\n");
120       ret = 1;
121       goto error;
122     }
123
124   if (memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0)
125     {
126       printf ("Encrypted result wrong.\n");
127       ret = 1;
128       goto error;
129     }
130
131   res = GNUNET_malloc (GNUNET_CRYPTO_AES_KEY_LENGTH);
132
133   if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
134       GNUNET_CRYPTO_aes_decrypt (result,
135                                  GNUNET_CRYPTO_AES_KEY_LENGTH,
136                                  &key,
137                                  (const struct
138                                   GNUNET_CRYPTO_AesInitializationVector *)
139                                  "testtesttesttest", res))
140     {
141       printf ("Wrong return value from decrypt block.\n");
142       ret = 1;
143       goto error;
144     }
145
146   if (memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH) != 0)
147     {
148       printf ("Decrypted result does not match input.\n");
149
150       ret = 1;
151     }
152
153 error:
154
155   GNUNET_free_non_null (res);
156
157   return ret;
158 }
159
160 int
161 main (int argc, char *argv[])
162 {
163   int failureCount = 0;
164
165   GNUNET_log_setup ("test-crypto-aes", "WARNING", NULL);
166   GNUNET_CRYPTO_random_disable_entropy_gathering ();
167   GNUNET_assert (strlen (INITVALUE) >
168                  sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
169   failureCount += testSymcipher ();
170   failureCount += verifyCrypto ();
171
172   if (failureCount != 0)
173     {
174       printf ("%d TESTS FAILED!\n", failureCount);
175       return -1;
176     }
177   return 0;
178 }
179
180 /* end of test_crypto_aes.c */