monotone

monotone Mtn Source Tree

Root/botan/secmem.h

1/*************************************************
2* Secure Memory Buffers Header File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_SECURE_MEMORY_BUFFERS_H__
7#define BOTAN_SECURE_MEMORY_BUFFERS_H__
8
9#include <botan/allocate.h>
10#include <botan/mem_ops.h>
11#include <algorithm>
12
13namespace Botan {
14
15/*************************************************
16* Variable Length Memory Buffer *
17*************************************************/
18template<typename T>
19class MemoryRegion
20 {
21 public:
22 u32bit size() const { return used; }
23 u32bit bits() const { return 8*used; }
24 u32bit is_empty() const { return (used == 0); }
25 u32bit has_items() const { return (used != 0); }
26
27 operator T* () { return buf; }
28 operator const T* () const { return buf; }
29
30 T* begin() { return buf; }
31 const T* begin() const { return buf; }
32
33 T* end() { return (buf + size()); }
34 const T* end() const { return (buf + size()); }
35
36 bool operator==(const MemoryRegion<T>& in) const
37 {
38 if(size() == in.size() && std::equal(begin(), end(), in.begin()))
39 return true;
40 return false;
41 }
42
43 bool operator<(const MemoryRegion<T>&) const;
44
45 bool operator!=(const MemoryRegion<T>& in) const
46 { return (!(*this == in)); }
47 MemoryRegion<T>& operator=(const MemoryRegion<T>& in)
48 { if(this != &in) set(in); return (*this); }
49
50 void copy(const T in[], u32bit n)
51 { copy_mem(buf, in, std::min(size(), n)); }
52 void copy(u32bit off, const T in[], u32bit n)
53 { copy_mem(buf + off, in, std::min(size() - off, n)); }
54
55 void set(const T in[], u32bit n) { create(n); copy(in, n); }
56 void set(const MemoryRegion<T>& in) { set(in.begin(), in.size()); }
57
58 void append(const T data[], u32bit n)
59 { grow_by(n); copy(size() - n, data, n); }
60 void append(T x) { append(&x, 1); }
61 void append(const MemoryRegion<T>& x) { append(x.begin(), x.size()); }
62
63 void clear() { clear_mem(buf, allocated); }
64 void destroy() { create(0); }
65
66 void create(u32bit);
67 void grow_to(u32bit) const;
68 void grow_by(u32bit n) const { grow_to(n + size()); }
69 void swap(MemoryRegion<T>&);
70
71 ~MemoryRegion() { deallocate(buf, allocated); }
72 protected:
73 MemoryRegion() { buf = 0; alloc = 0; used = allocated = 0; }
74 MemoryRegion(const MemoryRegion<T>& copy)
75 {
76 buf = 0;
77 used = allocated = 0;
78 alloc = copy.alloc;
79 set(copy.buf, copy.used);
80 }
81
82 void init(bool lock, u32bit size = 0)
83 { alloc = get_allocator(lock ? "" : "malloc"); create(size); }
84 private:
85 T* allocate(u32bit n) const { return (T*)alloc->allocate(sizeof(T)*n); }
86 void deallocate(T* p, u32bit n) const
87 { alloc->deallocate(p, sizeof(T)*n); }
88
89 mutable T* buf;
90 mutable u32bit used;
91 mutable u32bit allocated;
92 const Allocator* alloc;
93 };
94
95/*************************************************
96* Create a new buffer *
97*************************************************/
98template<typename T>
99void MemoryRegion<T>::create(u32bit n)
100 {
101 if(n <= allocated) { clear(); used = n; return; }
102 deallocate(buf, allocated);
103 buf = allocate(n);
104 allocated = used = n;
105 }
106
107/*************************************************
108* Increase the size of the buffer *
109*************************************************/
110template<typename T>
111void MemoryRegion<T>::grow_to(u32bit n) const
112 {
113 const u32bit VECTOR_OVER_ALLOCATE = BOTAN_VECTOR_OVER_ALLOCATE;
114
115 if(n <= used) return;
116 if(n <= allocated)
117 {
118 clear_mem(buf + used, n - used);
119 used = n;
120 return;
121 }
122 T* new_buf = allocate(n + VECTOR_OVER_ALLOCATE);
123 copy_mem(new_buf, buf, used);
124 deallocate(buf, allocated);
125 buf = new_buf;
126 used = n;
127 allocated = n + VECTOR_OVER_ALLOCATE;
128 }
129
130/*************************************************
131* Compare this buffer with another one *
132*************************************************/
133template<typename T>
134bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const
135 {
136 if(size() < in.size()) return true;
137 if(size() > in.size()) return false;
138
139 for(u32bit j = 0; j != size(); j++)
140 {
141 if(buf[j] < in[j]) return true;
142 if(buf[j] > in[j]) return false;
143 }
144
145 return false;
146 }
147
148/*************************************************
149* Swap this buffer with another one *
150*************************************************/
151template<typename T>
152void MemoryRegion<T>::swap(MemoryRegion<T>& x)
153 {
154 std::swap(buf, x.buf);
155 std::swap(used, x.used);
156 std::swap(allocated, x.allocated);
157 std::swap(alloc, x.alloc);
158 }
159
160/*************************************************
161* Unlocked Variable Length Buffer *
162*************************************************/
163template<typename T>
164class MemoryVector : public MemoryRegion<T>
165 {
166 public:
167 MemoryVector<T>& operator=(const MemoryRegion<T>& in)
168 { if(this != &in) set(in); return (*this); }
169
170 MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); }
171 MemoryVector(const T in[], u32bit n)
172 { MemoryRegion<T>::init(false); set(in, n); }
173 MemoryVector(const MemoryRegion<T>& in)
174 { MemoryRegion<T>::init(false); set(in); }
175 MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
176 { MemoryRegion<T>::init(false); set(in1); append(in2); }
177 };
178
179/*************************************************
180* Locked Variable Length Buffer *
181*************************************************/
182template<typename T>
183class SecureVector : public MemoryRegion<T>
184 {
185 public:
186 SecureVector<T>& operator=(const MemoryRegion<T>& in)
187 { if(this != &in) set(in); return (*this); }
188
189 SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); }
190 SecureVector(const T in[], u32bit n)
191 { MemoryRegion<T>::init(true); set(in, n); }
192 SecureVector(const MemoryRegion<T>& in)
193 { MemoryRegion<T>::init(true); set(in); }
194 SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
195 { MemoryRegion<T>::init(true); set(in1); append(in2); }
196 };
197
198/*************************************************
199* Locked Fixed Length Buffer *
200*************************************************/
201template<typename T, u32bit L>
202class SecureBuffer : public MemoryRegion<T>
203 {
204 public:
205 SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in)
206 { if(this != &in) set(in); return (*this); }
207
208 SecureBuffer() { MemoryRegion<T>::init(true, L); }
209 SecureBuffer(const T in[], u32bit n)
210 { MemoryRegion<T>::init(true, L); copy(in, n); }
211 private:
212 SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in)
213 { if(this != &in) set(in); return (*this); }
214 };
215
216}
217
218#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status