8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-23 04:03:04 +01:00

SHA2 Refactoring

This commit is contained in:
ibprovider 2019-06-03 19:08:04 +03:00 committed by Alexander Peshkov
parent b0c5f20341
commit 40bc3c37d2
2 changed files with 750 additions and 818 deletions

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,7 @@
* 3. Neither the name of the project nor the names of its contributors * 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* Updated for use in Firebird by Tony Whyman <tony@mwasoftware.co.uk> * Updated for use in Firebird by Tony Whyman <tony@mwasoftware.co.uk>
* *
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
@ -33,14 +33,14 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
/* /*
* This update is intended to make available the SHA-2 family of message * This update is intended to make available the SHA-2 family of message
* digests as C++ classes for use in Firebird. sha224, sha256, sha384 and * digests as C++ classes for use in Firebird. sha224, sha256, sha384 and
* sha512 are each implemented as separate classes. The class methods are * sha512 are each implemented as separate classes. The class methods are
* intended to be as similar as possible to the legacy class sha1 in order * intended to be as similar as possible to the legacy class sha1 in order
* to facilitate straightforward replacement. * to facilitate straightforward replacement.
* *
* This implementation also comes with a NIST compliancy test for each * This implementation also comes with a NIST compliancy test for each
* digest. This is enabled by building with the NIST_COMPLIANCY_TESTS symbol * digest. This is enabled by building with the NIST_COMPLIANCY_TESTS symbol
* defined. * defined.
@ -71,15 +71,15 @@
#define SHA224_BLOCK_SIZE SHA256_BLOCK_SIZE #define SHA224_BLOCK_SIZE SHA256_BLOCK_SIZE
namespace Firebird { namespace Firebird {
/* This template function provides a simple one line means of computing a SHA-2 /* This template function provides a simple one line means of computing a SHA-2
* digest from an arbitrary length message. * digest from an arbitrary length message.
*/ */
template<class SHA>void get_digest(const unsigned char *message, size_t len, unsigned char *digest) template<class SHA>void get_digest(const unsigned char* message, size_t len, unsigned char* digest)
{ {
SHA sha; SHA sha;
sha.process(len, message); sha.process(len, message);
sha.getHash(digest); sha.getHash(digest);
} }
@ -100,171 +100,173 @@ template<class SHA> void hashBased64(Firebird::string& hash, const Firebird::str
/* The sha2_base class is an abstract class that is the ancestor for all /* The sha2_base class is an abstract class that is the ancestor for all
* the SHA-2 classes. It defines all public methods for the classes and * the SHA-2 classes. It defines all public methods for the classes and
* a common model of use. * a common model of use.
* *
* When instatiated a SHA-2 class is already initialized for use. The message * When instatiated a SHA-2 class is already initialized for use. The message
* for which a digest is required is then fed to the class using one of * for which a digest is required is then fed to the class using one of
* the "process" methods, either as a single action or accumulatively. * the "process" methods, either as a single action or accumulatively.
* *
* When the entire message has been input, the resulting digest is returned * When the entire message has been input, the resulting digest is returned
* by a "getHash" method. Calling "getHash" also clears the digest and * by a "getHash" method. Calling "getHash" also clears the digest and
* re-initializes the SHA-2 generator ready to compute a new digest. * re-initializes the SHA-2 generator ready to compute a new digest.
* *
* A SHA-2 generator can be cleared down and re-initialized at any time * A SHA-2 generator can be cleared down and re-initialized at any time
* by calling the "reset" method. * by calling the "reset" method.
*/ */
class sha224_traits;
class sha256_traits;
class sha384_traits;
class sha512_traits;
class sha2_types
{
public:
typedef unsigned char uint8;
typedef unsigned int uint32;
typedef unsigned long long uint64;
};
template<class SHA_TRAITS>
class sha2_base : public GlobalStorage { class sha2_base : public GlobalStorage {
#else #else
class sha2_base { class sha2_base {
#endif #endif
private:
sha2_base(const sha2_base&);
sha2_base& operator = (const sha2_base&);
public: public:
sha2_base() {}; sha2_base()
{
SHA_TRAITS::sha_init(&m_ctx);
}
virtual ~sha2_base() {}; virtual ~sha2_base() {};
virtual const unsigned int get_DigestSize()=0;
virtual const unsigned int get_BlockSize()=0;
typedef unsigned char uint8;
typedef unsigned int uint32;
typedef unsigned long long uint64;
protected:
virtual void sha_init() {};
virtual void sha_update(const unsigned char *message, unsigned int len)=0;
virtual void sha_final(unsigned char *digest)=0;
public: public:
void reset() {sha_init();}; void reset()
{
SHA_TRAITS::sha_init(&m_ctx);
}
void process(size_t length, const void* bytes) void process(size_t length, const void* bytes)
{ {
sha_update(static_cast<const unsigned char*>(bytes), length); SHA_TRAITS::sha_update(&m_ctx, static_cast<const unsigned char*>(bytes), length);
} }
void process(size_t length, const unsigned char* message) void process(size_t length, const unsigned char* message)
{ {
sha_update(message, length); SHA_TRAITS::sha_update(&m_ctx, message, length);
} }
void process(const char* str) void process(const char* str)
{ {
process(strlen(str), str); SHA_TRAITS::sha_update(&m_ctx, reinterpret_cast<const unsigned char*>(str), strlen(str));
}
void getHash(unsigned char* digest)
{
SHA_TRAITS::sha_final(&m_ctx, digest);
SHA_TRAITS::sha_init(&m_ctx);
} }
void getHash(unsigned char *digest);
#ifndef NIST_COMPLIANCY_TESTS #ifndef NIST_COMPLIANCY_TESTS
void process(const UCharBuffer& bytes) void process(const UCharBuffer& bytes)
{ {
process(bytes.getCount(), bytes.begin()); SHA_TRAITS::sha_update(&m_ctx, bytes.begin(), bytes.getCount());
}
void getHash(UCharBuffer& h)
{
SHA_TRAITS::sha_final(&m_ctx, h.getBuffer(SHA_TRAITS::get_DigestSize()));
SHA_TRAITS::sha_init(&m_ctx);
} }
void getHash(UCharBuffer& h);
#endif #endif
private:
typename SHA_TRAITS::sha_ctx m_ctx;
}; };
class sha256 : public sha2_base { typedef sha2_base<sha224_traits> sha224;
public: typedef sha2_base<sha256_traits> sha256;
sha256(); typedef sha2_base<sha384_traits> sha384;
const unsigned int get_DigestSize() {return SHA256_DIGEST_SIZE;}; typedef sha2_base<sha512_traits> sha512;
const unsigned int get_BlockSize() {return SHA256_BLOCK_SIZE;};
struct sha256_ctx {
protected: unsigned int tot_len;
typedef struct { unsigned int len;
unsigned int tot_len; unsigned char block[2 * SHA256_BLOCK_SIZE];
unsigned int len; sha2_types::uint32 h[8];
unsigned char block[2 * SHA256_BLOCK_SIZE];
uint32 h[8]; void transf(const unsigned char* message, unsigned int block_nb);
} sha256_ctx;
private:
void sha256_init(sha256_ctx * ctx);
void sha256_update(sha256_ctx *ctx, const unsigned char *message,
unsigned int len);
void sha256_final(sha256_ctx *ctx, unsigned char *digest);
protected:
sha256_ctx ctx;
void sha256_transf(sha256_ctx * ctx, const unsigned char *message,
unsigned int block_nb);
void sha_init() {sha256_init(&ctx);};
void sha_update(const unsigned char *message, unsigned int len) {sha256_update(&ctx,message,len);};
void sha_final(unsigned char *digest) {sha256_final(&ctx,digest);};
}; };
class sha224 : public sha256 { class sha256_traits: private sha2_types {
public: public:
sha224(); typedef sha256_ctx sha_ctx;
const unsigned int get_DigestSize() {return SHA224_DIGEST_SIZE;};
const unsigned int get_BlockSize() {return SHA224_BLOCK_SIZE;};
private:
typedef sha256_ctx sha224_ctx;
void sha224_init(sha224_ctx *ctx);
void sha224_update(sha224_ctx *ctx, const unsigned char *message,
unsigned int len);
void sha224_final(sha224_ctx *ctx, unsigned char *digest);
protected: public:
void sha_init() {sha224_init(&ctx);}; static unsigned int get_DigestSize() {return SHA256_DIGEST_SIZE;};
void sha_update(const unsigned char *message, unsigned int len) {sha224_update(&ctx, message, len);};
void sha_final(unsigned char *digest) {sha224_final(&ctx,digest);}; static void sha_init(sha_ctx* ctx);
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
static void sha_final(sha_ctx *ctx, unsigned char* digest);
}; };
class sha512 : public sha2_base { class sha224_traits: private sha2_types {
public: public:
sha512(); typedef sha256_ctx sha_ctx;
const unsigned int get_DigestSize() {return SHA512_DIGEST_SIZE;};
const unsigned int get_BlockSize() {return SHA512_BLOCK_SIZE;};
protected:
typedef struct {
unsigned int tot_len;
unsigned int len;
unsigned char block[2 * SHA512_BLOCK_SIZE];
uint64 h[8];
} sha512_ctx;
private:
void sha512_init(sha512_ctx *ctx);
void sha512_update(sha512_ctx *ctx, const unsigned char *message,
unsigned int len);
void sha512_final(sha512_ctx *ctx, unsigned char *digest);
protected:
sha512_ctx ctx;
void sha512_transf(sha512_ctx *ctx, const unsigned char *message, public:
unsigned int block_nb); static unsigned int get_DigestSize() {return SHA224_DIGEST_SIZE;};
void sha_init() {sha512_init(&ctx);};
void sha_update(const unsigned char *message, unsigned int len) {sha512_update(&ctx, message, len);}; static void sha_init(sha_ctx* ctx);
void sha_final(unsigned char *digest) {sha512_final(&ctx, digest);};
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
static void sha_final(sha_ctx* ctx, unsigned char* digest);
};
struct sha512_ctx{
unsigned int tot_len;
unsigned int len;
unsigned char block[2 * SHA512_BLOCK_SIZE];
sha2_types::uint64 h[8];
void transf(const unsigned char* message, unsigned int block_nb);
}; };
class sha384 : public sha512 { class sha512_traits: private sha2_types {
public: public:
sha384(); typedef sha512_ctx sha_ctx;
const unsigned int get_DigestSize() {return SHA384_DIGEST_SIZE;};
const unsigned int get_BlockSize() {return SHA384_BLOCK_SIZE;};
private:
typedef sha512_ctx sha384_ctx;
void sha384_init(sha384_ctx *ctx);
void sha384_update(sha384_ctx *ctx, const unsigned char *message,
unsigned int len);
void sha384_final(sha384_ctx *ctx, unsigned char *digest);
protected: public:
void sha_init() {sha384_init(&ctx);}; static unsigned int get_DigestSize() {return SHA512_DIGEST_SIZE;};
void sha_update(const unsigned char *message, unsigned int len) {sha384_update(&ctx,message,len);};
void sha_final(unsigned char *digest) {sha384_final(&ctx, digest);};
static void sha_init(sha_ctx* ctx);
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
static void sha_final(sha_ctx* ctx, unsigned char* digest);
};
class sha384_traits: private sha2_types {
public:
typedef sha512_ctx sha_ctx;
public:
static unsigned int get_DigestSize() {return SHA384_DIGEST_SIZE;};
static void sha_init(sha_ctx* ctx);
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
static void sha_final(sha_ctx* ctx, unsigned char* digest);
}; };
} //Firebird } //Firebird
#endif /* !_SHA2_H */ #endif /* !_SHA2_H */