1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/builtin/des/destest.c */
3 /*
4 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
5 * All Rights Reserved.
6 *
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
11 *
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
25 */
26 /*
27 * Copyright (C) 1998 by the FundsXpress, INC.
28 *
29 * All rights reserved.
30 *
31 * Export of this software from the United States of America may require
32 * a specific license from the United States Government. It is the
33 * responsibility of any person or organization contemplating export to
34 * obtain such a license before exporting.
35 *
36 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
37 * distribute this software and its documentation for any purpose and
38 * without fee is hereby granted, provided that the above copyright
39 * notice appear in all copies and that both that copyright notice and
40 * this permission notice appear in supporting documentation, and that
41 * the name of FundsXpress. not be used in advertising or publicity pertaining
42 * to distribution of the software without specific, written prior
43 * permission. FundsXpress makes no representations about the suitability of
44 * this software for any purpose. It is provided "as is" without express
45 * or implied warranty.
46 *
47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50 */
51
52 /* Test a DES implementation against known inputs & outputs. */
53
54 #include "des_int.h"
55 #include <ctype.h>
56 #include <stdio.h>
57
58 void convert (char *, unsigned char []);
59
60 void des_cblock_print_file (mit_des_cblock, FILE *);
61
62 krb5_octet zeroblock[8] = {0,0,0,0,0,0,0,0};
63
64 int
main(int argc,char * argv[])65 main(int argc, char *argv[])
66 {
67 char block1[17], block2[17], block3[17];
68 /* Force tests of unaligned accesses. */
69 union { unsigned char c[8*4+3]; long l; } u;
70 unsigned char *ioblocks = u.c;
71 unsigned char *input = ioblocks+1;
72 unsigned char *output = ioblocks+10;
73 unsigned char *output2 = ioblocks+19;
74 unsigned char *key = ioblocks+27;
75 mit_des_key_schedule sched;
76 int num = 0;
77 int retval;
78
79 int error = 0;
80
81 while (scanf("%16s %16s %16s", block1, block2, block3) == 3) {
82 convert(block1, key);
83 convert(block2, input);
84 convert(block3, output);
85
86 retval = mit_des_key_sched(key, sched);
87 if (retval) {
88 fprintf(stderr, "des test: can't process key: %d\n", retval);
89 fprintf(stderr, "des test: %s %s %s\n", block1, block2, block3);
90 exit(1);
91 }
92 mit_des_cbc_encrypt((const mit_des_cblock *) input,
93 (mit_des_cblock *) output2, 8,
94 sched, zeroblock, 1);
95
96 if (memcmp((char *)output2, (char *)output, 8)) {
97 fprintf(stderr,
98 "DES ENCRYPT ERROR, key %s, text %s, real cipher %s, computed cyphertext %02X%02X%02X%02X%02X%02X%02X%02X\n",
99 block1, block2, block3,
100 output2[0],output2[1],output2[2],output2[3],
101 output2[4],output2[5],output2[6],output2[7]);
102 error++;
103 }
104
105 /*
106 * Now try decrypting....
107 */
108 mit_des_cbc_encrypt((const mit_des_cblock *) output,
109 (mit_des_cblock *) output2, 8,
110 sched, zeroblock, 0);
111
112 if (memcmp((char *)output2, (char *)input, 8)) {
113 fprintf(stderr,
114 "DES DECRYPT ERROR, key %s, text %s, real cipher %s, computed cleartext %02X%02X%02X%02X%02X%02X%02X%02X\n",
115 block1, block2, block3,
116 output2[0],output2[1],output2[2],output2[3],
117 output2[4],output2[5],output2[6],output2[7]);
118 error++;
119 }
120
121 num++;
122 }
123
124 if (error)
125 printf("destest: failed to pass the test\n");
126 else
127 printf("destest: %d tests passed successfully\n", num);
128
129 exit( (error > 256 && error % 256) ? 1 : error);
130 }
131
132 int value[128] = {
133 -1, -1, -1, -1, -1, -1, -1, -1,
134 -1, -1, -1, -1, -1, -1, -1, -1,
135 -1, -1, -1, -1, -1, -1, -1, -1,
136 -1, -1, -1, -1, -1, -1, -1, -1,
137 -1, -1, -1, -1, -1, -1, -1, -1,
138 -1, -1, -1, -1, -1, -1, -1, -1,
139 0, 1, 2, 3, 4, 5, 6, 7,
140 8, 9, -1, -1, -1, -1, -1, -1,
141 -1, 10, 11, 12, 13, 14, 15, -1,
142 -1, -1, -1, -1, -1, -1, -1, -1,
143 -1, -1, -1, -1, -1, -1, -1, -1,
144 -1, -1, -1, -1, -1, -1, -1, -1,
145 -1, -1, -1, -1, -1, -1, -1, -1,
146 -1, -1, -1, -1, -1, -1, -1, -1,
147 -1, -1, -1, -1, -1, -1, -1, -1,
148 -1, -1, -1, -1, -1, -1, -1, -1,
149 };
150
151 void
convert(char * text,unsigned char cblock[])152 convert(char *text, unsigned char cblock[])
153 {
154 int i;
155 for (i = 0; i < 8; i++) {
156 if (!isascii((unsigned char)text[i * 2]))
157 abort ();
158 if (value[(int) text[i*2]] == -1 || value[(int) text[i*2+1]] == -1) {
159 printf("Bad value byte %d in %s\n", i, text);
160 exit(1);
161 }
162 cblock[i] = 16*value[(int) text[i*2]] + value[(int) text[i*2+1]];
163 }
164 return;
165 }
166
167 /*
168 * Fake out the DES library, for the purposes of testing.
169 */
170
171 int
mit_des_is_weak_key(mit_des_cblock key)172 mit_des_is_weak_key(mit_des_cblock key)
173 {
174 return 0; /* fake it out for testing */
175 }
176
177 void
des_cblock_print_file(mit_des_cblock x,FILE * fp)178 des_cblock_print_file(mit_des_cblock x, FILE *fp)
179 {
180 unsigned char *y = (unsigned char *) x;
181 int i = 0;
182 fprintf(fp," 0x { ");
183
184 while (i++ < 8) {
185 fprintf(fp,"%x",*y++);
186 if (i < 8)
187 fprintf(fp,", ");
188 }
189 fprintf(fp," }");
190 }
191
192
193 #define smask(step) ((1<<step)-1)
194 #define pstep(x,step) (((x)&smask(step))^(((x)>>step)&smask(step)))
195 #define parity_char(x) pstep(pstep(pstep((x),4),2),1)
196
197 /*
198 * des_check_key_parity: returns true iff key has the correct des parity.
199 * See des_fix_key_parity for the definition of
200 * correct des parity.
201 */
202 int
mit_des_check_key_parity(mit_des_cblock key)203 mit_des_check_key_parity(mit_des_cblock key)
204 {
205 unsigned int i;
206
207 for (i=0; i<sizeof(mit_des_cblock); i++) {
208 if ((key[i] & 1) == parity_char(0xfe&key[i])) {
209 printf("warning: bad parity key:");
210 des_cblock_print_file(key, stdout);
211 putchar('\n');
212
213 return 1;
214 }
215 }
216
217 return(1);
218 }
219
220 void
mit_des_fixup_key_parity(mit_des_cblock key)221 mit_des_fixup_key_parity(mit_des_cblock key)
222 {
223 unsigned int i;
224 for (i=0; i<sizeof(mit_des_cblock); i++)
225 {
226 key[i] &= 0xfe;
227 key[i] |= 1^parity_char(key[i]);
228 }
229
230 return;
231 }
232