SecByteBlockSink patch

250 views
Skip to first unread message

Jeffrey Walton

unread,
Feb 13, 2011, 7:05:11 AM2/13/11
to Crypto++ Users
Hi Wei,

Below (and attached) is a patch which adds a SecByteBlockSink. I've
found it to be very convenient when using filters (even though its
embarrassingly simple).

filters.h seemed like a natural choice. In addition, the filter
properly detects overflow in case the input is tainted.

Jeff

$ cat filters.h.patch
Index: filters.h
===================================================================
--- filters.h (revision 525)
+++ filters.h (working copy)
@@ -10,6 +10,7 @@
#include "queue.h"
#include "algparam.h"
#include <deque>
+#include <limits>

NAMESPACE_BEGIN(CryptoPP)

@@ -805,6 +806,31 @@
{SourceInitialize(pumpAll,
MakeParameters("RandomNumberGeneratorPointer",
&rng)("RandomNumberStoreSize", length));}
};

+class CRYPTOPP_DLL SecByteBlockSink : public Bufferless<Sink>
+{
+public:
+ SecByteBlockSink(SecByteBlock& sbb) : m_sbb(sbb) { }
+
+ size_t Put2(const byte *inString, size_t length, int /*messageEnd*/,
bool /*blocking*/)
+ {
+ if(!inString || !length) return length;
+
+ const size_t size = m_sbb.size();
+ const size_t max = std::numeric_limits<std::size_t>::max() - size;
+
+ if(length > max)
+ InvalidArgument("SecByteBlockSink: buffer overflow");
+
+ m_sbb.resize(size+length);
+ memcpy(m_sbb.begin()+size, inString, length);
+
+ return 0;
+ }
+
+private:
+ SecByteBlock& m_sbb;
+};
+
NAMESPACE_END

#endif

filters.h.patch
Reply all
Reply to author
Forward
0 new messages