]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/lib/libdes/t_des.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / lib / libdes / t_des.c
1 /* $NetBSD: t_des.c,v 1.1 2010/08/25 16:46:36 jmmv Exp $ */
2
3 /*
4  * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) All rights
5  * reserved.
6  * 
7  * This package is an SSL implementation written by Eric Young
8  * (eay@cryptsoft.com). The implementation was written so as to conform with
9  * Netscapes SSL.
10  * 
11  * This library is free for commercial and non-commercial use as long as the
12  * following conditions are aheared to.  The following conditions apply to
13  * all code found in this distribution, be it the RC4, RSA, lhash, DES, etc.,
14  * code; not just the SSL code.  The SSL documentation included with this
15  * distribution is covered by the same copyright terms except that the holder
16  * is Tim Hudson (tjh@cryptsoft.com).
17  * 
18  * Copyright remains Eric Young's, and as such any Copyright notices in the code
19  * are not to be removed. If this package is used in a product, Eric Young
20  * should be given attribution as the author of the parts of the library
21  * used. This can be in the form of a textual message at program startup or
22  * in documentation (online or textual) provided with the package.
23  * 
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions are
26  * met: 1. Redistributions of source code must retain the copyright notice,
27  * this list of conditions and the following disclaimer. 2. Redistributions
28  * in binary form must reproduce the above copyright notice, this list of
29  * conditions and the following disclaimer in the documentation and/or other
30  * materials provided with the distribution. 3. All advertising materials
31  * mentioning features or use of this software must display the following
32  * acknowledgement: "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)" The word 'cryptographic' can be left out
34  * if the rouines from the library being used are not cryptographic related
35  * :-). 4. If you include any Windows specific code (or a derivative thereof)
36  * from the apps directory (application code) you must include an
37  * acknowledgement: "This product includes software written by Tim Hudson
38  * (tjh@cryptsoft.com)"
39  * 
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND ANY EXPRESS OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
43  * NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
44  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * 
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply
54  * be copied and put under another distribution licence [including the GNU
55  * Public Licence.]
56  */
57
58 #include <atf-c.h>
59 #include <des.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 #define crypt(c,s) (des_crypt((c),(s)))
66
67 /* tisk tisk - the test keys don't all have odd parity :-( */
68 /* test data */
69 #define NUM_TESTS 34
70 static unsigned char key_data[NUM_TESTS][8] = {
71         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
72         {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
73         {0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
74         {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
75         {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
76         {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
77         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
78         {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10},
79         {0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57},
80         {0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E},
81         {0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86},
82         {0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E},
83         {0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6},
84         {0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE},
85         {0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6},
86         {0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE},
87         {0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16},
88         {0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F},
89         {0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46},
90         {0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E},
91         {0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76},
92         {0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07},
93         {0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F},
94         {0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7},
95         {0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF},
96         {0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6},
97         {0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF},
98         {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
99         {0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E},
100         {0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE},
101         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
102         {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
103         {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
104         {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}
105 };
106
107 static unsigned char plain_data[NUM_TESTS][8] = {
108         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
109         {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
110         {0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
111         {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
112         {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
113         {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
114         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
115         {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
116         {0x01, 0xA1, 0xD6, 0xD0, 0x39, 0x77, 0x67, 0x42},
117         {0x5C, 0xD5, 0x4C, 0xA8, 0x3D, 0xEF, 0x57, 0xDA},
118         {0x02, 0x48, 0xD4, 0x38, 0x06, 0xF6, 0x71, 0x72},
119         {0x51, 0x45, 0x4B, 0x58, 0x2D, 0xDF, 0x44, 0x0A},
120         {0x42, 0xFD, 0x44, 0x30, 0x59, 0x57, 0x7F, 0xA2},
121         {0x05, 0x9B, 0x5E, 0x08, 0x51, 0xCF, 0x14, 0x3A},
122         {0x07, 0x56, 0xD8, 0xE0, 0x77, 0x47, 0x61, 0xD2},
123         {0x76, 0x25, 0x14, 0xB8, 0x29, 0xBF, 0x48, 0x6A},
124         {0x3B, 0xDD, 0x11, 0x90, 0x49, 0x37, 0x28, 0x02},
125         {0x26, 0x95, 0x5F, 0x68, 0x35, 0xAF, 0x60, 0x9A},
126         {0x16, 0x4D, 0x5E, 0x40, 0x4F, 0x27, 0x52, 0x32},
127         {0x6B, 0x05, 0x6E, 0x18, 0x75, 0x9F, 0x5C, 0xCA},
128         {0x00, 0x4B, 0xD6, 0xEF, 0x09, 0x17, 0x60, 0x62},
129         {0x48, 0x0D, 0x39, 0x00, 0x6E, 0xE7, 0x62, 0xF2},
130         {0x43, 0x75, 0x40, 0xC8, 0x69, 0x8F, 0x3C, 0xFA},
131         {0x07, 0x2D, 0x43, 0xA0, 0x77, 0x07, 0x52, 0x92},
132         {0x02, 0xFE, 0x55, 0x77, 0x81, 0x17, 0xF1, 0x2A},
133         {0x1D, 0x9D, 0x5C, 0x50, 0x18, 0xF7, 0x28, 0xC2},
134         {0x30, 0x55, 0x32, 0x28, 0x6D, 0x6F, 0x29, 0x5A},
135         {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
136         {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
137         {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
138         {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
139         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
140         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
141         {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
142 };
143
144 static unsigned char cipher_data[NUM_TESTS][8] = {
145         {0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7},
146         {0x73, 0x59, 0xB2, 0x16, 0x3E, 0x4E, 0xDC, 0x58},
147         {0x95, 0x8E, 0x6E, 0x62, 0x7A, 0x05, 0x55, 0x7B},
148         {0xF4, 0x03, 0x79, 0xAB, 0x9E, 0x0E, 0xC5, 0x33},
149         {0x17, 0x66, 0x8D, 0xFC, 0x72, 0x92, 0x53, 0x2D},
150         {0x8A, 0x5A, 0xE1, 0xF8, 0x1A, 0xB8, 0xF2, 0xDD},
151         {0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7},
152         {0xED, 0x39, 0xD9, 0x50, 0xFA, 0x74, 0xBC, 0xC4},
153         {0x69, 0x0F, 0x5B, 0x0D, 0x9A, 0x26, 0x93, 0x9B},
154         {0x7A, 0x38, 0x9D, 0x10, 0x35, 0x4B, 0xD2, 0x71},
155         {0x86, 0x8E, 0xBB, 0x51, 0xCA, 0xB4, 0x59, 0x9A},
156         {0x71, 0x78, 0x87, 0x6E, 0x01, 0xF1, 0x9B, 0x2A},
157         {0xAF, 0x37, 0xFB, 0x42, 0x1F, 0x8C, 0x40, 0x95},
158         {0x86, 0xA5, 0x60, 0xF1, 0x0E, 0xC6, 0xD8, 0x5B},
159         {0x0C, 0xD3, 0xDA, 0x02, 0x00, 0x21, 0xDC, 0x09},
160         {0xEA, 0x67, 0x6B, 0x2C, 0xB7, 0xDB, 0x2B, 0x7A},
161         {0xDF, 0xD6, 0x4A, 0x81, 0x5C, 0xAF, 0x1A, 0x0F},
162         {0x5C, 0x51, 0x3C, 0x9C, 0x48, 0x86, 0xC0, 0x88},
163         {0x0A, 0x2A, 0xEE, 0xAE, 0x3F, 0xF4, 0xAB, 0x77},
164         {0xEF, 0x1B, 0xF0, 0x3E, 0x5D, 0xFA, 0x57, 0x5A},
165         {0x88, 0xBF, 0x0D, 0xB6, 0xD7, 0x0D, 0xEE, 0x56},
166         {0xA1, 0xF9, 0x91, 0x55, 0x41, 0x02, 0x0B, 0x56},
167         {0x6F, 0xBF, 0x1C, 0xAF, 0xCF, 0xFD, 0x05, 0x56},
168         {0x2F, 0x22, 0xE4, 0x9B, 0xAB, 0x7C, 0xA1, 0xAC},
169         {0x5A, 0x6B, 0x61, 0x2C, 0xC2, 0x6C, 0xCE, 0x4A},
170         {0x5F, 0x4C, 0x03, 0x8E, 0xD1, 0x2B, 0x2E, 0x41},
171         {0x63, 0xFA, 0xC0, 0xD0, 0x34, 0xD9, 0xF7, 0x93},
172         {0x61, 0x7B, 0x3A, 0x0C, 0xE8, 0xF0, 0x71, 0x00},
173         {0xDB, 0x95, 0x86, 0x05, 0xF8, 0xC8, 0xC6, 0x06},
174         {0xED, 0xBF, 0xD1, 0xC6, 0x6C, 0x29, 0xCC, 0xC7},
175         {0x35, 0x55, 0x50, 0xB2, 0x15, 0x0E, 0x24, 0x51},
176         {0xCA, 0xAA, 0xAF, 0x4D, 0xEA, 0xF1, 0xDB, 0xAE},
177         {0xD5, 0xD4, 0x4F, 0xF7, 0x20, 0x68, 0x3D, 0x0D},
178         {0x2A, 0x2B, 0xB0, 0x08, 0xDF, 0x97, 0xC2, 0xF2}
179 };
180
181 static unsigned char cipher_ecb2[NUM_TESTS - 1][8] = {
182         {0x92, 0x95, 0xB5, 0x9B, 0xB3, 0x84, 0x73, 0x6E},
183         {0x19, 0x9E, 0x9D, 0x6D, 0xF3, 0x9A, 0xA8, 0x16},
184         {0x2A, 0x4B, 0x4D, 0x24, 0x52, 0x43, 0x84, 0x27},
185         {0x35, 0x84, 0x3C, 0x01, 0x9D, 0x18, 0xC5, 0xB6},
186         {0x4A, 0x5B, 0x2F, 0x42, 0xAA, 0x77, 0x19, 0x25},
187         {0xA0, 0x6B, 0xA9, 0xB8, 0xCA, 0x5B, 0x17, 0x8A},
188         {0xAB, 0x9D, 0xB7, 0xFB, 0xED, 0x95, 0xF2, 0x74},
189         {0x3D, 0x25, 0x6C, 0x23, 0xA7, 0x25, 0x2F, 0xD6},
190         {0xB7, 0x6F, 0xAB, 0x4F, 0xBD, 0xBD, 0xB7, 0x67},
191         {0x8F, 0x68, 0x27, 0xD6, 0x9C, 0xF4, 0x1A, 0x10},
192         {0x82, 0x57, 0xA1, 0xD6, 0x50, 0x5E, 0x81, 0x85},
193         {0xA2, 0x0F, 0x0A, 0xCD, 0x80, 0x89, 0x7D, 0xFA},
194         {0xCD, 0x2A, 0x53, 0x3A, 0xDB, 0x0D, 0x7E, 0xF3},
195         {0xD2, 0xC2, 0xBE, 0x27, 0xE8, 0x1B, 0x68, 0xE3},
196         {0xE9, 0x24, 0xCF, 0x4F, 0x89, 0x3C, 0x5B, 0x0A},
197         {0xA7, 0x18, 0xC3, 0x9F, 0xFA, 0x9F, 0xD7, 0x69},
198         {0x77, 0x2C, 0x79, 0xB1, 0xD2, 0x31, 0x7E, 0xB1},
199         {0x49, 0xAB, 0x92, 0x7F, 0xD0, 0x22, 0x00, 0xB7},
200         {0xCE, 0x1C, 0x6C, 0x7D, 0x85, 0xE3, 0x4A, 0x6F},
201         {0xBE, 0x91, 0xD6, 0xE1, 0x27, 0xB2, 0xE9, 0x87},
202         {0x70, 0x28, 0xAE, 0x8F, 0xD1, 0xF5, 0x74, 0x1A},
203         {0xAA, 0x37, 0x80, 0xBB, 0xF3, 0x22, 0x1D, 0xDE},
204         {0xA6, 0xC4, 0xD2, 0x5E, 0x28, 0x93, 0xAC, 0xB3},
205         {0x22, 0x07, 0x81, 0x5A, 0xE4, 0xB7, 0x1A, 0xAD},
206         {0xDC, 0xCE, 0x05, 0xE7, 0x07, 0xBD, 0xF5, 0x84},
207         {0x26, 0x1D, 0x39, 0x2C, 0xB3, 0xBA, 0xA5, 0x85},
208         {0xB4, 0xF7, 0x0F, 0x72, 0xFB, 0x04, 0xF0, 0xDC},
209         {0x95, 0xBA, 0xA9, 0x4E, 0x87, 0x36, 0xF2, 0x89},
210         {0xD4, 0x07, 0x3A, 0xF1, 0x5A, 0x17, 0x82, 0x0E},
211         {0xEF, 0x6F, 0xAF, 0xA7, 0x66, 0x1A, 0x7E, 0x89},
212         {0xC1, 0x97, 0xF5, 0x58, 0x74, 0x8A, 0x20, 0xE7},
213         {0x43, 0x34, 0xCF, 0xDA, 0x22, 0xC4, 0x86, 0xC8},
214         {0x08, 0xD7, 0xB4, 0xFB, 0x62, 0x9D, 0x08, 0x85}
215 };
216
217 static unsigned char cbc_key[8] = {
218         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
219 };
220 static unsigned char cbc2_key[8] = {
221         0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,
222 };
223 static unsigned char cbc3_key[8] = {
224         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
225 };
226 static unsigned char cbc_iv[8] = {
227         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
228 };
229 /*
230  * Changed the following text constant to binary so it will work on ebcdic
231  * machines :-)
232  */
233 /* static char cbc_data[40]="7654321 Now is the time for \0001"; */
234 static unsigned char cbc_data[40] = {
235         0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
236         0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
237         0x68, 0x65, 0x20, 0x74, 0x69, 0x6D, 0x65, 0x20,
238         0x66, 0x6F, 0x72, 0x20, 0x00, 0x31, 0x00, 0x00,
239         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240 };
241
242 static unsigned char cbc_ok[32] = {
243         0xcc, 0xd1, 0x73, 0xff, 0xab, 0x20, 0x39, 0xf4,
244         0xac, 0xd8, 0xae, 0xfd, 0xdf, 0xd8, 0xa1, 0xeb,
245         0x46, 0x8e, 0x91, 0x15, 0x78, 0x88, 0xba, 0x68,
246         0x1d, 0x26, 0x93, 0x97, 0xf7, 0xfe, 0x62, 0xb4,
247 };
248
249 #ifdef SCREW_THE_PARITY
250 #error "SCREW_THE_PARITY is not ment to be defined."
251 #error "Original vectors are preserved for reference only."
252 static unsigned char cbc2_key[8] = {
253         0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87,
254 };
255 static unsigned char xcbc_ok[32] = {
256         0x86, 0x74, 0x81, 0x0D, 0x61, 0xA4, 0xA5, 0x48,
257         0xB9, 0x93, 0x03, 0xE1, 0xB8, 0xBB, 0xBD, 0xBD,
258         0x64, 0x30, 0x0B, 0xB9, 0x06, 0x65, 0x81, 0x76,
259         0x04, 0x1D, 0x77, 0x62, 0x17, 0xCA, 0x2B, 0xD2,
260 };
261 #else
262 static unsigned char xcbc_ok[32] = {
263         0x84, 0x6B, 0x29, 0x14, 0x85, 0x1E, 0x9A, 0x29,
264         0x54, 0x73, 0x2F, 0x8A, 0xA0, 0xA6, 0x11, 0xC1,
265         0x15, 0xCD, 0xC2, 0xD7, 0x95, 0x1B, 0x10, 0x53,
266         0xA6, 0x3C, 0x5E, 0x03, 0xB2, 0x1A, 0xA3, 0xC4,
267 };
268 #endif
269
270 static unsigned char cbc3_ok[32] = {
271         0x3F, 0xE3, 0x01, 0xC9, 0x62, 0xAC, 0x01, 0xD0,
272         0x22, 0x13, 0x76, 0x3C, 0x1C, 0xBD, 0x4C, 0xDC,
273         0x79, 0x96, 0x57, 0xC0, 0x64, 0xEC, 0xF5, 0xD4,
274         0x1C, 0x67, 0x38, 0x12, 0xCF, 0xDE, 0x96, 0x75
275 };
276
277 static unsigned char pcbc_ok[32] = {
278         0xcc, 0xd1, 0x73, 0xff, 0xab, 0x20, 0x39, 0xf4,
279         0x6d, 0xec, 0xb4, 0x70, 0xa0, 0xe5, 0x6b, 0x15,
280         0xae, 0xa6, 0xbf, 0x61, 0xed, 0x7d, 0x9c, 0x9f,
281         0xf7, 0x17, 0x46, 0x3b, 0x8a, 0xb3, 0xcc, 0x88
282 };
283
284 static unsigned char cfb_key[8] = {
285         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
286 };
287 static unsigned char cfb_iv[8] = {
288         0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
289 };
290 static unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8];
291 static unsigned char plain[24] =
292 {
293         0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73,
294         0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
295         0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f,
296         0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
297 };
298 static unsigned char cfb_cipher8[24] = {
299         0xf3, 0x1f, 0xda, 0x07, 0x01, 0x14,
300         0x62, 0xee, 0x18, 0x7f, 0x43, 0xd8,
301         0x0a, 0x7c, 0xd9, 0xb5, 0xb0, 0xd2,
302         0x90, 0xda, 0x6e, 0x5b, 0x9a, 0x87,
303 };
304 static unsigned char cfb_cipher16[24] = {
305         0xF3, 0x09, 0x87, 0x87, 0x7F, 0x57,
306         0xF7, 0x3C, 0x36, 0xB6, 0xDB, 0x70,
307         0xD8, 0xD5, 0x34, 0x19, 0xD3, 0x86,
308         0xB2, 0x23, 0xB7, 0xB2, 0xAD, 0x1B,
309 };
310 static unsigned char cfb_cipher32[24] = {
311         0xF3, 0x09, 0x62, 0x49, 0xA4, 0xDF,
312         0xA4, 0x9F, 0x33, 0xDC, 0x7B, 0xAD,
313         0x4C, 0xC8, 0x9F, 0x64, 0xE4, 0x53,
314         0xE5, 0xEC, 0x67, 0x20, 0xDA, 0xB6,
315 };
316 static unsigned char cfb_cipher48[24] = {
317         0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4,
318         0x30, 0xB5, 0x15, 0xEC, 0xBB, 0x85,
319         0x97, 0x5A, 0x13, 0x8C, 0x68, 0x60,
320         0xE2, 0x38, 0x34, 0x3C, 0xDC, 0x1F,
321 };
322 static unsigned char cfb_cipher64[24] = {
323         0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4,
324         0x6E, 0x51, 0xA6, 0x9E, 0x83, 0x9B,
325         0x1A, 0x92, 0xF7, 0x84, 0x03, 0x46,
326         0x71, 0x33, 0x89, 0x8E, 0xA6, 0x22,
327 };
328
329 static unsigned char ofb_key[8] = {
330         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
331 };
332 static unsigned char ofb_iv[8] = {
333         0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
334 };
335 static unsigned char ofb_buf1[24], ofb_buf2[24], ofb_tmp[8];
336 static unsigned char ofb_cipher[24] =
337 {
338         0xf3, 0x09, 0x62, 0x49, 0xc7, 0xf4, 0x6e, 0x51,
339         0x35, 0xf2, 0x4a, 0x24, 0x2e, 0xeb, 0x3d, 0x3f,
340         0x3d, 0x6d, 0x5b, 0xe3, 0x25, 0x5a, 0xf8, 0xc3
341 };
342
343 static DES_LONG cbc_cksum_ret = 0xB462FEF7L;
344 static unsigned char cbc_cksum_data[8] = {
345         0x1D, 0x26, 0x93, 0x97, 0xf7, 0xfe, 0x62, 0xb4,
346 };
347
348 static char *
349 pt(unsigned char *p)
350 {
351         static char bufs[10][20];
352         static int bnum = 0;
353         char *ret;
354         int i;
355         static const char *f = "0123456789ABCDEF";
356
357         ret = &(bufs[bnum++][0]);
358         bnum %= 10;
359         for (i = 0; i < 8; i++) {
360                 ret[i * 2] = f[(p[i] >> 4) & 0xf];
361                 ret[i * 2 + 1] = f[p[i] & 0xf];
362         }
363         ret[16] = '\0';
364         return (ret);
365 }
366
367 static void
368 fail_cfb_buf(const char *msg, unsigned char *ptr)
369 {
370         char buf[1024];
371         int i;
372
373         *buf = '\0';
374         for (i = 0; i < 24; i += 8) {
375                 char buf2[128];
376                 snprintf(buf2, sizeof(buf2), "%s /", pt(&(cfb_buf1[i])));
377                 strlcat(buf, buf2, sizeof(buf));
378         }
379
380         atf_tc_fail_nonfatal("%s: %s", msg, buf);
381 }
382
383 #if !defined(LIBDES_LIT)
384 static void
385 cfb_test(int bits, unsigned char *cfb_cipher)
386 {
387         des_key_schedule ks;
388
389         des_set_key_checked(&cfb_key, ks);
390         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
391         des_cfb_encrypt(plain, cfb_buf1, bits, sizeof(plain), ks, &cfb_tmp,
392                         DES_ENCRYPT);
393         if (memcmp(cfb_cipher, cfb_buf1, sizeof(plain)) != 0)
394                 fail_cfb_buf("cfb_encrypt encrypt error", cfb_buf1);
395         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
396         des_cfb_encrypt(cfb_buf1, cfb_buf2, bits, sizeof(plain), ks, &cfb_tmp,
397                         DES_DECRYPT);
398         if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0)
399                 fail_cfb_buf("cfb_encrypt decrypt error", cfb_buf2);
400 }
401 #endif /* !defined(LIBDES_LIT) */
402
403 #if !defined(LIBDES_LIT)
404 static void
405 cfb64_test(unsigned char *cfb_cipher)
406 {
407         int n;
408         des_key_schedule ks;
409
410         des_set_key_checked(&cfb_key, ks);
411         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
412         n = 0;
413         des_cfb64_encrypt(plain, cfb_buf1, 12, ks, &cfb_tmp, &n, DES_ENCRYPT);
414         des_cfb64_encrypt(&(plain[12]), &(cfb_buf1[12]), sizeof(plain) - 12, ks,
415                           &cfb_tmp, &n, DES_ENCRYPT);
416         if (memcmp(cfb_cipher, cfb_buf1, sizeof(plain)) != 0)
417                 fail_cfb_buf("cfb_encrypt encrypt error", cfb_buf1);
418         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
419         n = 0;
420         des_cfb64_encrypt(cfb_buf1, cfb_buf2, 17, ks, &cfb_tmp, &n, DES_DECRYPT);
421         des_cfb64_encrypt(&(cfb_buf1[17]), &(cfb_buf2[17]),
422                           sizeof(plain) - 17, ks, &cfb_tmp, &n, DES_DECRYPT);
423         if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0)
424                 fail_cfb_buf("cfb_encrypt decrypt error", cfb_buf2);
425 }
426 #endif /* !defined(LIBDES_LIT) */
427
428 #if !defined(LIBDES_LIT)
429 static void
430 ede_cfb64_test(unsigned char *cfb_cipher)
431 {
432         int n;
433         des_key_schedule ks;
434
435         des_set_key_checked(&cfb_key, ks);
436         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
437         n = 0;
438         des_ede3_cfb64_encrypt(plain, cfb_buf1, 12, ks, ks, ks, &cfb_tmp, &n,
439                                DES_ENCRYPT);
440         des_ede3_cfb64_encrypt(&(plain[12]), &(cfb_buf1[12]),
441                                sizeof(plain) - 12, ks, ks, ks,
442                                &cfb_tmp, &n, DES_ENCRYPT);
443         if (memcmp(cfb_cipher, cfb_buf1, sizeof(plain)) != 0)
444                 fail_cfb_buf("ede_cfb_encrypt encrypt error", cfb_buf1);
445         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
446         n = 0;
447         des_ede3_cfb64_encrypt(cfb_buf1, cfb_buf2, (long) 17, ks, ks, ks,
448                                &cfb_tmp, &n, DES_DECRYPT);
449         des_ede3_cfb64_encrypt(&(cfb_buf1[17]), &(cfb_buf2[17]),
450                                sizeof(plain) - 17, ks, ks, ks,
451                                &cfb_tmp, &n, DES_DECRYPT);
452         if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0)
453                 fail_cfb_buf("ede_cfb_encrypt decrypt error", cfb_buf2);
454 }
455 #endif /* !defined(LIBDES_LIT) */
456
457 ATF_TC_WITHOUT_HEAD(cbcm);
458 #if defined(NO_DESCBCM)
459 ATF_TC_BODY(cbcm, tc)
460 {
461         atf_tc_skip("Test program built with NO_DESCBCM");
462 }
463 #else /* defined(NO_DESCBM) */
464 ATF_TC_BODY(cbcm, tc)
465 {
466         int i, j;
467         des_cblock iv3, iv2;
468         unsigned char cbc_in[40], cbc_out[40];
469         des_key_schedule ks, ks2, ks3;
470
471         if ((j = des_set_key_checked(&cbc_key, ks)) != 0) {
472                 atf_tc_fail_nonfatal("Key error %d\n", j);
473         }
474         if ((j = des_set_key_checked(&cbc2_key, ks2)) != 0) {
475                 atf_tc_fail_nonfatal("Key error %d\n", j);
476         }
477         if ((j = des_set_key_checked(&cbc3_key, ks3)) != 0) {
478                 atf_tc_fail_nonfatal("Key error %d\n", j);
479         }
480         memset(cbc_out, 0, 40);
481         memset(cbc_in, 0, 40);
482         i = strlen((char *) cbc_data) + 1;
483         /* i=((i+7)/8)*8; */
484         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
485         memset(iv2, '\0', sizeof iv2);
486
487         des_ede3_cbcm_encrypt(cbc_data, cbc_out, 16L, ks, ks2, ks3, &iv3, &iv2,
488                               DES_ENCRYPT);
489         des_ede3_cbcm_encrypt(&cbc_data[16], &cbc_out[16], i - 16, ks, ks2, ks3,
490                               &iv3, &iv2, DES_ENCRYPT);
491         /*
492          * if (memcmp(cbc_out,cbc3_ok, (unsigned int)(strlen((char
493          * *)cbc_data)+1+7)/8*8) != 0) { printf("des_ede3_cbc_encrypt encrypt
494          * error\n"); err=1; }
495          */
496         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
497         memset(iv2, '\0', sizeof iv2);
498         des_ede3_cbcm_encrypt(cbc_out, cbc_in, i, ks, ks2, ks3, &iv3, &iv2,
499                               DES_DECRYPT);
500         if (memcmp(cbc_in, cbc_data, strlen((char *) cbc_data) + 1) != 0) {
501                 char buf[1024];
502                 int n;
503
504                 *buf = '\0';
505                 for (n = 0; n < i; ++n) {
506                         char buf2[16];
507                         snprintf(buf2, sizeof(buf2), " %02x", cbc_data[n]);
508                         strlcat(buf, buf2, sizeof(buf));
509                 }
510                 strlcat(buf, ", ", sizeof(buf));
511                 for (n = 0; n < i; ++n) {
512                         char buf2[16];
513                         snprintf(buf2, sizeof(buf2), " %02x", cbc_in[n]);
514                         strlcat(buf, buf2, sizeof(buf));
515                 }
516
517                 atf_tc_fail_nonfatal("des_ede3_cbcm_encrypt decrypt error: %s",
518                                      buf);
519         }
520 }
521 #endif /* defined(NO_DESCBM) */
522
523 ATF_TC_WITHOUT_HEAD(ecb);
524 ATF_TC_BODY(ecb, tc)
525 {
526         int i;
527         des_cblock in, out, outin;
528         des_key_schedule ks;
529
530         for (i = 0; i < NUM_TESTS; i++) {
531                 des_set_key_unchecked(&key_data[i], ks);
532                 memcpy(in, plain_data[i], 8);
533                 memset(out, 0, 8);
534                 memset(outin, 0, 8);
535                 des_ecb_encrypt(&in, &out, ks, DES_ENCRYPT);
536                 des_ecb_encrypt(&out, &outin, ks, DES_DECRYPT);
537
538                 if (memcmp(out, cipher_data[i], 8) != 0) {
539                         atf_tc_fail_nonfatal("Encryption error %2d\nk=%s p=%s "
540                                              "o=%s act=%s\n", i + 1,
541                                              pt(key_data[i]), pt(in),
542                                              pt(cipher_data[i]), pt(out));
543                 }
544                 if (memcmp(in, outin, 8) != 0) {
545                         atf_tc_fail_nonfatal("Decryption error %2d\nk=%s p=%s "
546                                              "o=%s act=%s\n", i + 1,
547                                              pt(key_data[i]), pt(out), pt(in),
548                                              pt(outin));
549                 }
550         }
551 }
552
553 ATF_TC_WITHOUT_HEAD(ede_ecb);
554 #if defined(LIBDES_LIT)
555 ATF_TC_BODY(ede_ecb, tc)
556 {
557         atf_tc_skip("Test program built with LIBDES_LIT");
558 }
559 #else /* defined(LIBDES_LIT) */
560 ATF_TC_BODY(ede_ecb, tc)
561 {
562         int i;
563         des_cblock in, out, outin;
564         des_key_schedule ks, ks2, ks3;
565
566         for (i = 0; i < (NUM_TESTS - 1); i++) {
567                 des_set_key_unchecked(&key_data[i], ks);
568                 des_set_key_unchecked(&key_data[i + 1], ks2);
569                 des_set_key_unchecked(&key_data[i + 2], ks3);
570                 memcpy(in, plain_data[i], 8);
571                 memset(out, 0, 8);
572                 memset(outin, 0, 8);
573                 des_ecb2_encrypt(&in, &out, ks, ks2, DES_ENCRYPT);
574                 des_ecb2_encrypt(&out, &outin, ks, ks2, DES_DECRYPT);
575
576                 if (memcmp(out, cipher_ecb2[i], 8) != 0) {
577                         atf_tc_fail_nonfatal("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
578                          i + 1, pt(key_data[i]), pt(in), pt(cipher_ecb2[i]),
579                                pt(out));
580                 }
581                 if (memcmp(in, outin, 8) != 0) {
582                         atf_tc_fail_nonfatal("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
583                         i + 1, pt(key_data[i]), pt(out), pt(in), pt(outin));
584                 }
585         }
586 }
587 #endif /* defined(LIBDES_LIT) */
588
589 ATF_TC_WITHOUT_HEAD(cbc);
590 ATF_TC_BODY(cbc, tc)
591 {
592         int j;
593         des_cblock iv3;
594         des_key_schedule ks;
595         unsigned char cbc_in[40], cbc_out[40];
596
597         if ((j = des_set_key_checked(&cbc_key, ks)) != 0)
598                 atf_tc_fail_nonfatal("Key error %d\n", j);
599         memset(cbc_out, 0, 40);
600         memset(cbc_in, 0, 40);
601         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
602         des_ncbc_encrypt(cbc_data, cbc_out, strlen((char *) cbc_data) + 1, ks,
603                          &iv3, DES_ENCRYPT);
604         if (memcmp(cbc_out, cbc_ok, 32) != 0)
605                 atf_tc_fail_nonfatal("cbc_encrypt encrypt error\n");
606         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
607         des_ncbc_encrypt(cbc_out, cbc_in, strlen((char *) cbc_data) + 1, ks,
608                          &iv3, DES_DECRYPT);
609         if (memcmp(cbc_in, cbc_data, strlen((char *) cbc_data)) != 0)
610                 atf_tc_fail_nonfatal("cbc_encrypt decrypt error\n");
611 }
612
613 ATF_TC_WITHOUT_HEAD(desx_cbc);
614 #if defined(LIBDES_LIT)
615 ATF_TC_BODY(desx_cbc, tc)
616 {
617         atf_tc_skip("Test program built with LIBDES_LIT");
618 }
619 #else /* defined(LIBDES_LIT) */
620 ATF_TC_BODY(desx_cbc, tc)
621 {
622         int j;
623         des_cblock iv3;
624         des_key_schedule ks;
625         unsigned char cbc_in[40], cbc_out[40];
626
627         if ((j = des_set_key_checked(&cbc_key, ks)) != 0) {
628                 atf_tc_fail_nonfatal("Key error %d\n", j);
629         }
630         memset(cbc_out, 0, 40);
631         memset(cbc_in, 0, 40);
632         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
633         des_xcbc_encrypt(cbc_data, cbc_out, strlen((char *) cbc_data) + 1, ks,
634                          &iv3, &cbc2_key, &cbc3_key, DES_ENCRYPT);
635         if (memcmp(cbc_out, xcbc_ok, 32) != 0) {
636                 atf_tc_fail_nonfatal("des_xcbc_encrypt encrypt error\n");
637         }
638         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
639         des_xcbc_encrypt(cbc_out, cbc_in, strlen((char *) cbc_data) + 1, ks,
640                          &iv3, &cbc2_key, &cbc3_key, DES_DECRYPT);
641         if (memcmp(cbc_in, cbc_data, strlen((char *) cbc_data) + 1) != 0) {
642                 atf_tc_fail_nonfatal("des_xcbc_encrypt decrypt error\n");
643         }
644 }
645 #endif /* defined(LIBDES_LIT) */
646
647 ATF_TC_WITHOUT_HEAD(ede_cbc);
648 ATF_TC_BODY(ede_cbc, tc)
649 {
650         int i, j;
651         des_cblock iv3;
652         des_key_schedule ks, ks2, ks3;
653         unsigned char cbc_in[40], cbc_out[40];
654
655         if ((j = des_set_key_checked(&cbc_key, ks)) != 0)
656                 atf_tc_fail_nonfatal("Key error %d\n", j);
657         if ((j = des_set_key_checked(&cbc2_key, ks2)) != 0)
658                 atf_tc_fail_nonfatal("Key error %d\n", j);
659         if ((j = des_set_key_checked(&cbc3_key, ks3)) != 0)
660                 atf_tc_fail_nonfatal("Key error %d\n", j);
661         memset(cbc_out, 0, 40);
662         memset(cbc_in, 0, 40);
663         i = strlen((char *) cbc_data) + 1;
664         /* i=((i+7)/8)*8; */
665         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
666
667         des_ede3_cbc_encrypt(cbc_data, cbc_out, 16L, ks, ks2, ks3, &iv3,
668                              DES_ENCRYPT);
669         des_ede3_cbc_encrypt(&(cbc_data[16]), &(cbc_out[16]), i - 16, ks, ks2,
670                              ks3, &iv3, DES_ENCRYPT);
671         if (memcmp(cbc_out, cbc3_ok, (unsigned int)
672                    (strlen((char *) cbc_data) + 1 + 7) / 8 * 8) != 0)
673                 atf_tc_fail_nonfatal("des_ede3_cbc_encrypt encrypt error\n");
674         memcpy(iv3, cbc_iv, sizeof(cbc_iv));
675         des_ede3_cbc_encrypt(cbc_out, cbc_in, i, ks, ks2, ks3, &iv3,
676                              DES_DECRYPT);
677         if (memcmp(cbc_in, cbc_data, strlen((char *) cbc_data) + 1) != 0)
678                 atf_tc_fail_nonfatal("des_ede3_cbc_encrypt decrypt error\n");
679 }
680
681 ATF_TC_WITHOUT_HEAD(pcbc);
682 #if defined(LIBDES_LIT)
683 ATF_TC_BODY(pcbc, tc)
684 {
685         atf_tc_skip("Test program built with LIBDES_LIT");
686 }
687 #else /* defined(LIBDES_LIT) */
688 ATF_TC_BODY(pcbc, tc)
689 {
690         int j;
691         unsigned char cbc_in[40], cbc_out[40];
692         des_key_schedule ks;
693
694         if ((j = des_set_key_checked(&cbc_key, ks)) != 0) {
695                 atf_tc_fail_nonfatal("Key error %d\n", j);
696         }
697         memset(cbc_out, 0, 40);
698         memset(cbc_in, 0, 40);
699         des_pcbc_encrypt(cbc_data, cbc_out, strlen((char *) cbc_data) + 1, ks,
700                          &cbc_iv, DES_ENCRYPT);
701         if (memcmp(cbc_out, pcbc_ok, 32) != 0) {
702                 atf_tc_fail_nonfatal("pcbc_encrypt encrypt error\n");
703         }
704         des_pcbc_encrypt(cbc_out, cbc_in, strlen((char *) cbc_data) + 1, ks, &cbc_iv,
705                          DES_DECRYPT);
706         if (memcmp(cbc_in, cbc_data, strlen((char *) cbc_data) + 1) != 0) {
707                 atf_tc_fail_nonfatal("pcbc_encrypt decrypt error\n");
708         }
709 }
710 #endif /* defined(LIBDES_LIT) */
711
712 ATF_TC_WITHOUT_HEAD(cfb);
713 #if defined(LIBDES_LIT)
714 ATF_TC_BODY(cfb, tc)
715 {
716         atf_tc_skip("Test program built with LIBDES_LIT");
717 }
718 #else /* defined(LIBDES_LIT) */
719 ATF_TC_BODY(cfb, tc)
720 {
721         size_t i;
722         des_key_schedule ks;
723
724         printf("cfb8\n");
725         cfb_test(8, cfb_cipher8);
726         printf("cfb16\n");
727         cfb_test(16, cfb_cipher16);
728         printf("cfb32\n");
729         cfb_test(32, cfb_cipher32);
730         printf("cfb48\n");
731         cfb_test(48, cfb_cipher48);
732         printf("cfb64\n");
733         cfb_test(64, cfb_cipher64);
734
735         printf("cfb64()\n");
736         cfb64_test(cfb_cipher64);
737
738         des_set_key_checked(&cfb_key, ks);
739         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
740         for (i = 0; i < sizeof(plain); i++)
741                 des_cfb_encrypt(&(plain[i]), &(cfb_buf1[i]),
742                                 8, 1, ks, &cfb_tmp, DES_ENCRYPT);
743         if (memcmp(cfb_cipher8, cfb_buf1, sizeof(plain)) != 0)
744                 atf_tc_fail_nonfatal("cfb_encrypt small encrypt error\n");
745         memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
746         for (i = 0; i < sizeof(plain); i++)
747                 des_cfb_encrypt(&(cfb_buf1[i]), &(cfb_buf2[i]),
748                                 8, 1, ks, &cfb_tmp, DES_DECRYPT);
749         if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0)
750                 atf_tc_fail_nonfatal("cfb_encrypt small decrypt error\n");
751         printf("ede_cfb64()\n");
752         ede_cfb64_test(cfb_cipher64);
753 }
754 #endif /* defined(LIBDES_LIT) */
755
756 ATF_TC_WITHOUT_HEAD(ofb);
757 #if defined(LIBDES_LIT)
758 ATF_TC_BODY(ofb, tc)
759 {
760         atf_tc_skip("Test program built with LIBDES_LIT");
761 }
762 #else /* defined(LIBDES_LIT) */
763 ATF_TC_BODY(ofb, tc)
764 {
765         des_key_schedule ks;
766
767         des_set_key_checked(&ofb_key, ks);
768         memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
769         des_ofb_encrypt(plain, ofb_buf1, 64, sizeof(plain) / 8, ks, &ofb_tmp);
770         if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
771                 atf_tc_fail_nonfatal("ofb_encrypt encrypt error: "
772                                      "%02X %02X %02X %02X %02X %02X %02X %02X, "
773                                      "%02X %02X %02X %02X %02X %02X %02X %02X",
774                                      ofb_buf1[8 + 0], ofb_buf1[8 + 1],
775                                      ofb_buf1[8 + 2], ofb_buf1[8 + 3],
776                                      ofb_buf1[8 + 4], ofb_buf1[8 + 5],
777                                      ofb_buf1[8 + 6], ofb_buf1[8 + 7],
778                                      ofb_buf1[8 + 0], ofb_cipher[8 + 1],
779                                      ofb_cipher[8 + 2], ofb_cipher[8 + 3],
780                                      ofb_buf1[8 + 4], ofb_cipher[8 + 5],
781                                      ofb_cipher[8 + 6], ofb_cipher[8 + 7]);
782         }
783         memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
784         des_ofb_encrypt(ofb_buf1, ofb_buf2, 64, sizeof(ofb_buf1) / 8, ks,
785                         &ofb_tmp);
786         if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
787                 atf_tc_fail_nonfatal("ofb_encrypt decrypt error: "
788                                      "%02X %02X %02X %02X %02X %02X %02X %02X, "
789                                      "%02X %02X %02X %02X %02X %02X %02X %02X",
790                                      ofb_buf2[8 + 0], ofb_buf2[8 + 1],
791                                      ofb_buf2[8 + 2], ofb_buf2[8 + 3],
792                                      ofb_buf2[8 + 4], ofb_buf2[8 + 5],
793                                      ofb_buf2[8 + 6], ofb_buf2[8 + 7],
794                                      plain[8 + 0], plain[8 + 1],
795                                      plain[8 + 2], plain[8 + 3],
796                                      plain[8 + 4], plain[8 + 5],
797                                      plain[8 + 6], plain[8 + 7]);
798         }
799 }
800 #endif /* defined(LIBDES_LIT) */
801
802 ATF_TC_WITHOUT_HEAD(ofb64);
803 #if defined(LIBDES_LIT)
804 ATF_TC_BODY(ofb64, tc)
805 {
806         atf_tc_skip("Test program built with LIBDES_LIT");
807 }
808 #else /* defined(LIBDES_LIT) */
809 ATF_TC_BODY(ofb64, tc)
810 {
811         int num;
812         size_t i;
813         des_key_schedule ks;
814
815         des_set_key_checked(&ofb_key, ks);
816         memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
817         memset(ofb_buf1, 0, sizeof(ofb_buf1));
818         memset(ofb_buf2, 0, sizeof(ofb_buf1));
819         num = 0;
820         for (i = 0; i < sizeof(plain); i++) {
821                 des_ofb64_encrypt(&(plain[i]), &(ofb_buf1[i]), 1, ks, &ofb_tmp,
822                                   &num);
823         }
824         if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
825                 atf_tc_fail_nonfatal("ofb64_encrypt encrypt error\n");
826         }
827         memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
828         num = 0;
829         des_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), ks, &ofb_tmp, &num);
830         if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
831                 atf_tc_fail_nonfatal("ofb64_encrypt decrypt error\n");
832         }
833 }
834 #endif /* defined(LIBDES_LIT) */
835
836 ATF_TC_WITHOUT_HEAD(ede_ofb64);
837 #if defined(LIBDES_LIT)
838 ATF_TC_BODY(ede_ofb64, tc)
839 {
840         atf_tc_skip("Test program built with LIBDES_LIT");
841 }
842 #else /* defined(LIBDES_LIT) */
843 ATF_TC_BODY(ede_ofb64, tc)
844 {
845         int num;
846         size_t i;
847         des_key_schedule ks;
848
849         des_set_key_checked(&ofb_key, ks);
850         memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
851         memset(ofb_buf1, 0, sizeof(ofb_buf1));
852         memset(ofb_buf2, 0, sizeof(ofb_buf1));
853         num = 0;
854         for (i = 0; i < sizeof(plain); i++) {
855                 des_ede3_ofb64_encrypt(&(plain[i]), &(ofb_buf1[i]), 1, ks, ks, ks,
856                                        &ofb_tmp, &num);
857         }
858         if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
859                 atf_tc_fail_nonfatal("ede_ofb64_encrypt encrypt error\n");
860         }
861         memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
862         num = 0;
863         des_ede3_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), ks,
864                                ks, ks, &ofb_tmp, &num);
865         if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
866                 atf_tc_fail_nonfatal("ede_ofb64_encrypt decrypt error\n");
867         }
868 }
869 #endif /* defined(LIBDES_LIT) */
870
871 ATF_TC_WITHOUT_HEAD(cbc_cksum);
872 #if defined(LIBDES_LIT)
873 ATF_TC_BODY(cbc_cksum, tc)
874 {
875         atf_tc_skip("Test program built with LIBDES_LIT");
876 }
877 #else /* defined(LIBDES_LIT) */
878 ATF_TC_BODY(cbc_cksum, tc)
879 {
880         unsigned char cret[8];
881         des_key_schedule ks;
882         DES_LONG cs;
883
884         des_set_key_checked(&cbc_key, ks);
885         cs = des_cbc_cksum(cbc_data, &cret, strlen((char *) cbc_data), ks, &cbc_iv);
886         if (cs != cbc_cksum_ret) {
887                 atf_tc_fail_nonfatal("bad return value (%08lX), should be %08lX\n",
888                        (unsigned long) cs, (unsigned long) cbc_cksum_ret);
889         }
890         if (memcmp(cret, cbc_cksum_data, 8) != 0) {
891                 atf_tc_fail_nonfatal("bad cbc_cksum block returned\n");
892         }
893 }
894 #endif /* defined(LIBDES_LIT) */
895
896 ATF_TC_WITHOUT_HEAD(quad_cksum);
897 #if defined(LIBDES_LIT)
898 ATF_TC_BODY(quad_cksum, tc)
899 {
900         atf_tc_skip("Test program built with LIBDES_LIT");
901 }
902 #else /* defined(LIBDES_LIT) */
903 ATF_TC_BODY(quad_cksum, tc)
904 {
905         DES_LONG cs, lqret[4];
906
907         cs = quad_cksum(cbc_data, (des_cblock *) lqret,
908                 (long) strlen((char *) cbc_data), 2, (des_cblock *) cbc_iv);
909         if (cs != 0x70d7a63aL) {
910                 atf_tc_fail_nonfatal("quad_cksum error, ret %08lx should be 70d7a63a\n",
911                        (unsigned long) cs);
912         }
913         if (lqret[0] != 0x327eba8dL) {
914                 atf_tc_fail_nonfatal("quad_cksum error, out[0] %08lx is not %08lx\n",
915                        (unsigned long) lqret[0], 0x327eba8dUL);
916         }
917         if (lqret[1] != 0x201a49ccL) {
918                 atf_tc_fail_nonfatal("quad_cksum error, out[1] %08lx is not %08lx\n",
919                        (unsigned long) lqret[1], 0x201a49ccUL);
920         }
921         if (lqret[2] != 0x70d7a63aL) {
922                 atf_tc_fail_nonfatal("quad_cksum error, out[2] %08lx is not %08lx\n",
923                        (unsigned long) lqret[2], 0x70d7a63aUL);
924         }
925         if (lqret[3] != 0x501c2c26L) {
926                 atf_tc_fail_nonfatal("quad_cksum error, out[3] %08lx is not %08lx\n",
927                        (unsigned long) lqret[3], 0x501c2c26UL);
928         }
929 }
930 #endif /* defined(LIBDES_LIT) */
931
932 ATF_TC_WITHOUT_HEAD(align);
933 ATF_TC_BODY(align, tc)
934 {
935         int i;
936         unsigned char cbc_in[40], cbc_out[40];
937         des_key_schedule ks;
938
939         printf("input word alignment test");
940         for (i = 0; i < 4; i++) {
941                 printf(" %d", i);
942                 des_ncbc_encrypt(&(cbc_out[i]), cbc_in,
943                                  strlen((char *) cbc_data) + 1, ks,
944                                  &cbc_iv, DES_ENCRYPT);
945         }
946
947         printf("\noutput word alignment test");
948         for (i = 0; i < 4; i++) {
949                 printf(" %d", i);
950                 des_ncbc_encrypt(cbc_out, &(cbc_in[i]),
951                                  strlen((char *) cbc_data) + 1, ks,
952                                  &cbc_iv, DES_ENCRYPT);
953         }
954 }
955
956 ATF_TC_WITHOUT_HEAD(fast_crypt);
957 ATF_TC_BODY(fast_crypt, tc)
958 {
959         char *str;
960
961         str = crypt("testing", "ef");
962         if (strcmp("efGnQx2725bI2", str) != 0)
963                 atf_tc_fail_nonfatal("fast crypt error, %s should be efGnQx2725bI2\n", str);
964         str = crypt("bca76;23", "yA");
965         if (strcmp("yA1Rp/1hZXIJk", str) != 0)
966                 atf_tc_fail_nonfatal("fast crypt error, %s should be yA1Rp/1hZXIJk\n", str);
967 }
968
969 ATF_TP_ADD_TCS(tp)
970 {
971
972         ATF_TP_ADD_TC(tp, cbcm);
973         ATF_TP_ADD_TC(tp, ecb);
974         ATF_TP_ADD_TC(tp, ede_ecb);
975         ATF_TP_ADD_TC(tp, cbc);
976         ATF_TP_ADD_TC(tp, desx_cbc);
977         ATF_TP_ADD_TC(tp, ede_cbc);
978         ATF_TP_ADD_TC(tp, pcbc);
979         ATF_TP_ADD_TC(tp, cfb);
980         ATF_TP_ADD_TC(tp, ofb);
981         ATF_TP_ADD_TC(tp, ofb64);
982         ATF_TP_ADD_TC(tp, ede_ofb64);
983         ATF_TP_ADD_TC(tp, cbc_cksum);
984         ATF_TP_ADD_TC(tp, quad_cksum);
985         ATF_TP_ADD_TC(tp, align);
986         ATF_TP_ADD_TC(tp, fast_crypt);
987
988         return atf_no_error();
989 }