"Fossies" - the Fresh Open Source Software Archive

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

    1 /*
    2  * Copyright (C) 1998,1999,2000,2001 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 nofb_LTX__init_mcrypt
   24 #define _mcrypt_set_state nofb_LTX__mcrypt_set_state
   25 #define _mcrypt_get_state nofb_LTX__mcrypt_get_state
   26 #define _end_mcrypt nofb_LTX__end_mcrypt
   27 #define _mcrypt nofb_LTX__mcrypt
   28 #define _mdecrypt nofb_LTX__mdecrypt
   29 #define _has_iv nofb_LTX__has_iv
   30 #define _is_block_mode nofb_LTX__is_block_mode
   31 #define _is_block_algorithm_mode nofb_LTX__is_block_algorithm_mode
   32 #define _mcrypt_get_modes_name nofb_LTX__mcrypt_get_modes_name
   33 #define _mcrypt_mode_get_size nofb_LTX__mcrypt_mode_get_size
   34 #define _mcrypt_mode_version nofb_LTX__mcrypt_mode_version
   35 
   36 typedef struct ncfb_buf {
   37     byte*   enc_s_register;
   38     byte* s_register;
   39     int   s_register_pos;
   40     int   blocksize;
   41 } nOFB_BUFFER;
   42 
   43 /* nOFB MODE */
   44 
   45 int _init_mcrypt( nOFB_BUFFER* buf, void *key, int lenofkey, void *IV, int size)
   46 {
   47     buf->enc_s_register = buf->s_register = NULL;
   48     buf->s_register_pos = 0;
   49 
   50     buf->blocksize = size;    
   51 /* For ofb */
   52     buf->enc_s_register=malloc( size);
   53     if (buf->enc_s_register==NULL) goto freeall;
   54     
   55     buf->s_register=malloc( size);
   56     if (buf->s_register==NULL) goto freeall;
   57 
   58     if (IV!=NULL) {
   59         memcpy(buf->enc_s_register, IV, size);
   60         memcpy(buf->s_register, IV, size);
   61     } else {
   62         memset(buf->enc_s_register, 0, size);
   63         memset(buf->s_register, 0, size);
   64     }
   65 
   66 /* End nofb */
   67 
   68     return 0;
   69     freeall:
   70         free(buf->enc_s_register);
   71         free(buf->s_register);
   72         return -1;
   73 }
   74 
   75 int _mcrypt_set_state( nOFB_BUFFER* buf, byte *IV, int size)
   76 {
   77     buf->s_register_pos = IV[0];
   78     memcpy(buf->enc_s_register, &IV[1], size-1);
   79     memcpy(buf->s_register, &IV[1], size-1);
   80 
   81     return 0;
   82 }
   83 
   84 int _mcrypt_get_state( nOFB_BUFFER* buf, byte *IV, int *size)
   85 {
   86     if (*size < buf->blocksize + 1) {
   87         *size = buf->blocksize + 1;
   88         return -1;
   89     }
   90     *size = buf->blocksize + 1;
   91 
   92     IV[0] = buf->s_register_pos;
   93     memcpy( &IV[1], buf->s_register, buf->blocksize);
   94 
   95     return 0;
   96 }
   97 
   98 
   99 void _end_mcrypt( nOFB_BUFFER* buf) {
  100     free(buf->s_register);
  101     free(buf->enc_s_register);
  102 }
  103 
  104 inline static
  105 void xor_stuff( nOFB_BUFFER *buf, void* akey, void (*func)(void*,void*), byte* plain,  int blocksize, int xor_size)
  106 {
  107     void (*_mcrypt_block_encrypt) (void *, void *);
  108 
  109     _mcrypt_block_encrypt = func;
  110 
  111     if (xor_size == blocksize) {
  112         if (buf->s_register_pos == 0) {
  113 
  114             memcpy(buf->enc_s_register, buf->s_register, blocksize);
  115 
  116             _mcrypt_block_encrypt(akey, buf->enc_s_register);
  117 
  118             memcpy(buf->s_register, buf->enc_s_register, blocksize);
  119             
  120             memxor( plain, buf->enc_s_register, blocksize);
  121 
  122         } else {
  123             int size = blocksize - buf->s_register_pos;
  124 
  125             memxor( plain, &buf->enc_s_register[buf->s_register_pos],
  126                 size); 
  127         
  128             memcpy(buf->enc_s_register, buf->s_register, blocksize);
  129 
  130             _mcrypt_block_encrypt(akey, buf->enc_s_register);
  131 
  132             memcpy( buf->s_register, 
  133                 buf->enc_s_register, blocksize);
  134 
  135             memxor( &plain[size], buf->enc_s_register,
  136                 buf->s_register_pos);
  137 
  138             /* buf->s_register_pos remains the same */
  139         }
  140     } else { /* xor_size != blocksize */
  141         if (buf->s_register_pos == 0) {
  142             memcpy(buf->enc_s_register, buf->s_register, blocksize);
  143 
  144             _mcrypt_block_encrypt(akey, buf->enc_s_register);
  145 
  146             memcpy(buf->s_register, buf->enc_s_register, blocksize);
  147             
  148             memxor( plain, buf->enc_s_register, xor_size);
  149 
  150             buf->s_register_pos = xor_size;
  151         } else {
  152             int size = blocksize - buf->s_register_pos;
  153             int min_size =  size < xor_size ? size: xor_size;
  154 
  155             memxor( plain, &buf->enc_s_register[buf->s_register_pos],
  156                 min_size);
  157 
  158             buf->s_register_pos += min_size;
  159 
  160             if (min_size >= xor_size)
  161                 return;
  162 
  163             memcpy(buf->enc_s_register, buf->s_register, blocksize);
  164 
  165             _mcrypt_block_encrypt(akey, buf->enc_s_register);
  166 
  167             memcpy(buf->s_register, buf->enc_s_register, blocksize);
  168 
  169             memxor( &plain[min_size], buf->s_register,
  170                 xor_size - min_size);
  171 
  172             buf->s_register_pos = xor_size - min_size;
  173 
  174         }
  175     
  176     }
  177     return;
  178 }
  179 
  180 
  181 int _mcrypt( nOFB_BUFFER* buf,void *plaintext, int len, int blocksize, void* akey, void (*func)(void*,void*), void (*func2)(void*,void*))
  182 {               /* plaintext is n*blocksize bytes (nbit cfb) */
  183     byte* plain;
  184     int i, j=0;
  185     void (*_mcrypt_block_encrypt) (void *, void *);
  186     int modlen;
  187     
  188     _mcrypt_block_encrypt = func;
  189 
  190     plain = plaintext;
  191     for (j = 0; j < len / blocksize; j++) {
  192         xor_stuff( buf, akey, func, plain, blocksize, blocksize); 
  193         
  194         plain += blocksize;
  195 
  196     }
  197     modlen = len % blocksize;
  198     if (modlen > 0) {
  199         xor_stuff( buf, akey, func, plain, blocksize, modlen); 
  200     }
  201     
  202     return 0;
  203 }
  204 
  205 
  206 int _mdecrypt( nOFB_BUFFER* buf,void *plaintext, int len, int blocksize, void* akey, void (*func)(void*,void*), void (*func2)(void*,void*))
  207 {               /* plaintext is n*blocksize bytes (nbit cfb) */
  208     return _mcrypt( buf, plaintext, len, blocksize, akey, func, func2);
  209 }
  210 
  211 int _has_iv() { return 1; }
  212 int _is_block_mode() { return 0; }
  213 int _is_block_algorithm_mode() { return 1; }
  214 char *_mcrypt_get_modes_name() { return "nOFB";}
  215 int _mcrypt_mode_get_size () {return sizeof(nOFB_BUFFER);}
  216 
  217 
  218 word32 _mcrypt_mode_version() {
  219     return 20020307;
  220 }
  221 
  222 #ifdef WIN32
  223 # ifdef USE_LTDL
  224 WIN32DLL_DEFINE int main (void)
  225 {
  226        /* empty main function to avoid linker error (see cygwin FAQ) */
  227 }
  228 # endif
  229 #endif