2 This file is part of GNUnet.
3 (C) 2002, 2003, 2004, 2006 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @author Christian Grothoff
23 * @file util/test_crypto_symmetric.c
24 * @brief test for AES ciphers
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
30 #define TESTSTRING "Hello World!"
31 #define INITVALUE "InitializationVectorValueinitializationvectorvalue"
36 struct GNUNET_CRYPTO_SymmetricSessionKey key;
41 GNUNET_CRYPTO_symmetric_create_session_key (&key);
43 GNUNET_CRYPTO_symmetric_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &key,
45 GNUNET_CRYPTO_SymmetricInitializationVector *)
49 printf ("symciphertest failed: encryptBlock returned %d\n", size);
53 GNUNET_CRYPTO_symmetric_decrypt (result, size, &key,
55 GNUNET_CRYPTO_SymmetricInitializationVector *)
57 if (strlen (TESTSTRING) + 1 != size)
59 printf ("symciphertest failed: decryptBlock returned %d\n", size);
62 if (0 != strcmp (res, TESTSTRING))
64 printf ("symciphertest failed: %s != %s\n", res, TESTSTRING);
75 struct GNUNET_CRYPTO_SymmetricSessionKey key;
76 char result[GNUNET_CRYPTO_AES_KEY_LENGTH];
80 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
85 unsigned char raw_key_aes[] =
87 106, 74, 209, 88, 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,
91 unsigned char raw_key_twofish[] =
93 145, 55, 189, 135, 125, 180, 225, 108, 183, 54, 25,
94 169, 129, 188, 131, 75, 227, 245, 105, 10, 225, 15, 115, 159, 148, 184,
95 34, 191, 106, 74, 209, 88
97 unsigned char encrresult[] =
99 161, 152, 186, 231, 214, 55, 225, 206, 85, 43, 80, 134, 145, 198, 20,
100 233, 236, 57, 194, 10, 147, 149, 30, 106, 179, 54, 182, 247, 71, 204,
107 memcpy (key.aes_key, raw_key_aes, GNUNET_CRYPTO_AES_KEY_LENGTH);
108 memcpy (key.twofish_key, raw_key_twofish, GNUNET_CRYPTO_AES_KEY_LENGTH);
109 if (GNUNET_CRYPTO_AES_KEY_LENGTH !=
110 GNUNET_CRYPTO_symmetric_encrypt (plain, GNUNET_CRYPTO_AES_KEY_LENGTH, &key,
112 GNUNET_CRYPTO_SymmetricInitializationVector *)
113 "testtesttesttest", result))
115 printf ("Wrong return value from encrypt block.\n");
120 if (0 != memcmp (encrresult, result, GNUNET_CRYPTO_AES_KEY_LENGTH))
123 printf ("Encrypted result wrong.\n");
124 for (i=0;i<GNUNET_CRYPTO_AES_KEY_LENGTH;i++)
125 printf ("%u, ", (uint8_t) result[i]);
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,
134 GNUNET_CRYPTO_SymmetricInitializationVector *)
135 "testtesttesttest", res))
137 printf ("Wrong return value from decrypt block.\n");
141 if (0 != memcmp (res, plain, GNUNET_CRYPTO_AES_KEY_LENGTH))
143 printf ("Decrypted result does not match input.\n");
147 GNUNET_free_non_null (res);
153 main (int argc, char *argv[])
155 int failureCount = 0;
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 ();
163 if (failureCount != 0)
165 printf ("%d TESTS FAILED!\n", failureCount);
171 /* end of test_crypto_aes.c */