Annotation of researchv10no/cmd/odist/opkg/crypt.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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