"Fossies" - the Fresh Open Source Software Archive

Member "lessfs-1.7.0/lib_crypto.c" (30 Sep 2011, 3881 Bytes) of package /linux/misc/old/lessfs-1.7.0.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 "lib_crypto.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *   Lessfs: A data deduplicating filesystem.
    3  *   Copyright (C) 2008 Mark Ruijter <mruijter@lessfs.com>
    4  *
    5  *   This program is free software.
    6  *   You can redistribute lessfs and/or modify it under the terms of either
    7  *   (1) the GNU General Public License; either version 3 of the License,
    8  *   or (at your option) any later version as published by
    9  *   the Free Software Foundation; or (2) obtain a commercial license
   10  *   by contacting the Author.
   11  *
   12  *   This program is distributed in the hope that it will be useful,
   13  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
   14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   15  *   the GNU General Public License for more details.
   16  *
   17  *   You should have received a copy of the GNU General Public License
   18  *   along with this program;  if not, write to the Free Software
   19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   20  */
   21 
   22 #ifdef HAVE_CONFIG_H
   23 #include <config.h>
   24 #endif
   25 #ifndef LFATAL
   26 #include "lib_log.h"
   27 #endif
   28 
   29 
   30 #include<stdio.h>
   31 #include<stdlib.h>
   32 #include <string.h>
   33 #include <sys/types.h>
   34 #include <sys/stat.h>
   35 #include <fcntl.h>
   36 #include <pthread.h>
   37 #ifdef ENABLE_CRYPTO
   38 #include<openssl/ssl.h>
   39 #include<openssl/evp.h>
   40 #endif
   41 
   42 #include "lib_safe.h"
   43 #include "retcodes.h"
   44 #include "lib_cfg.h"
   45 
   46 extern char *logname;
   47 extern char *function;
   48 extern int debug;
   49 extern int BLKSIZE;
   50 extern char *passwd;
   51 
   52 #ifdef ENABLE_CRYPTO
   53 static pthread_mutex_t crypto_mutex = PTHREAD_MUTEX_INITIALIZER;
   54 #endif
   55 
   56 #define die_cryptoerr(f...) { LFATAL(f); exit(EXIT_CRYPTOERR); }
   57 
   58 typedef struct {
   59     unsigned long size;
   60     unsigned char *data;
   61 } DAT;
   62 
   63 unsigned char *safepassword()
   64 {
   65     int len;
   66     unsigned char *safepasswd;
   67 
   68     len = strlen((char *) config->passwd);
   69     if (len > 16)
   70         len = 16;
   71     safepasswd = s_malloc(16);
   72     memset(safepasswd, 65, 16);
   73     memcpy(safepasswd, config->passwd, len);
   74     return safepasswd;
   75 }
   76 
   77 #ifdef ENABLE_CRYPTO
   78 DAT *lfsencrypt(unsigned char *unenc, unsigned long size)
   79 {
   80     unsigned char *safepasswd;
   81     EVP_CIPHER_CTX ctx;
   82     DAT *encoded;
   83     int olen, tlen;
   84 
   85     FUNC;
   86 
   87     pthread_mutex_lock(&crypto_mutex);
   88     safepasswd = safepassword();
   89     EVP_CIPHER_CTX_init(&ctx);
   90     EVP_EncryptInit(&ctx, EVP_bf_cbc(), safepasswd, config->iv);
   91     encoded = s_malloc(sizeof(DAT));
   92     encoded->data = s_malloc(8 + size); //Blowfish can grow 64 bits
   93 
   94     if (EVP_EncryptUpdate(&ctx, encoded->data, &olen, unenc, size) != 1) {
   95         die_cryptoerr("error in encrypt update\n");
   96     }
   97 
   98     if (EVP_EncryptFinal(&ctx, encoded->data + olen, &tlen) != 1) {
   99         die_cryptoerr("error in encrypt final\n");
  100     }
  101     EVP_CIPHER_CTX_cleanup(&ctx);
  102     encoded->size = olen + tlen;
  103     if (encoded->size > 8 + size) {
  104         die_cryptoerr
  105             ("Unexpected fatal error : data has grown in size after encryption.\n");
  106     }
  107     s_free(safepasswd);
  108     pthread_mutex_unlock(&crypto_mutex);
  109     EFUNC;
  110     return encoded;
  111 }
  112 
  113 DAT *lfsdecrypt(DAT * data)
  114 {
  115     DAT *decrypted;
  116     unsigned char *safepasswd;
  117 
  118     int olen, tlen;
  119 
  120     FUNC;
  121     pthread_mutex_lock(&crypto_mutex);
  122     decrypted = (DAT *) s_malloc(sizeof(DAT));
  123     decrypted->data = s_malloc(data->size);
  124     safepasswd = safepassword();
  125 
  126     EVP_CIPHER_CTX ctx;
  127     EVP_CIPHER_CTX_init(&ctx);
  128     EVP_DecryptInit(&ctx, EVP_bf_cbc(), safepasswd, config->iv);
  129 
  130     if (EVP_DecryptUpdate
  131         (&ctx, decrypted->data, &olen, data->data, data->size) != 1) {
  132         die_cryptoerr("Unexpected fatal error while decrypting.\n");
  133     }
  134 
  135     if (EVP_DecryptFinal(&ctx, decrypted->data + olen, &tlen) != 1) {
  136         die_cryptoerr("Unexpected fatal error in decrypt final.\n");
  137     }
  138     olen += tlen;
  139     EVP_CIPHER_CTX_cleanup(&ctx);
  140     decrypted->size = olen;
  141     s_free(safepasswd);
  142     pthread_mutex_unlock(&crypto_mutex);
  143     EFUNC;
  144     return decrypted;
  145 }
  146 #endif