Skip to content

Commit

Permalink
Add OpenSSL hash algorithm replacements
Browse files Browse the repository at this point in the history
Will need reorganizing
  • Loading branch information
calccrypto committed Mar 16, 2019
1 parent ac2426f commit 061db39
Show file tree
Hide file tree
Showing 57 changed files with 1,101 additions and 311 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ if (USE_OPENSSL)
add_compile_options("-DOPENSSL")
else()
message(STATUS "Could not find OpenSSL. Using custom cryptographic algorithm implementation.")
set(USE_OPENSSL OFF)
endif()
endif()

Expand All @@ -102,6 +103,7 @@ add_library(OpenPGP_static STATIC
$<TARGET_OBJECTS:Compress>
$<TARGET_OBJECTS:Encryptions>
$<TARGET_OBJECTS:Hashes>
$<TARGET_OBJECTS:HashAlgs>
$<TARGET_OBJECTS:Misc>
$<TARGET_OBJECTS:Packets>
$<TARGET_OBJECTS:PKA>
Expand All @@ -119,6 +121,7 @@ add_library(OpenPGP_shared SHARED
$<TARGET_OBJECTS:Compress>
$<TARGET_OBJECTS:Encryptions>
$<TARGET_OBJECTS:Hashes>
$<TARGET_OBJECTS:HashAlgs>
$<TARGET_OBJECTS:Misc>
$<TARGET_OBJECTS:Packets>
$<TARGET_OBJECTS:PKA>
Expand Down
24 changes: 14 additions & 10 deletions include/Hashes/HashAlg.h → include/Hashes/Alg.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Hash.h
Alg.h
Base class for inheritance
Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com
Expand Down Expand Up @@ -28,14 +28,18 @@ THE SOFTWARE.

#include "common/includes.h"

class HashAlg{
public:
HashAlg();
virtual ~HashAlg();
virtual std::string hexdigest() = 0;
std::string digest();
virtual void update(const std::string & str) = 0;
virtual std::size_t digestsize() const = 0; // digest size in bits
};
namespace OpenPGP {
namespace Hash {
class Alg{
public:
Alg();
virtual ~Alg();
virtual std::string hexdigest() = 0;
std::string digest();
virtual void update(const std::string & str) = 0;
virtual std::size_t digestsize() const = 0; // digest size in bits
};
}
}

#endif
37 changes: 22 additions & 15 deletions include/Hashes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
cmake_minimum_required(VERSION 3.6.0)

install(FILES
HashAlg.h
Alg.h
Hashes.h
MD5_Const.h
MD5.h
MerkleDamgard.h
RIPEMD160_Const.h
RIPEMD160.h
RIPEMD_Const.h
SHA1.h
SHA224.h
SHA256_Const.h
SHA256.h
SHA2_Functions.h
SHA384.h
SHA512_Const.h
SHA512.h

DESTINATION include/Hashes)

if (NOT USE_OPENSSL)
install(FILES
MD5_Const.h
MD5.h
RIPEMD160_Const.h
RIPEMD160.h
RIPEMD_Const.h
SHA1.h
SHA224.h
SHA256_Const.h
SHA256.h
SHA2_Functions.h
SHA384.h
SHA512_Const.h
SHA512.h

DESTINATION include/Hashes)
else()
add_subdirectory(OpenSSL)
endif()
32 changes: 21 additions & 11 deletions include/Hashes/Hashes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,30 @@ THE SOFTWARE.

#include <map>
#include <memory>
#include <stdexcept>

#include "HashAlg.h"

#include "MD5.h"
#include "RIPEMD160.h"
#include "SHA1.h"
#include "SHA256.h"
#include "SHA224.h"
#include "SHA512.h"
#include "SHA384.h"
#include "Hashes/Alg.h"

#ifdef OPENSSL
#include "Hashes/OpenSSL/MD5.h"
#include "Hashes/OpenSSL/RIPEMD160.h"
#include "Hashes/OpenSSL/SHA1.h"
#include "Hashes/OpenSSL/SHA256.h"
#include "Hashes/OpenSSL/SHA224.h"
#include "Hashes/OpenSSL/SHA512.h"
#include "Hashes/OpenSSL/SHA384.h"
#else
#include "Hashes/MD5.h"
#include "Hashes/RIPEMD160.h"
#include "Hashes/SHA1.h"
#include "Hashes/SHA256.h"
#include "Hashes/SHA224.h"
#include "Hashes/SHA512.h"
#include "Hashes/SHA384.h"
#endif

namespace OpenPGP {
namespace Hash {

// 9.4. Hash Algorithms
//
// ID Algorithm Text Name
Expand Down Expand Up @@ -130,7 +140,7 @@ namespace OpenPGP {

std::string use(const uint8_t alg, const std::string & data = "");

typedef std::shared_ptr <HashAlg> Instance;
typedef std::shared_ptr <Alg> Instance;
Instance get_instance(const uint8_t alg, const std::string & data = "");
}
}
Expand Down
56 changes: 30 additions & 26 deletions include/Hashes/MD5.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,36 @@ THE SOFTWARE.

#include "MD5_Const.h"

class MD5 : public MerkleDamgard {
private:
struct context{
uint32_t h0, h1, h2, h3;
context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3) :
h0(h0),
h1(h1),
h2(h2),
h3(h3)
{}
~context(){
h0 = h1 = h2 = h3 = 0;
}
namespace OpenPGP {
namespace Hash {
class MD5 : public MerkleDamgard {
private:
struct context{
uint32_t h0, h1, h2, h3;
context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3) :
h0(h0),
h1(h1),
h2(h2),
h3(h3)
{}
~context(){
h0 = h1 = h2 = h3 = 0;
}
};
context ctx;

std::string to_little_end(const std::string & data) const;
void calc(const std::string & data, context & state) const;

public:
MD5();
MD5(const std::string & data);
void update(const std::string & data);
std::string hexdigest();
std::size_t blocksize() const;
std::size_t digestsize() const;
};
context ctx;

std::string to_little_end(const std::string & data) const;
void calc(const std::string & data, context & state) const;

public:
MD5();
MD5(const std::string & data);
void update(const std::string & data);
std::string hexdigest();
std::size_t blocksize() const;
std::size_t digestsize() const;
};
}
}

#endif
30 changes: 17 additions & 13 deletions include/Hashes/MD5_Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ THE SOFTWARE.
#ifndef __MD5_CONST__
#define __MD5_CONST__

const uint8_t MD5_R[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};

const uint32_t MD5_K[64] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
namespace OpenPGP {
namespace Hash {
const uint8_t MD5_R[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};

const uint32_t MD5_K[64] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
}
}

#endif
28 changes: 16 additions & 12 deletions include/Hashes/MerkleDamgard.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ THE SOFTWARE.
#ifndef __MERKLE_DAMGARD__
#define __MERKLE_DAMGARD__

#include "HashAlg.h"

class MerkleDamgard : public HashAlg {
protected:
std::string stack;
uint64_t clen;

public:
MerkleDamgard();
virtual ~MerkleDamgard();
virtual std::size_t blocksize() const = 0; // blocksize in bits
};
#include "Hashes/Alg.h"

namespace OpenPGP {
namespace Hash {
class MerkleDamgard : public Alg {
protected:
std::string stack;
uint64_t clen;

public:
MerkleDamgard();
virtual ~MerkleDamgard();
virtual std::size_t blocksize() const = 0; // blocksize in bits
};
}
}

#endif
14 changes: 14 additions & 0 deletions include/Hashes/OpenSSL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.6.0)

if (USE_OPENSSL)
install(FILES
MD5.h
RIPEMD160.h
SHA1.h
SHA224.h
SHA256.h
SHA384.h
SHA512.h

DESTINATION include/Hashes)
endif()
50 changes: 50 additions & 0 deletions include/Hashes/OpenSSL/MD5.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
MD5.h
MD5 hashing algorithm
Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef __OPENSSL_MD5__
#define __OPENSSL_MD5__

#include <openssl/md5.h>

#include "Hashes/MerkleDamgard.h"

namespace OpenPGP {
namespace Hash {
class MD5 : public MerkleDamgard {
private:
MD5_CTX ctx;

public:
MD5();
MD5(const std::string & data);
void update(const std::string & data);
std::string hexdigest();
std::size_t blocksize() const;
std::size_t digestsize() const;
};
}
}

#endif
Loading

0 comments on commit 061db39

Please sign in to comment.