12#include <com/sun/star/uno/RuntimeException.hpp>
14#include <rtl/ustring.hxx>
16#include <osl/endian.h>
17#include <config_oox.h>
24#include <openssl/evp.h>
25#include <openssl/sha.h>
36 HASH_HashType getNSSType()
const
45 return HASH_AlgSHA256;
47 return HASH_AlgSHA512;
55 const EVP_MD* getOpenSSLType()
const
80 if (!NSS_IsInitialized())
82 auto const e = NSS_NoDB_Init(
nullptr);
85 PRErrorCode error = PR_GetError();
86 const char* errorText = PR_ErrorToName(error);
87 throw css::uno::RuntimeException(
"NSS_NoDB_Init failed with " + OUString(errorText, strlen(errorText), RTL_TEXTENCODING_UTF8) +
" (" + OUString::number(
static_cast<int>(error)) +
")");
94 EVP_DigestInit_ex(
mpContext, getOpenSSLType(),
nullptr);
131 std::vector<unsigned char> hash(
getLength(), 0);
132 unsigned int digestWrittenLength;
134 HASH_End(
mpImpl->mpContext, hash.data(), &digestWrittenLength,
getLength());
136 EVP_DigestFinal_ex(
mpImpl->mpContext, hash.data(), &digestWrittenLength);
138 (void)digestWrittenLength;
169 const unsigned char* pInput,
size_t nLength,
170 const unsigned char* pSalt,
size_t nSaltLen,
171 sal_uInt32 nSpinCount,
178 if (!nSaltLen && !nSpinCount)
184 std::vector<unsigned char> initialData( nSaltLen +
nLength);
185 std::copy( pSalt, pSalt + nSaltLen, initialData.begin());
186 std::copy( pInput, pInput +
nLength, initialData.begin() + nSaltLen);
187 aHash.
update( initialData.data(), initialData.size());
188 rtl_secureZeroMemory( initialData.data(), initialData.size());
194 std::vector<unsigned char> hash( aHash.
finalize());
206 std::vector<unsigned char> data( hash.size() + nAddIter, 0);
207 for (sal_uInt32
i = 0;
i < nSpinCount; ++
i)
209 std::copy( hash.begin(), hash.end(), data.begin() + nHashPos);
215 std::swap(
p[0],
p[3] );
216 std::swap(
p[1],
p[2] );
217 memcpy( data.data() + nIterPos, &be, nAddIter);
219 memcpy( data.data() + nIterPos, &
i, nAddIter);
225 aReHash.
update( data.data(), data.size());
234 const OUString& rPassword,
235 const std::vector<unsigned char>& rSaltValue,
236 sal_uInt32 nSpinCount,
237 IterCount eIterCount,
240 const unsigned char* pPassBytes =
reinterpret_cast<const unsigned char*
>(rPassword.getStr());
241 const size_t nPassBytesLen = rPassword.getLength() * 2;
244 std::vector<unsigned char> vPass;
247 vPass.resize( nPassBytesLen);
248 std::copy( pPassBytes, pPassBytes + nPassBytesLen, vPass.begin());
249 unsigned char*
p = vPass.data();
250 unsigned char const *
const pEnd =
p + nPassBytesLen;
251 for ( ;
p < pEnd;
p += 2 )
253 std::swap( p[0], p[1] );
255 pPassBytes = vPass.data();
258 return calculateHash( pPassBytes, nPassBytesLen, rSaltValue.data(), rSaltValue.size(), nSpinCount,
std::vector< unsigned char > finalize()
std::unique_ptr< HashImpl > mpImpl
static std::vector< unsigned char > calculateHash(const unsigned char *pInput, size_t length, HashType eType)
void update(const unsigned char *pInput, size_t length)
@ APPEND
Iteration count prepended to hash iterations.
@ PREPEND
Iteration count not added to hash iterations.
const sal_uInt32 SHA256_HASH_LENGTH
const sal_uInt32 SHA512_HASH_LENGTH
const sal_uInt32 SHA1_HASH_LENGTH
const sal_uInt32 MD5_HASH_LENGTH
ParserContextSharedPtr mpContext