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