consolidate windows crypto provider implementation into single header (#1495)

consolidate windows crypto provider into single header
This commit is contained in:
Andrei Kurushin 2017-01-07 09:53:39 +03:00 committed by Arvid Norberg
parent 57f52adc88
commit ff8345101e
7 changed files with 180 additions and 235 deletions

View File

@ -184,6 +184,7 @@ nobase_include_HEADERS = \
aux_/has_block.hpp \
aux_/scope_end.hpp \
aux_/vector.hpp \
aux_/win_crypto_provider.hpp \
aux_/win_util.hpp \
aux_/non_owning_handle.hpp \
\

View File

@ -0,0 +1,165 @@
/*
Copyright (c) 2016, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_WIN_CRYPTO_PROVIDER_HPP
#define TORRENT_WIN_CRYPTO_PROVIDER_HPP
#include "libtorrent/config.hpp"
#include "libtorrent/error_code.hpp"
#include <windows.h>
#include <wincrypt.h>
namespace libtorrent { namespace aux
{
inline HCRYPTPROV crypt_acquire_provider(DWORD provider_type)
{
HCRYPTPROV ret;
if (CryptAcquireContext(&ret, nullptr, nullptr, provider_type
, CRYPT_VERIFYCONTEXT) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return ret;
}
inline void crypt_gen_random(span<char> buffer)
{
static HCRYPTPROV provider = crypt_acquire_provider(PROV_RSA_FULL);
if (!CryptGenRandom(provider, int(buffer.size())
, reinterpret_cast<BYTE*>(buffer.data())))
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
}
template <ALG_ID AlgId, DWORD ProviderType>
struct crypt_hash
{
crypt_hash() { m_hash = create(); }
crypt_hash(crypt_hash const& h) { m_hash = duplicate(h); }
~crypt_hash() { CryptDestroyHash(m_hash); }
crypt_hash& crypt_hash::operator=(crypt_hash const& h)
{
if (this == &h) return *this;
HCRYPTHASH temp = duplicate(h);
CryptDestroyHash(m_hash);
m_hash = temp;
return *this;
}
void reset()
{
HCRYPTHASH temp = create();
CryptDestroyHash(m_hash);
m_hash = temp;
}
void update(span<char const> data)
{
if (CryptHashData(m_hash, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
}
void get_hash(char *digest, size_t digest_size)
{
DWORD size = DWORD(digest_size);
if (CryptGetHashParam(m_hash, HP_HASHVAL
, reinterpret_cast<BYTE*>(digest), &size, 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
TORRENT_ASSERT(size == DWORD(digest_size));
}
private:
HCRYPTHASH create()
{
HCRYPTHASH ret;
if (CryptCreateHash(get_provider(), AlgId, 0, 0, &ret) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return ret;
}
HCRYPTHASH duplicate(crypt_hash const& h)
{
HCRYPTHASH ret;
if (CryptDuplicateHash(h.m_hash, 0, 0, &ret) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return ret;
}
HCRYPTPROV get_provider()
{
static HCRYPTPROV provider = crypt_acquire_provider(ProviderType);
return provider;
}
HCRYPTHASH m_hash;
};
} // namespace aux
} // namespace libtorrent
#endif

View File

@ -47,8 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <CommonCrypto/CommonDigest.h>
#elif TORRENT_USE_CRYPTOAPI
#include <windows.h>
#include <wincrypt.h>
#include "libtorrent/aux_/win_crypto_provider.hpp"
#elif defined TORRENT_USE_LIBCRYPTO
@ -114,7 +113,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_CTX m_context;
#elif TORRENT_USE_CRYPTOAPI
HCRYPTHASH m_context;
aux::crypt_hash<CALG_SHA1, PROV_RSA_FULL> m_context;
#elif defined TORRENT_USE_LIBCRYPTO
SHA_CTX m_context;
#else

View File

@ -47,8 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <CommonCrypto/CommonDigest.h>
#elif TORRENT_USE_CRYPTOAPI_SHA_512
#include <windows.h>
#include <wincrypt.h>
#include "libtorrent/aux_/win_crypto_provider.hpp"
#elif defined TORRENT_USE_LIBCRYPTO
@ -114,7 +113,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA512_CTX m_context;
#elif TORRENT_USE_CRYPTOAPI_SHA_512
HCRYPTHASH m_context;
aux::crypt_hash<CALG_SHA_512, PROV_RSA_AES> m_context;
#elif defined TORRENT_USE_LIBCRYPTO
SHA512_CTX m_context;
#else

View File

@ -35,34 +35,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp"
#include "libtorrent/aux_/openssl.hpp"
#if TORRENT_USE_CRYPTOAPI
namespace
{
HCRYPTPROV make_crypt_provider()
{
using namespace libtorrent;
HCRYPTPROV ret;
if (CryptAcquireContext(&ret, nullptr, nullptr, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return ret;
}
HCRYPTPROV get_crypt_provider()
{
static HCRYPTPROV prov = make_crypt_provider();
return prov;
}
}
#endif
namespace libtorrent
{
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
@ -77,14 +49,6 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Init(&m_context);
#elif TORRENT_USE_CRYPTOAPI
if (CryptCreateHash(get_crypt_provider(), CALG_SHA1, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
#elif defined TORRENT_USE_LIBCRYPTO
SHA1_Init(&m_context);
#else
@ -118,33 +82,6 @@ namespace libtorrent
gcry_md_copy(&m_context, h.m_context);
return *this;
}
#elif TORRENT_USE_CRYPTOAPI
hasher::hasher(hasher const& h)
{
if (CryptDuplicateHash(h.m_context, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
}
hasher& hasher::operator=(hasher const& h)
{
if (this == &h) return *this;
CryptDestroyHash(m_context);
if (CryptDuplicateHash(h.m_context, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return *this;
}
#else
hasher::hasher(hasher const&) = default;
hasher& hasher::operator=(hasher const&) = default;
@ -163,14 +100,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), CC_LONG(data.size()));
#elif TORRENT_USE_CRYPTOAPI
if (CryptHashData(m_context, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
m_context.update(data);
#elif defined TORRENT_USE_LIBCRYPTO
SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), data.size());
#else
@ -188,18 +118,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Final(reinterpret_cast<unsigned char*>(digest.data()), &m_context);
#elif TORRENT_USE_CRYPTOAPI
DWORD size = DWORD(digest.size());
if (CryptGetHashParam(m_context, HP_HASHVAL
, reinterpret_cast<BYTE*>(digest.data()), &size, 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
TORRENT_ASSERT(size == digest.size());
m_context.get_hash(digest.data(), digest.size());
#elif defined TORRENT_USE_LIBCRYPTO
SHA1_Final(reinterpret_cast<unsigned char*>(digest.data()), &m_context);
#else
@ -215,15 +134,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Init(&m_context);
#elif TORRENT_USE_CRYPTOAPI
CryptDestroyHash(m_context);
if (CryptCreateHash(get_crypt_provider(), CALG_SHA1, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
m_context.reset();
#elif defined TORRENT_USE_LIBCRYPTO
SHA1_Init(&m_context);
#else
@ -233,9 +144,7 @@ namespace libtorrent
hasher::~hasher()
{
#if TORRENT_USE_CRYPTOAPI
CryptDestroyHash(m_context);
#elif defined TORRENT_USE_LIBGCRYPT
#if defined TORRENT_USE_LIBGCRYPT
gcry_md_close(m_context);
#endif
}

View File

@ -35,34 +35,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp"
#include "libtorrent/aux_/openssl.hpp"
#if TORRENT_USE_CRYPTOAPI_SHA_512
namespace
{
HCRYPTPROV make_crypt_provider()
{
using namespace libtorrent;
HCRYPTPROV ret;
if (CryptAcquireContext(&ret, nullptr, nullptr, PROV_RSA_AES
, CRYPT_VERIFYCONTEXT) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return ret;
}
HCRYPTPROV get_crypt_provider()
{
static HCRYPTPROV prov = make_crypt_provider();
return prov;
}
}
#endif
namespace libtorrent
{
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
@ -77,14 +49,6 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA512_Init(&m_context);
#elif TORRENT_USE_CRYPTOAPI_SHA_512
if (CryptCreateHash(get_crypt_provider(), CALG_SHA_512, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
#elif defined TORRENT_USE_LIBCRYPTO
SHA512_Init(&m_context);
#else
@ -111,33 +75,6 @@ namespace libtorrent
gcry_md_copy(&m_context, h.m_context);
return *this;
}
#elif TORRENT_USE_CRYPTOAPI_SHA_512
hasher512::hasher512(hasher512 const& h)
{
if (CryptDuplicateHash(h.m_context, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
}
hasher512& hasher512::operator=(hasher512 const& h)
{
if (this == &h) return *this;
CryptDestroyHash(m_context);
if (CryptDuplicateHash(h.m_context, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return *this;
}
#else
hasher512::hasher512(hasher512 const&) = default;
hasher512& hasher512::operator=(hasher512 const&) = default;
@ -151,14 +88,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA512_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), CC_LONG(data.size()));
#elif TORRENT_USE_CRYPTOAPI_SHA_512
if (CryptHashData(m_context, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
m_context.update(data);
#elif defined TORRENT_USE_LIBCRYPTO
SHA512_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), data.size());
#else
@ -176,18 +106,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA512_Final(reinterpret_cast<unsigned char*>(digest.data()), &m_context);
#elif TORRENT_USE_CRYPTOAPI_SHA_512
DWORD size = DWORD(digest.size());
if (CryptGetHashParam(m_context, HP_HASHVAL
, reinterpret_cast<BYTE*>(digest.data()), &size, 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
TORRENT_ASSERT(size == digest.size());
m_context.get_hash(digest.data(), digest.size());
#elif defined TORRENT_USE_LIBCRYPTO
SHA512_Final(reinterpret_cast<unsigned char*>(digest.data()), &m_context);
#else
@ -203,15 +122,7 @@ namespace libtorrent
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA512_Init(&m_context);
#elif TORRENT_USE_CRYPTOAPI_SHA_512
CryptDestroyHash(m_context);
if (CryptCreateHash(get_crypt_provider(), CALG_SHA_512, 0, 0, &m_context) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
m_context.reset();
#elif defined TORRENT_USE_LIBCRYPTO
SHA512_Init(&m_context);
#else
@ -221,9 +132,7 @@ namespace libtorrent
hasher512::~hasher512()
{
#if TORRENT_USE_CRYPTOAPI_SHA_512
CryptDestroyHash(m_context);
#elif defined TORRENT_USE_LIBGCRYPT
#if defined TORRENT_USE_LIBGCRYPT
gcry_md_close(m_context);
#endif
}

View File

@ -38,8 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_push.hpp"
#if TORRENT_USE_CRYPTOAPI
#include <windows.h>
#include <wincrypt.h>
#include "libtorrent/aux_/win_crypto_provider.hpp"
#elif defined TORRENT_USE_LIBCRYPTO
extern "C" {
@ -51,34 +50,6 @@ extern "C" {
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#if TORRENT_USE_CRYPTOAPI
namespace
{
HCRYPTPROV make_crypt_provider()
{
using namespace libtorrent;
HCRYPTPROV ret;
if (CryptAcquireContext(&ret, nullptr, nullptr, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
return ret;
}
HCRYPTPROV get_crypt_provider()
{
static HCRYPTPROV prov = make_crypt_provider();
return prov;
}
}
#endif
namespace libtorrent
{
namespace aux
@ -98,15 +69,7 @@ namespace libtorrent
void random_bytes(span<char> buffer)
{
#if TORRENT_USE_CRYPTOAPI
if (!CryptGenRandom(get_crypt_provider(), int(buffer.size())
, reinterpret_cast<BYTE*>(buffer.data())))
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
aux::crypt_gen_random(buffer);
#elif defined TORRENT_USE_LIBCRYPTO
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO