maint: update copyright
[gnulib.git] / tests / test-arctwo.c
1 /*
2  * Copyright (C) 2005, 2010-2014 Free Software Foundation, Inc.
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 3 of the License, or
8  * (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 #include <stdio.h>
21 #include <string.h>
22 #include "arctwo.h"
23
24 int
25 main (int argc, char *argv[])
26 {
27   arctwo_context ctx;
28   char scratch[16];
29
30   /* Test vectors from Peter Gutmann's paper. */
31   static char key_1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
33   };
34   static char plaintext_1[] =
35     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
36   static const char ciphertext_1[] =
37     { 0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7 };
38
39   static char key_2[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
40     0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
41   };
42   static char plaintext_2[] =
43     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
44   static char ciphertext_2[] =
45     { 0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31 };
46
47   /* This one was checked against libmcrypt's RFC2268. */
48   static char key_3[] = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
50   };
51   static char plaintext_3[] =
52     { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
53   static char ciphertext_3[] =
54     { 0x8f, 0xd1, 0x03, 0x89, 0x33, 0x6b, 0xf9, 0x5e };
55
56   /* From RFC2268. */
57   static char key_4[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
58   static char plaintext_4[] =
59     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
60   static char ciphertext_4[] =
61     { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
62
63   static char key_5[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
64   static char plaintext_5[] =
65     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
66   static char ciphertext_5[] =
67     { 0x27, 0x8b, 0x27, 0xe4, 0x2e, 0x2f, 0x0d, 0x49 };
68
69   static char key_6[] = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
70   static char plaintext_6[] =
71     { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
72   static char ciphertext_6[] =
73     { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 };
74
75   /* First test. */
76   arctwo_setkey_ekb (&ctx, sizeof (key_1), key_1, 0);
77   arctwo_encrypt (&ctx, plaintext_1, scratch, ARCTWO_BLOCK_SIZE);
78
79   if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
80     return 1;
81
82   arctwo_setkey_ekb (&ctx, sizeof (key_1), key_1, 0);
83   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
84   if (memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
85     return 1;
86
87   /* Second test. */
88   arctwo_setkey_ekb (&ctx, sizeof (key_2), key_2, 0);
89   arctwo_encrypt (&ctx, plaintext_2, scratch, ARCTWO_BLOCK_SIZE);
90   if (memcmp (scratch, ciphertext_2, sizeof (ciphertext_2)))
91     return 1;
92
93   arctwo_setkey_ekb (&ctx, sizeof (key_2), key_2, 0);
94   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
95   if (memcmp (scratch, plaintext_2, sizeof (plaintext_2)))
96     return 1;
97
98   /* Third test. */
99   arctwo_setkey_ekb (&ctx, sizeof (key_3), key_3, 0);
100   arctwo_encrypt (&ctx, plaintext_3, scratch, ARCTWO_BLOCK_SIZE);
101
102   if (memcmp (scratch, ciphertext_3, sizeof (ciphertext_3)))
103     return 1;
104
105   arctwo_setkey_ekb (&ctx, sizeof (key_3), key_3, 0);
106   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
107   if (memcmp (scratch, plaintext_3, sizeof (plaintext_3)))
108     return 1;
109
110   /* Fourth test. */
111   arctwo_setkey_ekb (&ctx, sizeof (key_4), key_4, 63);
112   arctwo_encrypt (&ctx, plaintext_4, scratch, ARCTWO_BLOCK_SIZE);
113
114   if (memcmp (scratch, ciphertext_4, sizeof (ciphertext_4)))
115     {
116       size_t i;
117       printf ("expected:\n");
118       for (i = 0; i < sizeof (ciphertext_4); i++)
119         printf ("%02x ", ciphertext_4[i] & 0xFF);
120       printf ("\ncomputed:\n");
121       for (i = 0; i < sizeof (ciphertext_4); i++)
122         printf ("%02x ", scratch[i] & 0xFF);
123       printf ("\n");
124       return 1;
125     }
126
127   arctwo_setkey_ekb (&ctx, sizeof (key_4), key_4, 63);
128   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
129   if (memcmp (scratch, plaintext_4, sizeof (plaintext_4)))
130     return 1;
131
132   /* Fifth test. */
133   arctwo_setkey_ekb (&ctx, sizeof (key_5), key_5, 64);
134   arctwo_encrypt (&ctx, plaintext_5, scratch, ARCTWO_BLOCK_SIZE);
135
136   if (memcmp (scratch, ciphertext_5, sizeof (ciphertext_5)))
137     {
138       size_t i;
139       printf ("expected:\n");
140       for (i = 0; i < sizeof (ciphertext_5); i++)
141         printf ("%02x ", ciphertext_5[i] & 0xFF);
142       printf ("\ncomputed:\n");
143       for (i = 0; i < sizeof (ciphertext_5); i++)
144         printf ("%02x ", scratch[i] & 0xFF);
145       printf ("\n");
146       return 1;
147     }
148
149   arctwo_setkey_ekb (&ctx, sizeof (key_5), key_5, 64);
150   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
151   if (memcmp (scratch, plaintext_5, sizeof (plaintext_5)))
152     return 1;
153
154   /* Sixth test. */
155   arctwo_setkey_ekb (&ctx, 8, key_6, 64);
156   arctwo_encrypt (&ctx, plaintext_6, scratch, ARCTWO_BLOCK_SIZE);
157
158   if (memcmp (scratch, ciphertext_6, sizeof (ciphertext_6)))
159     {
160       size_t i;
161       printf ("expected:\n");
162       for (i = 0; i < sizeof (ciphertext_6); i++)
163         printf ("%02x ", ciphertext_6[i] & 0xFF);
164       printf ("\ncomputed:\n");
165       for (i = 0; i < sizeof (ciphertext_6); i++)
166         printf ("%02x ", scratch[i] & 0xFF);
167       printf ("\n");
168       return 1;
169     }
170
171   arctwo_setkey_ekb (&ctx, sizeof (key_6), key_6, 64);
172   arctwo_decrypt (&ctx, scratch, scratch, ARCTWO_BLOCK_SIZE);
173   if (memcmp (scratch, plaintext_6, sizeof (plaintext_6)))
174     return 1;
175
176   return 0;
177 }