4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
32 * A one-rotor machine designed along the lines of Enigma
33 * but considerably trivialized.
61 (void) strncpy(buf
, pw
, 8);
65 ret
= des_crypt(buf
, &buf
[8]);
68 (void) fprintf(stderr
, "crypt: setup failed, unable to"
69 " initialize rotors: %s\n", strerror(err
));
72 (void) strncpy(buf
, ret
, 13);
74 for (i
= 0; i
< 13; i
++)
75 seed
= seed
*buf
[i
] + i
;
76 for (i
= 0; i
< ROTORSZ
; i
++) {
80 for (i
= 0; i
< ROTORSZ
; i
++) {
81 seed
= 5*seed
+ buf
[i
%13];
82 random
= seed
% 65521;
84 ic
= (random
&MASK
)%(k
+1);
89 if (t3
[k
] != 0) continue;
90 ic
= (random
&MASK
) % k
;
91 while (t3
[ic
] != 0) ic
= (ic
+1) % k
;
95 for (i
= 0; i
< ROTORSZ
; i
++)
100 main(int argc
, char **argv
)
104 int i
, n1
, n2
, nchar
;
114 char keyvar
[] = "CrYpTkEy=XXXXXXXX";
118 if ((buf
= (char *)getpass("Enter key:")) == NULL
) {
119 (void) fprintf(stderr
, "Cannot open /dev/tty\n");
124 while ((c
= getopt(argc
, argv
, "pk")) != EOF
)
127 /* notify editor that exec has succeeded */
128 if (write(1, "y", 1) != 1)
130 if (read(0, key
, 8) != 8)
136 if ((s
= getenv("CrYpTkEy")) == (char *)NULL
) {
137 (void) fprintf(stderr
,
138 "CrYpTkEy not set.\n");
141 (void) strncpy(key
, s
, 8);
146 (void) fprintf(stderr
,
147 "usage: crypt [ -k ] [ key]\n");
150 if (pflag
== 0 && kflag
== 0) {
151 (void) strncpy(keyvar
+9, argv
[optind
], 8);
152 (void) putenv(keyvar
);
153 (void) execlp("crypt", "crypt", "-k", 0);
158 if ((nchar
= read(0, (char *)&header
, sizeof (header
)))
161 n1
= (int)(header
.offset
&MASK
);
162 n2
= (int)((header
.offset
>> 8) &MASK
);
163 nchar
= header
.count
;
164 buf
= (char *)malloc(nchar
);
166 if (read(0, buf
, nchar
) != nchar
)
169 *p1
= t2
[(t3
[(t1
[(*p1
+ n1
)&MASK
]+
170 n2
)&MASK
] - n2
)&MASK
] - n1
;
175 if (n2
== ROTORSZ
) n2
= 0;
179 nchar
= header
.count
;
180 if (write(1, buf
, nchar
) != nchar
)
188 while ((i
= getchar()) >= 0) {
189 i
= t2
[(t3
[(t1
[(i
+n1
)&MASK
]+n2
)&MASK
]-n2
)&MASK
]-n1
;
195 if (n2
== ROTORSZ
) n2
= 0;