Disk ARchive  2.6.13
Full featured and portable backup and archiving tool
crypto_sym.hpp
Go to the documentation of this file.
1 //*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2020 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include "archive_aux.hpp"
46 
47 namespace libdar
48 {
49 
52 
53  inline bool crypto_min_ver_libgcrypt_no_bug()
54  {
55 #if CRYPTO_AVAILABLE
56  return gcry_check_version(MIN_VERSION_GCRYPT_HASH_BUG);
57 #else
58  return true;
59 #endif
60  }
61 
63 
64  class crypto_sym : public tronconneuse
65  {
66  public:
67  crypto_sym(U_32 block_size,
68  const secu_string & password,
69  generic_file & encrypted_side,
70  bool no_initial_shift,
72  crypto_algo algo,
73  const std::string & salt, //< not used is use_pkcs5 below is not set
74  infinint iteration_count, //< not used if use_pkcs5 is not set
75  hash_algo kdf_hash, //< not used if use_pkcs5 is not set
76  bool use_pkcs5); //< must be set to true when password is human defined to add a key derivation
77  crypto_sym(const crypto_sym & ref) = delete;
78  crypto_sym(crypto_sym && ref) = delete;
79  crypto_sym & operator = (const crypto_sym & ref) = delete;
80  crypto_sym & operator = (crypto_sym && ref) = delete;
81  ~crypto_sym() { detruit(); };
82 
84  static size_t max_key_len(crypto_algo algo);
85 
87  static size_t max_key_len_libdar(crypto_algo algo);
88 
90  static bool is_a_strong_password(crypto_algo algo, const secu_string & password);
91 
93  static std::string generate_salt(U_I size);
94 
95  protected:
96  virtual U_32 encrypted_block_size_for(U_32 clear_block_size) override;
97  virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) override;
98  virtual U_32 encrypt_data(const infinint & block_num,
99  const char *clear_buf, const U_32 clear_size, const U_32 clear_allocated,
100  char *crypt_buf, U_32 crypt_size) override;
101  virtual U_32 decrypt_data(const infinint & block_num,
102  const char *crypt_buf, const U_32 crypt_size,
103  char *clear_buf, U_32 clear_size) override;
104 
105  private:
106 #if CRYPTO_AVAILABLE
107  gcry_cipher_hd_t clef;
108  gcry_cipher_hd_t essiv_clef;
109 #endif
111  unsigned char *ivec;
112  U_I algo_id;
113 
114  void detruit();
115 
117 
119 
120 #if CRYPTO_AVAILABLE
121  static void dar_set_essiv(const secu_string & key,
122  gcry_cipher_hd_t & IVkey,
123  const archive_version & ver,
124  crypto_algo main_cipher);
126 
132  static void make_ivec(const infinint & ref,
133  unsigned char *ivec,
134  U_I size,
135  const gcry_cipher_hd_t & IVkey);
136 
138  static secu_string pkcs5_pass2key(const secu_string & password,
139  const std::string & salt,
140  U_I iteration_count,
141  U_I hash_gcrypt,
142  U_I output_length);
143 
145  static U_I get_algo_id(crypto_algo algo);
146 #endif
147 
148 #ifdef LIBDAR_NO_OPTIMIZATION
149  static void self_test(void);
150 #endif
151  };
152 
154 
155 } // end of namespace
156 
157 #endif
set of datastructures used to interact with a catalogue object
class archive_version manages the version of the archive format
symetrical strong encryption, interface to grypt library
Definition: crypto_sym.hpp:65
U_I algo_id
algo ID in libgcrypt
Definition: crypto_sym.hpp:112
unsigned char * ivec
algo_block_size allocated in secure memory to be used as Initial Vector
Definition: crypto_sym.hpp:111
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 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
static std::string generate_salt(U_I size)
generates a random salt of given size
virtual 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) override
this method encrypts the clear data given
virtual U_32 clear_block_allocated_size_for(U_32 clear_block_size) override
it may be necessary by the inherited class have few more bytes allocated after the clear data given f...
size_t algo_block_size
the block size of the algorithm (main key)
Definition: crypto_sym.hpp:110
virtual U_32 encrypted_block_size_for(U_32 clear_block_size) override
defines the size necessary to encrypt a given amount of clear data
virtual U_32 decrypt_data(const infinint &block_num, const char *crypt_buf, const U_32 crypt_size, char *clear_buf, U_32 clear_size) override
this method decyphers data
static size_t max_key_len(crypto_algo algo)
returns the max key length in octets for the given algorithm
this is the interface class from which all other data transfer classes inherit
the arbitrary large positive integer class
class secu_string
Definition: secu_string.hpp:58
this is a partial implementation of the generic_file interface to cypher/decypher data block by block...
infinint block_num
block number we next read or write
U_32 clear_block_size
max amount of data that will be encrypted at once (must stay less than buf_size)
archive_version reading_ver
archive format we currently read
the crypto algoritm definition
hash_algo
hashing algorithm available
Definition: archive_aux.hpp:63
crypto_algo
the different cypher available for encryption (strong or weak)
Definition: crypto.hpp:50
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
this file contains the definition of secu_string class, a std::string like class but allocated in sec...
defines a block structured file.