monotone

monotone Mtn Source Tree

Root/botan/secmem.h

1/*************************************************
2* Secure Memory Buffers Header File *
3* (C) 1999-2007 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
12namespace Botan {
13
14/*************************************************
15* Variable Length Memory Buffer *
16*************************************************/
17template<typename T>
18class MemoryRegion
19 {
20 public:
21 u32bit size() const { return used; }
22 u32bit is_empty() const { return (used == 0); }
23 u32bit has_items() const { return (used != 0); }
24
25 operator T* () { return buf; }
26 operator const T* () const { return buf; }
27
28 T* begin() { return buf; }
29 const T* begin() const { return buf; }
30
31 T* end() { return (buf + size()); }
32 const T* end() const { return (buf + size()); }
33
34 bool operator==(const MemoryRegion<T>& other) const
35 {
36 return (size() == other.size() &&
37 same_mem(buf, other.buf, size()));
38 }
39
40 bool operator<(const MemoryRegion<T>&) const;
41
42 bool operator!=(const MemoryRegion<T>& in) const
43 { return (!(*this == in)); }
44 MemoryRegion<T>& operator=(const MemoryRegion<T>& in)
45 { if(this != &in) set(in); return (*this); }
46
47 void copy(const T in[], u32bit n)
48 { copy(0, in, n); }
49 void copy(u32bit off, const T in[], u32bit n)
50 { copy_mem(buf + off, in, (n > size() - off) ? (size() - off) : n); }
51
52 void set(const T in[], u32bit n) { create(n); copy(in, n); }
53 void set(const MemoryRegion<T>& in) { set(in.begin(), in.size()); }
54
55 void append(const T data[], u32bit n)
56 { grow_to(size()+n); copy(size() - n, data, n); }
57 void append(T x) { append(&x, 1); }
58 void append(const MemoryRegion<T>& x) { append(x.begin(), x.size()); }
59
60 void clear() { clear_mem(buf, allocated); }
61 void destroy() { create(0); }
62
63 void create(u32bit);
64 void grow_to(u32bit) const;
65 void swap(MemoryRegion<T>&);
66
67 ~MemoryRegion() { deallocate(buf, allocated); }
68 protected:
69 MemoryRegion() { buf = 0; alloc = 0; used = allocated = 0; }
70 MemoryRegion(const MemoryRegion<T>& copy)
71 {
72 buf = 0;
73 used = allocated = 0;
74 alloc = copy.alloc;
75 set(copy.buf, copy.used);
76 }
77
78 void init(bool locking, u32bit size = 0)
79 { alloc = Allocator::get(locking); create(size); }
80 private:
81 T* allocate(u32bit n) const
82 {
83 return static_cast<T*>(alloc->allocate(sizeof(T)*n));
84 }
85
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 mutable 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 if(n > used && n <= allocated)
114 {
115 clear_mem(buf + used, n - used);
116 used = n;
117 return;
118 }
119 else if(n > allocated)
120 {
121 T* new_buf = allocate(n);
122 copy_mem(new_buf, buf, used);
123 deallocate(buf, allocated);
124 buf = new_buf;
125 allocated = used = n;
126 }
127 }
128
129/*************************************************
130* Compare this buffer with another one *
131*************************************************/
132template<typename T>
133bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const
134 {
135 if(size() < in.size()) return true;
136 if(size() > in.size()) return false;
137
138 for(u32bit j = 0; j != size(); j++)
139 {
140 if(buf[j] < in[j]) return true;
141 if(buf[j] > in[j]) return false;
142 }
143
144 return false;
145 }
146
147/*************************************************
148* Swap this buffer with another one *
149*************************************************/
150template<typename T>
151void MemoryRegion<T>::swap(MemoryRegion<T>& x)
152 {
153 std::swap(buf, x.buf);
154 std::swap(used, x.used);
155 std::swap(allocated, x.allocated);
156 std::swap(alloc, x.alloc);
157 }
158
159/*************************************************
160* Unlocked Variable Length Buffer *
161*************************************************/
162template<typename T>
163class MemoryVector : public MemoryRegion<T>
164 {
165 public:
166 MemoryVector<T>& operator=(const MemoryRegion<T>& in)
167 { if(this != &in) set(in); return (*this); }
168
169 MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); }
170 MemoryVector(const T in[], u32bit n)
171 { MemoryRegion<T>::init(false); set(in, n); }
172 MemoryVector(const MemoryRegion<T>& in)
173 { MemoryRegion<T>::init(false); set(in); }
174 MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
175 { MemoryRegion<T>::init(false); set(in1); append(in2); }
176 };
177
178/*************************************************
179* Locked Variable Length Buffer *
180*************************************************/
181template<typename T>
182class SecureVector : public MemoryRegion<T>
183 {
184 public:
185 SecureVector<T>& operator=(const MemoryRegion<T>& in)
186 { if(this != &in) set(in); return (*this); }
187
188 SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); }
189 SecureVector(const T in[], u32bit n)
190 { MemoryRegion<T>::init(true); set(in, n); }
191 SecureVector(const MemoryRegion<T>& in)
192 { MemoryRegion<T>::init(true); set(in); }
193 SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
194 { MemoryRegion<T>::init(true); set(in1); append(in2); }
195 };
196
197/*************************************************
198* Locked Fixed Length Buffer *
199*************************************************/
200template<typename T, u32bit L>
201class SecureBuffer : public MemoryRegion<T>
202 {
203 public:
204 SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in)
205 { if(this != &in) set(in); return (*this); }
206
207 SecureBuffer() { MemoryRegion<T>::init(true, L); }
208 SecureBuffer(const T in[], u32bit n)
209 { MemoryRegion<T>::init(true, L); copy(in, n); }
210 private:
211 SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in)
212 { if(this != &in) set(in); return (*this); }
213 };
214
215}
216
217#endif

Archive Download this file

Branches

Tags

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