"Fossies" - the Fresh Open Source Software Archive

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

    1 /* This is an independent implementation of the encryption algorithm:   */
    2 /*                                                                      */
    3 /*         SAFER+ by Cylink                                             */
    4 /*                                                                      */
    5 /* which is a candidate algorithm in the Advanced Encryption Standard   */
    6 /* programme of the US National Institute of Standards and Technology.  */
    7 /*                                                                      */
    8 /* Copyright in this implementation is held by Dr B R Gladman but I     */
    9 /* hereby give permission for its free direct or derivative use subject */
   10 /* to acknowledgment of its origin and compliance with any conditions   */
   11 /* that the originators of the algorithm place on its exploitation.     */
   12 /*                                                                      */
   13 /* Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999     */
   14 
   15 /* $Id: saferplus.c,v 1.13 2003/01/19 17:48:27 nmav Exp $ */
   16 
   17 /* modified in order to use the libmcrypt API by Nikos Mavroyanopoulos 
   18  * All modifications are placed under the license of libmcrypt.
   19  */
   20 
   21 
   22 /* Timing data for SAFER+ (safer.c)
   23 
   24 Core timing without I/O endian conversion:
   25 
   26 128 bit key:
   27 Key Setup:    4278 cycles
   28 Encrypt:      1722 cycles =    14.9 mbits/sec
   29 Decrypt:      1709 cycles =    15.0 mbits/sec
   30 Mean:         1716 cycles =    14.9 mbits/sec
   31 
   32 192 bit key:
   33 Key Setup:    7426 cycles
   34 Encrypt:      2555 cycles =    10.0 mbits/sec
   35 Decrypt:      2530 cycles =    10.1 mbits/sec
   36 Mean:         2543 cycles =    10.1 mbits/sec
   37 
   38 256 bit key:
   39 Key Setup:   11313 cycles
   40 Encrypt:      3391 cycles =     7.5 mbits/sec
   41 Decrypt:      3338 cycles =     7.7 mbits/sec
   42 Mean:         3365 cycles =     7.6 mbits/sec
   43 
   44 Full timing with I/O endian conversion:
   45 
   46 128 bit key:
   47 Key Setup:    3977 cycles
   48 Encrypt:      1751 cycles =    14.6 mbits/sec
   49 Decrypt:      1734 cycles =    14.8 mbits/sec
   50 Mean:         1743 cycles =    14.7 mbits/sec
   51 
   52 192 bit key:
   53 Key Setup:    6490 cycles
   54 Encrypt:      2574 cycles =     9.9 mbits/sec
   55 Decrypt:      2549 cycles =    10.0 mbits/sec
   56 Mean:         2562 cycles =    10.0 mbits/sec
   57 
   58 256 bit key:
   59 Key Setup:    9487 cycles
   60 Encrypt:      3412 cycles =     7.5 mbits/sec
   61 Decrypt:      3372 cycles =     7.6 mbits/sec
   62 Mean:         3392 cycles =     7.5 mbits/sec
   63 
   64 */
   65 
   66 #include <libdefs.h>
   67 
   68 #include <mcrypt_modules.h>
   69 #include "saferplus.h"
   70 #include <stdlib.h>
   71 
   72 #define _mcrypt_set_key saferplus_LTX__mcrypt_set_key
   73 #define _mcrypt_encrypt saferplus_LTX__mcrypt_encrypt
   74 #define _mcrypt_decrypt saferplus_LTX__mcrypt_decrypt
   75 #define _mcrypt_get_size saferplus_LTX__mcrypt_get_size
   76 #define _mcrypt_get_block_size saferplus_LTX__mcrypt_get_block_size
   77 #define _is_block_algorithm saferplus_LTX__is_block_algorithm
   78 #define _mcrypt_get_key_size saferplus_LTX__mcrypt_get_key_size
   79 #define _mcrypt_get_supported_key_sizes saferplus_LTX__mcrypt_get_supported_key_sizes
   80 #define _mcrypt_get_algorithms_name saferplus_LTX__mcrypt_get_algorithms_name
   81 #define _mcrypt_self_test saferplus_LTX__mcrypt_self_test
   82 #define _mcrypt_algorithm_version saferplus_LTX__mcrypt_algorithm_version
   83 
   84 byte safer_expf[256] =
   85     { 1, 45, 226, 147, 190, 69, 21, 174, 120, 3, 135, 164, 184, 56, 207,
   86     63,
   87     8, 103, 9, 148, 235, 38, 168, 107, 189, 24, 52, 27, 187, 191, 114,
   88     247,
   89     64, 53, 72, 156, 81, 47, 59, 85, 227, 192, 159, 216, 211, 243, 141,
   90     177,
   91     255, 167, 62, 220, 134, 119, 215, 166, 17, 251, 244, 186, 146, 145,
   92     100, 131,
   93     241, 51, 239, 218, 44, 181, 178, 43, 136, 209, 153, 203, 140, 132,
   94     29, 20,
   95     129, 151, 113, 202, 95, 163, 139, 87, 60, 130, 196, 82, 92, 28,
   96     232, 160,
   97     4, 180, 133, 74, 246, 19, 84, 182, 223, 12, 26, 142, 222, 224, 57,
   98     252,
   99     32, 155, 36, 78, 169, 152, 158, 171, 242, 96, 208, 108, 234, 250,
  100     199, 217,
  101     0, 212, 31, 110, 67, 188, 236, 83, 137, 254, 122, 93, 73, 201, 50,
  102     194,
  103     249, 154, 248, 109, 22, 219, 89, 150, 68, 233, 205, 230, 70, 66,
  104     143, 10,
  105     193, 204, 185, 101, 176, 210, 198, 172, 30, 65, 98, 41, 46, 14,
  106     116, 80,
  107     2, 90, 195, 37, 123, 138, 42, 91, 240, 6, 13, 71, 111, 112, 157,
  108     126,
  109     16, 206, 18, 39, 213, 76, 79, 214, 121, 48, 104, 54, 117, 125, 228,
  110     237,
  111     128, 106, 144, 55, 162, 94, 118, 170, 197, 127, 61, 175, 165, 229,
  112     25, 97,
  113     253, 77, 124, 183, 11, 238, 173, 75, 34, 245, 231, 115, 35, 33,
  114     200, 5,
  115     225, 102, 221, 179, 88, 105, 99, 86, 15, 161, 49, 149, 23, 7, 58,
  116     40
  117 };
  118 
  119 byte safer_logf[512] = {
  120     128, 0, 176, 9, 96, 239, 185, 253, 16, 18, 159, 228, 105, 186, 173,
  121     248,
  122     192, 56, 194, 101, 79, 6, 148, 252, 25, 222, 106, 27, 93, 78, 168,
  123     130,
  124     112, 237, 232, 236, 114, 179, 21, 195, 255, 171, 182, 71, 68, 1,
  125     172, 37,
  126     201, 250, 142, 65, 26, 33, 203, 211, 13, 110, 254, 38, 88, 218, 50,
  127     15,
  128     32, 169, 157, 132, 152, 5, 156, 187, 34, 140, 99, 231, 197, 225,
  129     115, 198,
  130     175, 36, 91, 135, 102, 39, 247, 87, 244, 150, 177, 183, 92, 139,
  131     213, 84,
  132     121, 223, 170, 246, 62, 163, 241, 17, 202, 245, 209, 23, 123, 147,
  133     131, 188,
  134     189, 82, 30, 235, 174, 204, 214, 53, 8, 200, 138, 180, 226, 205,
  135     191, 217,
  136     208, 80, 89, 63, 77, 98, 52, 10, 72, 136, 181, 86, 76, 46, 107,
  137     158,
  138     210, 61, 60, 3, 19, 251, 151, 81, 117, 74, 145, 113, 35, 190, 118,
  139     42,
  140     95, 249, 212, 85, 11, 220, 55, 49, 22, 116, 215, 119, 167, 230, 7,
  141     219,
  142     164, 47, 70, 243, 97, 69, 103, 227, 12, 162, 59, 28, 133, 24, 4,
  143     29,
  144     41, 160, 143, 178, 90, 216, 166, 126, 238, 141, 83, 75, 161, 154,
  145     193, 14,
  146     122, 73, 165, 44, 129, 196, 199, 54, 43, 127, 67, 149, 51, 242,
  147     108, 104,
  148     109, 240, 2, 40, 206, 221, 155, 234, 94, 153, 124, 20, 134, 207,
  149     229, 66,
  150     184, 64, 120, 45, 58, 233, 100, 31, 146, 144, 125, 57, 111, 224,
  151     137, 48,
  152 
  153     128, 0, 176, 9, 96, 239, 185, 253, 16, 18, 159, 228, 105, 186, 173,
  154     248,
  155     192, 56, 194, 101, 79, 6, 148, 252, 25, 222, 106, 27, 93, 78, 168,
  156     130,
  157     112, 237, 232, 236, 114, 179, 21, 195, 255, 171, 182, 71, 68, 1,
  158     172, 37,
  159     201, 250, 142, 65, 26, 33, 203, 211, 13, 110, 254, 38, 88, 218, 50,
  160     15,
  161     32, 169, 157, 132, 152, 5, 156, 187, 34, 140, 99, 231, 197, 225,
  162     115, 198,
  163     175, 36, 91, 135, 102, 39, 247, 87, 244, 150, 177, 183, 92, 139,
  164     213, 84,
  165     121, 223, 170, 246, 62, 163, 241, 17, 202, 245, 209, 23, 123, 147,
  166     131, 188,
  167     189, 82, 30, 235, 174, 204, 214, 53, 8, 200, 138, 180, 226, 205,
  168     191, 217,
  169     208, 80, 89, 63, 77, 98, 52, 10, 72, 136, 181, 86, 76, 46, 107,
  170     158,
  171     210, 61, 60, 3, 19, 251, 151, 81, 117, 74, 145, 113, 35, 190, 118,
  172     42,
  173     95, 249, 212, 85, 11, 220, 55, 49, 22, 116, 215, 119, 167, 230, 7,
  174     219,
  175     164, 47, 70, 243, 97, 69, 103, 227, 12, 162, 59, 28, 133, 24, 4,
  176     29,
  177     41, 160, 143, 178, 90, 216, 166, 126, 238, 141, 83, 75, 161, 154,
  178     193, 14,
  179     122, 73, 165, 44, 129, 196, 199, 54, 43, 127, 67, 149, 51, 242,
  180     108, 104,
  181     109, 240, 2, 40, 206, 221, 155, 234, 94, 153, 124, 20, 134, 207,
  182     229, 66,
  183     184, 64, 120, 45, 58, 233, 100, 31, 146, 144, 125, 57, 111, 224,
  184     137, 48
  185 };
  186 
  187 
  188 /* byte  l_key[33 * 16]; */
  189 /* word32  k_bytes; */
  190 
  191 WIN32DLL_DEFINE
  192     int _mcrypt_set_key(SPI * sp_key, const word32 * in_key,
  193             const word32 key_len)
  194 {
  195     word32 wlk[9]; /* 36 bytes */
  196     byte *lk = (void*)wlk, by; /* lk should have 33 bytes */
  197     word32 i, j, k, l, m, *lkp = wlk;
  198 
  199     memset( wlk, 0, sizeof(wlk));
  200 
  201     for (i = 0; i < key_len / sizeof(word32); ++i) {
  202         lkp[i] = in_key[(key_len / sizeof(word32)) - i - 1];
  203     }
  204 
  205     sp_key->k_bytes = key_len;
  206     lk[sp_key->k_bytes] = 0;
  207 
  208     for (i = 0; i < sp_key->k_bytes; ++i) {
  209         lk[sp_key->k_bytes] ^= lk[i];
  210         sp_key->l_key[i] = lk[i];
  211     }
  212 
  213     for (i = 0; i < sp_key->k_bytes; ++i) {
  214         for (j = 0; j <= sp_key->k_bytes; ++j) {
  215             by = lk[j];
  216             lk[j] = by << 3 | by >> 5;
  217         }
  218 
  219         k = 17 * i + 35;
  220         l = 16 * i + 16;
  221         m = i + 1;
  222 
  223         if (i < 16) {
  224             for (j = 0; j < 16; ++j) {
  225                 sp_key->l_key[l + j] =
  226                     lk[m] +
  227                     safer_expf[safer_expf[(k + j) & 255]];
  228 
  229                 m = (m == sp_key->k_bytes ? 0 : m + 1);
  230             }
  231         } else {
  232             for (j = 0; j < 16; ++j) {
  233                 sp_key->l_key[l + j] =
  234                     lk[m] + safer_expf[(k + j) & 255];
  235 
  236                 m = (m == sp_key->k_bytes ? 0 : m + 1);
  237             }
  238         }
  239     }
  240     return 0;
  241 }
  242 
  243 void do_fr(byte x[16], byte * kp)
  244 {
  245     byte t;
  246 
  247     x[0] = safer_expf[x[0] ^ kp[0]] + kp[16];
  248     x[1] = safer_logf[x[1] + kp[1]] ^ kp[17];
  249     x[2] = safer_logf[x[2] + kp[2]] ^ kp[18];
  250     x[3] = safer_expf[x[3] ^ kp[3]] + kp[19];
  251 
  252     x[4] = safer_expf[x[4] ^ kp[4]] + kp[20];
  253     x[5] = safer_logf[x[5] + kp[5]] ^ kp[21];
  254     x[6] = safer_logf[x[6] + kp[6]] ^ kp[22];
  255     x[7] = safer_expf[x[7] ^ kp[7]] + kp[23];
  256 
  257     x[8] = safer_expf[x[8] ^ kp[8]] + kp[24];
  258     x[9] = safer_logf[x[9] + kp[9]] ^ kp[25];
  259     x[10] = safer_logf[x[10] + kp[10]] ^ kp[26];
  260     x[11] = safer_expf[x[11] ^ kp[11]] + kp[27];
  261 
  262     x[12] = safer_expf[x[12] ^ kp[12]] + kp[28];
  263     x[13] = safer_logf[x[13] + kp[13]] ^ kp[29];
  264     x[14] = safer_logf[x[14] + kp[14]] ^ kp[30];
  265     x[15] = safer_expf[x[15] ^ kp[15]] + kp[31];
  266 
  267     x[1] += x[0];
  268     x[0] += x[1];
  269     x[3] += x[2];
  270     x[2] += x[3];
  271     x[5] += x[4];
  272     x[4] += x[5];
  273     x[7] += x[6];
  274     x[6] += x[7];
  275     x[9] += x[8];
  276     x[8] += x[9];
  277     x[11] += x[10];
  278     x[10] += x[11];
  279     x[13] += x[12];
  280     x[12] += x[13];
  281     x[15] += x[14];
  282     x[14] += x[15];
  283 
  284     x[7] += x[0];
  285     x[0] += x[7];
  286     x[1] += x[2];
  287     x[2] += x[1];
  288     x[3] += x[4];
  289     x[4] += x[3];
  290     x[5] += x[6];
  291     x[6] += x[5];
  292     x[11] += x[8];
  293     x[8] += x[11];
  294     x[9] += x[10];
  295     x[10] += x[9];
  296     x[15] += x[12];
  297     x[12] += x[15];
  298     x[13] += x[14];
  299     x[14] += x[13];
  300 
  301     x[3] += x[0];
  302     x[0] += x[3];
  303     x[15] += x[2];
  304     x[2] += x[15];
  305     x[7] += x[4];
  306     x[4] += x[7];
  307     x[1] += x[6];
  308     x[6] += x[1];
  309     x[5] += x[8];
  310     x[8] += x[5];
  311     x[13] += x[10];
  312     x[10] += x[13];
  313     x[11] += x[12];
  314     x[12] += x[11];
  315     x[9] += x[14];
  316     x[14] += x[9];
  317 
  318     x[13] += x[0];
  319     x[0] += x[13];
  320     x[5] += x[2];
  321     x[2] += x[5];
  322     x[9] += x[4];
  323     x[4] += x[9];
  324     x[11] += x[6];
  325     x[6] += x[11];
  326     x[15] += x[8];
  327     x[8] += x[15];
  328     x[1] += x[10];
  329     x[10] += x[1];
  330     x[3] += x[12];
  331     x[12] += x[3];
  332     x[7] += x[14];
  333     x[14] += x[7];
  334 
  335     t = x[0];
  336     x[0] = x[14];
  337     x[14] = x[12];
  338     x[12] = x[10];
  339     x[10] = x[2];
  340     x[2] = x[8];
  341     x[8] = x[4];
  342     x[4] = t;
  343 
  344     t = x[1];
  345     x[1] = x[7];
  346     x[7] = x[11];
  347     x[11] = x[5];
  348     x[5] = x[13];
  349     x[13] = t;
  350 
  351     t = x[15];
  352     x[15] = x[3];
  353     x[3] = t;
  354 }
  355 
  356 void do_ir(byte x[16], byte * kp)
  357 {
  358     byte t;
  359 
  360     t = x[3];
  361     x[3] = x[15];
  362     x[15] = t;
  363 
  364     t = x[13];
  365     x[13] = x[5];
  366     x[5] = x[11];
  367     x[11] = x[7];
  368     x[7] = x[1];
  369     x[1] = t;
  370 
  371     t = x[4];
  372     x[4] = x[8];
  373     x[8] = x[2];
  374     x[2] = x[10];
  375     x[10] = x[12];
  376     x[12] = x[14];
  377     x[14] = x[0];
  378     x[0] = t;
  379 
  380     x[14] -= x[7];
  381     x[7] -= x[14];
  382     x[12] -= x[3];
  383     x[3] -= x[12];
  384     x[10] -= x[1];
  385     x[1] -= x[10];
  386     x[8] -= x[15];
  387     x[15] -= x[8];
  388     x[6] -= x[11];
  389     x[11] -= x[6];
  390     x[4] -= x[9];
  391     x[9] -= x[4];
  392     x[2] -= x[5];
  393     x[5] -= x[2];
  394     x[0] -= x[13];
  395     x[13] -= x[0];
  396 
  397     x[14] -= x[9];
  398     x[9] -= x[14];
  399     x[12] -= x[11];
  400     x[11] -= x[12];
  401     x[10] -= x[13];
  402     x[13] -= x[10];
  403     x[8] -= x[5];
  404     x[5] -= x[8];
  405     x[6] -= x[1];
  406     x[1] -= x[6];
  407     x[4] -= x[7];
  408     x[7] -= x[4];
  409     x[2] -= x[15];
  410     x[15] -= x[2];
  411     x[0] -= x[3];
  412     x[3] -= x[0];
  413 
  414     x[14] -= x[13];
  415     x[13] -= x[14];
  416     x[12] -= x[15];
  417     x[15] -= x[12];
  418     x[10] -= x[9];
  419     x[9] -= x[10];
  420     x[8] -= x[11];
  421     x[11] -= x[8];
  422     x[6] -= x[5];
  423     x[5] -= x[6];
  424     x[4] -= x[3];
  425     x[3] -= x[4];
  426     x[2] -= x[1];
  427     x[1] -= x[2];
  428     x[0] -= x[7];
  429     x[7] -= x[0];
  430 
  431     x[14] -= x[15];
  432     x[15] -= x[14];
  433     x[12] -= x[13];
  434     x[13] -= x[12];
  435     x[10] -= x[11];
  436     x[11] -= x[10];
  437     x[8] -= x[9];
  438     x[9] -= x[8];
  439     x[6] -= x[7];
  440     x[7] -= x[6];
  441     x[4] -= x[5];
  442     x[5] -= x[4];
  443     x[2] -= x[3];
  444     x[3] -= x[2];
  445     x[0] -= x[1];
  446     x[1] -= x[0];
  447 
  448     x[0] = safer_logf[x[0] - kp[16] + 256] ^ kp[0];
  449     x[1] = safer_expf[x[1] ^ kp[17]] - kp[1];
  450     x[2] = safer_expf[x[2] ^ kp[18]] - kp[2];
  451     x[3] = safer_logf[x[3] - kp[19] + 256] ^ kp[3];
  452 
  453     x[4] = safer_logf[x[4] - kp[20] + 256] ^ kp[4];
  454     x[5] = safer_expf[x[5] ^ kp[21]] - kp[5];
  455     x[6] = safer_expf[x[6] ^ kp[22]] - kp[6];
  456     x[7] = safer_logf[x[7] - kp[23] + 256] ^ kp[7];
  457 
  458     x[8] = safer_logf[x[8] - kp[24] + 256] ^ kp[8];
  459     x[9] = safer_expf[x[9] ^ kp[25]] - kp[9];
  460     x[10] = safer_expf[x[10] ^ kp[26]] - kp[10];
  461     x[11] = safer_logf[x[11] - kp[27] + 256] ^ kp[11];
  462 
  463     x[12] = safer_logf[x[12] - kp[28] + 256] ^ kp[12];
  464     x[13] = safer_expf[x[13] ^ kp[29]] - kp[13];
  465     x[14] = safer_expf[x[14] ^ kp[30]] - kp[14];
  466     x[15] = safer_logf[x[15] - kp[31] + 256] ^ kp[15];
  467 }
  468 
  469 WIN32DLL_DEFINE void _mcrypt_encrypt(SPI * spi, word32 * _blk)
  470 {
  471     byte *kp;
  472     word32 __blk[16];
  473     byte *blk = (void *) __blk;
  474 
  475     __blk[3] = _blk[0];
  476     __blk[2] = _blk[1];
  477     __blk[1] = _blk[2];
  478     __blk[0] = _blk[3];
  479 
  480     do_fr(blk, spi->l_key);
  481     do_fr(blk, spi->l_key + 32);
  482     do_fr(blk, spi->l_key + 64);
  483     do_fr(blk, spi->l_key + 96);
  484     do_fr(blk, spi->l_key + 128);
  485     do_fr(blk, spi->l_key + 160);
  486     do_fr(blk, spi->l_key + 192);
  487     do_fr(blk, spi->l_key + 224);
  488 
  489     if (spi->k_bytes > 16) {
  490         do_fr(blk, spi->l_key + 256);
  491         do_fr(blk, spi->l_key + 288);
  492         do_fr(blk, spi->l_key + 320);
  493         do_fr(blk, spi->l_key + 352);
  494     }
  495 
  496     if (spi->k_bytes > 24) {
  497         do_fr(blk, spi->l_key + 384);
  498         do_fr(blk, spi->l_key + 416);
  499         do_fr(blk, spi->l_key + 448);
  500         do_fr(blk, spi->l_key + 480);
  501     }
  502 
  503     kp = spi->l_key + 16 * spi->k_bytes;
  504 
  505     blk[0] ^= kp[0];
  506     blk[1] += kp[1];
  507     blk[2] += kp[2];
  508     blk[3] ^= kp[3];
  509     blk[4] ^= kp[4];
  510     blk[5] += kp[5];
  511     blk[6] += kp[6];
  512     blk[7] ^= kp[7];
  513     blk[8] ^= kp[8];
  514     blk[9] += kp[9];
  515     blk[10] += kp[10];
  516     blk[11] ^= kp[11];
  517     blk[12] ^= kp[12];
  518     blk[13] += kp[13];
  519     blk[14] += kp[14];
  520     blk[15] ^= kp[15];
  521 
  522     _blk[3] = (__blk[0]);
  523     _blk[2] = (__blk[1]);
  524     _blk[1] = (__blk[2]);
  525     _blk[0] = (__blk[3]);
  526 }
  527 
  528 
  529 
  530 WIN32DLL_DEFINE void _mcrypt_decrypt(SPI * spi, word32 * _blk)
  531 {
  532     byte *kp;
  533     word32 __blk[16];
  534     byte *blk = (void *) __blk;
  535 
  536     __blk[3] = _blk[0];
  537     __blk[2] = _blk[1];
  538     __blk[1] = _blk[2];
  539     __blk[0] = _blk[3];
  540 
  541     kp = spi->l_key + 16 * spi->k_bytes;
  542 
  543     blk[0] ^= kp[0];
  544     blk[1] -= kp[1];
  545     blk[2] -= kp[2];
  546     blk[3] ^= kp[3];
  547     blk[4] ^= kp[4];
  548     blk[5] -= kp[5];
  549     blk[6] -= kp[6];
  550     blk[7] ^= kp[7];
  551     blk[8] ^= kp[8];
  552     blk[9] -= kp[9];
  553     blk[10] -= kp[10];
  554     blk[11] ^= kp[11];
  555     blk[12] ^= kp[12];
  556     blk[13] -= kp[13];
  557     blk[14] -= kp[14];
  558     blk[15] ^= kp[15];
  559 
  560     if (spi->k_bytes > 24) {
  561         do_ir(blk, spi->l_key + 480);
  562         do_ir(blk, spi->l_key + 448);
  563         do_ir(blk, spi->l_key + 416);
  564         do_ir(blk, spi->l_key + 384);
  565     }
  566 
  567     if (spi->k_bytes > 16) {
  568         do_ir(blk, spi->l_key + 352);
  569         do_ir(blk, spi->l_key + 320);
  570         do_ir(blk, spi->l_key + 288);
  571         do_ir(blk, spi->l_key + 256);
  572     }
  573 
  574     do_ir(blk, spi->l_key + 224);
  575     do_ir(blk, spi->l_key + 192);
  576     do_ir(blk, spi->l_key + 160);
  577     do_ir(blk, spi->l_key + 128);
  578     do_ir(blk, spi->l_key + 96);
  579     do_ir(blk, spi->l_key + 64);
  580     do_ir(blk, spi->l_key + 32);
  581     do_ir(blk, spi->l_key);
  582 
  583     _blk[3] = (__blk[0]);
  584     _blk[2] = (__blk[1]);
  585     _blk[1] = (__blk[2]);
  586     _blk[0] = (__blk[3]);
  587 }
  588 
  589 
  590 
  591 WIN32DLL_DEFINE int _mcrypt_get_size()
  592 {
  593     return sizeof(SPI);
  594 }
  595 WIN32DLL_DEFINE int _mcrypt_get_block_size()
  596 {
  597     return 16;
  598 }
  599 WIN32DLL_DEFINE int _is_block_algorithm()
  600 {
  601     return 1;
  602 }
  603 WIN32DLL_DEFINE int _mcrypt_get_key_size()
  604 {
  605     return 32;
  606 }
  607 static const int key_sizes[] = { 16,24,32 };
  608 WIN32DLL_DEFINE const int *_mcrypt_get_supported_key_sizes(int *len)
  609 {
  610     *len = sizeof(key_sizes)/sizeof(int);
  611     return key_sizes;
  612 
  613 }
  614 WIN32DLL_DEFINE char *_mcrypt_get_algorithms_name()
  615 {
  616 return "Safer+";
  617 }
  618 
  619 #define CIPHER "97fa76704bf6b578549f65c6f75b228b"
  620 
  621 WIN32DLL_DEFINE int _mcrypt_self_test()
  622 {
  623     char *keyword;
  624     unsigned char plaintext[16];
  625     unsigned char ciphertext[16];
  626     int blocksize = _mcrypt_get_block_size(), j;
  627     void *key;
  628     unsigned char cipher_tmp[200];
  629 
  630     keyword = calloc(1, _mcrypt_get_key_size());
  631     if (keyword == NULL)
  632         return -1;
  633 
  634     for (j = 0; j < _mcrypt_get_key_size(); j++) {
  635         keyword[j] = ((j * 2 + 10) % 256);
  636     }
  637 
  638     for (j = 0; j < blocksize; j++) {
  639         plaintext[j] = j % 256;
  640     }
  641     key = malloc(_mcrypt_get_size());
  642     if (key == NULL)
  643         return -1;
  644 
  645     memcpy(ciphertext, plaintext, blocksize);
  646 
  647     _mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size());
  648     free(keyword);
  649 
  650     _mcrypt_encrypt(key, (void *) ciphertext);
  651 
  652     for (j = 0; j < blocksize; j++) {
  653         sprintf(&((char *) cipher_tmp)[2 * j], "%.2x",
  654             ciphertext[j]);
  655     }
  656 
  657     if (strcmp((char *) cipher_tmp, CIPHER) != 0) {
  658         printf("failed compatibility\n");
  659         printf("Expected: %s\nGot: %s\n", CIPHER,
  660                (char *) cipher_tmp);
  661         free(key);
  662         return -1;
  663     }
  664     _mcrypt_decrypt(key, (void *) ciphertext);
  665     free(key);
  666 
  667     if (strcmp(ciphertext, plaintext) != 0) {
  668         printf("failed internally\n");
  669         return -1;
  670     }
  671 
  672     return 0;
  673 }
  674 
  675 WIN32DLL_DEFINE word32 _mcrypt_algorithm_version()
  676 {
  677     return 20010801;
  678 }
  679 
  680 #ifdef WIN32
  681 # ifdef USE_LTDL
  682 WIN32DLL_DEFINE int main (void)
  683 {
  684        /* empty main function to avoid linker error (see cygwin FAQ) */
  685 }
  686 # endif
  687 #endif