1*17869Sralph static char *sccsid = "@(#)crypt.c 4.3 (Berkeley) 01/25/85"; 23981Smckusic 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]; 153981Smckusic char deck[ROTORSZ]; 16994Sbill char *getpass(); 173981Smckusic char buf[13]; 18994Sbill 19994Sbill setup(pw) 20994Sbill char *pw; 21994Sbill { 22994Sbill int ic, i, k, temp, pf[2]; 23*17869Sralph int pid, wpid; 24994Sbill unsigned random; 25994Sbill long seed; 26994Sbill 27994Sbill strncpy(buf, pw, 8); 28994Sbill while (*pw) 29994Sbill *pw++ = '\0'; 30994Sbill buf[8] = buf[0]; 31994Sbill buf[9] = buf[1]; 32994Sbill pipe(pf); 33*17869Sralph if ((pid=fork())==0) { 34994Sbill close(0); 35994Sbill close(1); 36994Sbill dup(pf[0]); 37994Sbill dup(pf[1]); 38994Sbill execl("/usr/lib/makekey", "-", 0); 39994Sbill execl("/lib/makekey", "-", 0); 40994Sbill exit(1); 41994Sbill } 42994Sbill write(pf[1], buf, 10); 43*17869Sralph while ((wpid = wait((int *)NULL)) != -1 && wpid != pid) 44*17869Sralph ; 45994Sbill if (read(pf[0], buf, 13) != 13) { 46994Sbill fprintf(stderr, "crypt: cannot generate key\n"); 47994Sbill exit(1); 48994Sbill } 49994Sbill seed = 123; 50994Sbill for (i=0; i<13; i++) 51994Sbill seed = seed*buf[i] + i; 523981Smckusic for(i=0;i<ROTORSZ;i++) { 53994Sbill t1[i] = i; 543981Smckusic deck[i] = i; 553981Smckusic } 56994Sbill for(i=0;i<ROTORSZ;i++) { 57994Sbill seed = 5*seed + buf[i%13]; 58994Sbill random = seed % 65521; 59994Sbill k = ROTORSZ-1 - i; 60994Sbill ic = (random&MASK)%(k+1); 61994Sbill random >>= 8; 62994Sbill temp = t1[k]; 63994Sbill t1[k] = t1[ic]; 64994Sbill t1[ic] = temp; 65994Sbill if(t3[k]!=0) continue; 66994Sbill ic = (random&MASK) % k; 67994Sbill while(t3[ic]!=0) ic = (ic+1) % k; 68994Sbill t3[k] = ic; 69994Sbill t3[ic] = k; 70994Sbill } 71994Sbill for(i=0;i<ROTORSZ;i++) 72994Sbill t2[t1[i]&MASK] = i; 73994Sbill } 74994Sbill 75994Sbill main(argc, argv) 76994Sbill char *argv[]; 77994Sbill { 783981Smckusic register i, n1, n2, nr1, nr2; 793981Smckusic int secureflg = 0; 80994Sbill 813981Smckusic if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 's') { 823981Smckusic argc--; 833981Smckusic argv++; 843981Smckusic secureflg = 1; 853981Smckusic } 86994Sbill if (argc != 2){ 87994Sbill setup(getpass("Enter key:")); 88994Sbill } 89994Sbill else 90994Sbill setup(argv[1]); 91994Sbill n1 = 0; 92994Sbill n2 = 0; 933981Smckusic nr2 = 0; 94994Sbill 95994Sbill while((i=getchar()) >=0) { 963981Smckusic if (secureflg) { 973981Smckusic nr1 = deck[n1]&MASK; 983981Smckusic nr2 = deck[nr1]&MASK; 993981Smckusic } else { 1003981Smckusic nr1 = n1; 1013981Smckusic } 1023981Smckusic i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1; 103994Sbill putchar(i); 104994Sbill n1++; 105994Sbill if(n1==ROTORSZ) { 106994Sbill n1 = 0; 107994Sbill n2++; 108994Sbill if(n2==ROTORSZ) n2 = 0; 1093981Smckusic if (secureflg) { 1103981Smckusic shuffle(deck); 1113981Smckusic } else { 1123981Smckusic nr2 = n2; 1133981Smckusic } 114994Sbill } 115994Sbill } 116994Sbill } 1173981Smckusic 1183981Smckusic shuffle(deck) 1193981Smckusic char deck[]; 1203981Smckusic { 1213981Smckusic int i, ic, k, temp; 1223981Smckusic unsigned random; 1233981Smckusic static long seed = 123; 1243981Smckusic 1253981Smckusic for(i=0;i<ROTORSZ;i++) { 1263981Smckusic seed = 5*seed + buf[i%13]; 1273981Smckusic random = seed % 65521; 1283981Smckusic k = ROTORSZ-1 - i; 1293981Smckusic ic = (random&MASK)%(k+1); 1303981Smckusic temp = deck[k]; 1313981Smckusic deck[k] = deck[ic]; 1323981Smckusic deck[ic] = temp; 1333981Smckusic } 1343981Smckusic } 135