Add BytePtr and ConstBytePtr overloads for SecByteBlock (GH #890)

This commit is contained in:
Jeffrey Walton 2019-10-03 04:12:16 -04:00
parent f5c817221b
commit 366fdce86f
7 changed files with 87 additions and 7 deletions

View File

@ -308,6 +308,7 @@ scrypt.h
seal.cpp
seal.h
secblock.h
secblockfwd.h
seckey.h
seed.cpp
seed.h

11
asn.cpp
View File

@ -6,7 +6,9 @@
#ifndef CRYPTOPP_IMPORTS
#include "cryptlib.h"
#include "asn.h"
#include "misc.h"
#include <iostream>
#include <iomanip>
@ -15,7 +17,6 @@
NAMESPACE_BEGIN(CryptoPP)
/// DER Length
size_t DERLengthEncode(BufferedTransformation &bt, lword length)
{
size_t i=0;
@ -129,7 +130,7 @@ size_t BERDecodeOctetString(BufferedTransformation &bt, SecByteBlock &str)
BERDecodeError();
str.New(bc);
if (bc != bt.Get(str, bc))
if (bc != bt.Get(BytePtr(str), bc))
BERDecodeError();
return bc;
}
@ -181,7 +182,7 @@ size_t BERDecodeTextString(BufferedTransformation &bt, SecByteBlock &str, byte a
BERDecodeError();
str.resize(bc);
if (bc != bt.Get(str, str.size()))
if (bc != bt.Get(BytePtr(str), BytePtrSize(str)))
BERDecodeError();
return bc;
@ -227,7 +228,7 @@ size_t BERDecodeDate(BufferedTransformation &bt, SecByteBlock &str, byte asnTag)
BERDecodeError();
str.resize(bc);
if (bc != bt.Get(str, str.size()))
if (bc != bt.Get(BytePtr(str), BytePtrSize(str)))
BERDecodeError();
return bc;
@ -263,7 +264,7 @@ size_t BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigne
BERDecodeError();
unusedBits = unused;
str.resize(bc-1);
if ((bc-1) != bt.Get(str, bc-1))
if ((bc-1) != bt.Get(BytePtr(str), bc-1))
BERDecodeError();
return bc-1;
}

View File

@ -531,6 +531,7 @@
<ClInclude Include="scrypt.h" />
<ClInclude Include="seal.h" />
<ClInclude Include="secblock.h" />
<ClInclude Include="secblockfwd.h" />
<ClInclude Include="seckey.h" />
<ClInclude Include="seed.h" />
<ClInclude Include="serpent.h" />

View File

@ -942,6 +942,9 @@
<ClInclude Include="secblock.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="secblockfwd.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="seckey.h">
<Filter>Header Files</Filter>
</ClInclude>

View File

@ -20,6 +20,28 @@
NAMESPACE_BEGIN(CryptoPP)
byte* BytePtr(SecByteBlock& str)
{
// Caller wants a writeable pointer
CRYPTOPP_ASSERT(str.empty() == false);
if (str.empty())
return NULLPTR;
return reinterpret_cast<byte*>(str.data());
}
const byte* ConstBytePtr(const SecByteBlock& str)
{
if (str.empty())
return NULLPTR;
return reinterpret_cast<const byte*>(str.data());
}
size_t BytePtrSize(const SecByteBlock& str)
{
return str.size();
}
void xorbuf(byte *buf, const byte *mask, size_t count)
{
CRYPTOPP_ASSERT(buf != NULLPTR);

27
misc.h
View File

@ -7,6 +7,7 @@
#define CRYPTOPP_MISC_H
#include "cryptlib.h"
#include "secblockfwd.h"
#include "smartptr.h"
#include "stdcpp.h"
#include "trap.h"
@ -432,6 +433,7 @@ inline size_t PtrByteDiff(const PTR pointer1, const PTR pointer2)
/// \param str std::string
/// \details BytePtr returns NULL pointer for an empty string.
/// \return Pointer to the first element of a string
/// \since Crypto++ 8.0
inline byte* BytePtr(std::string& str)
{
// Caller wants a writeable pointer
@ -442,16 +444,32 @@ inline byte* BytePtr(std::string& str)
return reinterpret_cast<byte*>(&str[0]);
}
/// \brief Pointer to the first element of a string
/// \param str SecByteBlock
/// \details BytePtr returns NULL pointer for an empty string.
/// \return Pointer to the first element of a string
/// \since Crypto++ 8.3
byte* BytePtr(SecByteBlock& str);
/// \brief Const pointer to the first element of a string
/// \param str std::string
/// \details ConstBytePtr returns non-NULL pointer for an empty string.
/// \return Pointer to the first element of a string
/// \since Crypto++ 8.0
inline const byte* ConstBytePtr(const std::string& str)
{
// Use c_str() so a pointer is always available
return reinterpret_cast<const byte*>(str.c_str());
if (str.empty())
return NULLPTR;
return reinterpret_cast<const byte*>(&str[0]);
}
/// \brief Const pointer to the first element of a string
/// \param str SecByteBlock
/// \details ConstBytePtr returns non-NULL pointer for an empty string.
/// \return Pointer to the first element of a string
/// \since Crypto++ 8.3
const byte* ConstBytePtr(const SecByteBlock& str);
/// \brief Size of a string
/// \param str std::string
/// \return size of a string
@ -460,6 +478,11 @@ inline size_t BytePtrSize(const std::string& str)
return str.size();
}
/// \brief Size of a string
/// \param str SecByteBlock
/// \return size of a string
size_t BytePtrSize(const SecByteBlock& str);
#if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
/// \brief Bounds checking replacement for memcpy()

29
secblockfwd.h Normal file
View File

@ -0,0 +1,29 @@
// secblockfwd.h - written and placed in the public domain by Jeffrey Walton
/// \file secblockfwd.h
/// \brief Forward declarations for SecBlock
/// \details secblock.h and misc.h have a circular dependency. secblockfwd.h
/// allows the library to sidestep the circular dependency, and reference
/// SecBlock classes without the full implementation.
/// \since Crypto++ 8.3
#ifndef CRYPTOPP_SECBLOCKFWD_H
#define CRYPTOPP_SECBLOCKFWD_H
#include "config.h"
NAMESPACE_BEGIN(CryptoPP)
template <class T, class A>
class SecBlock;
template <class T, bool A>
class AllocatorWithCleanup;
typedef SecBlock<byte, AllocatorWithCleanup<byte, false> > SecByteBlock;
typedef SecBlock<word, AllocatorWithCleanup<word, false> > SecWordBlock;
typedef SecBlock<byte, AllocatorWithCleanup<byte, true> > AlignedSecByteBlock;
NAMESPACE_END
#endif // CRYPTOPP_SECBLOCKFWD_H