"Fossies" - the Fresh Open Source Software Archive

Member "libmcrypt-2.5.8/modules/algorithms/enigma.c" (19 Jan 2003, 6283 Bytes) of package /linux/privat/old/libmcrypt-2.5.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "enigma.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *    "enigma.c" is in file cbw.tar from
    3  *      anonymous FTP host watmsg.waterloo.edu: pub/crypt/cbw.tar.Z
    4  *
    5  *      A one-rotor machine designed along the lines of Enigma
    6  *      but considerably trivialized.
    7  *
    8  *      A public-domain replacement for the UNIX "crypt" command.
    9  *      Changed to fit in mcrypt by nmav@hellug.gr
   10  */
   11 
   12 /* modified in order to use the libmcrypt API by Nikos Mavroyanopoulos 
   13  * All modifications are placed under the license of libmcrypt.
   14  */
   15 
   16 /* $Id: enigma.c,v 1.13 2003/01/19 17:48:27 nmav Exp $ */
   17 
   18 #include <libdefs.h>
   19 
   20 #include <mcrypt_modules.h>
   21 #include "enigma.h"
   22 
   23 #define _mcrypt_set_key enigma_LTX__mcrypt_set_key
   24 #define _mcrypt_encrypt enigma_LTX__mcrypt_encrypt
   25 #define _mcrypt_decrypt enigma_LTX__mcrypt_decrypt
   26 #define _mcrypt_get_size enigma_LTX__mcrypt_get_size
   27 #define _mcrypt_get_block_size enigma_LTX__mcrypt_get_block_size
   28 #define _is_block_algorithm enigma_LTX__is_block_algorithm
   29 #define _mcrypt_get_key_size enigma_LTX__mcrypt_get_key_size
   30 #define _mcrypt_get_algo_iv_size enigma_LTX__mcrypt_get_algo_iv_size
   31 #define _mcrypt_get_supported_key_sizes enigma_LTX__mcrypt_get_supported_key_sizes
   32 #define _mcrypt_get_algorithms_name enigma_LTX__mcrypt_get_algorithms_name
   33 #define _mcrypt_self_test enigma_LTX__mcrypt_self_test
   34 #define _mcrypt_algorithm_version enigma_LTX__mcrypt_algorithm_version
   35 
   36 /* it needs to be linked against libufc or libcrypt */
   37 /* it no longer needs that. It just needs the password to be
   38  * transformed by unix crypt() or the mhash SCRYPT 
   39  */
   40 
   41 WIN32DLL_DEFINE
   42     int _mcrypt_set_key(CRYPT_KEY * ckey, char *password, int plen,
   43             void *u1, int u2)
   44 {
   45     int ic, i, k, temp;
   46     unsigned random;
   47     sword32 seed;
   48 
   49 
   50     Bzero(ckey, sizeof(CRYPT_KEY));
   51     ckey->n1 = ckey->n2 = ckey->nr1 = ckey->nr2 = 0;
   52 
   53     if (plen > 13)
   54         plen = 13;
   55 
   56     memmove(ckey->cbuf, password, plen);
   57 
   58     seed = 123;
   59     for (i = 0; i < 13; i++)
   60         seed = seed * ckey->cbuf[i] + i;
   61     for (i = 0; i < ROTORSZ; i++) {
   62         ckey->t1[i] = i;
   63         ckey->deck[i] = i;
   64     }
   65     for (i = 0; i < ROTORSZ; i++) {
   66         seed = 5 * seed + ckey->cbuf[i % 13];
   67         random = seed % 65521;
   68         k = ROTORSZ - 1 - i;
   69         ic = (random & MASK) % (k + 1);
   70         random >>= 8;
   71 
   72         temp = ckey->t1[k];
   73         ckey->t1[k] = ckey->t1[ic];
   74         ckey->t1[ic] = temp;
   75         if (ckey->t3[k] != 0)
   76             continue;
   77 
   78         ic = (random & MASK) % k;
   79         while (ckey->t3[ic] != 0)
   80             ic = (ic + 1) % k;
   81         ckey->t3[k] = ic;
   82         ckey->t3[ic] = k;
   83     }
   84 
   85     for (i = 0; i < ROTORSZ; i++)
   86         ckey->t2[ckey->t1[i] & MASK] = i;
   87 
   88     return 0;
   89 }
   90 
   91 
   92 int shuffle(CRYPT_KEY * ckey)
   93 {
   94     int i, ic, k, temp;
   95     unsigned random;
   96     static sword32 seed = 123;
   97 
   98     for (i = 0; i < ROTORSZ; i++) {
   99         seed = 5 * seed + ckey->cbuf[i % 13];
  100         random = seed % 65521;
  101         k = ROTORSZ - 1 - i;
  102         ic = (random & MASK) % (k + 1);
  103         temp = ckey->deck[k];
  104         ckey->deck[k] = ckey->deck[ic];
  105         ckey->deck[ic] = temp;
  106     }
  107     return 0;
  108 }
  109 
  110 WIN32DLL_DEFINE
  111     void _mcrypt_encrypt(CRYPT_KEY * ckey, void *gtext, int textlen)
  112 {               /* 0 or 1 */
  113     int i, j;
  114     int secureflg = 0;
  115     char *text = gtext;
  116 
  117     for (j = 0; j < textlen; j++) {
  118 
  119         i = text[j];
  120         if (secureflg) {
  121             ckey->nr1 = ckey->deck[ckey->n1] & MASK;
  122             ckey->nr2 = ckey->deck[ckey->nr1] & MASK;
  123         } else {
  124             ckey->nr1 = ckey->n1;
  125         }
  126         i = ckey->
  127             t2[(ckey->
  128             t3[(ckey->t1[(i + ckey->nr1) & MASK] +
  129                 ckey->nr2) & MASK] - ckey->nr2) & MASK] -
  130             ckey->nr1;
  131         text[j] = i;
  132         ckey->n1++;
  133         if (ckey->n1 == ROTORSZ) {
  134             ckey->n1 = 0;
  135             ckey->n2++;
  136             if (ckey->n2 == ROTORSZ)
  137                 ckey->n2 = 0;
  138             if (secureflg) {
  139                 shuffle(ckey);
  140             } else {
  141                 ckey->nr2 = ckey->n2;
  142             }
  143         }
  144     }
  145 
  146     return;
  147 }
  148 
  149 WIN32DLL_DEFINE
  150     void _mcrypt_decrypt(CRYPT_KEY * ckey, void *gtext, int textlen)
  151 {               /* 0 or 1 */
  152     int i, j;
  153     int secureflg = 0;
  154     char *text = gtext;
  155 
  156 
  157     for (j = 0; j < textlen; j++) {
  158 
  159         i = text[j];
  160         if (secureflg) {
  161             ckey->nr1 = ckey->deck[ckey->n1] & MASK;
  162             ckey->nr2 = ckey->deck[ckey->nr1] & MASK;
  163         } else {
  164             ckey->nr1 = ckey->n1;
  165         }
  166         i = ckey->
  167             t2[(ckey->
  168             t3[(ckey->t1[(i + ckey->nr1) & MASK] +
  169                 ckey->nr2) & MASK] - ckey->nr2) & MASK] -
  170             ckey->nr1;
  171         text[j] = i;
  172         ckey->n1++;
  173         if (ckey->n1 == ROTORSZ) {
  174             ckey->n1 = 0;
  175             ckey->n2++;
  176             if (ckey->n2 == ROTORSZ)
  177                 ckey->n2 = 0;
  178             if (secureflg) {
  179                 shuffle(ckey);
  180             } else {
  181                 ckey->nr2 = ckey->n2;
  182             }
  183         }
  184     }
  185 
  186     return;
  187 }
  188 
  189 
  190 WIN32DLL_DEFINE int _mcrypt_get_size()
  191 {
  192     return sizeof(CRYPT_KEY);
  193 }
  194 WIN32DLL_DEFINE int _mcrypt_get_block_size()
  195 {
  196     return 1;
  197 }
  198 WIN32DLL_DEFINE int _mcrypt_get_algo_iv_size()
  199 {
  200     return 0;
  201 }
  202 WIN32DLL_DEFINE int _is_block_algorithm()
  203 {
  204     return 0;
  205 }
  206 WIN32DLL_DEFINE int _mcrypt_get_key_size()
  207 {
  208     return 13;
  209 }
  210 WIN32DLL_DEFINE const int *_mcrypt_get_supported_key_sizes(int *len)
  211 {
  212     *len = 0;
  213     return NULL;
  214 }
  215 
  216 WIN32DLL_DEFINE char *_mcrypt_get_algorithms_name()
  217 {
  218 return "enigma";
  219 }
  220 
  221 #define CIPHER "f3edda7da20f8975884600f014d32c7a08e59d7b"
  222 
  223 WIN32DLL_DEFINE int _mcrypt_self_test()
  224 {
  225     char *keyword;
  226     unsigned char plaintext[20];
  227     unsigned char ciphertext[20];
  228     int blocksize = 20, j;
  229     void *key;
  230     unsigned char cipher_tmp[200];
  231 
  232     keyword = calloc(1, _mcrypt_get_key_size());
  233     if (keyword == NULL)
  234         return -1;
  235 
  236     strcpy(keyword, "enadyotr");
  237 
  238     for (j = 0; j < blocksize; j++) {
  239         plaintext[j] = j % 256;
  240     }
  241     key = malloc(_mcrypt_get_size());
  242     if (key == NULL) {
  243         free(keyword);
  244         return -1;
  245     }
  246     memmove(ciphertext, plaintext, blocksize);
  247 
  248     _mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size(),
  249             NULL, 0);
  250     _mcrypt_encrypt(key, (void *) ciphertext, blocksize);
  251 
  252     for (j = 0; j < blocksize; j++) {
  253         sprintf(&((char *) cipher_tmp)[2 * j], "%.2x",
  254             ciphertext[j]);
  255     }
  256 
  257     if (strcmp((char *) cipher_tmp, CIPHER) != 0) {
  258         printf("failed compatibility\n");
  259         printf("Expected: %s\nGot: %s\n", CIPHER,
  260                (char *) cipher_tmp);
  261         free(keyword);
  262         free(key);
  263         return -1;
  264     }
  265 
  266     _mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size(),
  267             NULL, 0);
  268     free(keyword);
  269     
  270     _mcrypt_decrypt(key, (void *) ciphertext, blocksize);
  271     free(key);
  272 
  273     if (strcmp(ciphertext, plaintext) != 0) {
  274         printf("failed internally\n");
  275         return -1;
  276     }
  277 
  278     return 0;
  279 }
  280 
  281 WIN32DLL_DEFINE word32 _mcrypt_algorithm_version()
  282 {
  283     return 20010801;
  284 }
  285 
  286 #ifdef WIN32
  287 # ifdef USE_LTDL
  288 WIN32DLL_DEFINE int main (void)
  289 {
  290        /* empty main function to avoid linker error (see cygwin FAQ) */
  291 }
  292 # endif
  293 #endif