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