diff options
author | Kevin Smith <git@kismith.co.uk> | 2012-02-22 11:00:19 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2012-02-28 16:02:29 (GMT) |
commit | 0e4f068273ecaa2be24a046812893698a06481bc (patch) | |
tree | 9c3b7dbd3609a866c2123ea0c5a539b5c49d67dd /Swiften/StringCodecs/SHA1_Windows.cpp | |
parent | eca0f020873f7620c5125101113e2c1eb25b273e (diff) | |
download | swift-contrib-0e4f068273ecaa2be24a046812893698a06481bc.zip swift-contrib-0e4f068273ecaa2be24a046812893698a06481bc.tar.bz2 |
Make Swift more usable in a FIPS-140 environment
Don't allow DIGEST-MD5 when Windows is set to FIPS mode. Use
platform-provided hashing for SHA1.
Diffstat (limited to 'Swiften/StringCodecs/SHA1_Windows.cpp')
-rw-r--r-- | Swiften/StringCodecs/SHA1_Windows.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/Swiften/StringCodecs/SHA1_Windows.cpp b/Swiften/StringCodecs/SHA1_Windows.cpp new file mode 100644 index 0000000..8bd3dd2 --- /dev/null +++ b/Swiften/StringCodecs/SHA1_Windows.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2012 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + + +//http://msdn.microsoft.com/en-us/library/aa379908.aspx + +#include <Swiften/StringCodecs/SHA1_Windows.h> + +namespace Swift { + +SHA1::SHA1() : hCryptProv(NULL), hHash(NULL) { + bool hasContext = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + if (!hasContext) { +// DWORD error = GetLastError(); +// switch (error) { +// std::cerr << (long)error << std::endl; +// } +// assert(false); + hCryptProv = NULL; + } + + if (!CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hHash)) { + hHash = NULL; + } +} + +SHA1::~SHA1() { + if(hHash) { + CryptDestroyHash(hHash); + } + if(hCryptProv) { + CryptReleaseContext(hCryptProv,0); + } + +} + +SHA1& SHA1::update(const std::vector<unsigned char>& data) { + return update(vecptr(data), data.size()); +} + + +SHA1& SHA1::update(const unsigned char* data, size_t dataSize) { + if (!hHash || !hCryptProv) { + return *this; + } + BYTE* byteData = (BYTE *)data; + DWORD dataLength = dataSize; + bool hasHashed = CryptHashData(hHash, byteData, dataLength, 0); +// if (!hasHashed) { +// DWORD error = GetLastError(); +// switch (error) { +// std::cerr << (long)error << std::endl; +// } +// assert(false); +// } + return *this; +} + +std::vector<unsigned char> SHA1::getHash() const { + if (!hHash || !hCryptProv) { + return std::vector<unsigned char>(); + } + std::vector<unsigned char> result; + DWORD hashLength = sizeof(DWORD); + DWORD hashSize; + CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&hashSize, &hashLength, 0); + result.resize(static_cast<size_t>(hashSize)); + bool hasHashed = CryptGetHashParam(hHash, HP_HASHVAL, (BYTE*)vecptr(result), &hashSize, 0); + if (!hasHashed) { +// DWORD error = GetLastError(); +// switch (error) { +// std::cerr << (long)error << std::endl; +// } +// assert(false); + return std::vector<unsigned char>(); + } + result.resize(static_cast<size_t>(hashSize)); + return result; +} + + +ByteArray SHA1::getHash(const ByteArray& data) { + SHA1 hash; + hash.update(vecptr(data), data.size()); + return hash.getHash(); +} + +ByteArray SHA1::getHash(const SafeByteArray& data) { + SHA1 hash; + hash.update(vecptr(data), data.size()); + return hash.getHash(); +} + +} |