home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 198_01 / crypt.c < prev    next >
C/C++ Source or Header  |  1990-01-23  |  7KB  |  216 lines

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