2
Copyright (C) 1999-2007 The Botan Project. All rights reserved.
4
Redistribution and use in source and binary forms, for any use, with or without
5
modification, is permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions, and the following disclaimer.
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions, and the following disclaimer in the documentation
12
and/or other materials provided with the distribution.
14
THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
15
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
18
IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
19
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
namespace QCA { // WRAPNS_LINE
28
/*************************************************
29
* Secure Memory Buffers Header File *
30
* (C) 1999-2007 The Botan Project *
31
*************************************************/
33
#ifndef BOTAN_SECURE_MEMORY_BUFFERS_H__
34
#define BOTAN_SECURE_MEMORY_BUFFERS_H__
37
#include <botan/allocate.h>
38
namespace QCA { // WRAPNS_LINE
40
#include <botan/mem_ops.h>
41
namespace QCA { // WRAPNS_LINE
45
/*************************************************
46
* Variable Length Memory Buffer *
47
*************************************************/
52
u32bit size() const { return used; }
53
u32bit is_empty() const { return (used == 0); }
54
u32bit has_items() const { return (used != 0); }
56
operator T* () { return buf; }
57
operator const T* () const { return buf; }
59
T* begin() { return buf; }
60
const T* begin() const { return buf; }
62
T* end() { return (buf + size()); }
63
const T* end() const { return (buf + size()); }
65
bool operator==(const MemoryRegion<T>& other) const
67
return (size() == other.size() &&
68
same_mem(buf, other.buf, size()));
71
bool operator<(const MemoryRegion<T>&) const;
73
bool operator!=(const MemoryRegion<T>& in) const
74
{ return (!(*this == in)); }
75
MemoryRegion<T>& operator=(const MemoryRegion<T>& in)
76
{ if(this != &in) set(in); return (*this); }
78
void copy(const T in[], u32bit n)
80
void copy(u32bit off, const T in[], u32bit n)
81
{ copy_mem(buf + off, in, (n > size() - off) ? (size() - off) : n); }
83
void set(const T in[], u32bit n) { create(n); copy(in, n); }
84
void set(const MemoryRegion<T>& in) { set(in.begin(), in.size()); }
86
void append(const T data[], u32bit n)
87
{ grow_to(size()+n); copy(size() - n, data, n); }
88
void append(T x) { append(&x, 1); }
89
void append(const MemoryRegion<T>& x) { append(x.begin(), x.size()); }
91
void clear() { clear_mem(buf, allocated); }
92
void destroy() { create(0); }
95
void grow_to(u32bit) const;
96
void swap(MemoryRegion<T>&);
98
~MemoryRegion() { deallocate(buf, allocated); }
100
MemoryRegion() { buf = 0; alloc = 0; used = allocated = 0; }
101
MemoryRegion(const MemoryRegion<T>& copy)
104
used = allocated = 0;
106
set(copy.buf, copy.used);
109
void init(bool locking, u32bit size = 0)
110
{ alloc = Allocator::get(locking); create(size); }
112
T* allocate(u32bit n) const { return (T*)alloc->allocate(sizeof(T)*n); }
113
void deallocate(T* p, u32bit n) const
114
{ alloc->deallocate(p, sizeof(T)*n); }
118
mutable u32bit allocated;
119
mutable Allocator* alloc;
122
/*************************************************
123
* Create a new buffer *
124
*************************************************/
126
void MemoryRegion<T>::create(u32bit n)
128
if(n <= allocated) { clear(); used = n; return; }
129
deallocate(buf, allocated);
131
allocated = used = n;
134
/*************************************************
135
* Increase the size of the buffer *
136
*************************************************/
138
void MemoryRegion<T>::grow_to(u32bit n) const
140
if(n > used && n <= allocated)
142
clear_mem(buf + used, n - used);
146
else if(n > allocated)
148
T* new_buf = allocate(n);
149
copy_mem(new_buf, buf, used);
150
deallocate(buf, allocated);
152
allocated = used = n;
156
/*************************************************
157
* Compare this buffer with another one *
158
*************************************************/
160
bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const
162
if(size() < in.size()) return true;
163
if(size() > in.size()) return false;
165
for(u32bit j = 0; j != size(); j++)
167
if(buf[j] < in[j]) return true;
168
if(buf[j] > in[j]) return false;
174
/*************************************************
175
* Swap this buffer with another one *
176
*************************************************/
178
void MemoryRegion<T>::swap(MemoryRegion<T>& x)
180
std::swap(buf, x.buf);
181
std::swap(used, x.used);
182
std::swap(allocated, x.allocated);
183
std::swap(alloc, x.alloc);
186
/*************************************************
187
* Unlocked Variable Length Buffer *
188
*************************************************/
190
class MemoryVector : public MemoryRegion<T>
193
MemoryVector<T>& operator=(const MemoryRegion<T>& in)
194
{ if(this != &in) set(in); return (*this); }
196
MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); }
197
MemoryVector(const T in[], u32bit n)
198
{ MemoryRegion<T>::init(false); set(in, n); }
199
MemoryVector(const MemoryRegion<T>& in)
200
{ MemoryRegion<T>::init(false); set(in); }
201
MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
202
{ MemoryRegion<T>::init(false); set(in1); append(in2); }
205
/*************************************************
206
* Locked Variable Length Buffer *
207
*************************************************/
209
class SecureVector : public MemoryRegion<T>
212
SecureVector<T>& operator=(const MemoryRegion<T>& in)
213
{ if(this != &in) set(in); return (*this); }
215
SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); }
216
SecureVector(const T in[], u32bit n)
217
{ MemoryRegion<T>::init(true); set(in, n); }
218
SecureVector(const MemoryRegion<T>& in)
219
{ MemoryRegion<T>::init(true); set(in); }
220
SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
221
{ MemoryRegion<T>::init(true); set(in1); append(in2); }
224
/*************************************************
225
* Locked Fixed Length Buffer *
226
*************************************************/
227
template<typename T, u32bit L>
228
class SecureBuffer : public MemoryRegion<T>
231
SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in)
232
{ if(this != &in) set(in); return (*this); }
234
SecureBuffer() { MemoryRegion<T>::init(true, L); }
235
SecureBuffer(const T in[], u32bit n)
236
{ MemoryRegion<T>::init(true, L); copy(in, n); }
238
SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in)
239
{ if(this != &in) set(in); return (*this); }