home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / VILE327.ZIP / VILE327.TAR / vile3.27 / crypt.c < prev    next >
C/C++ Source or Header  |  1992-12-14  |  7KB  |  234 lines

  1. /*    Crypt:    Encryption routines for MicroEMACS
  2.  *        written by Dana Hoggatt and Daniel Lawrence
  3.  *
  4.  * $Log: crypt.c,v $
  5.  * Revision 1.5  1992/08/20  23:40:48  foxharp
  6.  * typo fixes -- thanks, eric
  7.  *
  8.  * Revision 1.4  1992/05/16  12:00:31  pgf
  9.  * prototypes/ansi/void-int stuff/microsoftC
  10.  *
  11.  * Revision 1.3  1991/11/01  14:38:00  pgf
  12.  * saber cleanup
  13.  *
  14.  * Revision 1.2  1991/08/07  12:35:07  pgf
  15.  * added RCS log messages
  16.  *
  17.  * revision 1.1
  18.  * date: 1990/09/21 10:24:54;
  19.  * initial vile RCS revision
  20. */
  21.  
  22. #include    <stdio.h>
  23. #include    "estruct.h"
  24. #include    "edef.h"
  25.  
  26. #if    CRYPT
  27. int
  28. setkey(f, n)    /* reset encryption key of current buffer */
  29. int f;        /* default flag */
  30. int n;        /* numeric argument */
  31. {
  32.     register int status;    /* return status */
  33.     int odisinp;        /* original vlaue of disinp */
  34.     char key[NPAT];        /* new encryption string */
  35.  
  36.     /* turn command input echo off */
  37.     odisinp = disinp;
  38.     disinp = FALSE;
  39.  
  40.     /* get the string to use as an encrytion string */
  41.     key[0] = 0;
  42.     status = mlreply("Encryption String: ", key, NPAT - 1);
  43.     disinp = odisinp;
  44.         if (status != TRUE)
  45.                 return(status);
  46.  
  47.     /* and encrypt it */
  48.     crypt((char *)NULL, 0);
  49.     crypt(key, strlen(key));
  50.  
  51.     /* and save it off */
  52.     strcpy(curbp->b_key, key);
  53.     mlwrite(" ");        /* clear it off the bottom line */
  54.     return(TRUE);
  55. }
  56.  
  57. /**********
  58.  *
  59.  *    crypt - in place encryption/decryption of a buffer
  60.  *
  61.  *    (C) Copyright 1986, Dana L. Hoggatt
  62.  *    1216, Beck Lane, Lafayette, IN
  63.  *
  64.  *    When consulting directly with the author of this routine, 
  65.  *    please refer to this routine as the "DLH-POLY-86-B CIPHER".  
  66.  *
  67.  *    This routine was written for Dan Lawrence, for use in V3.8 of
  68.  *    MicroEMACS, a public domain text/program editor.  
  69.  *
  70.  *    I kept the following goals in mind when preparing this function:
  71.  *
  72.  *        1.    All printable characters were to be encrypted back
  73.  *        into the printable range, control characters and
  74.  *        high-bit characters were to remain unaffected.  this
  75.  *        way, encrypted would still be just as cheap to 
  76.  *        transmit down a 7-bit data path as they were before.
  77.  *
  78.  *        2.    The encryption had to be portable.  The encrypted 
  79.  *        file from one computer should be able to be decrypted 
  80.  *        on another computer.
  81.  *
  82.  *        3.    The encryption had to be inexpensive, both in terms
  83.  *        of speed and space.
  84.  *
  85.  *        4.    The system needed to be secure against all but the
  86.  *        most determined of attackers.
  87.  *
  88.  *    For encryption of a block of data, one calls crypt passing 
  89.  *    a pointer to the data block and its length. The data block is 
  90.  *    encrypted in place, that is, the encrypted output overwrites 
  91.  *    the input.  Decryption is totally isomorphic, and is performed 
  92.  *    in the same manner by the same routine.  
  93.  *
  94.  *    Before using this routine for encrypting data, you are expected 
  95.  *    to specify an encryption key.  This key is an arbitrary string,
  96.  *    to be supplied by the user.  To set the key takes two calls to 
  97.  *    crypt().  First, you call 
  98.  *
  99.  *        crypt(NULL, vector)
  100.  *
  101.  *    This resets all internal control information.  Typically (and 
  102.  *    specifically in the case on MICRO-emacs) you would use a "vector" 
  103.  *    of 0.  Other values can be used to customize your editor to be 
  104.  *    "incompatible" with the normally distributed version.  For 
  105.  *    this purpose, the best results will be obtained by avoiding
  106.  *    multiples of 95.
  107.  *
  108.  *    Then, you "encrypt" your password by calling 
  109.  *
  110.  *        crypt(pass, strlen(pass))
  111.  *
  112.  *    where "pass" is your password string.  Crypt() will destroy 
  113.  *    the original copy of the password (it becomes encrypted), 
  114.  *    which is good.  You do not want someone on a multiuser system 
  115.  *    to peruse your memory space and bump into your password.  
  116.  *    Still, it is a better idea to erase the password buffer to 
  117.  *    defeat memory perusal by a more technical snooper.  
  118.  *
  119.  *    For the interest of cryptologists, at the heart of this 
  120.  *    function is a Beaufort Cipher.  The cipher alphabet is the 
  121.  *    range of printable characters (' ' to '~'), all "control" 
  122.  *    and "high-bit" characters are left unaltered.
  123.  *
  124.  *    The key is a variant autokey, derived from a weighted sum 
  125.  *    of all the previous clear text and cipher text.  A counter 
  126.  *    is used as salt to obliterate any simple cyclic behavior 
  127.  *    from the clear text, and key feedback is used to assure 
  128.  *    that the entire message is based on the original key, 
  129.  *    preventing attacks on the last part of the message as if 
  130.  *    it were a pure autokey system.
  131.  *
  132.  *    Overall security of encrypted data depends upon three 
  133.  *    factors:  the fundamental cryptographic system must be 
  134.  *    difficult to compromise; exhaustive searching of the key 
  135.  *    space must be computationally expensive; keys and plaintext 
  136.  *    must remain out of sight.  This system satisfies this set
  137.  *    of conditions to within the degree desired for MicroEMACS.
  138.  *
  139.  *    Though direct methods of attack (against systems such as 
  140.  *    this) do exist, they are not well known and will consume 
  141.  *    considerable amounts of computing time.  An exhaustive
  142.  *    search requires over a billion investigations, on average.
  143.  *
  144.  *    The choice, entry, storage, manipulation, alteration, 
  145.  *    protection and security of the keys themselves are the 
  146.  *    responsibility of the user.  
  147.  *
  148.  **********/
  149.  
  150. crypt(bptr, len)
  151. register char *bptr;    /* buffer of characters to be encrypted */
  152. register int len;    /* number of characters in the buffer */
  153. {
  154.     register int cc;    /* current character being considered */
  155.  
  156.     static long key = 0;    /* 29 bit encipherment key */
  157.     static int salt = 0;    /* salt to spice up key with */
  158.  
  159.     if (!bptr) {        /* is there anything here to encrypt? */
  160.         key = len;    /* set the new key */
  161.         salt = len;    /* set the new salt */
  162.         return;
  163.     }
  164.     while (len--) {        /* for every character in the buffer */
  165.  
  166.         cc = *bptr;    /* get a character out of the buffer */
  167.  
  168.         /* only encipher printable characters */
  169.         if ((cc >= ' ') && (cc <= '~')) {
  170.  
  171. /**  If the upper bit (bit 29) is set, feed it back into the key.  This 
  172.     assures us that the starting key affects the entire message.  **/
  173.  
  174.             key &= 0x1FFFFFFFL;    /* strip off overflow */
  175.             if (key & 0x10000000L) {
  176.                 key ^= 0x0040A001L;    /* feedback */
  177.             }
  178.  
  179. /**  Down-bias the character, perform a Beaufort encipherment, and 
  180.     up-bias the character again.  We want key to be positive 
  181.     so that the left shift here will be more portable and the 
  182.     mod95() faster   **/
  183.  
  184.             cc = mod95((int)(key % 95) - (cc - ' ')) + ' ';
  185.  
  186. /**  the salt will spice up the key a little bit, helping to obscure 
  187.     any patterns in the clear text, particularly when all the 
  188.     characters (or long sequences of them) are the same.  We do 
  189.     not want the salt to go negative, or it will affect the key 
  190.     too radically.  It is always a good idea to chop off cyclics 
  191.     to prime values.  **/
  192.  
  193.             if (++salt >= 20857) {    /* prime modulus */
  194.                 salt = 0;
  195.             }
  196.  
  197. /**  our autokey (a special case of the running key) is being 
  198.     generated by a weighted checksum of clear text, cipher 
  199.     text, and salt.   **/
  200.  
  201.             key = key + key + cc + *bptr + salt;
  202.         }
  203.         *bptr++ = cc;    /* put character back into buffer */
  204.     }
  205.     return;
  206. }
  207.  
  208. static int mod95(val)
  209.  
  210. register int val;
  211.  
  212. {
  213.     /*  The mathematical MOD does not match the computer MOD  */
  214.  
  215.     /*  Yes, what I do here may look strange, but it gets the
  216.         job done, and portably at that.  */
  217.  
  218.     while (val >= 9500)
  219.         val -= 9500;
  220.     while (val >= 950)
  221.         val -= 950;
  222.     while (val >= 95)
  223.         val -= 95;
  224.     while (val < 0)
  225.         val += 95;
  226.     return (val);
  227. }
  228. #else
  229. void
  230. nocrypt()
  231. {
  232. }
  233. #endif
  234.