Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-arctwo.c
1 /*
2  * Copyright (C) 2005 Free Software Foundation
3  * Written by Simon Josefsson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <string.h>
24 #include "arctwo.h"
25
26 int
27 main (int argc, char *argv[])
28 {
29   arctwo_context ctx;
30   char scratch[16];
31
32   /* Test vectors from Peter Gutmann's paper. */
33   static char key_1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
35   };
36   static char plaintext_1[] =
37     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
38   static const char ciphertext_1[] =
39     { 0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7 };
40
41   static char key_2[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
42     0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
43   };
44   static char plaintext_2[] =
45     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
46   static char ciphertext_2[] =
47     { 0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31 };
48
49   /* This one was checked against libmcrypt's RFC2268. */
50   static char key_3[] = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
52   };
53   static char plaintext_3[] =
54     { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
55   static char ciphertext_3[] =
56     { 0x8f, 0xd1, 0x03, 0x89, 0x33, 0x6b, 0xf9, 0x5e };
57
58   /* From RFC2268. */
59   static char key_4[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
60   static char plaintext_4[] =
61     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
62   static char ciphertext_4[] =
63     { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
64
65   static char key_5[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
66   static char plaintext_5[] =
67     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
68   static char ciphertext_5[] =
69     { 0x27, 0x8b, 0x27, 0xe4, 0x2e, 0x2f, 0x0d, 0x49 };
70
71   static char key_6[] = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
72   static char plaintext_6[] =
73     { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
74   static char ciphertext_6[] =
75     { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 };
76
77   /* First test. */
78   arctwo_setkey_ekb (&ctx, sizeof (key_1), key_1, 0);
79   arctwo_encrypt (&ctx, plaintext_1, scratch, ARCTWO_BLOCK_SIZE);
80
81   if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
82     return 1;
83
84   arctwo_setkey_ekb (&ctx, sizeof (key_1), key_1, 0);
85   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
86   if (memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
87     return 1;
88
89   /* Second test. */
90   arctwo_setkey_ekb (&ctx, sizeof (key_2), key_2, 0);
91   arctwo_encrypt (&ctx, plaintext_2, scratch, ARCTWO_BLOCK_SIZE);
92   if (memcmp (scratch, ciphertext_2, sizeof (ciphertext_2)))
93     return 1;
94
95   arctwo_setkey_ekb (&ctx, sizeof (key_2), key_2, 0);
96   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
97   if (memcmp (scratch, plaintext_2, sizeof (plaintext_2)))
98     return 1;
99
100   /* Third test. */
101   arctwo_setkey_ekb (&ctx, sizeof (key_3), key_3, 0);
102   arctwo_encrypt (&ctx, plaintext_3, scratch, ARCTWO_BLOCK_SIZE);
103
104   if (memcmp (scratch, ciphertext_3, sizeof (ciphertext_3)))
105     return 1;
106
107   arctwo_setkey_ekb (&ctx, sizeof (key_3), key_3, 0);
108   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
109   if (memcmp (scratch, plaintext_3, sizeof (plaintext_3)))
110     return 1;
111
112   /* Fourth test. */
113   arctwo_setkey_ekb (&ctx, sizeof (key_4), key_4, 63);
114   arctwo_encrypt (&ctx, plaintext_4, scratch, ARCTWO_BLOCK_SIZE);
115
116   if (memcmp (scratch, ciphertext_4, sizeof (ciphertext_4)))
117     {
118       size_t i;
119       printf ("expected:\n");
120       for (i = 0; i < sizeof (ciphertext_4); i++)
121         printf ("%02x ", ciphertext_4[i] & 0xFF);
122       printf ("\ncomputed:\n");
123       for (i = 0; i < sizeof (ciphertext_4); i++)
124         printf ("%02x ", scratch[i] & 0xFF);
125       printf ("\n");
126       return 1;
127     }
128
129   arctwo_setkey_ekb (&ctx, sizeof (key_4), key_4, 63);
130   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
131   if (memcmp (scratch, plaintext_4, sizeof (plaintext_4)))
132     return 1;
133
134   /* Fifth test. */
135   arctwo_setkey_ekb (&ctx, sizeof (key_5), key_5, 64);
136   arctwo_encrypt (&ctx, plaintext_5, scratch, ARCTWO_BLOCK_SIZE);
137
138   if (memcmp (scratch, ciphertext_5, sizeof (ciphertext_5)))
139     {
140       size_t i;
141       printf ("expected:\n");
142       for (i = 0; i < sizeof (ciphertext_5); i++)
143         printf ("%02x ", ciphertext_5[i] & 0xFF);
144       printf ("\ncomputed:\n");
145       for (i = 0; i < sizeof (ciphertext_5); i++)
146         printf ("%02x ", scratch[i] & 0xFF);
147       printf ("\n");
148       return 1;
149     }
150
151   arctwo_setkey_ekb (&ctx, sizeof (key_5), key_5, 64);
152   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
153   if (memcmp (scratch, plaintext_5, sizeof (plaintext_5)))
154     return 1;
155
156   /* Sixth test. */
157   arctwo_setkey_ekb (&ctx, 8, key_6, 64);
158   arctwo_encrypt (&ctx, plaintext_6, scratch, ARCTWO_BLOCK_SIZE);
159
160   if (memcmp (scratch, ciphertext_6, sizeof (ciphertext_6)))
161     {
162       size_t i;
163       printf ("expected:\n");
164       for (i = 0; i < sizeof (ciphertext_6); i++)
165         printf ("%02x ", ciphertext_6[i] & 0xFF);
166       printf ("\ncomputed:\n");
167       for (i = 0; i < sizeof (ciphertext_6); i++)
168         printf ("%02x ", scratch[i] & 0xFF);
169       printf ("\n");
170       return 1;
171     }
172
173   arctwo_setkey_ekb (&ctx, sizeof (key_6), key_6, 64);
174   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
175   if (memcmp (scratch, plaintext_6, sizeof (plaintext_6)))
176     return 1;
177
178   return 0;
179 }