"Fossies" - the Fresh Open Source Software Archive

Member "libmcrypt-2.5.8/modules/modes/cfb.c" (15 Jan 2003, 4336 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 "cfb.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 1998,1999,2000,2001,2002 Nikos Mavroyanopoulos
    3  * 
    4  * This library is free software; you can redistribute it and/or modify it 
    5  * under the terms of the GNU Library General Public License as published 
    6  * by the Free Software Foundation; either version 2 of the License, or 
    7  * (at your option) any later version.
    8  *
    9  * This library 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 GNU
   12  * Library General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU Library General Public
   15  * License along with this library; if not, write to the
   16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   17  * Boston, MA 02111-1307, USA.
   18  */
   19 
   20 #include <libdefs.h>
   21 #include <mcrypt_modules.h>
   22 
   23 #define _init_mcrypt cfb_LTX__init_mcrypt
   24 #define _mcrypt_set_state cfb_LTX__mcrypt_set_state
   25 #define _mcrypt_get_state cfb_LTX__mcrypt_get_state
   26 #define _end_mcrypt cfb_LTX__end_mcrypt
   27 #define _mcrypt cfb_LTX__mcrypt
   28 #define _mdecrypt cfb_LTX__mdecrypt
   29 #define _has_iv cfb_LTX__has_iv
   30 #define _is_block_mode cfb_LTX__is_block_mode
   31 #define _is_block_algorithm_mode cfb_LTX__is_block_algorithm_mode
   32 #define _mcrypt_get_modes_name cfb_LTX__mcrypt_get_modes_name
   33 #define _mcrypt_mode_get_size cfb_LTX__mcrypt_mode_get_size
   34 #define _mcrypt_mode_version cfb_LTX__mcrypt_mode_version
   35 
   36 typedef struct cfb_buf {
   37     byte* s_register;
   38     byte* enc_s_register;
   39     int blocksize;
   40 } CFB_BUFFER;
   41 
   42 /* CFB MODE */
   43 
   44 int _init_mcrypt( CFB_BUFFER* buf, void *key, int lenofkey, void *IV, int size)
   45 {
   46 
   47     buf->s_register = buf->enc_s_register = NULL;
   48     
   49     buf->blocksize = size;
   50 /* For cfb */
   51     buf->s_register=malloc( size);
   52     if (buf->s_register==NULL) goto freeall;
   53 
   54     buf->enc_s_register=malloc( size);
   55     if (buf->enc_s_register==NULL) goto freeall;
   56 
   57     if (IV!=NULL) {
   58         memcpy(buf->s_register, IV, size);
   59     } else {
   60         memset(buf->s_register, 0, size);   
   61     }
   62 /* End cfb */
   63     return 0;
   64 
   65     freeall:
   66         free(buf->s_register);
   67         free(buf->enc_s_register);
   68         return -1;
   69 }
   70 
   71 
   72 int _mcrypt_set_state( CFB_BUFFER* buf, void *IV, int size)
   73 {
   74     memcpy(buf->enc_s_register, IV, size);
   75     memcpy(buf->s_register, IV, size);
   76 
   77     return 0;
   78 }
   79 
   80 int _mcrypt_get_state( CFB_BUFFER* buf, byte *IV, int *size)
   81 {
   82     if (*size < buf->blocksize) {
   83         *size = buf->blocksize;
   84         return -1;
   85     }
   86     *size = buf->blocksize;
   87 
   88     memcpy( IV, buf->s_register, buf->blocksize);
   89 
   90     return 0;
   91 }
   92 
   93 
   94 void _end_mcrypt( CFB_BUFFER* buf) {
   95     free(buf->s_register);
   96     free(buf->enc_s_register);
   97 }
   98 
   99 int _mcrypt( CFB_BUFFER* buf, void *plaintext, int len, int blocksize, void* akey, void (*func)(void*,void*), void (*func2)(void*,void*))
  100 {               /* plaintext is 1 byte (8bit cfb) */
  101     char *plain = plaintext;
  102     int i, j;
  103     void (*_mcrypt_block_encrypt) (void *, void *);
  104 
  105     _mcrypt_block_encrypt = func;
  106 
  107     for (j = 0; j < len; j++) {
  108 
  109         memcpy(buf->enc_s_register, buf->s_register, blocksize);
  110 
  111         _mcrypt_block_encrypt(akey, buf->enc_s_register);
  112 
  113         plain[j] ^= buf->enc_s_register[0];
  114 
  115 /* Shift the register */
  116         for (i = 0; i < (blocksize - 1); i++)
  117             buf->s_register[i] = buf->s_register[i + 1];
  118 
  119         buf->s_register[blocksize - 1] = plain[j];
  120     }
  121 
  122     return 0;
  123 
  124 }
  125 
  126 
  127 int _mdecrypt( CFB_BUFFER* buf, void *plaintext, int len, int blocksize, void* akey, void (*func)(void*,void*), void (*func2)(void*,void*))
  128 {               /* plaintext is 1 byte (8bit ofb) */
  129     char *plain = plaintext;
  130     int i, j;
  131     void (*_mcrypt_block_encrypt) (void *, void *);
  132 
  133     _mcrypt_block_encrypt = func;
  134 
  135     for (j = 0; j < len; j++) {
  136 
  137         memcpy(buf->enc_s_register, buf->s_register, blocksize);
  138 
  139         _mcrypt_block_encrypt(akey, buf->enc_s_register);
  140 
  141 /* Shift the register */
  142         for (i = 0; i < (blocksize - 1); i++)
  143             buf->s_register[i] = buf->s_register[i + 1];
  144 
  145         buf->s_register[blocksize - 1] = plain[j];
  146 
  147         plain[j] ^= buf->enc_s_register[0];
  148 
  149     }
  150 
  151     return 0;
  152 }
  153 
  154 int _has_iv() { return 1; }
  155 int _is_block_mode() { return 0; }
  156 int _is_block_algorithm_mode() { return 1; }
  157 const char *_mcrypt_get_modes_name() { return "CFB"; }
  158 int _mcrypt_mode_get_size () {return sizeof(CFB_BUFFER);}
  159 
  160 word32 _mcrypt_mode_version() {
  161     return 20020310;
  162 }
  163 
  164 #ifdef WIN32
  165 # ifdef USE_LTDL
  166 WIN32DLL_DEFINE int main (void)
  167 {
  168        /* empty main function to avoid linker error (see cygwin FAQ) */
  169 }
  170 # endif
  171 #endif