xref: /onnv-gate/usr/src/cmd/crypt/crypt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  *	A one-rotor machine designed along the lines of Enigma
34*0Sstevel@tonic-gate  *	but considerably trivialized.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /* EXPORT DELETE START */
38*0Sstevel@tonic-gate #define	ECHO 010
39*0Sstevel@tonic-gate #include <stdio.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <unistd.h>
42*0Sstevel@tonic-gate #include <string.h>
43*0Sstevel@tonic-gate #include <crypt.h>
44*0Sstevel@tonic-gate #include <errno.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	ROTORSZ 256
47*0Sstevel@tonic-gate #define	MASK 0377
48*0Sstevel@tonic-gate char	t1[ROTORSZ];
49*0Sstevel@tonic-gate char	t2[ROTORSZ];
50*0Sstevel@tonic-gate char	t3[ROTORSZ];
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static void
setup(pw)53*0Sstevel@tonic-gate setup(pw)
54*0Sstevel@tonic-gate char *pw;
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	int ic, i, k, temp;
57*0Sstevel@tonic-gate 	unsigned random;
58*0Sstevel@tonic-gate 	char buf[13];
59*0Sstevel@tonic-gate 	long seed;
60*0Sstevel@tonic-gate 	char *ret;
61*0Sstevel@tonic-gate 	int err;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	(void) strncpy(buf, pw, 8);
64*0Sstevel@tonic-gate 	buf[8] = buf[0];
65*0Sstevel@tonic-gate 	buf[9] = buf[1];
66*0Sstevel@tonic-gate 	errno = 0;
67*0Sstevel@tonic-gate 	ret = des_crypt(buf, &buf[8]);
68*0Sstevel@tonic-gate 	if (ret == NULL) {
69*0Sstevel@tonic-gate 		err = errno;
70*0Sstevel@tonic-gate 		(void) fprintf(stderr, "crypt: setup failed, unable to"
71*0Sstevel@tonic-gate 		    " initialize rotors: %s\n", strerror(err));
72*0Sstevel@tonic-gate 		exit(1);
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 	(void) strncpy(buf, ret, 13);
75*0Sstevel@tonic-gate 	seed = 123;
76*0Sstevel@tonic-gate 	for (i = 0; i < 13; i++)
77*0Sstevel@tonic-gate 		seed = seed*buf[i] + i;
78*0Sstevel@tonic-gate 	for (i = 0; i < ROTORSZ; i++) {
79*0Sstevel@tonic-gate 		t1[i] = i;
80*0Sstevel@tonic-gate 		t3[i] = 0;
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 	for (i = 0; i < ROTORSZ; i++) {
83*0Sstevel@tonic-gate 		seed = 5*seed + buf[i%13];
84*0Sstevel@tonic-gate 		random = seed % 65521;
85*0Sstevel@tonic-gate 		k = ROTORSZ-1 - i;
86*0Sstevel@tonic-gate 		ic = (random&MASK)%(k+1);
87*0Sstevel@tonic-gate 		random >>= 8;
88*0Sstevel@tonic-gate 		temp = t1[k];
89*0Sstevel@tonic-gate 		t1[k] = t1[ic];
90*0Sstevel@tonic-gate 		t1[ic] = temp;
91*0Sstevel@tonic-gate 		if (t3[k] != 0) continue;
92*0Sstevel@tonic-gate 		ic = (random&MASK) % k;
93*0Sstevel@tonic-gate 		while (t3[ic] != 0) ic = (ic+1) % k;
94*0Sstevel@tonic-gate 		t3[k] = ic;
95*0Sstevel@tonic-gate 		t3[ic] = k;
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 	for (i = 0; i < ROTORSZ; i++)
98*0Sstevel@tonic-gate 		t2[t1[i]&MASK] = i;
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate /* EXPORT DELETE END */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate int
main(int argc,char ** argv)103*0Sstevel@tonic-gate main(int argc, char **argv)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate /* EXPORT DELETE START */
106*0Sstevel@tonic-gate 	extern int optind;
107*0Sstevel@tonic-gate 	char *p1;
108*0Sstevel@tonic-gate 	int i, n1, n2, nchar;
109*0Sstevel@tonic-gate 	int c;
110*0Sstevel@tonic-gate 	struct {
111*0Sstevel@tonic-gate 		long offset;
112*0Sstevel@tonic-gate 		unsigned int count;
113*0Sstevel@tonic-gate 	} header;
114*0Sstevel@tonic-gate 	int pflag = 0;
115*0Sstevel@tonic-gate 	int kflag = 0;
116*0Sstevel@tonic-gate 	char *buf;
117*0Sstevel@tonic-gate 	char key[8];
118*0Sstevel@tonic-gate 	char *keyvar = "CrYpTkEy=XXXXXXXX";
119*0Sstevel@tonic-gate 	char *s;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if (argc < 2) {
122*0Sstevel@tonic-gate 		if ((buf = (char *)getpass("Enter key:")) == NULL) {
123*0Sstevel@tonic-gate 			(void) fprintf(stderr, "Cannot open /dev/tty\n");
124*0Sstevel@tonic-gate 			exit(1);
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		setup(buf);
127*0Sstevel@tonic-gate 	} else {
128*0Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "pk")) != EOF)
129*0Sstevel@tonic-gate 			switch (c) {
130*0Sstevel@tonic-gate 			case 'p':
131*0Sstevel@tonic-gate 			/* notify editor that exec has succeeded */
132*0Sstevel@tonic-gate 				if (write(1, "y", 1) != 1)
133*0Sstevel@tonic-gate 					exit(1);
134*0Sstevel@tonic-gate 				if (read(0, key, 8) != 8)
135*0Sstevel@tonic-gate 					exit(1);
136*0Sstevel@tonic-gate 				setup(key);
137*0Sstevel@tonic-gate 				pflag = 1;
138*0Sstevel@tonic-gate 				break;
139*0Sstevel@tonic-gate 			case 'k':
140*0Sstevel@tonic-gate 				if ((s = getenv("CrYpTkEy")) == (char *)NULL) {
141*0Sstevel@tonic-gate 					(void) fprintf(stderr,
142*0Sstevel@tonic-gate 					    "CrYpTkEy not set.\n");
143*0Sstevel@tonic-gate 					exit(1);
144*0Sstevel@tonic-gate 				}
145*0Sstevel@tonic-gate 				(void) strncpy(key, s, 8);
146*0Sstevel@tonic-gate 				setup(key);
147*0Sstevel@tonic-gate 				kflag = 1;
148*0Sstevel@tonic-gate 				break;
149*0Sstevel@tonic-gate 			case '?':
150*0Sstevel@tonic-gate 				(void) fprintf(stderr,
151*0Sstevel@tonic-gate 				    "usage: crypt [ -k ] [ key]\n");
152*0Sstevel@tonic-gate 				exit(2);
153*0Sstevel@tonic-gate 			}
154*0Sstevel@tonic-gate 		if (pflag == 0 && kflag == 0) {
155*0Sstevel@tonic-gate 			(void) strncpy(keyvar+9, argv[optind], 8);
156*0Sstevel@tonic-gate 			(void) putenv(keyvar);
157*0Sstevel@tonic-gate 			(void) execlp("crypt", "crypt", "-k", 0);
158*0Sstevel@tonic-gate 		}
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 	if (pflag)
161*0Sstevel@tonic-gate 		for (;;) {
162*0Sstevel@tonic-gate 			if ((nchar = read(0, (char *)&header, sizeof (header)))
163*0Sstevel@tonic-gate 			    != sizeof (header))
164*0Sstevel@tonic-gate 				exit(nchar);
165*0Sstevel@tonic-gate 			n1 = (int)(header.offset&MASK);
166*0Sstevel@tonic-gate 			n2 = (int)((header.offset >> 8) &MASK);
167*0Sstevel@tonic-gate 			nchar = header.count;
168*0Sstevel@tonic-gate 			buf = (char *)malloc(nchar);
169*0Sstevel@tonic-gate 			p1 = buf;
170*0Sstevel@tonic-gate 			if (read(0, buf, nchar) != nchar)
171*0Sstevel@tonic-gate 				exit(1);
172*0Sstevel@tonic-gate 			while (nchar--) {
173*0Sstevel@tonic-gate 				*p1 = t2[(t3[(t1[(*p1 + n1)&MASK]+
174*0Sstevel@tonic-gate 				    n2)&MASK] - n2)&MASK] - n1;
175*0Sstevel@tonic-gate 				n1++;
176*0Sstevel@tonic-gate 				if (n1 == ROTORSZ) {
177*0Sstevel@tonic-gate 					n1 = 0;
178*0Sstevel@tonic-gate 					n2++;
179*0Sstevel@tonic-gate 					if (n2 == ROTORSZ) n2 = 0;
180*0Sstevel@tonic-gate 				}
181*0Sstevel@tonic-gate 				p1++;
182*0Sstevel@tonic-gate 			}
183*0Sstevel@tonic-gate 			nchar = header.count;
184*0Sstevel@tonic-gate 			if (write(1, buf, nchar) != nchar)
185*0Sstevel@tonic-gate 				exit(1);
186*0Sstevel@tonic-gate 			free(buf);
187*0Sstevel@tonic-gate 		}
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	n1 = 0;
190*0Sstevel@tonic-gate 	n2 = 0;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	while ((i = getchar()) >= 0) {
193*0Sstevel@tonic-gate 		i = t2[(t3[(t1[(i+n1)&MASK]+n2)&MASK]-n2)&MASK]-n1;
194*0Sstevel@tonic-gate 		(void) putchar(i);
195*0Sstevel@tonic-gate 		n1++;
196*0Sstevel@tonic-gate 		if (n1 == ROTORSZ) {
197*0Sstevel@tonic-gate 			n1 = 0;
198*0Sstevel@tonic-gate 			n2++;
199*0Sstevel@tonic-gate 			if (n2 == ROTORSZ) n2 = 0;
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 	return (0);
203*0Sstevel@tonic-gate /* EXPORT DELETE END */
204*0Sstevel@tonic-gate }
205