Annotation of researchv10dc/cmd/asd/crypt.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *     encryption service routines
        !             3:  */
        !             4: 
        !             5: #include <stdio.h>
        !             6: 
        !             7: /* encryption parameters and tables */
        !             8: #define ROTORSZ 256
        !             9: #define MASK 0377
        !            10: static char    t1[ROTORSZ];
        !            11: static char    t2[ROTORSZ];
        !            12: static char    t3[ROTORSZ];
        !            13: static char    t4[ROTORSZ];
        !            14: 
        !            15: /* current rotor settings */
        !            16: static int N1, N2;
        !            17: 
        !            18: resetN12()
        !            19: {
        !            20:        N1 = N2 = 0;
        !            21: }
        !            22: 
        !            23: setup(pw)
        !            24: char *pw;
        !            25: {
        !            26:        int ic, i, k, temp, pf[2];
        !            27:        unsigned random;
        !            28:        char buf[13];
        !            29:        long seed;
        !            30:        int pid;
        !            31: 
        !            32:        resetN12();
        !            33: 
        !            34:        strncpy(buf, pw, 8);
        !            35:        while (*pw)
        !            36:                *pw++ = '\0';
        !            37:        buf[8] = buf[0];
        !            38:        buf[9] = buf[1];
        !            39:        pipe(pf);
        !            40: 
        !            41:        switch (pid = fork()) {
        !            42: 
        !            43:        case -1:
        !            44:                fprintf (stderr, "seal: cannot fork\n");
        !            45:                exit (1);
        !            46: 
        !            47:        case 0:
        !            48:                close(0);
        !            49:                close(1);
        !            50:                dup(pf[0]);
        !            51:                dup(pf[1]);
        !            52:                execl("/usr/lib/makekey", "-", 0);
        !            53:                execl("/lib/makekey", "-", 0);
        !            54:                exit(1);
        !            55: 
        !            56:        default:
        !            57:                write(pf[1], buf, 10);
        !            58:                while (wait ((int *) NULL) != pid)
        !            59:                        ;
        !            60:        }
        !            61: 
        !            62:        if (read(pf[0], buf, 13) != 13) {
        !            63:                fprintf(stderr, "seal: cannot generate key\n");
        !            64:                exit(1);
        !            65:        }
        !            66:        seed = 123;
        !            67:        for (i=0; i<13; i++)
        !            68:                seed = seed*buf[i] + i;
        !            69:        for(i=0;i<ROTORSZ;i++)
        !            70:                t1[i] = i;
        !            71:        for(i=0;i<ROTORSZ;i++) {
        !            72:                seed = 5*seed + buf[i%13];
        !            73:                random = seed % 65521;
        !            74:                k = ROTORSZ-1 - i;
        !            75:                ic = (random&MASK)%(k+1);
        !            76:                random >>= 8;
        !            77:                temp = t1[k];
        !            78:                t1[k] = t1[ic];
        !            79:                t1[ic] = temp;
        !            80:                if(t3[k]!=0) continue;
        !            81:                ic = (random&MASK) % k;
        !            82:                while(t3[ic]!=0) ic = (ic+1) % k;
        !            83:                t3[k] = ic;
        !            84:                t3[ic] = k;
        !            85:        }
        !            86:        for(i=0;i<ROTORSZ;i++){
        !            87:                t2[t1[i]&MASK] = i;
        !            88:                t4[i] = (t1[i] + t3[i]) & 0377;
        !            89:        }
        !            90: }
        !            91: 
        !            92: mangle (buf, limit)
        !            93:        char *buf;
        !            94:        register char *limit;
        !            95: {
        !            96:        register int i;
        !            97:        register char *p;
        !            98:        register int n1 = N1, n2 = N2;
        !            99:        int n3;
        !           100: 
        !           101:        p = buf;
        !           102: 
        !           103:        while(p < limit) {
        !           104:                i = *p;
        !           105:                n3 = t4[n1];
        !           106:                i = t2[(t3[(t1[(i+n3)&MASK]+n2)&MASK]-n2)&MASK]-n3;
        !           107:                *p++ = i;
        !           108:                n1++;
        !           109:                if(n1==ROTORSZ) {
        !           110:                        n1 = 0;
        !           111:                        n2++;
        !           112:                        if(n2==ROTORSZ) n2 = 0;
        !           113:                }
        !           114:        }
        !           115:        N1 = n1;
        !           116:        N2 = n2;
        !           117: }

unix.superglobalmegacorp.com

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