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