monotone

monotone Mtn Source Tree

Root/botan/secmem.h

1/*************************************************
2* Secure Memory Buffers Header File *
3* (C) 1999-2006 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 { return (T*)alloc->allocate(sizeof(T)*n); }
82 void deallocate(T* p, u32bit n) const
83 { alloc->deallocate(p, sizeof(T)*n); }
84
85 mutable T* buf;
86 mutable u32bit used;
87 mutable u32bit allocated;
88 mutable Allocator* alloc;
89 };
90
91/*************************************************
92* Create a new buffer *
93*************************************************/
94template<typename T>
95void MemoryRegion<T>::create(u32bit n)
96 {
97 if(n <= allocated) { clear(); used = n; return; }
98 deallocate(buf, allocated);
99 buf = allocate(n);
100 allocated = used = n;
101 }
102
103/*************************************************
104* Increase the size of the buffer *
105*************************************************/
106template<typename T>
107void MemoryRegion<T>::grow_to(u32bit n) const
108 {
109 if(n > used && n <= allocated)
110 {
111 clear_mem(buf + used, n - used);
112 used = n;
113 return;
114 }
115 else if(n > allocated)
116 {
117 T* new_buf = allocate(n);
118 copy_mem(new_buf, buf, used);
119 deallocate(buf, allocated);
120 buf = new_buf;
121 allocated = used = n;
122 }
123 }
124
125/*************************************************
126* Compare this buffer with another one *
127*************************************************/
128template<typename T>
129bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const
130 {
131 if(size() < in.size()) return true;
132 if(size() > in.size()) return false;
133
134 for(u32bit j = 0; j != size(); j++)
135 {
136 if(buf[j] < in[j]) return true;
137 if(buf[j] > in[j]) return false;
138 }
139
140 return false;
141 }
142
143/*************************************************
144* Swap this buffer with another one *
145*************************************************/
146template<typename T>
147void MemoryRegion<T>::swap(MemoryRegion<T>& x)
148 {
149 std::swap(buf, x.buf);
150 std::swap(used, x.used);
151 std::swap(allocated, x.allocated);
152 std::swap(alloc, x.alloc);
153 }
154
155/*************************************************
156* Unlocked Variable Length Buffer *
157*************************************************/
158template<typename T>
159class MemoryVector : public MemoryRegion<T>
160 {
161 public:
162 MemoryVector<T>& operator=(const MemoryRegion<T>& in)
163 { if(this != &in) set(in); return (*this); }
164
165 MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); }
166 MemoryVector(const T in[], u32bit n)
167 { MemoryRegion<T>::init(false); set(in, n); }
168 MemoryVector(const MemoryRegion<T>& in)
169 { MemoryRegion<T>::init(false); set(in); }
170 MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
171 { MemoryRegion<T>::init(false); set(in1); append(in2); }
172 };
173
174/*************************************************
175* Locked Variable Length Buffer *
176*************************************************/
177template<typename T>
178class SecureVector : public MemoryRegion<T>
179 {
180 public:
181 SecureVector<T>& operator=(const MemoryRegion<T>& in)
182 { if(this != &in) set(in); return (*this); }
183
184 SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); }
185 SecureVector(const T in[], u32bit n)
186 { MemoryRegion<T>::init(true); set(in, n); }
187 SecureVector(const MemoryRegion<T>& in)
188 { MemoryRegion<T>::init(true); set(in); }
189 SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
190 { MemoryRegion<T>::init(true); set(in1); append(in2); }
191 };
192
193/*************************************************
194* Locked Fixed Length Buffer *
195*************************************************/
196template<typename T, u32bit L>
197class SecureBuffer : public MemoryRegion<T>
198 {
199 public:
200 SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in)
201 { if(this != &in) set(in); return (*this); }
202
203 SecureBuffer() { MemoryRegion<T>::init(true, L); }
204 SecureBuffer(const T in[], u32bit n)
205 { MemoryRegion<T>::init(true, L); copy(in, n); }
206 private:
207 SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in)
208 { if(this != &in) set(in); return (*this); }
209 };
210
211}
212
213#endif

Archive Download this file

Branches

Tags

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