afsconfig-and-rcsid-all-around-20010705
[openafs.git] / src / des / enc.c
1 /*
2  * Copyright 1988 by the Massachusetts Institute of Technology.
3  *
4  * For copying and distribution information, please see the file
5  * <mit-copyright.h>.
6  */
7
8 #include <afs/param.h>
9 #include <afsconfig.h>
10
11 RCSID("$Header$");
12
13 #include <mit-cpyright.h>
14 #include <des.h>
15 #ifdef BSDUNIX
16 #include <sys/file.h>
17 #endif
18 #include <stdio.h>
19
20 Key_schedule KEYSCHED;
21 C_Block key = {0,1,2,3,4,5,6,7};
22 C_Block sum;
23 char inbuf[512+8];              /* leave room for cksum and len */
24 char oubuf[512+8];
25 int debug;
26 int ind;
27 int oud;
28 afs_int32 orig_size;
29
30 #include "AFS_component_version_number.c"
31
32 main(argc,argv)
33     int argc;
34     char *argv[];
35 {
36     register int encrypt;
37     register afs_int32 length;
38     register int *p;
39     afs_int32 ivec[2];
40     if (argc != 4) {
41         fprintf (stderr, "%s: Usage: %s infile outfile mode.\n",
42                  argv[0], argv[0]);
43         exit (1);
44     }
45     if (!strcmp(argv[3], "e"))
46         encrypt = 1;
47     else if (!strcmp(argv[3], "d"))
48         encrypt = 0;
49     else {
50         fprintf (stderr, "%s: Mode must be e (encrypt) or d (decrypt).\n",
51                  argv[0]);
52         exit (1);
53     }
54     if ((ind = open(argv[1], O_RDONLY, 0666)) < 0) {
55         fprintf (stderr, "%s: Cannot open %s for input.\n",
56                  argv[0], argv[1]);
57         exit (1);
58     }
59     if (!strcmp(argv[2], "-"))
60         oud = dup(1);
61     else if ((oud = open(argv[2], O_CREAT|O_WRONLY, 0666)) < 0) {
62         fprintf (stderr, "%s: Cannot open %s for output.\n",
63                  argv[0], argv[2]);
64         exit (1);
65     }
66 #ifdef notdef
67     (void) freopen ("/dev/tty", "r", stdin);
68     (void) freopen ("/dev/tty", "w", stdout);
69 #endif
70     read_password (key, "\n\07\07Enter Key> ", 1);
71     if (key_sched (key, KEYSCHED) < 0) {
72         fprintf (stderr, "%s: Key parity error\n", argv[0]);
73         exit (1);
74     }
75     ivec[0] = 0;
76     ivec[1] = 0;
77     bcopy(key,sum,sizeof(C_Block));
78     for (;;) {
79         if ((length = read (ind, inbuf, 512)) < 0) {
80             fprintf (stderr, "%s: Error reading from input.\n",
81                      argv[0]);
82             exit (1);
83         } else if (length == 0) {
84             fprintf (stderr, "\n");
85             break;
86         }
87         if (encrypt) {
88 #ifdef notdef
89             sum = quad_cksum(inbuf,NULL,length,1,sum);
90 #endif
91             quad_cksum(inbuf,sum,length,1,sum);
92             orig_size += length;
93             fprintf(stderr,
94                     "\nlength = %d tot length = %d quad_sum = %X %X",
95                     length, orig_size, *(afs_uint32 *) sum,
96                     *((afs_uint32 *) sum+1));
97             fflush(stderr);
98         }
99         pcbc_encrypt (inbuf, oubuf, (afs_int32) length, KEYSCHED, ivec,
100                       encrypt);
101         if (!encrypt) {
102 #ifdef notdef
103             sum = quad_cksum(oubuf,NULL,length,1,sum);
104 #endif
105             quad_cksum(oubuf,sum,length,1,sum);
106             orig_size += length;
107             fprintf(stderr,
108                     "\nlength = %d tot length = %d quad_sum = %X ",
109                     length, orig_size, *(afs_uint32 *) sum,
110                     *((afs_uint32 *) sum+1));
111         }
112         length = (length+7)& ~07;
113         write (oud, oubuf, length);
114         if (!encrypt)
115             p = (int *)&oubuf[length-8];
116         else
117             p = (int *)&inbuf[length-8];
118         ivec[0] = *p++;
119         ivec[1] = *p;
120     }
121
122     fprintf(stderr,"\ntot length = %d quad_sum = %X\n",
123             orig_size,sum);
124     /* if encrypting, now put the original length and checksum in */
125 }