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