47 #ifndef _MIRA_AESFILTER_H_ 48 #define _MIRA_AESFILTER_H_ 51 #include <boost/iostreams/concepts.hpp> 52 #include <boost/iostreams/filter/symmetric.hpp> 53 #include <boost/asio/basic_streambuf.hpp> 116 template<
typename Reflector>
120 "The AES key bit length.");
122 "The used block cipher mode.");
123 r.member(
"MessageDigestAlgorithm",
mdAlgo,
124 "The message digest algorithm for key generation.");
126 "Number of times the key material is hashed.");
127 r.member(
"Salt",
salt,
"The salt data.");
140 template<
typename Alloc>
141 struct AESFilterAllocatorTraits
143 #ifndef BOOST_NO_STD_ALLOCATOR 144 typedef typename Alloc::template rebind<char>::other
type;
146 typedef std::allocator<char>
type;
153 template<
typename Alloc,
155 struct AESFilterAllocator :
159 typedef typename Base::size_type size_type;
161 BOOST_STATIC_CONSTANT(
bool, custom =
162 (!boost::is_same<std::allocator<char>, Base>::value));
165 static void* allocate(
void*
self, uint32 items, uint32 size);
166 static void deallocate(
void*
self,
void* address);
175 typedef char char_type;
184 template<
typename Alloc>
185 void init(
const AESConfiguration& cfg,
const std::string& key,
186 bool encrypt, AESFilterAllocator<Alloc>& alloc)
188 initFilter(cfg, key, encrypt, &alloc);
192 bool encrypt(
const char*& ioSrcBegin,
199 bool decrypt(
const char*& ioSrcBegin,
209 void initFilter(
const AESConfiguration& cfg,
const std::string& key,
210 bool encrypt,
void* alloc);
217 AESConfiguration mCfg;
222 boost::asio::basic_streambuf<> mInputBuffer;
223 boost::asio::basic_streambuf<> mOutputBuffer;
226 size_t mWrkBufferSize;
232 template<
typename Alloc = std::allocator<
char> >
233 class AESEncryptionImpl :
234 public AESFilterBase,
235 public AESFilterAllocator<Alloc>
238 AESEncryptionImpl(
const AESConfiguration& cfg,
const std::string& key);
239 ~AESEncryptionImpl();
241 bool filter(
const char* &ioSrcBegin,
const char* iSrcEnd,
242 char* &ioDestBegin,
char* iDestEnd,
bool iFlush);
250 template<
typename Alloc = std::allocator<
char> >
251 class AESDecryptionImpl :
252 public AESFilterBase,
253 public AESFilterAllocator<Alloc>
256 AESDecryptionImpl(
const AESConfiguration& cfg,
const std::string& key);
257 ~AESDecryptionImpl();
259 bool filter(
const char* &ioSrcBegin,
const char* iSrcEnd,
260 char* &ioDestBegin,
char* iDestEnd,
bool iFlush);
272 template<
typename Alloc = std::allocator<
char> >
273 struct BasicAESEncryptionFilter :
274 boost::iostreams::symmetric_filter<Private::AESEncryptionImpl<Alloc>, Alloc>
277 typedef Private::AESEncryptionImpl<Alloc> impl_type;
278 typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
281 typedef typename base_type::char_type char_type;
282 typedef typename base_type::category category;
284 BasicAESEncryptionFilter(
const AESConfiguration& cfg,
285 const std::string& key,
286 int bufferSize = boost::iostreams::default_device_buffer_size);
288 BOOST_IOSTREAMS_PIPABLE(BasicAESEncryptionFilter, 1)
293 template<typename Alloc =
std::allocator<
char> >
294 struct BasicAESDecryptionFilter :
295 boost::iostreams::symmetric_filter<Private::AESDecryptionImpl<Alloc>, Alloc>
298 typedef Private::AESDecryptionImpl<Alloc> impl_type;
299 typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
302 typedef typename base_type::char_type char_type;
303 typedef typename base_type::category category;
305 BasicAESDecryptionFilter(
const AESConfiguration& cfg,
306 const std::string& key,
307 int bufferSize = boost::iostreams::default_device_buffer_size);
309 BOOST_IOSTREAMS_PIPABLE(BasicAESDecryptionFilter, 1)
376 template<
typename Alloc,
typename Base>
377 void* AESFilterAllocator<Alloc, Base>::allocate(
void*
self, uint32 items,
380 size_type len = items * size;
382 static_cast<allocator_type*
>(
self)->allocate
383 (len +
sizeof(size_type)
384 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) 388 *
reinterpret_cast<size_type*
>(ptr) = len;
389 return ptr +
sizeof(size_type);
392 template<
typename Alloc,
typename Base>
393 void AESFilterAllocator<Alloc, Base>::deallocate(
void*
self,
void* address)
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);
403 template<
typename Alloc>
405 const std::string& key)
407 init(cfg, key,
true,
static_cast<AESFilterAllocator<Alloc>&
>(*
this));
410 template<
typename Alloc>
411 AESEncryptionImpl<Alloc>::~AESEncryptionImpl()
416 template<
typename Alloc>
417 bool AESEncryptionImpl<Alloc>::filter(
const char* &ioSrcBegin,
420 char* iDestEnd,
bool iFlush)
422 return(encrypt(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
425 template<
typename Alloc>
426 void AESEncryptionImpl<Alloc>::close()
434 template<
typename Alloc>
436 const std::string& key)
438 init(cfg, key,
false,
static_cast<AESFilterAllocator<Alloc>&
>(*
this));
441 template<
typename Alloc>
442 AESDecryptionImpl<Alloc>::~AESDecryptionImpl()
447 template<
typename Alloc>
448 bool AESDecryptionImpl<Alloc>::filter(
const char* &ioSrcBegin,
451 char* iDestEnd,
bool iFlush)
453 return(decrypt(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
456 template<
typename Alloc>
457 void AESDecryptionImpl<Alloc>::close()
469 template<
typename Alloc>
470 BasicAESEncryptionFilter<Alloc>::BasicAESEncryptionFilter(
471 const AESConfiguration& cfg,
const std::string& key,
int bufferSize) :
472 base_type(bufferSize, cfg, key)
479 template<
typename Alloc>
480 BasicAESDecryptionFilter<Alloc>::BasicAESDecryptionFilter(
481 const AESConfiguration& cfg,
const std::string& key,
int bufferSize) :
482 base_type(bufferSize, cfg, key)
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
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
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