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