• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

crypt/crypt.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //
00003 // $Id: crypt.h 3342 2010-09-17 18:32:00Z java $
00004 //
00005 // Copyright (C) 2008-2010  Rafael Ostertag
00006 //
00007 // This file is part of YAPET.
00008 //
00009 // YAPET is free software: you can redistribute it and/or modify it under the
00010 // terms of the GNU General Public License as published by the Free Software
00011 // Foundation, either version 3 of the License, or (at your option) any later
00012 // version.
00013 //
00014 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00015 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00016 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00017 // details.
00018 //
00019 // You should have received a copy of the GNU General Public License along with
00020 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Additional permission under GNU GPL version 3 section 7
00023 //
00024 // If you modify this program, or any covered work, by linking or combining it
00025 // with the OpenSSL project's OpenSSL library (or a modified version of that
00026 // library), containing parts covered by the terms of the OpenSSL or SSLeay
00027 // licenses, Rafael Ostertag grants you additional permission to convey the
00028 // resulting work.  Corresponding Source for a non-source form of such a
00029 // combination shall include the source code for the parts of OpenSSL used as
00030 // well as that of the covered work.
00031 //
00032 
00033 #ifndef _CRYPT_H
00034 #define _CRYPT_H
00035 
00036 #ifdef HAVE_CONFIG_H
00037 # include <config.h>
00038 #endif
00039 
00040 #ifdef HAVE_INTTYPES_H
00041 # include <inttypes.h>
00042 #endif
00043 
00044 #include "../intl.h"
00045 
00046 
00047 #include <openssl/evp.h>
00048 
00049 #include "yapetexception.h"
00050 #include "key.h"
00051 #include "bdbuffer.h"
00052 #include "record.h"
00053 
00054 namespace YAPET {
00071     class Crypt {
00072         private:
00081             const EVP_CIPHER* cipher;
00088             uint32_t iv_length;
00095             uint32_t key_length;
00101             Key key;
00102 
00103         public:
00105             Crypt (const Key& k) throw (YAPETException);
00106             Crypt (const Crypt& c);
00107             inline ~Crypt() {}
00108 
00118             inline uint32_t getIVLength() const {
00119                 return iv_length;
00120             }
00129             inline uint32_t getKeyLength() const {
00130                 return key_length;
00131             }
00132 
00156             template<class T>
00157             BDBuffer* encrypt (const Record<T>& data)
00158             throw (YAPETException, YAPETEncryptionException) {
00159                 if (key.ivec_size() != iv_length)
00160                     throw YAPETException (_ ("IVec length missmatch") );
00161 
00162                 EVP_CIPHER_CTX ctx;
00163                 EVP_CIPHER_CTX_init (&ctx);
00164                 int retval = EVP_EncryptInit_ex (&ctx,
00165                                                  cipher,
00166                                                  NULL,
00167                                                  key,
00168                                                  key.getIVec() );
00169 
00170                 if (retval == 0) {
00171                     EVP_CIPHER_CTX_cleanup (&ctx);
00172                     throw YAPETEncryptionException (_ ("Error initializing encryption engine") );
00173                 }
00174 
00175                 retval = EVP_CIPHER_CTX_set_key_length (&ctx, key.size() );
00176 
00177                 if (retval == 0) {
00178                     EVP_CIPHER_CTX_cleanup (&ctx);
00179                     throw YAPETException (_ ("Error setting the key length") );
00180                 }
00181 
00182                 BDBuffer* encdata =
00183                     new BDBuffer (data.size() + EVP_MAX_BLOCK_LENGTH);
00184                 int outlen;
00185                 retval = EVP_EncryptUpdate (&ctx,
00186                                             *encdata,
00187                                             &outlen,
00188                                             data,
00189                                             data.size() );
00190 
00191                 if (retval == 0) {
00192                     EVP_CIPHER_CTX_cleanup (&ctx);
00193                     delete encdata;
00194                     throw YAPETEncryptionException (_ ("Error encrypting data") );
00195                 }
00196 
00197                 int tmplen;
00198                 retval = EVP_EncryptFinal_ex (&ctx,
00199                                               encdata->at (outlen),
00200                                               &tmplen);
00201 
00202                 if (retval == 0) {
00203                     EVP_CIPHER_CTX_cleanup (&ctx);
00204                     delete encdata;
00205                     throw YAPETEncryptionException (_ ("Error finalizing encryption") );
00206                 }
00207 
00208                 encdata->resize (outlen + tmplen);
00209                 EVP_CIPHER_CTX_cleanup (&ctx);
00210                 return encdata;
00211             }
00212 
00234             template<class T>
00235             Record<T>* decrypt (const BDBuffer& data)
00236             throw (YAPETException, YAPETEncryptionException) {
00237                 if ( ( (unsigned int) key.ivec_size() ) != iv_length)
00238                     throw YAPETException (_ ("IVec length missmatch") );
00239 
00240                 EVP_CIPHER_CTX ctx;
00241                 EVP_CIPHER_CTX_init (&ctx);
00242                 int retval = EVP_DecryptInit_ex (&ctx,
00243                                                  cipher,
00244                                                  NULL,
00245                                                  key,
00246                                                  key.getIVec() );
00247 
00248                 if (retval == 0) {
00249                     EVP_CIPHER_CTX_cleanup (&ctx);
00250                     throw YAPETEncryptionException (_ ("Error initializing encryption engine") );
00251                 }
00252 
00253                 retval = EVP_CIPHER_CTX_set_key_length (&ctx, key.size() );
00254 
00255                 if (retval == 0) {
00256                     EVP_CIPHER_CTX_cleanup (&ctx);
00257                     throw YAPETException (_ ("Error setting the key length") );
00258                 }
00259 
00260                 BDBuffer* decdata = new BDBuffer (data.size() );
00261                 int outlen;
00262                 retval = EVP_DecryptUpdate (&ctx,
00263                                             *decdata,
00264                                             &outlen,
00265                                             data,
00266                                             data.size() );
00267 
00268                 if (retval == 0) {
00269                     EVP_CIPHER_CTX_cleanup (&ctx);
00270                     delete decdata;
00271                     throw YAPETEncryptionException (_ ("Error decrypting data") );
00272                 }
00273 
00274                 int tmplen;
00275                 retval = EVP_DecryptFinal_ex (&ctx,
00276                                               decdata->at (outlen),
00277                                               &tmplen);
00278 
00279                 if (retval == 0) {
00280                     EVP_CIPHER_CTX_cleanup (&ctx);
00281                     delete decdata;
00282                     throw YAPETEncryptionException (_ ("Error finalizing decryption") );
00283                 }
00284 
00285                 decdata->resize (outlen + tmplen);
00286                 EVP_CIPHER_CTX_cleanup (&ctx);
00287         Record<T>* r = NULL;
00288         try {
00289             r = new Record<T>;
00290             *r = *decdata;
00291         } catch (...) {
00292             if ( r != NULL)
00293             delete r;
00294             delete decdata;
00295             throw;
00296         }
00297                 delete decdata;
00298                 return r;
00299             }
00300 
00301             const Crypt& operator= (const Crypt& c);
00302     };
00303 }
00304 
00305 #endif // _CRYPT_H

Generated on Sun Sep 19 2010 15:37:13 for YAPET by  doxygen 1.7.1