3
* aria2 - The high speed download utility
5
* Copyright (C) 2013 Nils Maier
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
* In addition, as a special exception, the copyright holders give
22
* permission to link the code of portions of this program with the
23
* OpenSSL library under certain conditions as described in each
24
* individual source file, and distribute linked combinations
26
* You must obey the GNU General Public License in all respects
27
* for all of the code used other than OpenSSL. If you modify
28
* file(s) with this exception, you may extend this exception to your
29
* version of the file(s), but you are not obligated to do so. If you
30
* do not wish to do so, delete this exception statement from your
31
* version. If you delete this exception statement from all source
32
* files in the program, then also delete it here.
36
#include "MessageDigestImpl.h"
41
#include "DlAbortEx.h"
42
#include "LogFactory.h"
45
using namespace aria2;
52
if (!::CryptAcquireContext(&provider_, nullptr, nullptr,
53
PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
54
if (!::CryptAcquireContext(&provider_, nullptr, nullptr, PROV_RSA_AES,
55
CRYPT_VERIFYCONTEXT)) {
56
throw DL_ABORT_EX("Failed to get cryptographic provider");
61
::CryptReleaseContext(provider_, 0);
70
static Context context_;
72
inline size_t getAlgLength(ALG_ID id)
76
if (!::CryptCreateHash(context.get(), id, 0, 0, &hash)) {
77
throw DL_ABORT_EX(fmt("Failed to initialize hash %d", id));
81
DWORD len = sizeof(rv);
82
if (!::CryptGetHashParam(hash, HP_HASHSIZE, reinterpret_cast<BYTE*>(&rv),
84
throw DL_ABORT_EX("Failed to initialize hash(2)");
86
::CryptDestroyHash(hash);
97
class MessageDigestBase : public MessageDigestImpl {
104
::CryptDestroyHash(hash_);
110
MessageDigestBase() : hash_(0), len_(0) { reset(); }
111
virtual ~MessageDigestBase() { destroy(); }
113
static size_t length() {
114
MessageDigestBase<id> rv;
115
return rv.getDigestLength();
117
virtual size_t getDigestLength() const CXX11_OVERRIDE {
120
virtual void reset() CXX11_OVERRIDE {
122
if (!::CryptCreateHash(context_.get(), id, 0, 0, &hash_)) {
123
throw DL_ABORT_EX("Failed to create hash");
125
DWORD len = sizeof(len_);
126
if (!::CryptGetHashParam(hash_, HP_HASHSIZE, reinterpret_cast<BYTE*>(&len_),
128
throw DL_ABORT_EX("Failed to initialize hash");
131
virtual void update(const void* data, size_t length) CXX11_OVERRIDE {
132
auto bytes = reinterpret_cast<const unsigned char*>(data);
134
DWORD l = std::min(length, (size_t)std::numeric_limits<uint32_t>::max());
135
if (!::CryptHashData(hash_, bytes, l, 0)) {
136
throw DL_ABORT_EX("Failed to update hash");
142
virtual void digest(unsigned char* md) CXX11_OVERRIDE {
144
if (!::CryptGetHashParam(hash_, HP_HASHVAL, md, &len, 0)) {
145
throw DL_ABORT_EX("Failed to create hash digest");
150
typedef MessageDigestBase<CALG_MD5> MessageDigestMD5;
151
typedef MessageDigestBase<CALG_SHA1> MessageDigestSHA1;
152
typedef MessageDigestBase<CALG_SHA_256> MessageDigestSHA256;
153
typedef MessageDigestBase<CALG_SHA_384> MessageDigestSHA384;
154
typedef MessageDigestBase<CALG_SHA_512> MessageDigestSHA512;
156
std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
158
return std::unique_ptr<MessageDigestImpl>(new MessageDigestSHA1());
162
MessageDigestImpl::hashes_t initialize() {
163
MessageDigestImpl::hashes_t rv = {
164
{ "sha-1", MessageDigestImpl::make_hi<MessageDigestSHA1>() },
165
{ "md5", MessageDigestImpl::make_hi<MessageDigestMD5>() },
169
rv.insert({ "sha-256", MessageDigestImpl::make_hi<MessageDigestSHA256>() });
171
catch (RecoverableException &ex) {
172
printf("SHA-256 is not supported on this machine");
175
rv.insert({ "sha-384", MessageDigestImpl::make_hi<MessageDigestSHA384>() });
177
catch (RecoverableException &ex) {
178
printf("SHA-384 is not supported on this machine");
181
rv.insert({ "sha-512", MessageDigestImpl::make_hi<MessageDigestSHA512>() });
183
catch (RecoverableException &ex) {
184
printf("SHA-512 is not supported on this machine");
191
MessageDigestImpl::hashes_t MessageDigestImpl::hashes = initialize();