|
|
1.1 root 1: /*
2: * Machine Independent Input/Output Routines
3: * D.P.Mitchell 83/06/08.
4: */
5:
6: #include <stdio.h>
7: #include "crypt.h"
8: #define MAGIC 0xe48d
9:
10: extern Block random;
11: extern int permutation[];
12: extern int pflag;
13:
14: /*
15: * Traffic-encryption layer (plaintext source)
16: */
17: int
18: p_source(output)
19: register Block *output;
20: {
21: static int state = FIRST8;
22: static long nblocks;
23: static long rand;
24: register c;
25: register n;
26: int all_junk;
27:
28: output->left = 0;
29: output->right = 0;
30: all_junk = 1;
31: for (n = 0; n < 64; n += 8) {
32: switch (state) {
33: case FIRST8:
34: make_sum(&random);
35: *output = random;
36: state = GETCHAR;
37: nblocks = 1;
38: return 1;
39: case GETCHAR:
40: all_junk = 0;
41: c = getchar();
42: if (c == EOF) {
43: state = ENDTEXT;
44: c = ETX;
45: break;
46: }
47: if (c == ETX) {
48: state = REPEATETX;
49: break;
50: }
51: break;
52: case REPEATETX:
53: all_junk = 0;
54: state = GETCHAR;
55: c = ETX;
56: break;
57: case ENDTEXT:
58: state = PADJUNK;
59: rand = NEXT(random.left);
60: for (;;) {
61: c = NEXT(rand);
62: if (((c >> 24) & 0377) != ETX)
63: break;
64: rand = NEXT(rand);
65: }
66: case PADJUNK:
67: if (all_junk && nblocks % SUPERSIZE == 0)
68: return 0;
69: rand = NEXT(rand);
70: c = (rand >> 24) & 0377;
71: break;
72: }
73: if (n > 31)
74: output->right |= c << (n & 31);
75: else
76: output->left |= c << (n & 31);
77: }
78: nblocks++;
79: return 1;
80: }
81:
82: /*
83: * traffic-decryption layer (plaintext sink)
84: */
85: p_sink(input)
86: register Block *input;
87: {
88: static int state = FIRST8;
89: register c;
90: register n;
91:
92: for (n = 0; n < 64; n += 8) {
93: if (n > 31)
94: c = (input->right >> (n & 31)) & 0377;
95: else
96: c = (input->left >> (n & 31)) & 0377;
97: switch (state) {
98: case FIRST8:
99: check_sum(input);
100: state = PUTCHAR;
101: return;
102: case PUTCHAR:
103: if (c == ETX) {
104: state = FOUNDETX;
105: break;
106: }
107: putchar(c);
108: break;
109: case FOUNDETX:
110: if (c == ETX) {
111: state = PUTCHAR;
112: putchar(ETX);
113: break;
114: }
115: state = ENDTEXT;
116: break;
117: case ENDTEXT:
118: break;
119: }
120: }
121: }
122:
123: /*
124: * permutation-decryption layer (ciphertext source)
125: */
126: int
127: c_source(output)
128: Block *output;
129: {
130: register long n;
131: register int *ip;
132: static int c_buffer[128];
133: static index = 128;
134:
135: if (index > 127) {
136: if (pflag) {
137: if (!in_alpha(c_buffer))
138: return 0;
139: } else {
140: for (n = 0; n < 128; n++)
141: if ((c_buffer[permutation[n]] = getchar()) == EOF)
142: return 0;
143: }
144: index = 0;
145: }
146: ip = &c_buffer[index];
147: n = *ip++;
148: n |= *ip++ << 8;
149: n |= *ip++ << 16;
150: n |= *ip++ << 24;
151: output->left = n;
152: n = *ip++;
153: n |= *ip++ << 8;
154: n |= *ip++ << 16;
155: n |= *ip << 24;
156: output->right = n;
157: index += 8;
158: return 1;
159: }
160:
161: /*
162: * permutation-encryption layer (ciphertext sink)
163: */
164: c_sink(input)
165: Block *input;
166: {
167: register long n;
168: register int *ip;
169: static int c_buffer[128];
170: static int index = 0;
171:
172: ip = &c_buffer[index];
173: n = input->left;
174: *ip++ = n & 0xff;
175: *ip++ = (n >> 8) & 0xff;
176: *ip++ = (n >> 16) & 0xff;
177: *ip++ = (n >> 24) & 0xff;
178: n = input->right;
179: *ip++ = n & 0xff;
180: *ip++ = (n >> 8) & 0xff;
181: *ip++ = (n >> 16) & 0xff;
182: *ip = (n >> 24) & 0xff;
183: index += 8;
184: if (index > 127) {
185: if (pflag)
186: out_alpha(c_buffer);
187: else {
188: for (n = 0; n < 128; n++)
189: putchar(c_buffer[permutation[n]]);
190: }
191: index = 0;
192: }
193: }
194:
195: make_sum(block)
196: Block *block;
197: {
198: long a, b, c;
199: long sum;
200:
201: a = block->right & 0xffff;
202: b = (block->right >> 16) & 0xffff;
203: c = block->left & 0xffff;
204: sum = (MAGIC + 13*a + 23*b + 31*c) & 0xffff;
205: block->left = (sum << 16) | c;
206: }
207:
208: check_sum(block)
209: Block *block;
210: {
211: long a, b, c, d;
212: long sum;
213:
214: a = block->right & 0xffff;
215: b = (block->right >> 16) & 0xffff;
216: c = block->left & 0xffff;
217: d = (block->left >> 16) & 0xffff;
218: sum = (MAGIC + 13*a + 23*b + 31*c) & 0xffff;
219: if (sum != d) {
220: fprintf(stderr, "Wrong key\n");
221: exit(1);
222: }
223: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.