diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/def_alg.cpp | 5 | ||||
-rw-r--r-- | src/hash/par_hash/modinfo.txt | 10 | ||||
-rw-r--r-- | src/hash/par_hash/par_hash.cpp (renamed from src/par_hash.cpp) | 0 | ||||
-rw-r--r-- | src/hash/par_hash/par_hash.h | 33 | ||||
-rw-r--r-- | src/secalloc/allocators/allocate.h | 35 | ||||
-rw-r--r-- | src/secalloc/allocators/defalloc.cpp (renamed from src/defalloc.cpp) | 0 | ||||
-rw-r--r-- | src/secalloc/allocators/defalloc.h | 39 | ||||
-rw-r--r-- | src/secalloc/allocators/mem_pool.cpp (renamed from src/mem_pool.cpp) | 0 | ||||
-rw-r--r-- | src/secalloc/allocators/mem_pool.h | 74 | ||||
-rw-r--r-- | src/secalloc/allocators/modinfo.txt | 14 | ||||
-rw-r--r-- | src/secalloc/allocators/secmem.h | 217 |
11 files changed, 426 insertions, 1 deletions
diff --git a/src/def_alg.cpp b/src/def_alg.cpp index 7bcc84378..80d3a771f 100644 --- a/src/def_alg.cpp +++ b/src/def_alg.cpp @@ -7,7 +7,6 @@ #include <botan/libstate.h> #include <botan/parsing.h> -#include <botan/par_hash.h> #include <botan/mode_pad.h> #if defined(BOTAN_HAS_AES) @@ -176,6 +175,10 @@ #include <botan/whrlpool.h> #endif +#if defined(BOTAN_HAS_PARALLEL_HASH) + #include <botan/par_hash.h> +#endif + #if defined(BOTAN_HAS_CBC_MAC) #include <botan/cbc_mac.h> #endif diff --git a/src/hash/par_hash/modinfo.txt b/src/hash/par_hash/modinfo.txt new file mode 100644 index 000000000..45716aac8 --- /dev/null +++ b/src/hash/par_hash/modinfo.txt @@ -0,0 +1,10 @@ +realname "Parallel Hash" + +define PARALLEL_HASH + +load_on auto + +<add> +par_hash.cpp +par_hash.h +</add> diff --git a/src/par_hash.cpp b/src/hash/par_hash/par_hash.cpp index 12786523f..12786523f 100644 --- a/src/par_hash.cpp +++ b/src/hash/par_hash/par_hash.cpp diff --git a/src/hash/par_hash/par_hash.h b/src/hash/par_hash/par_hash.h new file mode 100644 index 000000000..844a6fb50 --- /dev/null +++ b/src/hash/par_hash/par_hash.h @@ -0,0 +1,33 @@ +/************************************************* +* Parallel Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_PAR_HASH_H__ +#define BOTAN_PAR_HASH_H__ + +#include <botan/base.h> +#include <vector> + +namespace Botan { + +/************************************************* +* Parallel * +*************************************************/ +class BOTAN_DLL Parallel : public HashFunction + { + public: + void clear() throw(); + std::string name() const; + HashFunction* clone() const; + Parallel(const std::vector<std::string>&); + ~Parallel(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + std::vector<HashFunction*> hashes; + }; + +} + +#endif diff --git a/src/secalloc/allocators/allocate.h b/src/secalloc/allocators/allocate.h new file mode 100644 index 000000000..efbb77291 --- /dev/null +++ b/src/secalloc/allocators/allocate.h @@ -0,0 +1,35 @@ +/************************************************* +* Allocator Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_ALLOCATOR_H__ +#define BOTAN_ALLOCATOR_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/************************************************* +* Allocator Interface * +*************************************************/ +class BOTAN_DLL Allocator + { + public: + static Allocator* get(bool); + + virtual void* allocate(u32bit) = 0; + virtual void deallocate(void*, u32bit) = 0; + + virtual std::string type() const = 0; + + virtual void init() {} + virtual void destroy() {} + + virtual ~Allocator() {} + }; + +} + +#endif diff --git a/src/defalloc.cpp b/src/secalloc/allocators/defalloc.cpp index 5fb8e1447..5fb8e1447 100644 --- a/src/defalloc.cpp +++ b/src/secalloc/allocators/defalloc.cpp diff --git a/src/secalloc/allocators/defalloc.h b/src/secalloc/allocators/defalloc.h new file mode 100644 index 000000000..f162f5c71 --- /dev/null +++ b/src/secalloc/allocators/defalloc.h @@ -0,0 +1,39 @@ +/************************************************* +* Basic Allocators Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_BASIC_ALLOC_H__ +#define BOTAN_BASIC_ALLOC_H__ + +#include <botan/mem_pool.h> + +namespace Botan { + +/************************************************* +* Malloc Allocator * +*************************************************/ +class BOTAN_DLL Malloc_Allocator : public Allocator + { + public: + void* allocate(u32bit); + void deallocate(void*, u32bit); + + std::string type() const { return "malloc"; } + }; + +/************************************************* +* Locking Allocator * +*************************************************/ +class BOTAN_DLL Locking_Allocator : public Pooling_Allocator + { + public: + std::string type() const { return "locking"; } + private: + void* alloc_block(u32bit); + void dealloc_block(void*, u32bit); + }; + +} + +#endif diff --git a/src/mem_pool.cpp b/src/secalloc/allocators/mem_pool.cpp index 74d09d5df..74d09d5df 100644 --- a/src/mem_pool.cpp +++ b/src/secalloc/allocators/mem_pool.cpp diff --git a/src/secalloc/allocators/mem_pool.h b/src/secalloc/allocators/mem_pool.h new file mode 100644 index 000000000..3d28034e7 --- /dev/null +++ b/src/secalloc/allocators/mem_pool.h @@ -0,0 +1,74 @@ +/************************************************* +* Pooling Allocator Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_POOLING_ALLOCATOR_H__ +#define BOTAN_POOLING_ALLOCATOR_H__ + +#include <botan/allocate.h> +#include <botan/exceptn.h> +#include <botan/mutex.h> +#include <utility> +#include <vector> + +namespace Botan { + +/************************************************* +* Pooling Allocator * +*************************************************/ +class BOTAN_DLL Pooling_Allocator : public Allocator + { + public: + void* allocate(u32bit); + void deallocate(void*, u32bit); + + void destroy(); + + Pooling_Allocator(); + ~Pooling_Allocator(); + private: + void get_more_core(u32bit); + byte* allocate_blocks(u32bit); + + virtual void* alloc_block(u32bit) = 0; + virtual void dealloc_block(void*, u32bit) = 0; + + class BOTAN_DLL Memory_Block + { + public: + Memory_Block(void*); + + static u32bit bitmap_size() { return BITMAP_SIZE; } + static u32bit block_size() { return BLOCK_SIZE; } + + bool contains(void*, u32bit) const throw(); + byte* alloc(u32bit) throw(); + void free(void*, u32bit) throw(); + + bool operator<(const Memory_Block& other) const + { + if(buffer < other.buffer && other.buffer < buffer_end) + return false; + return (buffer < other.buffer); + } + private: + typedef u64bit bitmap_type; + static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type); + static const u32bit BLOCK_SIZE = 64; + + bitmap_type bitmap; + byte* buffer, *buffer_end; + }; + + static const u32bit PREF_SIZE = BOTAN_MEM_POOL_CHUNK_SIZE; + + std::vector<Memory_Block> blocks; + std::vector<Memory_Block>::iterator last_used; + std::vector<std::pair<void*, u32bit> > allocated; + Mutex* mutex; + }; + +} + +#endif diff --git a/src/secalloc/allocators/modinfo.txt b/src/secalloc/allocators/modinfo.txt new file mode 100644 index 000000000..e2d8d2a16 --- /dev/null +++ b/src/secalloc/allocators/modinfo.txt @@ -0,0 +1,14 @@ +realname "Secure Memory Allocation" + +load_on auto + +define SECMEM + +<add> +defalloc.cpp +mem_pool.cpp +allocate.h +defalloc.h +mem_pool.h +secmem.h +</add> diff --git a/src/secalloc/allocators/secmem.h b/src/secalloc/allocators/secmem.h new file mode 100644 index 000000000..37adf7f42 --- /dev/null +++ b/src/secalloc/allocators/secmem.h @@ -0,0 +1,217 @@ +/************************************************* +* Secure Memory Buffers Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_SECURE_MEMORY_BUFFERS_H__ +#define BOTAN_SECURE_MEMORY_BUFFERS_H__ + +#include <botan/allocate.h> +#include <botan/mem_ops.h> + +namespace Botan { + +/************************************************* +* Variable Length Memory Buffer * +*************************************************/ +template<typename T> +class MemoryRegion + { + public: + u32bit size() const { return used; } + u32bit is_empty() const { return (used == 0); } + u32bit has_items() const { return (used != 0); } + + operator T* () { return buf; } + operator const T* () const { return buf; } + + T* begin() { return buf; } + const T* begin() const { return buf; } + + T* end() { return (buf + size()); } + const T* end() const { return (buf + size()); } + + bool operator==(const MemoryRegion<T>& other) const + { + return (size() == other.size() && + same_mem(buf, other.buf, size())); + } + + bool operator<(const MemoryRegion<T>&) const; + + bool operator!=(const MemoryRegion<T>& in) const + { return (!(*this == in)); } + MemoryRegion<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + void copy(const T in[], u32bit n) + { copy(0, in, n); } + void copy(u32bit off, const T in[], u32bit n) + { copy_mem(buf + off, in, (n > size() - off) ? (size() - off) : n); } + + void set(const T in[], u32bit n) { create(n); copy(in, n); } + void set(const MemoryRegion<T>& in) { set(in.begin(), in.size()); } + + void append(const T data[], u32bit n) + { grow_to(size()+n); copy(size() - n, data, n); } + void append(T x) { append(&x, 1); } + void append(const MemoryRegion<T>& x) { append(x.begin(), x.size()); } + + void clear() { clear_mem(buf, allocated); } + void destroy() { create(0); } + + void create(u32bit); + void grow_to(u32bit); + void swap(MemoryRegion<T>&); + + ~MemoryRegion() { deallocate(buf, allocated); } + protected: + MemoryRegion() { buf = 0; alloc = 0; used = allocated = 0; } + MemoryRegion(const MemoryRegion<T>& other) + { + buf = 0; + used = allocated = 0; + alloc = other.alloc; + set(other.buf, other.used); + } + + void init(bool locking, u32bit length = 0) + { alloc = Allocator::get(locking); create(length); } + private: + T* allocate(u32bit n) + { + return static_cast<T*>(alloc->allocate(sizeof(T)*n)); + } + + void deallocate(T* p, u32bit n) + { alloc->deallocate(p, sizeof(T)*n); } + + T* buf; + u32bit used; + u32bit allocated; + Allocator* alloc; + }; + +/************************************************* +* Create a new buffer * +*************************************************/ +template<typename T> +void MemoryRegion<T>::create(u32bit n) + { + if(n <= allocated) { clear(); used = n; return; } + deallocate(buf, allocated); + buf = allocate(n); + allocated = used = n; + } + +/************************************************* +* Increase the size of the buffer * +*************************************************/ +template<typename T> +void MemoryRegion<T>::grow_to(u32bit n) + { + if(n > used && n <= allocated) + { + clear_mem(buf + used, n - used); + used = n; + return; + } + else if(n > allocated) + { + T* new_buf = allocate(n); + copy_mem(new_buf, buf, used); + deallocate(buf, allocated); + buf = new_buf; + allocated = used = n; + } + } + +/************************************************* +* Compare this buffer with another one * +*************************************************/ +template<typename T> +bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const + { + if(size() < in.size()) return true; + if(size() > in.size()) return false; + + for(u32bit j = 0; j != size(); j++) + { + if(buf[j] < in[j]) return true; + if(buf[j] > in[j]) return false; + } + + return false; + } + +/************************************************* +* Swap this buffer with another one * +*************************************************/ +template<typename T> +void MemoryRegion<T>::swap(MemoryRegion<T>& x) + { + std::swap(buf, x.buf); + std::swap(used, x.used); + std::swap(allocated, x.allocated); + std::swap(alloc, x.alloc); + } + +/************************************************* +* Unlocked Variable Length Buffer * +*************************************************/ +template<typename T> +class MemoryVector : public MemoryRegion<T> + { + public: + MemoryVector<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); } + MemoryVector(const T in[], u32bit n) + { MemoryRegion<T>::init(false); set(in, n); } + MemoryVector(const MemoryRegion<T>& in) + { MemoryRegion<T>::init(false); set(in); } + MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2) + { MemoryRegion<T>::init(false); set(in1); append(in2); } + }; + +/************************************************* +* Locked Variable Length Buffer * +*************************************************/ +template<typename T> +class SecureVector : public MemoryRegion<T> + { + public: + SecureVector<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); } + SecureVector(const T in[], u32bit n) + { MemoryRegion<T>::init(true); set(in, n); } + SecureVector(const MemoryRegion<T>& in) + { MemoryRegion<T>::init(true); set(in); } + SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2) + { MemoryRegion<T>::init(true); set(in1); append(in2); } + }; + +/************************************************* +* Locked Fixed Length Buffer * +*************************************************/ +template<typename T, u32bit L> +class SecureBuffer : public MemoryRegion<T> + { + public: + SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in) + { if(this != &in) set(in); return (*this); } + + SecureBuffer() { MemoryRegion<T>::init(true, L); } + SecureBuffer(const T in[], u32bit n) + { MemoryRegion<T>::init(true, L); copy(in, n); } + private: + SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + }; + +} + +#endif |