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