crypt/crypt.cc

Go to the documentation of this file.
00001 // $Id: crypt.cc,v 1.4 2008-02-07 23:39:09 rafi Exp $
00002 //
00003 // YAPET -- Yet Another Password Encryption Tool
00004 // Copyright (C) 2008  Rafael Ostertag
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #include "crypt.h"
00021 
00022 using namespace YAPET;
00023 
00037 Crypt::Crypt(const Key& k) throw(YAPETException) : cipher(NULL),
00038                          iv_length(0),
00039                          key_length(0),
00040                          key(k){
00041     cipher = EVP_bf_cbc();
00042     if (cipher == NULL)
00043     throw YAPETException("Unable to get cipher");
00044 
00045     // Test if key length is ok
00046     EVP_CIPHER_CTX ctx;
00047     EVP_CIPHER_CTX_init(&ctx);
00048 
00049     int retval = EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, NULL, 0);
00050     if (retval == 0) {
00051     EVP_CIPHER_CTX_cleanup(&ctx);
00052     throw YAPETException("Error initializing cipher");
00053     }
00054 
00055     retval = EVP_CIPHER_CTX_set_key_length(&ctx, key.size());
00056     if (retval == 0) {
00057     EVP_CIPHER_CTX_cleanup(&ctx);
00058     throw YAPETException("Error setting the key length");
00059     }
00060     
00061     iv_length = EVP_CIPHER_CTX_iv_length(&ctx);
00062     key_length = EVP_CIPHER_CTX_key_length(&ctx);
00063 
00064     EVP_CIPHER_CTX_cleanup(&ctx);
00065 }
00066 
00067 Crypt::Crypt(const Crypt& c) : cipher(c.cipher),
00068                    iv_length(c.iv_length),
00069                    key_length(c.key_length),
00070                    key(c.key) {
00071 }
00072 
00073 const Crypt&
00074 Crypt::operator=(const Crypt& c) {
00075     if (this == &c) return *this;
00076 
00077     iv_length = c.iv_length;
00078     key_length = c.key_length;
00079     cipher = c.cipher;
00080     key = c.key;
00081 
00082     return *this;
00083 }

Generated on Wed Feb 27 16:15:41 2008 for YAPET by  doxygen 1.5.4