stamp out rcs keywords
[krb5.git] / src / lib / des425 / verify.c
1 /*
2  * lib/des425/verify.c
3  *
4  * Copyright 1988,1990 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.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * Program to test the correctness of the DES library
25  * implementation.
26  *
27  * exit returns  0 ==> success
28  *              -1 ==> error
29  */
30
31
32 #include <stdio.h>
33 #include <errno.h>
34 #include "./des.h"
35
36 extern char *errmsg();
37 extern int errno;
38 extern int des_string_to_key();
39 extern int des_key_sched();
40 extern int des_ecb_encrypt();
41 extern int des_cbc_encrypt();
42 extern exit();
43 char *progname;
44 int nflag = 2;
45 int vflag;
46 int mflag;
47 int zflag;
48 int pid;
49 int des_debug;
50 des_key_schedule KS;
51 unsigned char cipher_text[64];
52 unsigned char clear_text[64] = "Now is the time for all " ;
53 unsigned char clear_text2[64] = "7654321 Now is the time for ";
54 unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
55 unsigned char output[64];
56 unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
57 unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
58 unsigned char *input;
59
60 /* 0x0123456789abcdef */
61 unsigned char default_key[8] = {
62     0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
63 };
64 unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
65 unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
66 des_cblock s_key;
67 unsigned char default_ivec[8] = {
68     0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
69 };
70 unsigned char *ivec;
71 unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
72 int i,j;
73
74 /*
75  * Can also add :
76  * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
77  */
78
79 main(argc,argv)
80     int argc;
81     char *argv[];
82 {
83     /* Local Declarations */
84     long in_length;
85
86     progname=argv[0];           /* salt away invoking program */
87
88     /* Assume a long is four bytes */
89     if (sizeof(long) != 4) {
90         printf("\nERROR,  size of long is %d",sizeof(long));
91         exit(-1);
92     }
93
94     while (--argc > 0 && (*++argv)[0] == '-')
95         for (i=1; argv[0][i] != '\0'; i++) {
96             switch (argv[0][i]) {
97
98                 /* debug flag */
99             case 'd':
100                 des_debug=3;
101                 continue;
102
103             case 'z':
104                 zflag = 1;
105                 continue;
106
107             case 'm':
108                 mflag = 1;
109                 continue;
110
111             default:
112                 printf("%s: illegal flag \"%c\" ",
113                        progname,argv[0][i]);
114                 exit(1);
115             }
116         };
117
118     if (argc) {
119         fprintf(stderr, "Usage: %s [-dmz]\n", progname);
120         exit(1);
121     }
122
123     /* use known input and key */
124
125     /* ECB zero text zero key */
126     if (zflag) {
127         input = zero_text;
128         des_key_sched(zero_key,KS);
129         printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
130         do_encrypt(input,cipher_text);
131         printf("\tcipher  = (low to high bytes)\n\t\t");
132         for (j = 0; j<=7; j++)
133             printf("%02x ",cipher_text[j]);
134         printf("\n");
135         do_decrypt(output,cipher_text);
136         return(0);
137     }
138
139     if (mflag) {
140         input = msb_text;
141         des_key_sched(key3,KS);
142         printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
143         printf("key = 0, cipher = 0x??\n");
144         do_encrypt(input,cipher_text);
145         printf("\tcipher  = (low to high bytes)\n\t\t");
146         for (j = 0; j<=7; j++) {
147             printf("%02x ",cipher_text[j]);
148         }
149         printf("\n");
150         do_decrypt(output,cipher_text);
151         return(0);
152     }
153
154     /* ECB mode Davies and Price */
155     {
156         input = zero_text;
157         des_key_sched(key2,KS);
158         printf("Examples per FIPS publication 81, keys ivs and cipher\n");
159         printf("in hex.  These are the correct answers, see below for\n");
160         printf("the actual answers.\n\n");
161         printf("Examples per Davies and Price.\n\n");
162         printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
163         printf("\tclear = 0\n");
164         printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
165         printf("ACTUAL ECB\n");
166         printf("\tclear \"%s\"\n", input);
167         do_encrypt(input,cipher_text);
168         printf("\tcipher  = (low to high bytes)\n\t\t");
169         for (j = 0; j<=7; j++)
170             printf("%02x ",cipher_text[j]);
171         printf("\n\n");
172         do_decrypt(output,cipher_text);
173     }
174
175     /* ECB mode */
176     {
177         des_key_sched(default_key,KS);
178         input = clear_text;
179         ivec = default_ivec;
180         printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
181         printf("\tclear = \"Now is the time for all \"\n");
182         printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
183         printf("ACTUAL ECB\n\tclear \"%s\"",input);
184         do_encrypt(input,cipher_text);
185         printf("\n\tcipher      = (low to high bytes)\n\t\t");
186         for (j = 0; j<=7; j++) {
187             printf("%02x ",cipher_text[j]);
188         }
189         printf("\n\n");
190         do_decrypt(output,cipher_text);
191     }
192
193     /* CBC mode */
194     printf("EXAMPLE CBC\tkey = 0123456789abcdef");
195     printf("\tiv = 1234567890abcdef\n");
196     printf("\tclear = \"Now is the time for all \"\n");
197     printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
198     printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
199     printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
200
201     printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
202     in_length = strlen(input);
203     des_cbc_encrypt(input,cipher_text,(long) in_length,KS,ivec,1);
204     printf("\tciphertext = (low to high bytes)\n");
205     for (i = 0; i <= 7; i++) {
206         printf("\t\t");
207         for (j = 0; j <= 7; j++) {
208             printf("%02x ",cipher_text[i*8+j]);
209         }
210         printf("\n");
211     }
212     des_cbc_encrypt(cipher_text,clear_text,(long) in_length,KS,ivec,0);
213     printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
214
215     printf("EXAMPLE CBC checksum");
216     printf("\tkey =  0123456789abcdef\tiv =  1234567890abcdef\n");
217     printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
218     printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
219     printf("or some part thereof\n");
220     input = clear_text2;
221     des_cbc_cksum(input,cipher_text,(long) strlen(input),KS,ivec,1);
222     printf("ACTUAL CBC checksum\n");
223     printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
224     for (j = 0; j<=7; j++)
225         printf("%02x ",cipher_text[j]);
226     printf("\n\n");
227     exit(0);
228 }
229
230 flip(array)
231     char *array;
232 {
233     register old,new,i,j;
234     /* flips the bit order within each byte from 0 lsb to 0 msb */
235     for (i = 0; i<=7; i++) {
236         old = *array;
237         new = 0;
238         for (j = 0; j<=7; j++) {
239             if (old & 01)
240                 new = new | 01;
241             if (j < 7) {
242                 old = old >> 1;
243                 new = new << 1;
244             }
245         }
246         *array = new;
247         array++;
248     }
249 }
250
251 do_encrypt(in,out)
252     char *in;
253     char *out;
254 {
255     for (i =1; i<=nflag; i++) {
256         des_ecb_encrypt(in,out,KS,1);
257         if (des_debug) {
258             printf("\nclear %s\n",in);
259             for (j = 0; j<=7; j++)
260                 printf("%02 X ",in[j] & 0xff);
261             printf("\tcipher ");
262             for (j = 0; j<=7; j++)
263                 printf("%02X ",out[j] & 0xff);
264         }
265     }
266 }
267
268 do_decrypt(in,out)
269     char *out;
270     char *in;
271     /* try to invert it */
272 {
273     for (i =1; i<=nflag; i++) {
274         des_ecb_encrypt(out,in,KS,0);
275         if (des_debug) {
276             printf("clear %s\n",in);
277             for (j = 0; j<=7; j++)
278                 printf("%02X ",in[j] & 0xff);
279             printf("\tcipher ");
280             for (j = 0; j<=7; j++)
281                 printf("%02X ",out[j] & 0xff);
282         }
283     }
284 }