Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
crypto.hpp
Go to the documentation of this file.
1 //*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 // $Id: crypto.hpp,v 1.21 2011/01/09 17:25:58 edrusb Rel $
22 //
23 /*********************************************************************/
24 //
25 
29 
30 #ifndef CRYPTO_HPP
31 #define CRYPTO_HPP
32 
33 extern "C"
34 {
35 #if HAVE_GCRYPT_H
36 #include <gcrypt.h>
37 #endif
38 }
39 
40 #include "../my_config.h"
41 #include <string>
42 
43 #include "tronconneuse.hpp"
44 #include "header_version.hpp"
45 #include "secu_string.hpp"
46 
47 namespace libdar
48 {
49 
51 
55  {
63  };
64 
67 
68  extern void crypto_split_algo_pass(const secu_string & all, crypto_algo & algo, secu_string & pass);
69 
70 
72  //
73  //
74 
77  class crypto_sym : public tronconneuse
78  {
79  public:
80  crypto_sym(U_32 block_size,
81  const secu_string & password,
82  generic_file & encrypted_side,
83  bool no_initial_shift,
84  const archive_version & reading_ver,
85  crypto_algo algo); //< must be a symetrical algo (else an exception is thrown)
86  ~crypto_sym() { detruit(); };
87 
88  protected:
89  U_32 encrypted_block_size_for(U_32 clear_block_size);
90  U_32 clear_block_allocated_size_for(U_32 clear_block_size);
91  U_32 encrypt_data(const infinint & block_num,
92  const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
93  char *crypt_buf, U_32 crypt_size);
94  U_32 decrypt_data(const infinint & block_num,
95  const char *crypt_buf, const U_32 crypt_size,
96  char *clear_buf, U_32 clear_size);
97 
98  private:
99 #if CRYPTO_AVAILABLE
100  gcry_cipher_hd_t clef; //< used to encrypt/decrypt the data
101  gcry_cipher_hd_t essiv_clef; //< used to build the Initialization Vector
102 #endif
103  size_t algo_block_size; //< the block size of the algorithm
104  unsigned char *ivec; //< algo_block_size allocated in secure memory to be used as Initial Vector
105  U_I algo_id; //< algo ID in libgcrypt
106  archive_version reading_version;
107 
108  secu_string pkcs5_pass2key(const secu_string & password, //< human provided password
109  const std::string & salt, //< salt string
110  U_I iteration_count, //< number of time to shake the melange
111  U_I output_length); //< length of the string to return
112  void dar_set_essiv(const secu_string & key); //< assign essiv from the given (hash) string
113  void make_ivec(const infinint & ref, unsigned char *ivec, U_I size);
114  void self_test(void);
115  void detruit();
116  };
117 
119 
120 } // end of namespace
121 
122 #endif