From 987e85f676a70608a7b044ba84be565cf259b5f5 Mon Sep 17 00:00:00 2001 From: Adriano dos Santos Fernandes Date: Fri, 21 Jul 2017 15:22:17 +0000 Subject: [PATCH] Added wrapper classes for libtomcrypt's hash algorithms. --- src/common/classes/Hash.h | 47 ++++++++++++++ src/common/classes/TomCryptHash.cpp | 96 +++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/common/classes/TomCryptHash.cpp diff --git a/src/common/classes/Hash.h b/src/common/classes/Hash.h index b0ee25052f..76501e7cc2 100644 --- a/src/common/classes/Hash.h +++ b/src/common/classes/Hash.h @@ -366,6 +366,53 @@ namespace Firebird private: SINT64 hashNumber = 0; }; + + class LibTomCryptHashContext : public HashContext + { + public: + struct Descriptor; + + private: + struct State; + + protected: + LibTomCryptHashContext(MemoryPool& pool, const Descriptor* descriptor); + + public: + virtual ~LibTomCryptHashContext(); + + public: + virtual void update(const void* data, FB_SIZE_T length); + virtual void finish(Buffer& result); + + private: + const Descriptor* descriptor; + State* statePtr; + }; + + class Md5HashContext FB_FINAL : public LibTomCryptHashContext + { + public: + Md5HashContext(MemoryPool& pool); + }; + + class Sha1HashContext FB_FINAL : public LibTomCryptHashContext + { + public: + Sha1HashContext(MemoryPool& pool); + }; + + class Sha256HashContext FB_FINAL : public LibTomCryptHashContext + { + public: + Sha256HashContext(MemoryPool& pool); + }; + + class Sha512HashContext FB_FINAL : public LibTomCryptHashContext + { + public: + Sha512HashContext(MemoryPool& pool); + }; } // namespace Firebird #endif // CLASSES_HASH_H diff --git a/src/common/classes/TomCryptHash.cpp b/src/common/classes/TomCryptHash.cpp new file mode 100644 index 0000000000..14fba11623 --- /dev/null +++ b/src/common/classes/TomCryptHash.cpp @@ -0,0 +1,96 @@ +/* + * Hashing using libtomcrypt library. + * + * The contents of this file are subject to the Initial + * Developer's Public License Version 1.0 (the "License"); + * you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl. + * + * Software distributed under the License is distributed AS IS, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. + * See the License for the specific language governing rights + * and limitations under the License. + * + * The Original Code was created by Adriano dos Santos Fernandes + * for the Firebird Open Source RDBMS project. + * + * Copyright (c) 2017 Adriano dos Santos Fernandes + * and all contributors signed below. + * + * All Rights Reserved. + * Contributor(s): ______________________________________. + */ + +#include "firebird.h" +#include "../common/classes/Hash.h" +#include "../common/tomcrypt/tomcrypt.h" + +using namespace Firebird; + + +struct LibTomCryptHashContext::Descriptor +{ + const ltc_hash_descriptor* tcDesc; +}; + +struct LibTomCryptHashContext::State +{ + hash_state tcState; +}; + + +LibTomCryptHashContext::LibTomCryptHashContext(MemoryPool& pool, const Descriptor* aDescriptor) + : descriptor(aDescriptor) +{ + statePtr = FB_NEW_POOL(pool) State(); + descriptor->tcDesc->init(&statePtr->tcState); +} + +LibTomCryptHashContext::~LibTomCryptHashContext() +{ + delete statePtr; +} + +void LibTomCryptHashContext::update(const void* data, FB_SIZE_T length) +{ + descriptor->tcDesc->process(&statePtr->tcState, static_cast(data), length); +} + +void LibTomCryptHashContext::finish(Buffer& result) +{ + unsigned char* hashResult = result.getBuffer(descriptor->tcDesc->hashsize); + descriptor->tcDesc->done(&statePtr->tcState, hashResult); +} + + +static LibTomCryptHashContext::Descriptor md5Descriptor{&md5_desc}; + +Md5HashContext::Md5HashContext(MemoryPool& pool) + : LibTomCryptHashContext(pool, &md5Descriptor) +{ +} + + +static LibTomCryptHashContext::Descriptor sha1Descriptor{&sha1_desc}; + +Sha1HashContext::Sha1HashContext(MemoryPool& pool) + : LibTomCryptHashContext(pool, &sha1Descriptor) +{ +} + + +static LibTomCryptHashContext::Descriptor sha256Descriptor{&sha256_desc}; + +Sha256HashContext::Sha256HashContext(MemoryPool& pool) + : LibTomCryptHashContext(pool, &sha256Descriptor) +{ +} + + +static LibTomCryptHashContext::Descriptor sha512Descriptor{&sha512_desc}; + +Sha512HashContext::Sha512HashContext(MemoryPool& pool) + : LibTomCryptHashContext(pool, &sha512Descriptor) +{ +}