Disk ARchive  2.5.3
Full featured and portable backup and archiving tool
crypto_sym.hpp
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 
25 
26 #ifndef CRYPTO_SYM_HPP
27 #define CRYPTO_SYM_HPP
28 
29 extern "C"
30 {
31 #if HAVE_GCRYPT_H
32 #ifndef GCRYPT_NO_DEPRECATED
33 #define GCRYPT_NO_DEPRECATED
34 #endif
35 #include <gcrypt.h>
36 #endif
37 }
38 
39 #include "../my_config.h"
40 #include <string>
41 
42 #include "tronconneuse.hpp"
43 #include "secu_string.hpp"
44 #include "crypto.hpp"
45 
46 namespace libdar
47 {
48 
51 
53  {
54 #if CRYPTO_AVAILABLE
55  return gcry_check_version(MIN_VERSION_GCRYPT_HASH_BUG);
56 #else
57  return true;
58 #endif
59  }
60 
63  class crypto_sym : public tronconneuse
64  {
65  public:
66  crypto_sym(U_32 block_size,
67  const secu_string & password,
68  generic_file & encrypted_side,
69  bool no_initial_shift,
70  const archive_version & reading_ver,
71  crypto_algo algo,
72  bool use_pkcs5); //< must be set to true when password is human defined, false when password is randomly generated by pseudo-random generator and has the maximum length for the given algorithm
73  ~crypto_sym() { detruit(); };
74 
76  static size_t max_key_len(crypto_algo algo);
77 
79  static size_t max_key_len_libdar(crypto_algo algo);
80 
82  static bool is_a_strong_password(crypto_algo algo, const secu_string & password);
83 
84  protected:
85  U_32 encrypted_block_size_for(U_32 clear_block_size);
86  U_32 clear_block_allocated_size_for(U_32 clear_block_size);
87  U_32 encrypt_data(const infinint & block_num,
88  const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
89  char *crypt_buf, U_32 crypt_size);
90  U_32 decrypt_data(const infinint & block_num,
91  const char *crypt_buf, const U_32 crypt_size,
92  char *clear_buf, U_32 clear_size);
93 
94  private:
95 #if CRYPTO_AVAILABLE
96  gcry_cipher_hd_t clef; //< used to encrypt/decrypt the data
97  gcry_cipher_hd_t essiv_clef; //< used to build the Initialization Vector
98 #endif
99  size_t algo_block_size; //< the block size of the algorithm (main key)
100  unsigned char *ivec; //< algo_block_size allocated in secure memory to be used as Initial Vector
101  U_I algo_id; //< algo ID in libgcrypt
102 
103  void detruit();
104 
108 
109 #if CRYPTO_AVAILABLE
110  static void dar_set_essiv(const secu_string & key, //< the key to base the essiv on
111  gcry_cipher_hd_t & IVkey, //< assign essiv from the given (hash) string
112  const archive_version & ver, //< archive format we read or write
113  crypto_algo main_cipher); //< the choice of the algo for essiv depends on the cipher used for the main key
121  static void make_ivec(const infinint & ref,
122  unsigned char *ivec,
123  U_I size,
124  const gcry_cipher_hd_t & IVkey);
125 
127  static secu_string pkcs5_pass2key(const secu_string & password, //< human provided password
128  const std::string & salt, //< salt string
129  U_I iteration_count, //< number of time to shake the melange
130  U_I output_length); //< length of the string to return
131 
133  static U_I get_algo_id(crypto_algo algo);
134 #endif
135 
136 #ifdef LIBDAR_NO_OPTIMIZATION
137  static void self_test(void);
138 #endif
139  };
140 
142 
143 } // end of namespace
144 
145 #endif
this is a partial implementation of the generic_file interface to cypher/decypher data block by block...
static bool is_a_strong_password(crypto_algo algo, const secu_string &password)
check whether the given password is reported as strong in regard to the given cipher ...
the crypto algoritm definition
static size_t max_key_len_libdar(crypto_algo algo)
returns the max key length in octets to use to compute a key from a user provided password ...
static size_t max_key_len(crypto_algo algo)
returns the max key length in octets for the given algorithm
crypto_algo
the different cypher available for encryption (strong or weak)
Definition: crypto.hpp:51
bool crypto_min_ver_libgcrypt_no_bug()
Definition: crypto_sym.hpp:52
U_32 encrypted_block_size_for(U_32 clear_block_size)
defines the size necessary to encrypt a given amount of clear data
class secu_string
Definition: secu_string.hpp:57
this is the interface class from which all other data transfer classes inherit
U_32 decrypt_data(const infinint &block_num, const char *crypt_buf, const U_32 crypt_size, char *clear_buf, U_32 clear_size)
this method decyphers data
U_32 clear_block_allocated_size_for(U_32 clear_block_size)
it may be necessary by the inherited class have few more bytes allocated after the clear data given f...
defines a block structured file.Mainly used for strong encryption.
the arbitrary large positive integer class
class archive_version manages the version of the archive format
U_32 encrypt_data(const infinint &block_num, const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated, char *crypt_buf, U_32 crypt_size)
this method encrypts the clear data given
this file contains the definition of secu_string class, a std::string like class but allocated in sec...
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47