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

crypt/crypt.cc

Go to the documentation of this file.
00001 // $Id: crypt.cc 3342 2010-09-17 18:32:00Z java $
00002 //
00003 // Copyright (C) 2008-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 // Additional permission under GNU GPL version 3 section 7
00021 //
00022 // If you modify this program, or any covered work, by linking or combining it
00023 // with the OpenSSL project's OpenSSL library (or a modified version of that
00024 // library), containing parts covered by the terms of the OpenSSL or SSLeay
00025 // licenses, Rafael Ostertag grants you additional permission to convey the
00026 // resulting work.  Corresponding Source for a non-source form of such a
00027 // combination shall include the source code for the parts of OpenSSL used as
00028 // well as that of the covered work.
00029 //
00030 
00031 #include "../intl.h"
00032 
00033 #include "crypt.h"
00034 
00035 using namespace YAPET;
00036 
00050 Crypt::Crypt (const Key& k) throw (YAPETException) : cipher (NULL),
00051         iv_length (0),
00052         key_length (0),
00053         key (k) {
00054     cipher = EVP_bf_cbc();
00055 
00056     if (cipher == NULL)
00057         throw YAPETException (_ ("Unable to get cipher") );
00058 
00059     // Test if key length is ok
00060     EVP_CIPHER_CTX ctx;
00061     EVP_CIPHER_CTX_init (&ctx);
00062     int retval = EVP_CipherInit_ex (&ctx, cipher, NULL, NULL, NULL, 0);
00063 
00064     if (retval == 0) {
00065         EVP_CIPHER_CTX_cleanup (&ctx);
00066         throw YAPETException (_ ("Error initializing cipher") );
00067     }
00068 
00069     retval = EVP_CIPHER_CTX_set_key_length (&ctx, key.size() );
00070 
00071     if (retval == 0) {
00072         EVP_CIPHER_CTX_cleanup (&ctx);
00073         throw YAPETException (_ ("Error setting the key length") );
00074     }
00075 
00076     iv_length = EVP_CIPHER_CTX_iv_length (&ctx);
00077     key_length = EVP_CIPHER_CTX_key_length (&ctx);
00078     EVP_CIPHER_CTX_cleanup (&ctx);
00079 }
00080 
00081 Crypt::Crypt (const Crypt& c) : cipher (c.cipher),
00082         iv_length (c.iv_length),
00083         key_length (c.key_length),
00084         key (c.key) {
00085 }
00086 
00087 const Crypt&
00088 Crypt::operator= (const Crypt & c) {
00089     if (this == &c) return *this;
00090 
00091     iv_length = c.iv_length;
00092     key_length = c.key_length;
00093     cipher = c.cipher;
00094     key = c.key;
00095     return *this;
00096 }

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