Botan
1.11.15
|
00001 /* 00002 * Helper include for mode implementations 00003 * (C) 2015 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_MODE_UTILS_H__ 00009 #define BOTAN_MODE_UTILS_H__ 00010 00011 #include <botan/cipher_mode.h> 00012 #include <botan/internal/algo_registry.h> 00013 #include <botan/block_cipher.h> 00014 #include <botan/loadstor.h> 00015 #include <botan/internal/xor_buf.h> 00016 #include <botan/internal/rounding.h> 00017 #include <botan/internal/bit_ops.h> 00018 #include <algorithm> 00019 00020 namespace Botan { 00021 00022 template<typename T> 00023 T* make_block_cipher_mode(const Transform::Spec& spec) 00024 { 00025 if(BlockCipher* bc = Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0))) 00026 return new T(bc->clone()); 00027 return nullptr; 00028 } 00029 00030 template<typename T, size_t LEN1> 00031 T* make_block_cipher_mode_len(const Transform::Spec& spec) 00032 { 00033 if(BlockCipher* bc = Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0))) 00034 { 00035 const size_t len1 = spec.arg_as_integer(1, LEN1); 00036 return new T(bc->clone(), len1); 00037 } 00038 00039 return nullptr; 00040 } 00041 00042 template<typename T, size_t LEN1, size_t LEN2> 00043 T* make_block_cipher_mode_len2(const Transform::Spec& spec) 00044 { 00045 if(BlockCipher* bc = Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0))) 00046 { 00047 const size_t len1 = spec.arg_as_integer(1, LEN1); 00048 const size_t len2 = spec.arg_as_integer(2, LEN2); 00049 return new T(bc->clone(), len1, len2); 00050 } 00051 00052 return nullptr; 00053 } 00054 00055 #define BOTAN_REGISTER_BLOCK_CIPHER_MODE(E, D) \ 00056 BOTAN_REGISTER_NAMED_T(Transform, #E, E, make_block_cipher_mode<E>); \ 00057 BOTAN_REGISTER_NAMED_T(Transform, #D, D, make_block_cipher_mode<D>); 00058 00059 #define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(E, D, LEN) \ 00060 BOTAN_REGISTER_NAMED_T(Transform, #E, E, (make_block_cipher_mode_len<E, LEN>)); \ 00061 BOTAN_REGISTER_NAMED_T(Transform, #D, D, (make_block_cipher_mode_len<D, LEN>)); 00062 00063 #define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN2(E, D, LEN1, LEN2) \ 00064 BOTAN_REGISTER_NAMED_T(Transform, #E, E, (make_block_cipher_mode_len2<E, LEN1, LEN2>)); \ 00065 BOTAN_REGISTER_NAMED_T(Transform, #D, D, (make_block_cipher_mode_len2<D, LEN1, LEN2>)); 00066 00067 } 00068 00069 #endif