Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
crypto.hpp
Go to the documentation of this file.
00001 //*********************************************************************/
00002 // dar - disk archive - a backup/restoration program
00003 // Copyright (C) 2002-2052 Denis Corbin
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // to contact the author : http://dar.linux.free.fr/email.html
00020 /*********************************************************************/
00021 // $Id$
00022 //
00023 /*********************************************************************/
00024 //
00025 
00029 
00030 #ifndef CRYPTO_HPP
00031 #define CRYPTO_HPP
00032 
00033 extern "C"
00034 {
00035 #if HAVE_GCRYPT_H
00036 #include <gcrypt.h>
00037 #endif
00038 }
00039 
00040 #include "../my_config.h"
00041 #include <string>
00042 
00043 #include "tronconneuse.hpp"
00044 #include "header_version.hpp"
00045 #include "secu_string.hpp"
00046 
00047 namespace libdar
00048 {
00049 
00051 
00054     enum crypto_algo
00055     {
00056         crypto_none,          
00057         crypto_scrambling,    
00058         crypto_blowfish,      
00059         crypto_aes256,        
00060         crypto_twofish256,    
00061         crypto_serpent256,    
00062         crypto_camellia256    
00063     };
00064 
00067 
00068     extern void crypto_split_algo_pass(const secu_string & all, crypto_algo & algo, secu_string & pass);
00069 
00070 
00072         //
00073         //
00074 
00077     class crypto_sym : public tronconneuse
00078     {
00079     public:
00080         crypto_sym(U_32 block_size,
00081                    const secu_string & password,
00082                    generic_file & encrypted_side,
00083                    bool no_initial_shift,
00084                    const archive_version & reading_ver,
00085                    crypto_algo algo); //< must be a symetrical algo (else an exception is thrown)
00086         ~crypto_sym() { detruit(); };
00087 
00088     protected:
00089         U_32 encrypted_block_size_for(U_32 clear_block_size);
00090         U_32 clear_block_allocated_size_for(U_32 clear_block_size);
00091         U_32 encrypt_data(const infinint & block_num,
00092                           const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
00093                           char *crypt_buf, U_32 crypt_size);
00094         U_32 decrypt_data(const infinint & block_num,
00095                           const char *crypt_buf, const U_32 crypt_size,
00096                           char *clear_buf, U_32 clear_size);
00097 
00098     private:
00099 #if CRYPTO_AVAILABLE
00100         gcry_cipher_hd_t clef;       //< used to encrypt/decrypt the data
00101         gcry_cipher_hd_t essiv_clef; //< used to build the Initialization Vector
00102 #endif
00103         size_t algo_block_size;         //< the block size of the algorithm
00104         unsigned char *ivec;            //< algo_block_size allocated in secure memory to be used as Initial Vector
00105         U_I algo_id;                    //< algo ID in libgcrypt
00106         archive_version reading_version;
00107 
00108         secu_string pkcs5_pass2key(const secu_string & password,         //< human provided password
00109                                    const std::string & salt,             //< salt string
00110                                    U_I iteration_count,                  //< number of time to shake the melange
00111                                    U_I output_length);                   //< length of the string to return
00112         void dar_set_essiv(const secu_string & key);                     //< assign essiv from the given (hash) string
00113         void make_ivec(const infinint & ref, unsigned char *ivec, U_I size);
00114         void self_test(void);
00115         void detruit();
00116     };
00117 
00119 
00120 } // end of namespace
00121 
00122 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines