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