Annotation of 43BSDTahoe/usr.bin/crypt.c, revision 1.1

1.1     ! root        1: static char *sccsid = "@(#)crypt.c     4.3 (Berkeley) 1/25/85";
        !             2: 
        !             3: /*
        !             4:  *     A one-rotor machine designed along the lines of Enigma
        !             5:  *     but considerably trivialized.
        !             6:  */
        !             7: 
        !             8: #define ECHO 010
        !             9: #include <stdio.h>
        !            10: #define ROTORSZ 256
        !            11: #define MASK 0377
        !            12: char   t1[ROTORSZ];
        !            13: char   t2[ROTORSZ];
        !            14: char   t3[ROTORSZ];
        !            15: char   deck[ROTORSZ];
        !            16: char   *getpass();
        !            17: char   buf[13];
        !            18: 
        !            19: setup(pw)
        !            20: char *pw;
        !            21: {
        !            22:        int ic, i, k, temp, pf[2];
        !            23:        int pid, wpid;
        !            24:        unsigned random;
        !            25:        long seed;
        !            26: 
        !            27:        strncpy(buf, pw, 8);
        !            28:        while (*pw)
        !            29:                *pw++ = '\0';
        !            30:        buf[8] = buf[0];
        !            31:        buf[9] = buf[1];
        !            32:        pipe(pf);
        !            33:        if ((pid=fork())==0) {
        !            34:                close(0);
        !            35:                close(1);
        !            36:                dup(pf[0]);
        !            37:                dup(pf[1]);
        !            38:                execl("/usr/lib/makekey", "-", 0);
        !            39:                execl("/lib/makekey", "-", 0);
        !            40:                exit(1);
        !            41:        }
        !            42:        write(pf[1], buf, 10);
        !            43:        while ((wpid = wait((int *)NULL)) != -1 && wpid != pid)
        !            44:            ;
        !            45:        if (read(pf[0], buf, 13) != 13) {
        !            46:                fprintf(stderr, "crypt: cannot generate key\n");
        !            47:                exit(1);
        !            48:        }
        !            49:        seed = 123;
        !            50:        for (i=0; i<13; i++)
        !            51:                seed = seed*buf[i] + i;
        !            52:        for(i=0;i<ROTORSZ;i++) {
        !            53:                t1[i] = i;
        !            54:                deck[i] = i;
        !            55:        }
        !            56:        for(i=0;i<ROTORSZ;i++) {
        !            57:                seed = 5*seed + buf[i%13];
        !            58:                random = seed % 65521;
        !            59:                k = ROTORSZ-1 - i;
        !            60:                ic = (random&MASK)%(k+1);
        !            61:                random >>= 8;
        !            62:                temp = t1[k];
        !            63:                t1[k] = t1[ic];
        !            64:                t1[ic] = temp;
        !            65:                if(t3[k]!=0) continue;
        !            66:                ic = (random&MASK) % k;
        !            67:                while(t3[ic]!=0) ic = (ic+1) % k;
        !            68:                t3[k] = ic;
        !            69:                t3[ic] = k;
        !            70:        }
        !            71:        for(i=0;i<ROTORSZ;i++)
        !            72:                t2[t1[i]&MASK] = i;
        !            73: }
        !            74: 
        !            75: main(argc, argv)
        !            76: char *argv[];
        !            77: {
        !            78:        register i, n1, n2, nr1, nr2;
        !            79:        int secureflg = 0;
        !            80: 
        !            81:        if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 's') {
        !            82:                argc--;
        !            83:                argv++;
        !            84:                secureflg = 1;
        !            85:        }
        !            86:        if (argc != 2){
        !            87:                setup(getpass("Enter key:"));
        !            88:        }
        !            89:        else
        !            90:                setup(argv[1]);
        !            91:        n1 = 0;
        !            92:        n2 = 0;
        !            93:        nr2 = 0;
        !            94: 
        !            95:        while((i=getchar()) >=0) {
        !            96:                if (secureflg) {
        !            97:                        nr1 = deck[n1]&MASK;
        !            98:                        nr2 = deck[nr1]&MASK;
        !            99:                } else {
        !           100:                        nr1 = n1;
        !           101:                }
        !           102:                i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
        !           103:                putchar(i);
        !           104:                n1++;
        !           105:                if(n1==ROTORSZ) {
        !           106:                        n1 = 0;
        !           107:                        n2++;
        !           108:                        if(n2==ROTORSZ) n2 = 0;
        !           109:                        if (secureflg) {
        !           110:                                shuffle(deck);
        !           111:                        } else {
        !           112:                                nr2 = n2;
        !           113:                        }
        !           114:                }
        !           115:        }
        !           116: }
        !           117: 
        !           118: shuffle(deck)
        !           119:        char deck[];
        !           120: {
        !           121:        int i, ic, k, temp;
        !           122:        unsigned random;
        !           123:        static long seed = 123;
        !           124: 
        !           125:        for(i=0;i<ROTORSZ;i++) {
        !           126:                seed = 5*seed + buf[i%13];
        !           127:                random = seed % 65521;
        !           128:                k = ROTORSZ-1 - i;
        !           129:                ic = (random&MASK)%(k+1);
        !           130:                temp = deck[k];
        !           131:                deck[k] = deck[ic];
        !           132:                deck[ic] = temp;
        !           133:        }
        !           134: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.