# 2.11BSD#431 - /usr/src/usr.bin/crypt.c

##### Functions

    main              X
    setup             X
    shuffle           X

##### Variables

    buf               X
    deck              X
    t1                X
    t2                X
    t3                X

##### Macros

    ECHO              X
    MASK              X
    ROTORSZ           X

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

#### Defined functions

main defined in line 76; never used
setup defined in line 21; used 2 times

- in line 88-91(2)

shuffle defined in line 119; used 1 times

- in line 111

#### Defined variables

buf defined in line 19; used 10 times

- in line 29-33(5), 43-46(2), 52, 58, 127

deck defined in line 17; used 10 times

- in line 55, 98-99(2), 111, 119-120(2), 131-133(4)

sccsid defined in line 2; never used
t1 defined in line 14; used 7 times

- in line 54, 63-65(4), 73, 103

t2 defined in line 15; used 2 times

- in line 73, 103

t3 defined in line 16; used 5 times

- in line 66-70(4), 103

#### Defined macros

ECHO defined in line 10; never used
MASK defined in line 13; used 9 times

- in line 61, 67, 73, 98-103(5), 130

ROTORSZ defined in line 12; used 12 times

- in line 14-17(4), 53-60(3), 72, 106-109(2), 126-129(2)

Last modified: 1996-10-24 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1215 |