Add des, des-tests, gc-des, gc-des-tests modules.
[gnulib.git] / tests / test-des.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 /* Adapted for gnulib by Simon Josefsson, based on Libgcrypt. */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "des.h"
30
31 int
32 main (int argc, char *argv[])
33 {
34   /*
35    * DES Maintenance Test
36    */
37   {
38     int i;
39     char key[8] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
40     char input[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
41     char result[8] = { 0x24, 0x6e, 0x9d, 0xb9, 0xc5, 0x50, 0x38, 0x1a };
42     char temp1[8], temp2[8], temp3[8];
43     des_ctx des;
44
45     for (i = 0; i < 64; ++i)
46       {
47         des_setkey (&des, key);
48         des_ecb_encrypt (&des, input, temp1);
49         des_ecb_encrypt (&des, temp1, temp2);
50         des_setkey (&des, temp2);
51         des_ecb_decrypt (&des, temp1, temp3);
52         memcpy (key, temp3, 8);
53         memcpy (input, temp1, 8);
54       }
55     if (memcmp (temp3, result, 8))
56       return 1;
57   }
58
59
60   /*
61    * Self made Triple-DES test  (Does somebody know an official test?)
62    */
63   {
64     int i;
65     char input[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
66     char key1[8] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
67     char key2[8] = { 0x11, 0x22, 0x33, 0x44, 0xff, 0xaa, 0xcc, 0xdd };
68     char result[8] = { 0x7b, 0x38, 0x3b, 0x23, 0xa2, 0x7d, 0x26, 0xd3 };
69
70     tripledes_ctx des3;
71
72     for (i = 0; i < 16; ++i)
73       {
74         tripledes_set2keys (&des3, key1, key2);
75         tripledes_ecb_encrypt (&des3, input, key1);
76         tripledes_ecb_decrypt (&des3, input, key2);
77         tripledes_set3keys (&des3, key1, input, key2);
78         tripledes_ecb_encrypt (&des3, input, input);
79       }
80     if (memcmp (input, result, 8))
81       return 1;
82   }
83
84   /*
85    * More Triple-DES test.  These are testvectors as used by SSLeay,
86    * thanks to Jeroen C. van Gelderen.
87    */
88   {
89     struct
90     {
91       char key[24];
92       char plain[8];
93       char cipher[8];
94     } testdata[] =
95     {
96       {
97         {
98         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
99             0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
100             0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
101         {
102         0x95, 0xF8, 0xA5, 0xE5, 0xDD, 0x31, 0xD9, 0x00},
103         {
104       0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
105       {
106         {
107         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
108             0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
109             0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
110         {
111         0x9D, 0x64, 0x55, 0x5A, 0x9A, 0x10, 0xB8, 0x52,},
112         {
113         0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00}
114       },
115       {
116         {
117         0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E,
118             0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E,
119             0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E},
120         {
121         0x51, 0x45, 0x4B, 0x58, 0x2D, 0xDF, 0x44, 0x0A},
122         {
123         0x71, 0x78, 0x87, 0x6E, 0x01, 0xF1, 0x9B, 0x2A}
124       },
125       {
126         {
127         0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6,
128             0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6,
129             0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6},
130         {
131         0x42, 0xFD, 0x44, 0x30, 0x59, 0x57, 0x7F, 0xA2},
132         {
133         0xAF, 0x37, 0xFB, 0x42, 0x1F, 0x8C, 0x40, 0x95}
134       },
135       {
136         {
137         0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
138             0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
139             0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
140         {
141         0x73, 0x6F, 0x6D, 0x65, 0x64, 0x61, 0x74, 0x61},
142         {
143         0x3D, 0x12, 0x4F, 0xE2, 0x19, 0x8B, 0xA3, 0x18}
144       },
145       {
146         {
147         0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
148             0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
149             0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
150         {
151         0x73, 0x6F, 0x6D, 0x65, 0x64, 0x61, 0x74, 0x61},
152         {
153         0xFB, 0xAB, 0xA1, 0xFF, 0x9D, 0x05, 0xE9, 0xB1}
154       },
155       {
156         {
157         0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
158             0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
159             0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10},
160         {
161         0x73, 0x6F, 0x6D, 0x65, 0x64, 0x61, 0x74, 0x61},
162         {
163         0x18, 0xd7, 0x48, 0xe5, 0x63, 0x62, 0x05, 0x72}
164       },
165       {
166         {
167         0x03, 0x52, 0x02, 0x07, 0x67, 0x20, 0x82, 0x17,
168             0x86, 0x02, 0x87, 0x66, 0x59, 0x08, 0x21, 0x98,
169             0x64, 0x05, 0x6A, 0xBD, 0xFE, 0xA9, 0x34, 0x57},
170         {
171         0x73, 0x71, 0x75, 0x69, 0x67, 0x67, 0x6C, 0x65},
172         {
173         0xc0, 0x7d, 0x2a, 0x0f, 0xa5, 0x66, 0xfa, 0x30}
174       },
175       {
176         {
177         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
178             0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
179             0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02},
180         {
181         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
182         {
183         0xe6, 0xe6, 0xdd, 0x5b, 0x7e, 0x72, 0x29, 0x74}
184       },
185       {
186         {
187         0x10, 0x46, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20,
188             0x91, 0x07, 0xD0, 0x15, 0x89, 0x19, 0x01, 0x01,
189             0x19, 0x07, 0x92, 0x10, 0x98, 0x1A, 0x01, 0x01},
190         {
191         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
192         {
193         0xe1, 0xef, 0x62, 0xc3, 0x32, 0xfe, 0x82, 0x5b}
194       }
195     };
196
197     char result[8];
198     int i;
199     tripledes_ctx des3;
200
201     for (i = 0; i < sizeof (testdata) / sizeof (*testdata); ++i)
202       {
203         tripledes_set3keys (&des3, testdata[i].key,
204                             testdata[i].key + 8, testdata[i].key + 16);
205
206         tripledes_ecb_encrypt (&des3, testdata[i].plain, result);
207         if (memcmp (testdata[i].cipher, result, 8))
208           {
209             return 1;
210           }
211
212         tripledes_ecb_decrypt (&des3, testdata[i].cipher, result);
213         if (memcmp (testdata[i].plain, result, 8))
214           {
215             return 1;
216           }
217       }
218   }
219
220   return 0;
221 }