home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / pc / crypto / newdes.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  6.6 KB  |  200 lines

  1. /*--- newdes.c -- Code to implement the NEWDES algorithm.
  2.  *
  3.  *   See Robert Scott's article "Wide-open Encryption Design Offers
  4.  *   Flexible Implementations" in Volume 9, Number 1 (January 1985)
  5.  *   of Cryptologia.
  6.  *   This algorithm resembles the Data Encryption Standard, but is easier
  7.  *   to implement in software and is supposed to be more secure.
  8.  *   It is not to be confused with another algorithm--known by the
  9.  *   same name--which is simply DES without the initial and final
  10.  *   permutations.  The NEWDES here is a completely different
  11.  *   algorithm.
  12.  *
  13.  *   Based on my March 1988 8086 assembly language version.
  14.  *
  15.  *   Mark Riordan    12 August 1990.
  16.  *   This code is hereby placed in the public domain.
  17.  */
  18. #include "newdespr.h"
  19. #include "newdes.h"
  20.  
  21. #define SIZE_ROTOR    256
  22.  
  23.  
  24. unsigned char newdes_rotor[SIZE_ROTOR]
  25.  = {
  26.     32,137,239,188,102,125,221, 72,212, 68, 81, 37, 86,237,147,149,
  27.     70,229, 17,124,115,207, 33, 20,122,143, 25,215, 51,183,138,142,
  28.    146,211,110,173,  1,228,189, 14,103, 78,162, 36,253,167,116,255,
  29.    158, 45,185, 50, 98,168,250,235, 54,141,195,247,240, 63,148,  2,
  30.    224,169,214,180, 62, 22,117,108, 19,172,161,159,160, 47, 43,171,
  31.    194,175,178, 56,196,112, 23,220, 89, 21,164,130,157,  8, 85,251,
  32.    216, 44, 94,179,226, 38, 90,119, 40,202, 34,206, 35, 69,231,246,
  33.     29,109, 74, 71,176,  6, 60,145, 65, 13, 77,151, 12,127, 95,199,
  34.     57,101,  5,232,150,210,129, 24,181, 10,121,187, 48,193,139,252,
  35.    219, 64, 88,233, 96,128, 80, 53,191,144,218, 11,106,132,155,104,
  36.     91,136, 31, 42,243, 66,126,135, 30, 26, 87,186,182,154,242,123,
  37.     82,166,208, 39,152,190,113,205,114,105,225, 84, 73,163, 99,111,
  38.    204, 61,200,217,170, 15,198, 28,192,254,134,234,222,  7,236,248,
  39.    201, 41,177,156, 92,131, 67,249,245,184,203,  9,241,  0, 27, 46,
  40.    133,174, 75, 18, 93,209,100,120, 76,213, 16, 83,  4,107,140, 52,
  41.     58, 55,  3,244, 97,197,238,227,118, 49, 79,230,223,165,153, 59
  42. };
  43.  
  44. unsigned char newdes_key_unravelled[NEWDES_SIZE_KEY_UNRAV];
  45.  
  46.  
  47. /*--- function NewdesBuf -------------------------------------------
  48.  *
  49.  * Encipher or decipher a buffer of data.
  50.  *
  51.  *    Entry    buf            points to the buffer.
  52.  *             block_length   is the number of bytes in the buffer.
  53.  *                            If it is not a multiple of 8, it will be
  54.  *                            rounded up to the next multiple of 8--
  55.  *                            so make sure that the buffer is long enough.
  56.  *             newdes_key_unravelled   points to the key.  It has
  57.  *                            been "unravelled" as necessary for either
  58.  *                            enciphering or deciphering.
  59.  *             newdes_rotor   is the fundamental mapping function
  60.  *                            (array) for NEWDES.
  61.  *
  62.  *    Exit     Returns the number of bytes now in the buffer
  63.  *     (rounded up as described above).
  64.  */
  65. unsigned int NewdesBuf(buf,buf_length,keyptr)
  66. unsigned char *buf;
  67. unsigned int buf_length;
  68. unsigned char *keyptr;
  69. {
  70.    unsigned int mylen, mylen2;
  71.  
  72.    if(buf_length > 0) {
  73.       mylen2 = mylen = (((buf_length - 1) / NEWDES_BLOCK_BYTES) + 1) * NEWDES_BLOCK_BYTES;
  74.    }
  75.  
  76.    for(;mylen; mylen -= NEWDES_BLOCK_BYTES){
  77.       NewdesBlock(buf,keyptr);
  78.       buf += NEWDES_BLOCK_BYTES;
  79.    }
  80.  
  81.    return(mylen2);
  82. }
  83.  
  84. /*--- function NewdesBlock -----------------------------------------
  85.  *
  86.  *  Encipher or decipher an 8-byte block.
  87.  *
  88.  *    Entry    block         points to the block.
  89.  *             newdes_key_unravelled   points to the key.  It has
  90.  *                           been "unravelled" as necessary for either
  91.  *                           enciphering or deciphering.
  92.  *             newdes_rotor  is the fundamental mapping function
  93.  *                           (array) for NEWDES.
  94.  */
  95. void NewdesBlock(block,keyptr)
  96. unsigned char *block;
  97. unsigned char *keyptr;
  98. {
  99.    register unsigned char *byteptr = block;
  100.    int count;
  101.  
  102. #define B0 (*byteptr)
  103. #define B1 (*(byteptr+1))
  104. #define B2 (*(byteptr+2))
  105. #define B3 (*(byteptr+3))
  106. #define B4 (*(byteptr+4))
  107. #define B5 (*(byteptr+5))
  108. #define B6 (*(byteptr+6))
  109. #define B7 (*(byteptr+7))
  110.  
  111.    for(count=8; count--;) {
  112.       B4 = B4 ^ newdes_rotor[B0 ^ *(keyptr++)];
  113.       B5 = B5 ^ newdes_rotor[B1 ^ *(keyptr++)];
  114.       B6 = B6 ^ newdes_rotor[B2 ^ *(keyptr++)];
  115.       B7 = B7 ^ newdes_rotor[B3 ^ *(keyptr++)];
  116.  
  117.       B1 = B1 ^ newdes_rotor[B4 ^ *(keyptr++)];
  118.       B2 = B2 ^ newdes_rotor[B4 ^ B5];
  119.       B3 = B3 ^ newdes_rotor[B6 ^ *(keyptr++)];
  120.       B0 = B0 ^ newdes_rotor[B7 ^ *(keyptr++)];
  121.    }
  122.    B4 = B4 ^ newdes_rotor[B0 ^ *(keyptr++)];
  123.    B5 = B5 ^ newdes_rotor[B1 ^ *(keyptr++)];
  124.    B6 = B6 ^ newdes_rotor[B2 ^ *(keyptr++)];
  125.    B7 = B7 ^ newdes_rotor[B3 ^ *(keyptr++)];
  126. }
  127.  
  128. /*--- function NewdesSetKeyEncipher ---------------------------------
  129.  *
  130.  *    Set newdes to encipher using a given key.
  131.  *
  132.  *    Entry    key   points to a 15-byte key.
  133.  *
  134.  *    Exit     key_rav   contains the key set up properly
  135.  *                   for use in newdes_block, for enciphering.
  136.  */
  137. void NewdesSetKeyEncipher(key,key_rav)
  138. unsigned char *key;
  139. unsigned char *key_rav;
  140. {
  141.    unsigned char *kuserptr, *kunravptr;
  142.    int outloopct = NEWDES_SIZE_KEY_UNRAV / NEWDES_USER_KEY_BYTES;
  143.    int inloopct;
  144.  
  145.    kunravptr = key_rav;
  146.    for(;outloopct--;) {
  147.       kuserptr = key;
  148.       for(inloopct=NEWDES_USER_KEY_BYTES; inloopct--;) {
  149.          *(kunravptr++) = *(kuserptr++);
  150.       }
  151.    }
  152. }
  153.  
  154.  
  155. /*--- function NewdesSetKeyDecipher ---------------------------------
  156.  *
  157.  *    Set newdes to decipher using a given key.
  158.  *
  159.  *    Entry    key   points to a 15-byte key.
  160.  *
  161.  *    Exit     key_rav   contains the key set up properly
  162.  *                     for use in newdes_block, for deciphering.
  163.  */
  164. void NewdesSetKeyDecipher(key,key_rav)
  165. unsigned char *key;
  166. unsigned char *key_rav;
  167. {
  168.    unsigned char *kunravptr;
  169.    int outloopct = NEWDES_SIZE_KEY_UNRAV / NEWDES_USER_KEY_BYTES;
  170.    int userkeyidx;
  171.  
  172.    kunravptr = key_rav;
  173.    userkeyidx = 11;
  174.    while (1) {
  175.       *(kunravptr++) = key[userkeyidx];
  176.       userkeyidx++;
  177.       if(userkeyidx == NEWDES_USER_KEY_BYTES) userkeyidx = 0;
  178.  
  179.       *(kunravptr++) = key[userkeyidx];
  180.       userkeyidx++;
  181.       if(userkeyidx == NEWDES_USER_KEY_BYTES) userkeyidx = 0;
  182.  
  183.       *(kunravptr++) = key[userkeyidx];
  184.       userkeyidx++;
  185.       if(userkeyidx == NEWDES_USER_KEY_BYTES) userkeyidx = 0;
  186.  
  187.       *(kunravptr++) = key[userkeyidx];
  188.       userkeyidx = (userkeyidx+9) % 15;
  189.  
  190.       if(userkeyidx == 12) break;
  191.  
  192.       *(kunravptr++) = key[userkeyidx++];
  193.       *(kunravptr++) = key[userkeyidx++];
  194.  
  195.       *(kunravptr++) = key[userkeyidx];
  196.  
  197.       userkeyidx = (userkeyidx+9) % 15;
  198.    }
  199. }
  200.