1*3981Smckusic static char *sccsid = "@(#)crypt.c 4.2 (Berkeley) 07/09/81"; 2*3981Smckusic 3994Sbill /* 4994Sbill * A one-rotor machine designed along the lines of Enigma 5994Sbill * but considerably trivialized. 6994Sbill */ 7994Sbill 8994Sbill #define ECHO 010 9994Sbill #include <stdio.h> 10994Sbill #define ROTORSZ 256 11994Sbill #define MASK 0377 12994Sbill char t1[ROTORSZ]; 13994Sbill char t2[ROTORSZ]; 14994Sbill char t3[ROTORSZ]; 15*3981Smckusic char deck[ROTORSZ]; 16994Sbill char *getpass(); 17*3981Smckusic char buf[13]; 18994Sbill 19994Sbill setup(pw) 20994Sbill char *pw; 21994Sbill { 22994Sbill int ic, i, k, temp, pf[2]; 23994Sbill unsigned random; 24994Sbill long seed; 25994Sbill 26994Sbill strncpy(buf, pw, 8); 27994Sbill while (*pw) 28994Sbill *pw++ = '\0'; 29994Sbill buf[8] = buf[0]; 30994Sbill buf[9] = buf[1]; 31994Sbill pipe(pf); 32994Sbill if (fork()==0) { 33994Sbill close(0); 34994Sbill close(1); 35994Sbill dup(pf[0]); 36994Sbill dup(pf[1]); 37994Sbill execl("/usr/lib/makekey", "-", 0); 38994Sbill execl("/lib/makekey", "-", 0); 39994Sbill exit(1); 40994Sbill } 41994Sbill write(pf[1], buf, 10); 42994Sbill wait((int *)NULL); 43994Sbill if (read(pf[0], buf, 13) != 13) { 44994Sbill fprintf(stderr, "crypt: cannot generate key\n"); 45994Sbill exit(1); 46994Sbill } 47994Sbill seed = 123; 48994Sbill for (i=0; i<13; i++) 49994Sbill seed = seed*buf[i] + i; 50*3981Smckusic for(i=0;i<ROTORSZ;i++) { 51994Sbill t1[i] = i; 52*3981Smckusic deck[i] = i; 53*3981Smckusic } 54994Sbill for(i=0;i<ROTORSZ;i++) { 55994Sbill seed = 5*seed + buf[i%13]; 56994Sbill random = seed % 65521; 57994Sbill k = ROTORSZ-1 - i; 58994Sbill ic = (random&MASK)%(k+1); 59994Sbill random >>= 8; 60994Sbill temp = t1[k]; 61994Sbill t1[k] = t1[ic]; 62994Sbill t1[ic] = temp; 63994Sbill if(t3[k]!=0) continue; 64994Sbill ic = (random&MASK) % k; 65994Sbill while(t3[ic]!=0) ic = (ic+1) % k; 66994Sbill t3[k] = ic; 67994Sbill t3[ic] = k; 68994Sbill } 69994Sbill for(i=0;i<ROTORSZ;i++) 70994Sbill t2[t1[i]&MASK] = i; 71994Sbill } 72994Sbill 73994Sbill main(argc, argv) 74994Sbill char *argv[]; 75994Sbill { 76*3981Smckusic register i, n1, n2, nr1, nr2; 77*3981Smckusic int secureflg = 0; 78994Sbill 79*3981Smckusic if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 's') { 80*3981Smckusic argc--; 81*3981Smckusic argv++; 82*3981Smckusic secureflg = 1; 83*3981Smckusic } 84994Sbill if (argc != 2){ 85994Sbill setup(getpass("Enter key:")); 86994Sbill } 87994Sbill else 88994Sbill setup(argv[1]); 89994Sbill n1 = 0; 90994Sbill n2 = 0; 91*3981Smckusic nr2 = 0; 92994Sbill 93994Sbill while((i=getchar()) >=0) { 94*3981Smckusic if (secureflg) { 95*3981Smckusic nr1 = deck[n1]&MASK; 96*3981Smckusic nr2 = deck[nr1]&MASK; 97*3981Smckusic } else { 98*3981Smckusic nr1 = n1; 99*3981Smckusic } 100*3981Smckusic i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1; 101994Sbill putchar(i); 102994Sbill n1++; 103994Sbill if(n1==ROTORSZ) { 104994Sbill n1 = 0; 105994Sbill n2++; 106994Sbill if(n2==ROTORSZ) n2 = 0; 107*3981Smckusic if (secureflg) { 108*3981Smckusic shuffle(deck); 109*3981Smckusic } else { 110*3981Smckusic nr2 = n2; 111*3981Smckusic } 112994Sbill } 113994Sbill } 114994Sbill } 115*3981Smckusic 116*3981Smckusic shuffle(deck) 117*3981Smckusic char deck[]; 118*3981Smckusic { 119*3981Smckusic int i, ic, k, temp; 120*3981Smckusic unsigned random; 121*3981Smckusic static long seed = 123; 122*3981Smckusic 123*3981Smckusic for(i=0;i<ROTORSZ;i++) { 124*3981Smckusic seed = 5*seed + buf[i%13]; 125*3981Smckusic random = seed % 65521; 126*3981Smckusic k = ROTORSZ-1 - i; 127*3981Smckusic ic = (random&MASK)%(k+1); 128*3981Smckusic temp = deck[k]; 129*3981Smckusic deck[k] = deck[ic]; 130*3981Smckusic deck[ic] = temp; 131*3981Smckusic } 132*3981Smckusic } 133