MIRA
AESFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_AESFILTER_H_
48 #define _MIRA_AESFILTER_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/iostreams/concepts.hpp>
52 #include <boost/iostreams/filter/symmetric.hpp>
53 #include <boost/asio/basic_streambuf.hpp>
54 #endif
55 
56 #include <platform/Types.h>
57 #include <error/Exceptions.h>
58 
59 namespace mira {
60 
62 
65  AES_128 = 1,
68 };
69 
72 {
73  AES_ECB = 1,
79 };
80 
83 {
88 };
89 
92 {
95 
98 
101 
104  uint16 nrRounds;
105 
107  std::string salt;
108 
113  nrRounds(5)
114  {}
115 
116  template<typename Reflector>
117  void reflect(Reflector& r)
118  {
119  r.member("BitLength", bitLength,
120  "The AES key bit length.");
121  r.member("BlockCipherMode", blockCipherMode,
122  "The used block cipher mode.");
123  r.member("MessageDigestAlgorithm", mdAlgo,
124  "The message digest algorithm for key generation.");
125  r.member("NrRounds", nrRounds,
126  "Number of times the key material is hashed.");
127  r.member("Salt", salt, "The salt data.");
128  }
129 };
130 
132 
134 
135 namespace Private {
136 
138 // An allocator trait for the boost iostream filter
139 
140 template<typename Alloc>
141 struct AESFilterAllocatorTraits
142 {
143 #ifndef BOOST_NO_STD_ALLOCATOR
144  typedef typename Alloc::template rebind<char>::other type;
145 #else
146  typedef std::allocator<char> type;
147 #endif
148 };
149 
151 // An allocator for the boost iostreams filter
152 
153 template< typename Alloc,
154  typename Base = BOOST_DEDUCED_TYPENAME AESFilterAllocatorTraits<Alloc>::type >
155 struct AESFilterAllocator :
156  private Base
157 {
158 private:
159  typedef typename Base::size_type size_type;
160 public:
161  BOOST_STATIC_CONSTANT(bool, custom =
162  (!boost::is_same<std::allocator<char>, Base>::value));
163  typedef typename AESFilterAllocatorTraits<Alloc>::type allocator_type;
164 
165  static void* allocate(void* self, uint32 items, uint32 size);
166  static void deallocate(void* self, void* address);
167 };
168 
170 // An internal class of the AES filter class
171 
172 class MIRA_BASE_EXPORT AESFilterBase
173 {
174 public:
175  typedef char char_type;
176 
177 protected:
179  AESFilterBase();
180 
182  ~AESFilterBase();
183 
184  template<typename Alloc>
185  void init(const AESConfiguration& cfg, const std::string& key,
186  bool encrypt, AESFilterAllocator<Alloc>& alloc)
187  {
188  initFilter(cfg, key, encrypt, &alloc);
189  }
190 
192  bool encrypt(const char*& ioSrcBegin,
193  const char* oSrcEnd,
194  char*& ioDestBegin,
195  char* iDestEnd,
196  bool iFlush);
197 
199  bool decrypt(const char*& ioSrcBegin,
200  const char* iSrcEnd,
201  char*& ioDestBegin,
202  char* iDestEnd,
203  bool iFlush);
204 
206  void reset();
207 
208 private:
209  void initFilter(const AESConfiguration& cfg, const std::string& key,
210  bool encrypt, void* alloc);
211 
212 private:
213  struct Context;
214  Context* mCtx;
215 
216 private:
217  AESConfiguration mCfg;
218 
219  bool mEncryptMode;
220  bool mFinalCalled;
221 
222  boost::asio::basic_streambuf<> mInputBuffer;
223  boost::asio::basic_streambuf<> mOutputBuffer;
224 
225  uint8* mWrkBuffer;
226  size_t mWrkBufferSize;
227 };
228 
230 // Template name: AESEncryptionImpl
231 
232 template<typename Alloc = std::allocator<char> >
233 class AESEncryptionImpl :
234  public AESFilterBase,
235  public AESFilterAllocator<Alloc>
236 {
237 public:
238  AESEncryptionImpl(const AESConfiguration& cfg, const std::string& key);
239  ~AESEncryptionImpl();
240 
241  bool filter(const char* &ioSrcBegin, const char* iSrcEnd,
242  char* &ioDestBegin, char* iDestEnd, bool iFlush);
243 
244  void close();
245 };
246 
248 // Template name: AESDecryptionImpl
249 
250 template<typename Alloc = std::allocator<char> >
251 class AESDecryptionImpl :
252  public AESFilterBase,
253  public AESFilterAllocator<Alloc>
254 {
255 public:
256  AESDecryptionImpl(const AESConfiguration& cfg, const std::string& key);
257  ~AESDecryptionImpl();
258 
259  bool filter(const char* &ioSrcBegin, const char* iSrcEnd,
260  char* &ioDestBegin, char* iDestEnd, bool iFlush);
261 
262  void close();
263 };
264 
266 
267 } // end of namespace Private
268 
270 // BasicAESEncryptionFilter
271 
272 template<typename Alloc = std::allocator<char> >
273 struct BasicAESEncryptionFilter :
274  boost::iostreams::symmetric_filter<Private::AESEncryptionImpl<Alloc>, Alloc>
275 {
276 private:
277  typedef Private::AESEncryptionImpl<Alloc> impl_type;
278  typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
279 
280 public:
281  typedef typename base_type::char_type char_type;
282  typedef typename base_type::category category;
283 
284  BasicAESEncryptionFilter(const AESConfiguration& cfg,
285  const std::string& key,
286  int bufferSize = boost::iostreams::default_device_buffer_size);
287 };
288 BOOST_IOSTREAMS_PIPABLE(BasicAESEncryptionFilter, 1)
289 
290 // BasicAESDecryptionFilter
292 
293 template<typename Alloc = std::allocator<char> >
294 struct BasicAESDecryptionFilter :
295  boost::iostreams::symmetric_filter<Private::AESDecryptionImpl<Alloc>, Alloc>
296 {
297 private:
298  typedef Private::AESDecryptionImpl<Alloc> impl_type;
299  typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
300 
301 public:
302  typedef typename base_type::char_type char_type;
303  typedef typename base_type::category category;
304 
305  BasicAESDecryptionFilter(const AESConfiguration& cfg,
306  const std::string& key,
307  int bufferSize = boost::iostreams::default_device_buffer_size);
308 };
309 BOOST_IOSTREAMS_PIPABLE(BasicAESDecryptionFilter, 1)
310 
311 
314 
316 
338 typedef BasicAESEncryptionFilter<> AESEncryptionFilter;
339 
363 typedef BasicAESDecryptionFilter<> AESDecryptionFilter;
364 
366 // Template implementation
368 
370 
371 namespace Private {
372 
374 // Implementation of template AES_allocator
375 
376 template<typename Alloc, typename Base>
377 void* AESFilterAllocator<Alloc, Base>::allocate(void* self, uint32 items,
378  uint32 size)
379 {
380  size_type len = items * size;
381  char* ptr =
382  static_cast<allocator_type*>(self)->allocate
383  (len + sizeof(size_type)
384  #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
385  , (char*)0
386  #endif
387  );
388  *reinterpret_cast<size_type*>(ptr) = len;
389  return ptr + sizeof(size_type);
390 }
391 
392 template<typename Alloc, typename Base>
393 void AESFilterAllocator<Alloc, Base>::deallocate(void* self, void* address)
394 {
395  char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
396  size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
397  static_cast<allocator_type*>(self)->deallocate(ptr, len);
398 }
399 
401 // Implementation of AESEncryptionImpl
402 
403 template<typename Alloc>
404 AESEncryptionImpl<Alloc>::AESEncryptionImpl(const AESConfiguration& cfg,
405  const std::string& key)
406 {
407  init(cfg, key, true, static_cast<AESFilterAllocator<Alloc>&>(*this));
408 }
409 
410 template<typename Alloc>
411 AESEncryptionImpl<Alloc>::~AESEncryptionImpl()
412 {
413  reset();
414 }
415 
416 template<typename Alloc>
417 bool AESEncryptionImpl<Alloc>::filter(const char* &ioSrcBegin,
418  const char* iSrcEnd,
419  char* &ioDestBegin,
420  char* iDestEnd, bool iFlush)
421 {
422  return(encrypt(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
423 }
424 
425 template<typename Alloc>
426 void AESEncryptionImpl<Alloc>::close()
427 {
428  reset();
429 }
430 
432 // Implementation of AESDecryptionImpl
433 
434 template<typename Alloc>
435 AESDecryptionImpl<Alloc>::AESDecryptionImpl(const AESConfiguration& cfg,
436  const std::string& key)
437 {
438  init(cfg, key, false, static_cast<AESFilterAllocator<Alloc>&>(*this));
439 }
440 
441 template<typename Alloc>
442 AESDecryptionImpl<Alloc>::~AESDecryptionImpl()
443 {
444  reset();
445 }
446 
447 template<typename Alloc>
448 bool AESDecryptionImpl<Alloc>::filter(const char* &ioSrcBegin,
449  const char* iSrcEnd,
450  char* &ioDestBegin,
451  char* iDestEnd, bool iFlush)
452 {
453  return(decrypt(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
454 }
455 
456 template<typename Alloc>
457 void AESDecryptionImpl<Alloc>::close()
458 {
459  reset();
460 }
461 
463 
464 } // end of namespace Private
465 
467 // Implementation of BasicAESEncryptionFilter
468 
469 template<typename Alloc>
470 BasicAESEncryptionFilter<Alloc>::BasicAESEncryptionFilter(
471  const AESConfiguration& cfg, const std::string& key, int bufferSize) :
472  base_type(bufferSize, cfg, key)
473 {
474 }
475 
477 // Implementation of BasicAESDecryptionFilter
478 
479 template<typename Alloc>
480 BasicAESDecryptionFilter<Alloc>::BasicAESDecryptionFilter(
481  const AESConfiguration& cfg, const std::string& key, int bufferSize) :
482  base_type(bufferSize, cfg, key)
483 {
484 }
485 
487 
489 
491 
492 } // namespaces
493 
494 #endif
BasicAESDecryptionFilter AESDecryptionFilter
A AES private decryption filter for boost::iostreams.
Definition: AESFilter.h:363
Typedefs for OS independent basic data types.
Definition: SyncTimedRead.h:62
AESBitLength bitLength
The AES key bit length.
Definition: AESFilter.h:94
Cipher-block chaining.
Definition: AESFilter.h:74
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
uint16 nrRounds
Number of times the key material is hashed.
Definition: AESFilter.h:104
Cipher feedback (128 feedback bits)
Definition: AESFilter.h:77
STL namespace.
void reflect(Reflector &r)
Definition: AESFilter.h:117
Use SHA512 as message digest.
Definition: AESFilter.h:87
Electronic codebook.
Definition: AESFilter.h:73
Definition: AESFilter.h:67
Commonly used exception classes.
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Output feedback.
Definition: AESFilter.h:78
Cipher feedback (8 feedback bits)
Definition: AESFilter.h:76
AESConfiguration()
Definition: AESFilter.h:109
Use SHA1 as message digest.
Definition: AESFilter.h:85
AESMessageDigestAlgo
The supported message digest algorithm for key generation.
Definition: AESFilter.h:82
AESBitLength
The supported AES bit lengths.
Definition: AESFilter.h:64
The AES configuration for encryption and decryption.
Definition: AESFilter.h:91
std::string salt
The salt data. Must have length 8 or zero.
Definition: AESFilter.h:107
Use SHA256 as message digest.
Definition: AESFilter.h:86
Definition: AESFilter.h:66
Cipher feedback (1 feedback bit)
Definition: AESFilter.h:75
Use MD5 as message digest.
Definition: AESFilter.h:84
AESBlockCipherMode blockCipherMode
The used block cipher mode.
Definition: AESFilter.h:97
AESBlockCipherMode
The supported block cipher modes for the AES encryption/decryption.
Definition: AESFilter.h:71
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
BasicAESEncryptionFilter AESEncryptionFilter
A AES public encryption filter for boost::iostreams.
Definition: AESFilter.h:338
Definition: AESFilter.h:65
AESMessageDigestAlgo mdAlgo
The message digest algorithm for key generation.
Definition: AESFilter.h:100