"Fossies" - the Fresh Open Source Software Archive

Member "libmcrypt-2.5.8/modules/algorithms/xtea.c" (19 Jan 2003, 5336 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 "xtea.c" see the Fossies "Dox" file reference documentation.

    1 /**********************************************************
    2    TEA - Tiny Encryption Algorithm
    3    Feistel cipher by David Wheeler & Roger M. Needham
    4    (extended version)
    5  **********************************************************/
    6 
    7 /* $Id: xtea.c,v 1.13 2003/01/19 17:48:27 nmav Exp $ */
    8 
    9 /* modified in order to use the libmcrypt API by Nikos Mavroyanopoulos 
   10  * All modifications are placed under the license of libmcrypt.
   11  */
   12 
   13 #include <libdefs.h>
   14 
   15 #include <mcrypt_modules.h>
   16 
   17 #define _mcrypt_set_key xtea_LTX__mcrypt_set_key
   18 #define _mcrypt_encrypt xtea_LTX__mcrypt_encrypt
   19 #define _mcrypt_decrypt xtea_LTX__mcrypt_decrypt
   20 #define _mcrypt_get_size xtea_LTX__mcrypt_get_size
   21 #define _mcrypt_get_block_size xtea_LTX__mcrypt_get_block_size
   22 #define _is_block_algorithm xtea_LTX__is_block_algorithm
   23 #define _mcrypt_get_key_size xtea_LTX__mcrypt_get_key_size
   24 #define _mcrypt_get_supported_key_sizes xtea_LTX__mcrypt_get_supported_key_sizes
   25 #define _mcrypt_get_algorithms_name xtea_LTX__mcrypt_get_algorithms_name
   26 #define _mcrypt_self_test xtea_LTX__mcrypt_self_test
   27 #define _mcrypt_algorithm_version xtea_LTX__mcrypt_algorithm_version
   28 
   29 #define ROUNDS 32
   30 #define DELTA 0x9e3779b9    /* sqr(5)-1 * 2^31 */
   31 
   32 /**********************************************************
   33    Input values:    k[4]    128-bit key
   34             v[2]    64-bit plaintext block
   35    Output values:   v[2]    64-bit ciphertext block 
   36  **********************************************************/
   37 
   38 WIN32DLL_DEFINE
   39     int _mcrypt_set_key(word32 * k, word32 * input_key, int len)
   40 {
   41     k[0] = 0;
   42     k[2] = 0;
   43     k[1] = 0;
   44     k[3] = 0;
   45     memmove(k, input_key, len);
   46     return 0;
   47 }
   48 
   49 WIN32DLL_DEFINE void _mcrypt_encrypt(word32 * k, word32 * v)
   50 {
   51 #ifdef WORDS_BIGENDIAN
   52     word32 y = v[0], z = v[1];
   53 #else
   54     word32 y = byteswap32(v[0]), z = byteswap32(v[1]);
   55 #endif
   56     word32 limit, sum = 0;
   57     int N = ROUNDS;
   58 
   59     limit = DELTA * N;
   60 #ifdef WORDS_BIGENDIAN
   61     while (sum != limit) {
   62         y += (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum & 3]);
   63         sum += DELTA;
   64         z += (((y << 4) ^ (y >> 5)) + y) ^ (sum +
   65                             k[(sum >> 11) & 3]);
   66     }
   67 #else
   68     while (sum != limit) {
   69         y += (((z << 4) ^ (z >> 5)) + z) ^ (sum +
   70                             byteswap32(k
   71                                    [sum & 3]));
   72         sum += DELTA;
   73         z += (((y << 4) ^ (y >> 5)) + y) ^ (sum +
   74                             byteswap32(k
   75                                    [(sum >>
   76                                  11) &
   77                                 3]));
   78     }
   79 #endif
   80 
   81 #ifdef WORDS_BIGENDIAN
   82     v[0] = y;
   83     v[1] = z;
   84 #else
   85     v[0] = byteswap32(y);
   86     v[1] = byteswap32(z);
   87 #endif
   88 }
   89 
   90 WIN32DLL_DEFINE void _mcrypt_decrypt(word32 * k, word32 * v)
   91 {
   92 #ifdef WORDS_BIGENDIAN
   93     word32 y = v[0], z = v[1];
   94 #else
   95     word32 y = byteswap32(v[0]), z = byteswap32(v[1]);
   96 #endif
   97     word32 limit, sum = 0;
   98     int N = (-ROUNDS);
   99 
  100     limit = DELTA * N;
  101 #ifdef WORDS_BIGENDIAN
  102 
  103     sum = DELTA * (-N);
  104     while (sum) {
  105         z -= (((y << 4) ^ (y >> 5)) + y) ^ (sum +
  106                             k[(sum >> 11) & 3]);
  107         sum -= DELTA;
  108         y -= (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum & 3]);
  109 #else
  110     sum = DELTA * (-N);
  111     while (sum) {
  112         z -= (((y << 4) ^ (y >> 5)) + y) ^ (sum +
  113                             byteswap32(k
  114                                    [(sum >>
  115                                  11) &
  116                                 3]));
  117         sum -= DELTA;
  118         y -= (((z << 4) ^ (z >> 5)) + z) ^ (sum +
  119                             byteswap32(k
  120                                    [sum & 3]));
  121 #endif
  122     }
  123 #ifdef WORDS_BIGENDIAN
  124     v[0] = y;
  125     v[1] = z;
  126 #else
  127     v[0] = byteswap32(y);
  128     v[1] = byteswap32(z);
  129 #endif
  130 }
  131 
  132 /*
  133 void _mcrypt_encrypt(word32 * k, word32 * v)
  134 {
  135     _mcrypt_tean(k, v, ROUNDS);
  136 }
  137 
  138 void _mcrypt_decrypt(word32 * k, word32 * v)
  139 {
  140     _mcrypt_tean(k, v, -ROUNDS);
  141 }
  142 */
  143 
  144 WIN32DLL_DEFINE int _mcrypt_get_size()
  145 {
  146     return 4 * sizeof(word32);
  147 }
  148 WIN32DLL_DEFINE int _mcrypt_get_block_size()
  149 {
  150     return 8;
  151 }
  152 WIN32DLL_DEFINE int _is_block_algorithm()
  153 {
  154     return 1;
  155 }
  156 WIN32DLL_DEFINE int _mcrypt_get_key_size()
  157 {
  158     return 16;
  159 }
  160 
  161 static const int key_sizes[] = { 16 };
  162 WIN32DLL_DEFINE const int *_mcrypt_get_supported_key_sizes(int *len)
  163 {
  164     *len = sizeof(key_sizes)/sizeof(int);
  165     return key_sizes;
  166 }
  167 
  168 WIN32DLL_DEFINE const char *_mcrypt_get_algorithms_name()
  169 {
  170 return "xTEA";
  171 }
  172 
  173 #define CIPHER "f61e7ff6da7cdb27"
  174 
  175 WIN32DLL_DEFINE int _mcrypt_self_test()
  176 {
  177     char *keyword;
  178     unsigned char plaintext[16];
  179     unsigned char ciphertext[16];
  180     int blocksize = _mcrypt_get_block_size(), j;
  181     void *key;
  182     unsigned char cipher_tmp[200];
  183 
  184     keyword = calloc(1, _mcrypt_get_key_size());
  185     if (keyword == NULL)
  186         return -1;
  187 
  188     for (j = 0; j < _mcrypt_get_key_size(); j++) {
  189         keyword[j] = ((j * 2 + 10) % 256);
  190     }
  191 
  192     for (j = 0; j < blocksize; j++) {
  193         plaintext[j] = j % 256;
  194     }
  195     key = malloc(_mcrypt_get_size());
  196     if (key == NULL)
  197         return -1;
  198 
  199     memcpy(ciphertext, plaintext, blocksize);
  200 
  201     _mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size());
  202     free(keyword);
  203     _mcrypt_encrypt(key, (void *) ciphertext);
  204 
  205     for (j = 0; j < blocksize; j++) {
  206         sprintf(&((char *) cipher_tmp)[2 * j], "%.2x",
  207             ciphertext[j]);
  208     }
  209 
  210     if (strcmp((char *) cipher_tmp, CIPHER) != 0) {
  211         printf("failed compatibility\n");
  212         printf("Expected: %s\nGot: %s\n", CIPHER,
  213                (char *) cipher_tmp);
  214         free(key);
  215         return -1;
  216     }
  217     _mcrypt_decrypt(key, (void *) ciphertext);
  218     free(key);
  219 
  220     if (strcmp(ciphertext, plaintext) != 0) {
  221         printf("failed internally\n");
  222         return -1;
  223     }
  224 
  225     return 0;
  226 }
  227 
  228 WIN32DLL_DEFINE word32 _mcrypt_algorithm_version()
  229 {
  230     return 20010801;
  231 }
  232 
  233 #ifdef WIN32
  234 # ifdef USE_LTDL
  235 WIN32DLL_DEFINE int main (void)
  236 {
  237        /* empty main function to avoid linker error (see cygwin FAQ) */
  238 }
  239 # endif
  240 #endif