libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Gcrypt.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef BASELIB_SECURITY_GCRYPT_H_
32 #define BASELIB_SECURITY_GCRYPT_H_
33 
34 #include "../Exception.h"
35 #include <gcrypt.h>
36 
37 #include <memory>
38 #include <vector>
39 #include <cstdint>
40 
41 namespace BaseLib
42 {
43 namespace Security
44 {
45 
51 class GcryptException : public Exception
52 {
53 public:
54  explicit GcryptException(const std::string& message) : Exception(message) {}
55 };
56 
57 class Gcrypt
58 {
59 private:
63  bool _keySet = false;
64 
68  int _algorithm = 0;
69 
73  int _mode = 0;
74 
78  unsigned int _flags = 0;
79 
83  gcry_cipher_hd_t _handle = nullptr;
84 
85 public:
94  Gcrypt(int algorithm, int mode, unsigned int flags);
95 
99  virtual ~Gcrypt();
100 
104  void reset();
105 
111  gcry_cipher_hd_t getHandle() { return _handle; };
112 
119  static std::string getError(int32_t errorCode);
120 
124  size_t getBlockSize();
125 
131  template<typename Data> void setIv(const Data& iv);
132 
138  void setIv(const void* iv, const size_t length);
139 
145  template<typename Data> void setCounter(const Data& counter);
146 
152  void setCounter(const void* counter, const size_t length);
153 
159  template<typename Data> void setKey(const Data& key);
160 
166  void setKey(const void* key, const size_t length);
167 
173  void encrypt(void* out, const size_t outLength, const void* in, const size_t inLength);
174 
180  template<typename DataOut, typename DataIn> void encrypt(DataOut& out, const DataIn& in);
181 
187  void decrypt(void* out, const size_t outLength, const void* in, const size_t inLength);
188 
194  template<typename DataOut, typename DataIn> void decrypt(DataOut& out, const DataIn& in);
195 
199  bool authenticate(const void* in, const size_t inLength);
200 
204  template<typename DataIn> bool authenticate(const DataIn& in);
205 };
206 
207 typedef std::shared_ptr<Gcrypt> PGcrypt;
208 
209 }
210 }
211 #endif
Definition: BaseLib.cpp:34
std::shared_ptr< Gcrypt > PGcrypt
Definition: Gcrypt.h:207
Exception class for Gcrypt.
Definition: Gcrypt.h:51
gcry_cipher_hd_t getHandle()
Returns the underlying gcry_cipher_hd_t.
Definition: Gcrypt.h:111
GcryptException(const std::string &message)
Definition: Gcrypt.h:54
Base class for all exceptions defined in Homegear.
Definition: Exception.h:41
Definition: Gcrypt.h:57