CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
encrypt.h
Go to the documentation of this file.
1 
13 #ifndef CVMFS_CRYPTO_ENCRYPT_H_
14 #define CVMFS_CRYPTO_ENCRYPT_H_
15 
16 #include <pthread.h>
17 
18 #include <map>
19 #include <string>
20 
21 #include "crypto/hash.h"
22 #include "gtest/gtest_prod.h"
23 #include "util/export.h"
24 #include "util/single_copy.h"
25 
26 namespace cipher {
27 
28 enum Algorithms {
30  kNone, // needs to be last
31 };
32 
33 
39  public:
40  static const unsigned kMaxSize = 64;
41 
42  static Key *CreateRandomly(const unsigned size);
43  static Key *CreateFromFile(const std::string &path);
44  static Key *CreateFromString(const std::string &key);
45  bool SaveToFile(const std::string &path);
46  ~Key();
47 
48  unsigned size() const { return size_; }
49  const unsigned char *data() const { return data_; }
50  std::string ToBase64() const;
51 
52  private:
53  Key() : data_(NULL), size_(0) { }
54  unsigned char *data_;
55  unsigned size_;
56 };
57 
58 
64  public:
65  virtual ~AbstractKeyDatabase() { }
66  virtual bool StoreNew(const Key *key, std::string *id) = 0;
67  virtual const Key *Find(const std::string &id) = 0;
68 };
69 
70 
72  public:
74  virtual ~MemoryKeyDatabase();
75  virtual bool StoreNew(const Key *key, std::string *id);
76  virtual const Key *Find(const std::string &id);
77 
78  private:
79  pthread_mutex_t *lock_;
80  std::map<std::string, const Key *> database_;
81 };
82 
83 
90  public:
91  static Cipher *Create(const Algorithms a);
92  virtual ~Cipher() { }
93 
94  bool Encrypt(const std::string &plaintext, const Key &key,
95  std::string *ciphertext);
96  static bool Decrypt(const std::string &ciphertext, const Key &key,
97  std::string *plaintext);
98 
99  virtual std::string name() const = 0;
100  virtual Algorithms algorithm() const = 0;
101  virtual unsigned key_size() const = 0;
102  virtual unsigned iv_size() const = 0;
103  virtual unsigned block_size() const = 0;
104 
105  protected:
106  Cipher() { }
107  virtual std::string DoEncrypt(const std::string &plaintext,
108  const Key &key) = 0;
109  virtual std::string DoDecrypt(const std::string &ciphertext,
110  const Key &key) = 0;
111 };
112 
113 
118  FRIEND_TEST(T_Encrypt, Aes_256_Cbc_Iv);
119 
120  public:
121  static const unsigned kKeySize = 256/8;
122  static const unsigned kIvSize = 128/8;
123  static const unsigned kBlockSize = 128/8;
124 
125  virtual ~CipherAes256Cbc() { }
126 
127  virtual std::string name() const { return "AES-256-CBC"; }
128  virtual Algorithms algorithm() const { return kAes256Cbc; }
129  virtual unsigned key_size() const { return kKeySize; }
130  virtual unsigned iv_size() const { return kIvSize; }
131  virtual unsigned block_size() const { return kBlockSize; }
132 
133  protected:
134  virtual std::string DoEncrypt(const std::string &plaintext, const Key &key);
135  virtual std::string DoDecrypt(const std::string &ciphertext, const Key &key);
136 
137  private:
138  shash::Md5 GenerateIv(const Key &key);
139 };
140 
141 
146  public:
147  virtual ~CipherNone() { }
148 
149  virtual std::string name() const { return "FOR TESTING ONLY"; }
150  virtual Algorithms algorithm() const { return kNone; }
151  virtual unsigned key_size() const { return 256/8; }
152  virtual unsigned iv_size() const { return 128/8; }
153  virtual unsigned block_size() const { return 128/8; }
154 
155  protected:
156  virtual std::string DoEncrypt(const std::string &plaintext, const Key &key);
157  virtual std::string DoDecrypt(const std::string &ciphertext, const Key &key);
158 };
159 
160 } // namespace cipher
161 
162 #endif // CVMFS_CRYPTO_ENCRYPT_H_
virtual unsigned block_size() const
Definition: encrypt.h:131
static Publisher * Create(const SettingsPublisher &settings)
virtual unsigned iv_size() const
Definition: encrypt.h:130
Algorithms
Definition: encrypt.h:28
unsigned size() const
Definition: encrypt.h:48
#define CVMFS_EXPORT
Definition: export.h:11
virtual ~CipherNone()
Definition: encrypt.h:147
std::map< std::string, const Key * > database_
Definition: encrypt.h:80
char algorithm
virtual unsigned key_size() const
Definition: encrypt.h:151
virtual std::string name() const
Definition: encrypt.h:149
virtual unsigned block_size() const
Definition: encrypt.h:153
unsigned size_
Definition: encrypt.h:55
virtual std::string name() const
Definition: encrypt.h:127
unsigned char * data_
Definition: encrypt.h:54
virtual ~Cipher()
Definition: encrypt.h:92
virtual ~CipherAes256Cbc()
Definition: encrypt.h:125
const unsigned char * data() const
Definition: encrypt.h:49
virtual Algorithms algorithm() const
Definition: encrypt.h:150
virtual unsigned key_size() const
Definition: encrypt.h:129
virtual ~AbstractKeyDatabase()
Definition: encrypt.h:65
pthread_mutex_t * lock_
Definition: encrypt.h:79
static void size_t size
Definition: smalloc.h:54
virtual Algorithms algorithm() const
Definition: encrypt.h:128
virtual unsigned iv_size() const
Definition: encrypt.h:152