home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / pc / crypto / descrypt.txt < prev    next >
Encoding:
Text File  |  2003-06-11  |  34.5 KB  |  968 lines

  1. Article  21
  2.  
  3. From: 2004dss@ucsbuxa.ucsb.edu (Diane Shelander)
  4. Newsgroups: sci.crypt
  5. Subject: Re: Password Probabilities
  6. Keywords: password, ASCII
  7. Message-ID: <7635@hub.ucsb.edu>
  8. Date: 4 Dec 90 06:22:44 GMT
  9. References: <1990Nov22.211909.29282@weitek.COM> <529@tygra.ddmi.com> <7577@hub.ucsb.edu>
  10. Sender: news@hub.ucsb.edu
  11. Lines: 954
  12.  
  13.  
  14.  
  15. The following is a shell archive of a program called des.  des provides
  16. NBS DEA traceable encryption and decryption using Electronic Code Book
  17. (ECB) mode.
  18.  
  19. The files include are:
  20.  
  21.     Makefile
  22.     des.c
  23.     getopt.c   (public domain)
  24.     des.test   test vectors derived from NBS Special Pub 5000-20
  25.     des.1      man page for des (may be viewed with psview and psroff
  26.                                  see Makefile)
  27.  
  28. The des uses libcrypt which may not be functional for decryption on your
  29. system.  See des.1 (appended).  The crypt() call is used for initializing
  30. the E permutation array, as it is not otherwise initialized, resulting in
  31. the use of R[0] only in f(R,K), which isn't very secure.  The test vectors
  32. are included for such a reason, in case someone wants to play with their
  33. version of libcrypt.  This program is similar to descrypt.c by Stephen
  34. Kochan and Patrick Wood in "Unix System Security" (Hayden Books).  I wrote
  35. the code several years ago, but resurrected it when I saw the bit in byte
  36. ordering wrong in descrypt.c.  The test vector feature (-t) (see des.1)
  37. can be used or modified for generating keys for a key management system
  38. for a secure network.
  39.  
  40. des can accept either an ascii key or a hex key, and uses it untransformed.
  41. (descrypt.c uses a crypt() call to stir up the key).
  42.  
  43.  
  44. Using libcrypt, des runs on my IRIS 4D-25/G at about 2.8 kbytes/sec.
  45. I have a faster impelementation of the Data Encryption Algorithm and
  46. a new version of des, but am not ready to release it.  The new version
  47. runs at about 10 kbytes/sec.
  48.  
  49. -------- cut here -------
  50. #  /bin/sh only
  51. cat << SH_EOF > Makefile
  52. #
  53. .SUFFIXES: .prt  .vw .1
  54. SRCS = des.c getopt.c
  55.  
  56. UNDER_RCS = $(SRCS) des.test des.1
  57.  
  58. all:   des
  59.  
  60. neat:
  61.        rm -f *.o *BAK .emacs*
  62.  
  63. clobber: neat
  64.        rm -f des
  65.  
  66. ci:
  67.        ci -f -u $(UNDER_RCS)
  68.  
  69. firstci: rcsdir ci
  70.        rcs -q -U $(UNDER_RCS)
  71.        chmod u+w $(UNDER_RCS)
  72. rcsdir:
  73.        mkdir RCS
  74.  
  75. des: des.o getopt.o
  76.        cc -o des  des.c getopt.o -lcrypt
  77.        strip des
  78.  
  79. getopt.o: getopt.c
  80.        cc -c getopt.o getopt.c
  81.  
  82. .1.prt:
  83.        tbl $*.1 | psroff -man -h
  84.  
  85. .1.vw:
  86.        tbl $*.1 | psroff -man -t | psview -F1.0
  87. SH_EOF
  88. cat << SH_EOF > des.c
  89. /*
  90.  * Copyright (c) 1990 David G. Koontz.
  91.  * All rights reserved.
  92.  *
  93.  * Redistribution and use in source and binary forms are permitted
  94.  * provided that the above copyright notice and this paragraph are
  95.  * duplicated in all such forms and that any documentation,
  96.  * advertisiing materials, and other materials related to such
  97.  * distribution and use acknowledge that the software was developed
  98.  * by the above mentioned individual.
  99.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  100.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  101.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE
  102.  */
  103. #ifndef lint
  104. char copyright[]=
  105.     "@(#) Copyright (c) 1990 David G. Koontz\n All rights reserved.\n";
  106. #endif
  107.  
  108. /*  des -- perform encryption/decryption using DES algorithm */
  109.  
  110. #define TRUE           1
  111. #define FALSE          0
  112. #define SHIFT          1
  113. #define NOSHIFT                0
  114. extern int  optind,opterr;
  115. extern char *optarg;
  116. #include <stdio.h>
  117. #include <string.h>
  118. #include <sys/types.h>
  119. char *getpass();
  120.  
  121. static char null_salt[8] = {0x2e,0x2e,0,0,0,0,0,0};
  122.  
  123. #define BYTES_PER_BLOCK            8
  124. #define NYBBLES_PER_BLOCK   16
  125.  
  126. #define NUMBER_OF_KEY_BYTES 8
  127. #define BITS_PER_NYBBLE            4
  128. #define BITS_PER_BYTE      8
  129. #define BITS_PER_BLOCK     64
  130. #define BIT(x)             ( 1 << x)
  131. #define TODEC              16
  132. #define ENCRYPT                    0
  133. #define DECRYPT                    1
  134.  
  135. main (argc,argv)
  136. int argc;
  137. char **argv;
  138. {
  139.     unsigned char input[BYTES_PER_BLOCK], output[BYTES_PER_BLOCK];
  140.     unsigned char bits[BITS_PER_BLOCK];
  141.     unsigned char key[NUMBER_OF_KEY_BYTES];
  142.     unsigned char testinput[128];
  143.     unsigned char testkey[NYBBLES_PER_BLOCK+1];
  144.     unsigned char testplain[NYBBLES_PER_BLOCK+1];
  145.     unsigned char testcipher[NYBBLES_PER_BLOCK+1];
  146.     unsigned char testresult[NYBBLES_PER_BLOCK+1];
  147.     int len,c;
  148.     int verbose = FALSE;
  149.     int testerrors = 0;
  150.     int totalerrors = 0;
  151.     int testcount = 0;
  152.     int needkey = TRUE;
  153.     int desmode = ENCRYPT;
  154.     int testmode = FALSE;
  155.     char padchar = 0x20;       /* default pad character is space */
  156.  
  157.     if (argc < 2)
  158.        goto usage;
  159.  
  160.     crypt(null_salt,null_salt);              /* initialize the E[] array */
  161.  
  162.     while (( c = getopt(argc,argv,"edk:K:p:tv")) != EOF) {
  163.        switch (c){
  164.            case 'e':
  165.                desmode = ENCRYPT;
  166.                break;
  167.            case 'd':
  168.                desmode = DECRYPT;
  169.                break;
  170.            case 'k':
  171.                if (needkey) {
  172.                    needkey = FALSE;
  173.                    if ( (c = optarg[0]) == '-')  /* prevent lost data */
  174.                        goto usage;
  175.                    if ( (c = strlen(optarg)) < 8) {
  176.                        fprintf(stderr,"%s: key must be 8 char\n",argv[0]);
  177.                        exit(1);
  178.                    }
  179.                    loadkey(optarg,SHIFT);
  180.                }
  181.                else {
  182.                    fprintf(stderr,"%s: too many keys\n",argv[0]);
  183.                    exit(1);
  184.                }
  185.                break;
  186.            case 'K':
  187.                if ( needkey ) {
  188.                    needkey = FALSE;
  189.                    strxtoc(optarg,key);  /* will complain about bad format */
  190.                    while (*optarg) *optarg++ = 0;
  191.                    loadkey(key,NOSHIFT);
  192.                }
  193.                else {
  194.                    fprintf(stderr,"%s: too many keys\n",argv[0]);
  195.                    exit(1);
  196.                }
  197.                break;
  198.            case 'p':
  199.                padchar = (unsigned char) strtol(optarg,0,TODEC);
  200.                break;
  201.            case 't':
  202.                testmode = TRUE;
  203.                break;
  204.            case 'v':
  205.                verbose = TRUE;
  206.                break;
  207.            case '?':
  208. usage:         fprintf(stderr,"Usage: %s -e | -d ",argv[0]);
  209.                fprintf(stderr,"[-k key | -K hex_key] ");
  210.                fprintf(stderr,"[-p hex_pad_char]\n\n");
  211.                fprintf(stderr,"   Or: %s -t [-v]\n\n",argv[0]);
  212.                exit(1);
  213.                break;
  214.        }
  215.     }
  216.     if (needkey && !testmode ) {
  217.        strncpy(key,getpass("key: "),8);
  218.        if ( (c = strlen(key)) < 8)  {
  219.            fprintf(stderr,"%s: key must be 8 char\n",argv[0]);
  220.            exit(1);
  221.         }
  222.        loadkey(key,SHIFT);
  223.     }
  224.  
  225.     if ( !testmode)        /* regular operation loop */
  226.  
  227.        while ((len = fread(input, 1, BYTES_PER_BLOCK, stdin)) > 0) {
  228.            if ( len < BYTES_PER_BLOCK ) {      /* encrypt mode only */
  229.                while (len < BYTES_PER_BLOCK)
  230.                input[len++]=padchar;
  231.            }
  232.            bytes_to_bits(input, bits);
  233.            encrypt(bits,desmode);
  234.            bits_to_bytes(bits,output);
  235.            fwrite(output, 1, BYTES_PER_BLOCK, stdout);
  236.        }
  237.     else    /* test mode */
  238.     {
  239.        while (fgets(testinput,(sizeof testinput) -1, stdin) != NULL ) {
  240.  
  241.            if ( strncmp(testinput,"encrypt",7) == 0) { /* mode = encode */
  242.                desmode = ENCRYPT;
  243.                fprintf(stderr,"%s",testinput);
  244.            }
  245.            else
  246.            if ( strncmp(testinput,"decrypt",7) == 0) { /* mode = decode */
  247.                fprintf(stderr,"%s",testinput);
  248.                desmode = DECRYPT;
  249.            }
  250.            else
  251.            if ( strncmp(testinput," ",1) == 0) { /* key, plain & cipher */
  252.                testcount++;
  253.                len = sscanf(testinput,"%s%s%s*",
  254.                    testkey, testplain, testcipher);
  255.                if ( verbose )  {
  256.                    fprintf(stderr," %s %s %s\n", testkey, testplain,
  257.                                                           testcipher);
  258.                }
  259.                strxtoc(testkey,key);
  260.                loadkey(key,NOSHIFT);
  261.                strxtoc(testplain,input);
  262.                bytes_to_bits(input,bits);
  263.                encrypt(bits,desmode);
  264.                bits_to_bytes(bits,output);
  265.                strctox(output,testresult);
  266.                if ( (len = strncasecmp(testcipher,testresult,16)) != 0 ) {
  267.                    fprintf(stderr,"Test: %d -- ERROR expected %s got %s\n",
  268.                            testcount,testcipher,testresult);
  269.                    testerrors++;
  270.                }
  271.            }
  272.            else {                                /* nothing but eyewash */
  273.                if ( testcount ) {
  274.                    fprintf(stderr," %d tests performed\n",testcount);
  275.                    fprintf(stderr," ERRORS on these tests : %d\n",testerrors);
  276.                    totalerrors +=testerrors;
  277.                    testcount = 0;
  278.                    testerrors = 0;
  279.                }
  280.                fprintf(stderr,"%s",testinput);
  281.            }
  282.        }
  283.     fprintf(stderr,"Total Errors = %d\n",totalerrors);
  284.     }
  285.     return(0);
  286. }
  287.  
  288. loadkey(key,shift)
  289. char *key;
  290. int shift;
  291. {
  292.     int i;
  293.     char bits[BITS_PER_BLOCK];
  294.     if (shift)
  295.        for (i=0;i<NUMBER_OF_KEY_BYTES;i++)
  296.            key[i] = (key[i] << 1);
  297.     bytes_to_bits (key, bits);
  298.     setkey(bits);
  299.     while (*key) *key++ = 0;
  300. }
  301.  
  302. bytes_to_bits (bytes,bits)
  303. unsigned char *bytes,*bits;
  304. {
  305. register int i,j,k;
  306.  
  307.     for(i=0,j=0;i < BYTES_PER_BLOCK;i++) {
  308.        for (k=7;k >= 0;k--) {
  309.            if (bytes[i] & BIT(k))
  310.                bits[j++]=1;
  311.            else
  312.                bits[j++]=0;
  313.        }
  314.     }
  315. }
  316.  
  317. bits_to_bytes (bits,bytes)
  318. unsigned char *bits,*bytes;
  319. {
  320. register int i,j,k;
  321.  
  322.     for (i=0,j=0;i < BYTES_PER_BLOCK;i++)
  323.        bytes[i]=0;
  324.  
  325.     for (i=0,j=0;i < BITS_PER_BYTE;i++) {
  326.        for (k=7;k >= 0;k--)
  327.            if(bits[j++])
  328.                bytes[i] |= BIT(k);
  329.     }
  330. }
  331.  
  332. strxtoc(hexstr,charstr)  /* converts 16 hex digit strings to char strings */
  333. char *hexstr,*charstr;
  334. {
  335. #define UPPER_NYBBLE   ( !(val & 1) )
  336.  
  337.     unsigned char c;
  338.     int val;
  339.     for (val = 0; val < NYBBLES_PER_BLOCK;val++) {
  340.        if ((hexstr[val] >= '0') && (hexstr[val] <= '9'))
  341.            if (UPPER_NYBBLE)
  342.                c = (hexstr[val] - '0') << BITS_PER_NYBBLE;
  343.            else
  344.                c += hexstr[val] - '0';
  345.        else
  346.        if ((hexstr[val] >= 'A') && (hexstr[val] <= 'F'))
  347.            if (UPPER_NYBBLE)
  348.                c = (hexstr[val] - 'A' +10) << BITS_PER_NYBBLE;
  349.            else
  350.                c += hexstr[val] - 'A' +10;
  351.        else
  352.        if ((hexstr[val] >= 'a') && (hexstr[val] <= 'f'))
  353.            if (UPPER_NYBBLE)
  354.                c = (hexstr[val] - 'a' +10) << BITS_PER_NYBBLE;
  355.            else
  356.                c += hexstr[val] - 'a' +10;
  357.        else {
  358.            fprintf(stderr,"hex conversion error: %s - char %d\n",hexstr,val);
  359.            if ((val = strlen(hexstr)) != NYBBLES_PER_BLOCK)
  360.                fprintf(stderr,"hex string length != 16\n");
  361.            exit(1);
  362.        }
  363.        if ( UPPER_NYBBLE)          /* 2nd nybble of each char */
  364.            charstr[val>>1] = 0;
  365.        else
  366.            charstr[val>>1] = c;
  367.     }
  368. }
  369.  
  370. strctox(charstr,hexstr)  /* converts 8 char string to 16 hex digit string */
  371. char *charstr,*hexstr;
  372. {
  373.     unsigned char c;
  374.     int i;
  375.     for (i = 0; i < 8; i++) {
  376.        c = charstr[i] >> BITS_PER_NYBBLE;  /* uppper nybble */
  377.        if ( c <= 9)
  378.            *hexstr++ = c + '0';
  379.        else
  380.            *hexstr++ = c + '7';
  381.  
  382.        c = (charstr[i] & 0xf);
  383.        if ( c <= 9)
  384.            *hexstr++ = c + '0';
  385.        else
  386.            *hexstr++ = c + '7';
  387.  
  388.     }
  389.     *hexstr = 0;           /* following NULL terminator */
  390. }
  391. SH_EOF
  392. cat << SH_EOF > getopt.c
  393. #define NULL   0
  394. #define EOF    (-1)
  395. #define ERR(s, c)      if(opterr){\
  396.        extern int strlen(), write();\
  397.        char errbuf[2];\
  398.        errbuf[0] = c; errbuf[1] = '\n';\
  399.        (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  400.        (void) write(2, s, (unsigned)strlen(s));\
  401.        (void) write(2, errbuf, 2);}
  402.  
  403. extern int strcmp();
  404. /* bsd based system will require index */
  405. #define strchr index
  406. extern char *strchr();
  407.  
  408. int    opterr = 1;
  409. int    optind = 1;
  410. int    optopt;
  411. char   *optarg;
  412.  
  413. int
  414. getopt(argc, argv, opts)
  415. int    argc;
  416. char   **argv, *opts;
  417. {
  418.        static int sp = 1;
  419.        register int c;
  420.        register char *cp;
  421.  
  422.        if(sp == 1)
  423.                if(optind >= argc ||
  424.                   argv[optind][0] != '-' || argv[optind][1] == '\0')
  425.                        return(EOF);
  426.                else if(strcmp(argv[optind], "--") == NULL) {
  427.                        optind++;
  428.                        return(EOF);
  429.                }
  430.        optopt = c = argv[optind][sp];
  431.        if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  432.                ERR(": illegal option -- ", c);
  433.                if(argv[optind][++sp] == '\0') {
  434.                        optind++;
  435.                        sp = 1;
  436.                }
  437.                return('?');
  438.        }
  439.        if(*++cp == ':') {
  440.                if(argv[optind][sp+1] != '\0')
  441.                        optarg = &argv[optind++][sp+1];
  442.                else if(++optind >= argc) {
  443.                        ERR(": option requires an argument -- ", c);
  444.                        sp = 1;
  445.                        return('?');
  446.                } else
  447.                        optarg = argv[optind++];
  448.                sp = 1;
  449.        } else {
  450.                if(argv[optind][++sp] == '\0') {
  451.                        sp = 1;
  452.                        optind++;
  453.                }
  454.                optarg = NULL;
  455.        }
  456.        return(c);
  457. }
  458. SH_EOF
  459. cat << SH_EOF > des.test
  460. # Test vectors for DES Electronic Code Book (ECB)
  461. # implementation, derived from:
  462. #   "Validating the Correctness of Hardware
  463. #   Implementations of the NBS Data Encryption Standard"
  464. #   NBS Special Publication 500-20, 1980.
  465. #
  466. #    Initial Permutation and Expansion test
  467. #
  468. encrypt
  469. #
  470.  0101010101010101 95F8A5E5DD31D900 8000000000000000
  471.  0101010101010101 DD7F121CA5015619 4000000000000000
  472.  0101010101010101 2E8653104F3834EA 2000000000000000
  473.  0101010101010101 4BD388FF6CD81D4F 1000000000000000
  474.  0101010101010101 20B9E767B2FB1456 0800000000000000
  475.  0101010101010101 55579380D77138EF 0400000000000000
  476.  0101010101010101 6CC5DEFAAF04512F 0200000000000000
  477.  0101010101010101 0D9F279BA5D87260 0100000000000000
  478.  0101010101010101 D9031B0271BD5A0A 0080000000000000
  479.  0101010101010101 424250B37C3DD951 0040000000000000
  480.  0101010101010101 B8061B7ECD9A21E5 0020000000000000
  481.  0101010101010101 F15D0F286B65BD28 0010000000000000
  482.  0101010101010101 ADD0CC8D6E5DEBA1 0008000000000000
  483.  0101010101010101 E6D5F82752AD63D1 0004000000000000
  484.  0101010101010101 ECBFE3BD3F591A5E 0002000000000000
  485.  0101010101010101 F356834379D165CD 0001000000000000
  486.  0101010101010101 2B9F982F20037FA9 0000800000000000
  487.  0101010101010101 889DE068A16F0BE6 0000400000000000
  488.  0101010101010101 E19E275D846A1298 0000200000000000
  489.  0101010101010101 329A8ED523D71AEC 0000100000000000
  490.  0101010101010101 E7FCE22557D23C97 0000080000000000
  491.  0101010101010101 12A9F5817FF2D65D 0000040000000000
  492.  0101010101010101 A484C3AD38DC9C19 0000020000000000
  493.  0101010101010101 FBE00A8A1EF8AD72 0000010000000000
  494.  0101010101010101 750D079407521363 0000008000000000
  495.  0101010101010101 64FEED9C724C2FAF 0000004000000000
  496.  0101010101010101 F02B263B328E2B60 0000002000000000
  497.  0101010101010101 9D64555A9A10B852 0000001000000000
  498.  0101010101010101 D106FF0BED5255D7 0000000800000000
  499.  0101010101010101 E1652C6B138C64A5 0000000400000000
  500.  0101010101010101 E428581186EC8F46 0000000200000000
  501.  0101010101010101 AEB5F5EDE22D1A36 0000000100000000
  502.  0101010101010101 E943D7568AEC0C5C 0000000080000000
  503.  0101010101010101 DF98C8276F54B04B 0000000040000000
  504.  0101010101010101 B160E4680F6C696F 0000000020000000
  505.  0101010101010101 FA0752B07D9C4AB8 0000000010000000
  506.  0101010101010101 CA3A2B036DBC8502 0000000008000000
  507.  0101010101010101 5E0905517BB59BCF 0000000004000000
  508.  0101010101010101 814EEB3B91D90726 0000000002000000
  509.  0101010101010101 4D49DB1532919C9F 0000000001000000
  510.  0101010101010101 25EB5FC3F8CF0621 0000000000800000
  511.  0101010101010101 AB6A20C0620D1C6F 0000000000400000
  512.  0101010101010101 79E90DBC98F92CCA 0000000000200000
  513.  0101010101010101 866ECEDD8072BB0E 0000000000100000
  514.  0101010101010101 8B54536F2F3E64A8 0000000000080000
  515.  0101010101010101 EA51D3975595B86B 0000000000040000
  516.  0101010101010101 CAFFC6AC4542DE31 0000000000020000
  517.  0101010101010101 8DD45A2DDF90796C 0000000000010000
  518.  0101010101010101 1029D55E880EC2D0 0000000000008000
  519.  0101010101010101 5D86CB23639DBEA9 0000000000004000
  520.  0101010101010101 1D1CA853AE7C0C5F 0000000000002000
  521.  0101010101010101 CE332329248F3228 0000000000001000
  522.  0101010101010101 8405D1ABE24FB942 0000000000000800
  523.  0101010101010101 E643D78090CA4207 0000000000000400
  524.  0101010101010101 48221B9937748A23 0000000000000200
  525.  0101010101010101 DD7C0BBD61FAFD54 0000000000000100
  526.  0101010101010101 2FBC291A570DB5C4 0000000000000080
  527.  0101010101010101 E07C30D7E4E26E12 0000000000000040
  528.  0101010101010101 0953E2258E8E90A1 0000000000000020
  529.  0101010101010101 5B711BC4CEEBF2EE 0000000000000010
  530.  0101010101010101 CC083F1E6D9E85F6 0000000000000008
  531.  0101010101010101 D2FD8867D50D2DFE 0000000000000004
  532.  0101010101010101 06E7EA22CE92708F 0000000000000002
  533.  0101010101010101 166B40B44ABA4BD6 0000000000000001
  534. #
  535. #    Inverse Permutation and Expansion test
  536. #
  537. encrypt
  538. #
  539.  0101010101010101 8000000000000000 95F8A5E5DD31D900
  540.  0101010101010101 4000000000000000 DD7F121CA5015619
  541.  0101010101010101 2000000000000000 2E8653104F3834EA
  542.  0101010101010101 1000000000000000 4BD388FF6CD81D4F
  543.  0101010101010101 0800000000000000 20B9E767B2FB1456
  544.  0101010101010101 0400000000000000 55579380D77138EF
  545.  0101010101010101 0200000000000000 6CC5DEFAAF04512F
  546.  0101010101010101 0100000000000000 0D9F279BA5D87260
  547.  0101010101010101 0080000000000000 D9031B0271BD5A0A
  548.  0101010101010101 0040000000000000 424250B37C3DD951
  549.  0101010101010101 0020000000000000 B8061B7ECD9A21E5
  550.  0101010101010101 0010000000000000 F15D0F286B65BD28
  551.  0101010101010101 0008000000000000 ADD0CC8D6E5DEBA1
  552.  0101010101010101 0004000000000000 E6D5F82752AD63D1
  553.  0101010101010101 0002000000000000 ECBFE3BD3F591A5E
  554.  0101010101010101 0001000000000000 F356834379D165CD
  555.  0101010101010101 0000800000000000 2B9F982F20037FA9
  556.  0101010101010101 0000400000000000 889DE068A16F0BE6
  557.  0101010101010101 0000200000000000 E19E275D846A1298
  558.  0101010101010101 0000100000000000 329A8ED523D71AEC
  559.  0101010101010101 0000080000000000 E7FCE22557D23C97
  560.  0101010101010101 0000040000000000 12A9F5817FF2D65D
  561.  0101010101010101 0000020000000000 A484C3AD38DC9C19
  562.  0101010101010101 0000010000000000 FBE00A8A1EF8AD72
  563.  0101010101010101 0000008000000000 750D079407521363
  564.  0101010101010101 0000004000000000 64FEED9C724C2FAF
  565.  0101010101010101 0000002000000000 F02B263B328E2B60
  566.  0101010101010101 0000001000000000 9D64555A9A10B852
  567.  0101010101010101 0000000800000000 D106FF0BED5255D7
  568.  0101010101010101 0000000400000000 E1652C6B138C64A5
  569.  0101010101010101 0000000200000000 E428581186EC8F46
  570.  0101010101010101 0000000100000000 AEB5F5EDE22D1A36
  571.  0101010101010101 0000000080000000 E943D7568AEC0C5C
  572.  0101010101010101 0000000040000000 DF98C8276F54B04B
  573.  0101010101010101 0000000020000000 B160E4680F6C696F
  574.  0101010101010101 0000000010000000 FA0752B07D9C4AB8
  575.  0101010101010101 0000000008000000 CA3A2B036DBC8502
  576.  0101010101010101 0000000004000000 5E0905517BB59BCF
  577.  0101010101010101 0000000002000000 814EEB3B91D90726
  578.  0101010101010101 0000000001000000 4D49DB1532919C9F
  579.  0101010101010101 0000000000800000 25EB5FC3F8CF0621
  580.  0101010101010101 0000000000400000 AB6A20C0620D1C6F
  581.  0101010101010101 0000000000200000 79E90DBC98F92CCA
  582.  0101010101010101 0000000000100000 866ECEDD8072BB0E
  583.  0101010101010101 0000000000080000 8B54536F2F3E64A8
  584.  0101010101010101 0000000000040000 EA51D3975595B86B
  585.  0101010101010101 0000000000020000 CAFFC6AC4542DE31
  586.  0101010101010101 0000000000010000 8DD45A2DDF90796C
  587.  0101010101010101 0000000000008000 1029D55E880EC2D0
  588.  0101010101010101 0000000000004000 5D86CB23639DBEA9
  589.  0101010101010101 0000000000002000 1D1CA853AE7C0C5F
  590.  0101010101010101 0000000000001000 CE332329248F3228
  591.  0101010101010101 0000000000000800 8405D1ABE24FB942
  592.  0101010101010101 0000000000000400 E643D78090CA4207
  593.  0101010101010101 0000000000000200 48221B9937748A23
  594.  0101010101010101 0000000000000100 DD7C0BBD61FAFD54
  595.  0101010101010101 0000000000000080 2FBC291A570DB5C4
  596.  0101010101010101 0000000000000040 E07C30D7E4E26E12
  597.  0101010101010101 0000000000000020 0953E2258E8E90A1
  598.  0101010101010101 0000000000000010 5B711BC4CEEBF2EE
  599.  0101010101010101 0000000000000008 CC083F1E6D9E85F6
  600.  0101010101010101 0000000000000004 D2FD8867D50D2DFE
  601.  0101010101010101 0000000000000002 06E7EA22CE92708F
  602.  0101010101010101 0000000000000001 166B40B44ABA4BD6
  603. #
  604. #   Key Permutation tests
  605. #
  606. encrypt
  607. #
  608.  8001010101010101 0000000000000000 95A8D72813DAA94D
  609.  4001010101010101 0000000000000000 0EEC1487DD8C26D5
  610.  2001010101010101 0000000000000000 7AD16FFB79C45926
  611.  1001010101010101 0000000000000000 D3746294CA6A6CF3
  612.  0801010101010101 0000000000000000 809F5F873C1FD761
  613.  0401010101010101 0000000000000000 C02FAFFEC989D1FC
  614.  0201010101010101 0000000000000000 4615AA1D33E72F10
  615.  0180010101010101 0000000000000000 2055123350C00858
  616.  0140010101010101 0000000000000000 DF3B99D6577397C8
  617.  0120010101010101 0000000000000000 31FE17369B5288C9
  618.  0110010101010101 0000000000000000 DFDD3CC64DAE1642
  619.  0108010101010101 0000000000000000 178C83CE2B399D94
  620.  0104010101010101 0000000000000000 50F636324A9B7F80
  621.  0102010101010101 0000000000000000 A8468EE3BC18F06D
  622.  0101800101010101 0000000000000000 A2DC9E92FD3CDE92
  623.  0101400101010101 0000000000000000 CAC09F797D031287
  624.  0101200101010101 0000000000000000 90BA680B22AEB525
  625.  0101100101010101 0000000000000000 CE7A24F350E280B6
  626.  0101080101010101 0000000000000000 882BFF0AA01A0B87
  627.  0101040101010101 0000000000000000 25610288924511C2
  628.  0101020101010101 0000000000000000 C71516C29C75D170
  629.  0101018001010101 0000000000000000 5199C29A52C9F059
  630.  0101014001010101 0000000000000000 C22F0A294A71F29F
  631.  0101012001010101 0000000000000000 EE371483714C02EA
  632.  0101011001010101 0000000000000000 A81FBD448F9E522F
  633.  0101010801010101 0000000000000000 4F644C92E192DFED
  634.  0101010401010101 0000000000000000 1AFA9A66A6DF92AE
  635.  0101010201010101 0000000000000000 B3C1CC715CB879D8
  636.  0101010180010101 0000000000000000 19D032E64AB0BD8B
  637.  0101010140010101 0000000000000000 3CFAA7A7DC8720DC
  638.  0101010120010101 0000000000000000 B7265F7F447AC6F3
  639.  0101010110010101 0000000000000000 9DB73B3C0D163F54
  640.  0101010108010101 0000000000000000 8181B65BABF4A975
  641.  0101010104010101 0000000000000000 93C9B64042EAA240
  642.  0101010102010101 0000000000000000 5570530829705592
  643.  0101010101800101 0000000000000000 8638809E878787A0
  644.  0101010101400101 0000000000000000 41B9A79AF79AC208
  645.  0101010101200101 0000000000000000 7A9BE42F2009A892
  646.  0101010101100101 0000000000000000 29038D56BA6D2745
  647.  0101010101080101 0000000000000000 5495C6ABF1E5DF51
  648.  0101010101040101 0000000000000000 AE13DBD561488933
  649.  0101010101020101 0000000000000000 024D1FFA8904E389
  650.  0101010101018001 0000000000000000 D1399712F99BF02E
  651.  0101010101014001 0000000000000000 14C1D7C1CFFEC79E
  652.  0101010101012001 0000000000000000 1DE5279DAE3BED6F
  653.  0101010101011001 0000000000000000 E941A33F85501303
  654.  0101010101010801 0000000000000000 DA99DBBC9A03F379
  655.  0101010101010401 0000000000000000 B7FC92F91D8E92E9
  656.  0101010101010201 0000000000000000 AE8E5CAA3CA04E85
  657.  0101010101010180 0000000000000000 9CC62DF43B6EED74
  658.  0101010101010140 0000000000000000 D863DBB5C59A91A0
  659.  0101010101010120 0000000000000000 A1AB2190545B91D7
  660.  0101010101010110 0000000000000000 0875041E64C570F7
  661.  0101010101010108 0000000000000000 5A594528BEBEF1CC
  662.  0101010101010104 0000000000000000 FCDB3291DE21F0C0
  663.  0101010101010102 0000000000000000 869EFD7F9F265A09
  664. #
  665. #   Test of right-shifts in Decryption
  666. #
  667. decrypt
  668. #
  669.  8001010101010101 95A8D72813DAA94D 0000000000000000
  670.  4001010101010101 0EEC1487DD8C26D5 0000000000000000
  671.  2001010101010101 7AD16FFB79C45926 0000000000000000
  672.  1001010101010101 D3746294CA6A6CF3 0000000000000000
  673.  0801010101010101 809F5F873C1FD761 0000000000000000
  674.  0401010101010101 C02FAFFEC989D1FC 0000000000000000
  675.  0201010101010101 4615AA1D33E72F10 0000000000000000
  676.  0180010101010101 2055123350C00858 0000000000000000
  677.  0140010101010101 DF3B99D6577397C8 0000000000000000
  678.  0120010101010101 31FE17369B5288C9 0000000000000000
  679.  0110010101010101 DFDD3CC64DAE1642 0000000000000000
  680.  0108010101010101 178C83CE2B399D94 0000000000000000
  681.  0104010101010101 50F636324A9B7F80 0000000000000000
  682.  0102010101010101 A8468EE3BC18F06D 0000000000000000
  683.  0101800101010101 A2DC9E92FD3CDE92 0000000000000000
  684.  0101400101010101 CAC09F797D031287 0000000000000000
  685.  0101200101010101 90BA680B22AEB525 0000000000000000
  686.  0101100101010101 CE7A24F350E280B6 0000000000000000
  687.  0101080101010101 882BFF0AA01A0B87 0000000000000000
  688.  0101040101010101 25610288924511C2 0000000000000000
  689.  0101020101010101 C71516C29C75D170 0000000000000000
  690.  0101018001010101 5199C29A52C9F059 0000000000000000
  691.  0101014001010101 C22F0A294A71F29F 0000000000000000
  692.  0101012001010101 EE371483714C02EA 0000000000000000
  693.  0101011001010101 A81FBD448F9E522F 0000000000000000
  694.  0101010801010101 4F644C92E192DFED 0000000000000000
  695.  0101010401010101 1AFA9A66A6DF92AE 0000000000000000
  696.  0101010201010101 B3C1CC715CB879D8 0000000000000000
  697.  0101010180010101 19D032E64AB0BD8B 0000000000000000
  698.  0101010140010101 3CFAA7A7DC8720DC 0000000000000000
  699.  0101010120010101 B7265F7F447AC6F3 0000000000000000
  700.  0101010110010101 9DB73B3C0D163F54 0000000000000000
  701.  0101010108010101 8181B65BABF4A975 0000000000000000
  702.  0101010104010101 93C9B64042EAA240 0000000000000000
  703.  0101010102010101 5570530829705592 0000000000000000
  704.  0101010101800101 8638809E878787A0 0000000000000000
  705.  0101010101400101 41B9A79AF79AC208 0000000000000000
  706.  0101010101200101 7A9BE42F2009A892 0000000000000000
  707.  0101010101100101 29038D56BA6D2745 0000000000000000
  708.  0101010101080101 5495C6ABF1E5DF51 0000000000000000
  709.  0101010101040101 AE13DBD561488933 0000000000000000
  710.  0101010101020101 024D1FFA8904E389 0000000000000000
  711.  0101010101018001 D1399712F99BF02E 0000000000000000
  712.  0101010101014001 14C1D7C1CFFEC79E 0000000000000000
  713.  0101010101012001 1DE5279DAE3BED6F 0000000000000000
  714.  0101010101011001 E941A33F85501303 0000000000000000
  715.  0101010101010801 DA99DBBC9A03F379 0000000000000000
  716.  0101010101010401 B7FC92F91D8E92E9 0000000000000000
  717.  0101010101010201 AE8E5CAA3CA04E85 0000000000000000
  718.  0101010101010180 9CC62DF43B6EED74 0000000000000000
  719.  0101010101010140 D863DBB5C59A91A0 0000000000000000
  720.  0101010101010120 A1AB2190545B91D7 0000000000000000
  721.  0101010101010110 0875041E64C570F7 0000000000000000
  722.  0101010101010108 5A594528BEBEF1CC 0000000000000000
  723.  0101010101010104 FCDB3291DE21F0C0 0000000000000000
  724.  0101010101010102 869EFD7F9F265A09 0000000000000000
  725. #
  726. #   Data permutation test
  727. #
  728. encrypt
  729. #
  730.  1046913489980131 0000000000000000 88D55E54F54C97B4
  731.  1007103489988020 0000000000000000 0C0CC00C83EA48FD
  732.  10071034C8980120 0000000000000000 83BC8EF3A6570183
  733.  1046103489988020 0000000000000000 DF725DCAD94EA2E9
  734.  1086911519190101 0000000000000000 E652B53B550BE8B0
  735.  1086911519580101 0000000000000000 AF527120C485CBB0
  736.  5107B01519580101 0000000000000000 0F04CE393DB926D5
  737.  1007B01519190101 0000000000000000 C9F00FFC74079067
  738.  3107915498080101 0000000000000000 7CFD82A593252B4E
  739.  3107919498080101 0000000000000000 CB49A2F9E91363E3
  740.  10079115B9080140 0000000000000000 00B588BE70D23F56
  741.  3107911598080140 0000000000000000 406A9A6AB43399AE
  742.  1007D01589980101 0000000000000000 6CB773611DCA9ADA
  743.  9107911589980101 0000000000000000 67FD21C17DBB5D70
  744.  9107D01589190101 0000000000000000 9592CB4110430787
  745.  1007D01598980120 0000000000000000 A6B7FF68A318DDD3
  746.  1007940498190101 0000000000000000 4D102196C914CA16
  747.  0107910491190401 0000000000000000 2DFA9F4573594965
  748.  0107910491190101 0000000000000000 B46604816C0E0774
  749.  0107940491190401 0000000000000000 6E7E6221A4F34E87
  750.  19079210981A0101 0000000000000000 AA85E74643233199
  751.  1007911998190801 0000000000000000 2E5A19DB4D1962D6
  752.  10079119981A0801 0000000000000000 23A866A809D30894
  753.  1007921098190101 0000000000000000 D812D961F017D320
  754.  100791159819010B 0000000000000000 055605816E58608F
  755.  1004801598190101 0000000000000000 ABD88E8B1B7716F1
  756.  1004801598190102 0000000000000000 537AC95BE69DA1E1
  757.  1004801598190108 0000000000000000 AED0F6AE3C25CDD8
  758.  1002911498100104 0000000000000000 B3E35A5EE53E7B8D
  759.  1002911598190104 0000000000000000 61C79C71921A2EF8
  760.  1002911598100201 0000000000000000 E2F5728F0995013C
  761.  1002911698100101 0000000000000000 1AEAC39A61F0A464
  762. #
  763. #   S-Box test
  764. #
  765. encrypt
  766. #
  767.  7CA110454A1A6E57 01A1D6D039776742 690F5B0D9A26939B
  768.  0131D9619DC1376E 5CD54CA83DEF57DA 7A389D10354BD271
  769.  07A1133E4A0B2686 0248D43806F67172 868EBB51CAB4599A
  770.  3849674C2602319E 51454B582DDF440A 7178876E01F19B2A
  771.  04B915BA43FEB5B6 42FD443059577FA2 AF37FB421F8C4095
  772.  0113B970FD34F2CE 059B5E0851CF143A 86A560F10EC6D85B
  773.  0170F175468FB5E6 0756D8E0774761D2 0CD3DA020021DC09
  774.  43297FAD38E373FE 762514B829BF486A EA676B2CB7DB2B7A
  775.  07A7137045DA2A16 3BDD119049372802 DFD64A815CAF1A0F
  776.  04689104C2FD3B2F 26955F6835AF609A 5C513C9C4886C088
  777.  37D06BB516CB7546 164D5E404F275232 0A2AEEAE3FF4AB77
  778.  1F08260D1AC2465E 6B056E18759F5CCA EF1BF03E5DFA575A
  779.  584023641ABA6176 004BD6EF09176062 88BF0DB6D70DEE56
  780.  025816164629B007 480D39006EE762F2 A1F9915541020B56
  781.  49793EBC79B3258F 437540C8698F3CFA 6FBF1CAFCFFD0556
  782.  4FB05E1515AB73A7 072D43A077075292 2F22E49BAB7CA1AC
  783.  49E95D6D4CA229BF 02FE55778117F12A 5A6B612CC26CCE4A
  784.  018310DC409B26D6 1D9D5C5018F728C2 5F4C038ED12B2E41
  785.  1C587F1C13924FEF 305532286D6F295A 63FAC0D034D9F793
  786. # Errors found:
  787.  
  788. SH_EOF
  789. cat << SH_EOF > des.1
  790. .ds ]T 15 Nov 90
  791. .ds CP \s-2\(co\f3 Copyright 1990 by David G. Koontz\f1\s+2
  792. .de}F
  793. .ev1
  794. .}E
  795. .if\\n()s 'sp |\\n(.pu-1v-1p
  796. .if\\n()t 'sp |\\n(.pu-3v
  797. .ifn 'sp |\\n(.pu-4v
  798. .ifn .tl Page %\\*(]T
  799. .if\\n()s .tl - % -
  800. .if\\n()t \{.if o .tl Page %\\*(CP\\*(]T
  801. .ife .tl \\*(]T\\*(CPPage % \}
  802. 'bp
  803. .ev
  804. ..
  805. .de}C
  806. ..
  807. .po 1.0i
  808. .nrIN 0.5i
  809. .nr)S 12
  810. .TH \f3DES 1 "" "\s+1\f6/dev/ktz\f1\s-1"
  811. .SH \f3NAME
  812. des \- encrypt/decrypt using verified NBS DES algorithm
  813. .SH \f3SYNOPSIS
  814. .B des
  815. -e \|\(bv \|-d [-k key \|\(bv \|-K hex_key] [-p pad_character], or
  816. .br
  817. .B des
  818. -t [-v]
  819. .SH \f3DESCRIPTION
  820. .I Des\^
  821. encrypts or decrypts according to the
  822. .B -e
  823. (encrypt) and
  824. .B -d
  825. (decrypt) flags reading from standard input and writing to standard output.
  826. The
  827. .B -k
  828. flag passes a string of 8 characters to be used as key.  The
  829. .B -K
  830. flag passes a 16 character hexidecimal key.  The DES algorithm
  831. produces a 64 bit cipher product using a 56 bit key and 64 bit plaintext value.
  832. .PP
  833. If no key is passed
  834. .I des\^
  835. uses the routine getpass("key: ") to
  836. demand an 8 character key from standard input.  All key inputs must be of
  837. the correct length or
  838. .I des\^
  839. will  terminate.
  840. .PP
  841. Character string keys (
  842. .B -k
  843. and
  844. .B getpass()
  845. ) have each byte shifted left one bit, to leave bit[0] undefined.  Bit[0] is
  846. used as a parity bit, and is otherwise uninvolved in DES keys.  Shifting
  847. character string keys provides retention of all ascii defined bits and
  848. results in a 56 bit key.  Hex key strings, such as from the
  849. .B -K
  850. option or from the test vector, are not shifted.  It is the hex key provider's
  851. responsibility to properly position 7 bits of key into the upper 7 bits of each
  852. hex byte.
  853. .PP
  854. The
  855. .B -p
  856. flag passes a hexidecimal value of the range 0x0 - 0xFF to be used in place
  857. of the default pad character (0x20).
  858. The pad character is used to fill out the last 8 byte block if the input
  859. stream does not supply muliples of 8 bytes.
  860. .SH \f3TESTMODE
  861. The
  862. .B -t
  863. and
  864. .B -v
  865. flags are used to toggle
  866. .I test\^
  867. and
  868. .I verbose\^
  869. modes.  Testmode switches input format to test vectors used in DES algorithm
  870. verification.  Each vector includes a 16 hexdigit key, a 16 hexdigit data
  871. value and a 16 hexdigit result value.  All output during testmode operation is
  872. made to standard error output.  A summary of tests is output irregardless of
  873. the
  874. .I -v\^
  875. flag.  Verbose mode outputs the test source and error locations.
  876. .SH \f3TEST VECTOR FORMAT
  877.  An input line must contain less than 127 characters.  A line containing
  878. a leading space is considered a vector.  A line without a leading
  879. space is a comment.  The
  880. .I des\^
  881. mode is set for vectors following a comment line that contains either
  882. .B encrypt
  883. or
  884. .B decrypt
  885. as the first 7 characters.
  886. A line containing a vector is comprised
  887. of a leading space and three 16 hexdigit values.
  888. .PP
  889. A trailing comment line in the vector file forces output of the error
  890. total.
  891. .SH \f3NORMAL OPERATION
  892. .I Des\^
  893. encrypts and decrypts with the same key:
  894. .PP
  895. .RS
  896. des -e -k key < clear >cypher
  897. .br
  898. des -d -k key < cypher
  899. .RE
  900. .PP
  901. will output a stream identical to that contained in clear, with the exeception
  902. of the possibility of up to 7 pad characters being appended to the decoded
  903. stream.
  904. .PP
  905. Files encrypted by
  906. .I des\^
  907. are compatible with the National Bureau of Standards Digital Encryption
  908. Standard in an electronic code book (ECB) implementation.
  909. .PP
  910. .I Des\^
  911. utilizes the libcrypt library used by the passwd and makekey programs.
  912. .I Des\^
  913. is slower than the
  914. .B crypt
  915. command it is intended to replace, a trade off for
  916. higher security.
  917. .I Des\^
  918. also has the capabilty of decrypting concatenated files, if
  919. all used the same key schedule and key.
  920. .bp
  921. .SH \f3EXAMPLE
  922. .IP
  923. des -e -k abcdefgh < foo.c > fum
  924. .PP
  925. will use the string "abcdefgh" as key
  926. to encrypt the contents of "foo.c", and place the encrypted
  927. output in file "fum".  File "fum" at this point will be unreadable.
  928. .SM \f3NOTE:\f1
  929. that the original file, "foo.c", remains in readable form.
  930. To obtain readable print-out of the file "fum", it
  931. could be decoded as follows:
  932. .IP
  933. des -d < fum
  934. .PP
  935. After the response:
  936. .IP
  937. key:
  938. .PP
  939. the user types in "abcdefgh".
  940. .SH \f3FILES
  941. .ta \w'des.test\ \ \ \ 'u
  942. des.test       test vector file, derived from \f2NBS Special Pub 500-20
  943. .br
  944. .DT
  945. .SH \f3SEE ALSO
  946. crypt(1),
  947. makekey(1).
  948. .SH \f3BUGS
  949. Encryption of large files is time consuming.  Some implementations of
  950. libcrypt do not allow the
  951. .I edflag\^
  952. in the routine encrypt(block,edflag) to be set to decode (TRUE).
  953. This is done in compliance with export restrictions on cryptographic
  954. systems.  This may be defeated by clever programming in some cases.
  955. .PP
  956. .I Des\^
  957. also uses padding of the input to assemble the last block of data.  This can
  958. result in up to 7 characters added to the decoded data.  If having the pad
  959. character selectable is not flexible enough, a pipeline filter can be written
  960. to protect and stuff a pad value in the plaintext domain, and strip pad
  961. characters from the decoded output.
  962. .br
  963. See
  964. .I crypt\^
  965. for additional bugs.
  966. SH_EOF
  967.  
  968.