"Fossies" - the Fresh Open Source Software Archive

Member "libmcrypt-2.5.8/src/cipher_test.c" (19 Feb 2007, 3666 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 "cipher_test.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *    Copyright (C) 1998,1999,2000,2002 Nikos Mavroyanopoulos
    3  *
    4  *    This program is free software; you can redistribute it and/or modify
    5  *    it under the terms of the GNU General Public License as published by
    6  *    the Free Software Foundation; either version 2 of the License, or
    7  *    (at your option) any later version.
    8  *
    9  *    This program is distributed in the hope that it will be useful,
   10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  *    GNU General Public License for more details.
   13  *
   14  *    You should have received a copy of the GNU General Public License
   15  *    along with this program; if not, write to the Free Software
   16  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   17  */
   18 
   19 #include "../include/mutils/mcrypt.h"
   20 #include <stdio.h>
   21 #include <stdlib.h>
   22 #include <string.h>
   23 
   24 /* Prints plaintext and ciphertext in hex for all the algorithms */
   25 
   26 #define ALGORITHMS_DIR "../modules/algorithms/.libs"
   27 #define MODES_DIR "../modules/modes/.libs"
   28 /* #define ALGORITHMS_DIR NULL
   29  * #define MODES_DIR NULL
   30  */
   31 #define TEXT "a small text, just to test the implementation"
   32 
   33 int main()
   34 {
   35     MCRYPT td, td2;
   36     int i, t, imax;
   37     int j, jmax, ivsize;
   38     int x = 0, siz;
   39     char **names;
   40     char **modes;
   41     char *text;
   42     unsigned char *IV;
   43     unsigned char *key;
   44     int keysize;
   45     
   46     names = mcrypt_list_algorithms (ALGORITHMS_DIR, &jmax);
   47     modes = mcrypt_list_modes (MODES_DIR, &imax);
   48 
   49     if (names==NULL || modes==NULL) {
   50         fprintf(stderr, "Error getting algorithms/modes\n");
   51         exit(1);
   52     }
   53     
   54     for (j=0;j<jmax;j++) {
   55         printf( "Algorithm: %s... ", names[j]);
   56 
   57         if (mcrypt_module_self_test( names[j], ALGORITHMS_DIR)==0) {
   58             printf( "ok\n");
   59         } else {
   60             x=1;
   61             printf( "\n");
   62         }
   63         printf( "Modes:\n");
   64             for (i=0;i<imax;i++) {
   65                 td = mcrypt_module_open(names[j], ALGORITHMS_DIR, modes[i], MODES_DIR);
   66                 td2 = mcrypt_module_open(names[j], ALGORITHMS_DIR, modes[i], MODES_DIR);
   67                 if (td != MCRYPT_FAILED && td2 != MCRYPT_FAILED) {
   68                     keysize = mcrypt_enc_get_key_size(td);
   69                     key = calloc(1, keysize);
   70                     if (key==NULL) exit(1);
   71                     
   72                     for (t=0;t<keysize;t++)
   73                         key[t] = (t % 255) + 13;
   74                     
   75                     ivsize = mcrypt_enc_get_iv_size(td);
   76                     if (ivsize>0) {
   77                         IV = calloc( 1, ivsize);
   78                         if (IV==NULL) exit(1);
   79                         for (t=0;t<ivsize;t++)
   80                             IV[t] = (t*2 % 255) + 15;
   81                     }
   82                     if (mcrypt_generic_init( td, key, keysize, IV) < 0) {
   83                         fprintf(stderr, "Failed to Initialize algorithm!\n");
   84                         return -1;
   85                     }
   86 
   87                     if (mcrypt_enc_is_block_mode(td)!=0)
   88                         siz = (strlen(TEXT) / mcrypt_enc_get_block_size(td))*mcrypt_enc_get_block_size(td);
   89                     else siz = strlen(TEXT);
   90 
   91                     text = calloc( 1, siz);
   92                     if (text==NULL) exit(1);
   93                     
   94                     memmove( text, TEXT, siz);
   95 
   96                     mcrypt_generic( td, text, siz);
   97 
   98                     if (mcrypt_generic_init( td2, key, keysize, IV) < 0) {
   99                         fprintf(stderr, "Failed to Initialize algorithm!\n");
  100                         return -1;
  101                     }
  102 
  103                     mdecrypt_generic( td2, text, siz);
  104                     if ( memcmp( text, TEXT, siz) == 0) {
  105                         printf( "   %s: ok\n", modes[i]);
  106                     } else {
  107                         printf( "   %s: failed\n", modes[i]);
  108                         x=1;
  109                     }
  110                     mcrypt_generic_deinit(td);
  111                     mcrypt_generic_deinit(td2);
  112                     mcrypt_module_close(td);
  113                     mcrypt_module_close(td2);                   free(text);
  114                     free(key);
  115                     if (ivsize>0) free(IV);
  116                 }
  117             }
  118         printf("\n");
  119         
  120     }   
  121     mcrypt_free_p(names, jmax);
  122     mcrypt_free_p(modes, imax);
  123     
  124 
  125     if (x>0) fprintf(stderr, "\nProbably some of the algorithms listed above failed. "
  126                 "Try not to use these algorithms, and file a bug report to mcrypt-dev@lists.hellug.gr\n\n");
  127     return x;
  128 }