[-] Redundant AuCrypto::EHashType (replaced with the complete AuHashing::EHashType)

[*] Minor RSA refactor
This commit is contained in:
Reece Wilson 2022-09-25 10:56:03 +01:00
parent 0ded2ec77c
commit cae8788bb6
27 changed files with 138 additions and 106 deletions

View File

@ -19,7 +19,7 @@ namespace Aurora::Crypto::CA
public:
virtual void AddSignature(const AuSPtr<RSA::IRSAPublic> &CA,
const Memory::ByteBuffer &sig,
EHashType method,
Aurora::Hashing::EHashType method,
EPaddingType type) = 0;
virtual void AddPublicCert(const X509::Certificate &cert) = 0;

View File

@ -23,7 +23,11 @@ namespace Aurora::Crypto
};
}
#if 0
#include "EHashType.hpp"
#else
#include <Aurora/Hashing/EHashType.hpp>
#endif
#include "EKeyType.hpp"
#include "EPaddingType.hpp"
#include "AES/AES.hpp"

View File

@ -12,7 +12,7 @@ namespace Aurora::Crypto::ECC
struct IECCPrivate
{
virtual bool Sign(const Memory::MemoryViewRead &plainText,
EHashType method,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out) = 0;
virtual bool Sign(const Memory::MemoryViewRead &hash,

View File

@ -16,7 +16,7 @@ namespace Aurora::Crypto::ECC
virtual bool Verify(const Memory::MemoryViewRead &plaintext,
const Memory::MemoryViewRead &signature,
EHashType method) = 0;
Aurora::Hashing::EHashType method) = 0;
virtual bool AsPublicECC(Memory::ByteBuffer &out) = 0;

View File

@ -9,6 +9,7 @@
namespace Aurora::Crypto
{
#if 0
AUE_DEFINE(EHashType,
(
eMD5,
@ -22,4 +23,5 @@ namespace Aurora::Crypto
eSHA3_48_384,
eSHA3_64_512
));
#endif
}

View File

@ -30,6 +30,6 @@ namespace Aurora::Crypto::HMAC
};
AUKN_SHARED_API(HMAC, IHMACContext,
EHashType algorithm,
Aurora::Hashing::EHashType algorithm,
const Memory::MemoryViewRead &sharedSecret);
}

View File

@ -13,7 +13,7 @@ namespace Aurora::Crypto::RSA
struct IRSAPrivate
{
virtual bool Sign(const Memory::MemoryViewRead &payload,
EHashType method,
Aurora::Hashing::EHashType method,
EPaddingType type,
Memory::ByteBuffer &out) = 0;

View File

@ -14,7 +14,7 @@ namespace Aurora::Crypto::RSA
{
virtual bool Verify(const Memory::MemoryViewRead &plainText,
const Memory::MemoryViewRead &signature,
EHashType method,
Aurora::Hashing::EHashType method,
EPaddingType type) = 0;
virtual bool Encrypt(const Memory::MemoryViewRead &plainText,

View File

@ -13,7 +13,7 @@ namespace Aurora::Crypto::RSA
{
AU_COPY_MOVE_DEF(RSAMeta);
ERSAKeyType type;
EKeyType side;
ERSAKeyType encoding;
EKeyType type;
};
}

View File

@ -30,8 +30,11 @@ namespace Crypto
gHashSha3_384 = register_hash(&sha3_384_desc);
gHashSha3_512 = register_hash(&sha3_512_desc);
gHashMD5 = register_hash(&md5_desc);
register_hash(&rmd128_desc);
register_hash(&rmd160_desc);
gHashMD4 = register_hash(&md4_desc);
gHashRMD128 = register_hash(&rmd128_desc);
gHashRMD160 = register_hash(&rmd160_desc);
gHashRMD256 = register_hash(&rmd256_desc);
gHashRMD320 = register_hash(&rmd320_desc);
gPrngYarrow = register_prng(&yarrow_desc);
gAesCipher = register_cipher(&aes_desc);
}
@ -46,4 +49,63 @@ namespace Crypto
TomCryptInit();
MBedTlsInit();
}
int PaddingToType(Aurora::Crypto::EPaddingType type)
{
switch (type)
{
case Aurora::Crypto::EPaddingType::ePaddingNone:
return 0;
case Aurora::Crypto::EPaddingType::ePKCS_1_5:
return LTC_PKCS_1_V1_5;
case Aurora::Crypto::EPaddingType::ePKCS_1_5_NA1:
return LTC_PKCS_1_V1_5_NA1;
case Aurora::Crypto::EPaddingType::ePKCS_OAEP:
return LTC_PKCS_1_OAEP;
case Aurora::Crypto::EPaddingType::ePKCS_1_PSS:
return LTC_PKCS_1_PSS;
default:
return 0xFF;
}
}
int HashMethodToId(Aurora::Hashing::EHashType type)
{
switch (type)
{
case AuHashing::EHashType::eMD4:
return gHashMD4;
case AuHashing::EHashType::eMD5:
return gHashMD5;
case AuHashing::EHashType::eRMD128:
return gHashRMD128;
case AuHashing::EHashType::eRMD160:
return gHashRMD160;
case AuHashing::EHashType::eRMD256:
return gHashRMD256;
case AuHashing::EHashType::eRMD320:
return gHashRMD320;
case AuHashing::EHashType::eSHA1:
return gHashSha1;
case AuHashing::EHashType::eSHA2_32:
return gHashSha256;
case AuHashing::EHashType::eSHA2_64:
return gHashSha512;
case AuHashing::EHashType::eSHA2_48:
return gHashSha384;
case AuHashing::EHashType::eTiger:
return gHashTiger;
case AuHashing::EHashType::eSHA3_28:
return gHashSha3_224;
case AuHashing::EHashType::eSHA3_32:
return gHashSha3_256;
case AuHashing::EHashType::eSHA3_48:
return gHashSha3_384;
case AuHashing::EHashType::eSHA3_64:
return gHashSha3_512;
default:
return 0xFF;
}
}
}

View File

@ -12,6 +12,7 @@ namespace Crypto
inline int gAesCipher;
inline int gHashTiger;
inline int gHashSha1;
inline int gHashMD4;
inline int gHashMD5;
inline int gHashSha256;
inline int gHashSha384;
@ -21,6 +22,15 @@ namespace Crypto
inline int gHashSha3_224;
inline int gHashSha3_512;
inline int gPrngYarrow;
inline int gHashRMD128;
inline int gHashRMD160;
inline int gHashRMD256;
inline int gHashRMD320;
void InitCrypto();
int PaddingToType(Aurora::Crypto::EPaddingType type);
int HashMethodToId(Aurora::Hashing::EHashType type);
}

View File

@ -13,51 +13,5 @@
namespace Aurora::Crypto
{
static int PaddingToType(EPaddingType type)
{
switch (type)
{
case EPaddingType::ePaddingNone:
return 0;
case EPaddingType::ePKCS_1_5:
return LTC_PKCS_1_V1_5;
case EPaddingType::ePKCS_1_5_NA1:
return LTC_PKCS_1_V1_5_NA1;
case EPaddingType::ePKCS_OAEP:
return LTC_PKCS_1_OAEP;
case EPaddingType::ePKCS_1_PSS:
return LTC_PKCS_1_PSS;
default:
return 0xFF;
}
}
static int HashMethodToId(EHashType type)
{
switch (type)
{
case EHashType::eTiger_24_192:
return ::Crypto::gHashTiger;
case EHashType::eSHA1_20_160:
return ::Crypto::gHashSha1;
case EHashType::eSHA2_32_256:
return ::Crypto::gHashSha256;
case EHashType::eSHA2_48_384:
return ::Crypto::gHashSha384;
case EHashType::eSHA2_64_512:
return ::Crypto::gHashSha512;
case EHashType::eSHA3_28_224:
return ::Crypto::gHashSha3_224;
case EHashType::eSHA3_32_256:
return ::Crypto::gHashSha3_256;
case EHashType::eSHA3_48_384:
return ::Crypto::gHashSha3_384;
case EHashType::eSHA3_64_512:
return ::Crypto::gHashSha3_512;
case EHashType::eMD5:
return ::Crypto::gHashMD5;
default:
return 0xFF;
}
}
}

View File

@ -23,7 +23,7 @@ namespace Aurora::Crypto::ECC
}
bool PrivateCurve25519Impl::Sign(const Memory::MemoryViewRead &plainText,
EHashType method,
AuHashing::EHashType method,
Memory::ByteBuffer &out)
{
const int salt = 0;
@ -39,7 +39,7 @@ namespace Aurora::Crypto::ECC
return {};
}
int hash = HashMethodToId(method);
int hash = ::Crypto::HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);

View File

@ -15,7 +15,7 @@ namespace Aurora::Crypto::ECC
~PrivateCurve25519Impl();
bool Sign(const Memory::MemoryViewRead &plainText,
EHashType method,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out) override;
bool Sign(const Memory::MemoryViewRead &hash,

View File

@ -11,7 +11,7 @@
namespace Aurora::Crypto::ECC
{
PublicCurve25519Impl::PublicCurve25519Impl(bool isX25519, curve25519_key &&key) : key_(key), isX25519_(isX25519)
PublicCurve25519Impl::PublicCurve25519Impl(bool isX25519, curve25519_key &&key) : key_(key), bIsX25519_(isX25519)
{
}
@ -25,7 +25,7 @@ namespace Aurora::Crypto::ECC
{
int ok = 0;
if (this->isX25519_)
if (this->bIsX25519_)
{
return false;
}
@ -42,12 +42,12 @@ namespace Aurora::Crypto::ECC
return {};
}
auto ret = ed25519_verify(reinterpret_cast<const unsigned char *>(hash.ptr), hash.length,
reinterpret_cast<const unsigned char *>(signature.ptr), signature.length,
auto iRet = ed25519_verify(AuReinterpretCast<const unsigned char *>(hash.ptr), hash.length,
AuReinterpretCast<const unsigned char *>(signature.ptr), signature.length,
&ok, &key_);
if (ret != CRYPT_OK)
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
@ -56,9 +56,9 @@ namespace Aurora::Crypto::ECC
bool PublicCurve25519Impl::Verify(const Memory::MemoryViewRead &plaintext,
const Memory::MemoryViewRead &signature,
EHashType method)
AuHashing::EHashType method)
{
if (this->isX25519_)
if (this->bIsX25519_)
{
return false;
}
@ -75,7 +75,7 @@ namespace Aurora::Crypto::ECC
return {};
}
int hash = HashMethodToId(method);
int hash = ::Crypto::HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);
@ -90,12 +90,12 @@ namespace Aurora::Crypto::ECC
}
unsigned long hashSize = hashVec.size();
auto ret = hash_memory(hash,
reinterpret_cast<const unsigned char *>(plaintext.ptr), plaintext.length,
reinterpret_cast<unsigned char *>(hashVec.data()), &hashSize);
if (ret != CRYPT_OK)
auto iRet = hash_memory(hash,
AuReinterpretCast<const unsigned char *>(plaintext.ptr), plaintext.length,
AuReinterpretCast<unsigned char *>(hashVec.data()), &hashSize);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
@ -116,7 +116,7 @@ namespace Aurora::Crypto::ECC
actualSize = 4096;
if (this->isX25519_)
if (this->bIsX25519_)
{
ret = ::x25519_export(out.writePtr, &actualSize, PK_PUBLIC, &this->key_);
}
@ -138,7 +138,7 @@ namespace Aurora::Crypto::ECC
EECCCurve PublicCurve25519Impl::GetType()
{
return this->isX25519_ ? EECCCurve::eCurveX25519 : EECCCurve::eCurveEd25519;
return this->bIsX25519_ ? EECCCurve::eCurveX25519 : EECCCurve::eCurveEd25519;
}
const curve25519_key &PublicCurve25519Impl::GetKey()

View File

@ -19,7 +19,7 @@ namespace Aurora::Crypto::ECC
bool Verify(const Memory::MemoryViewRead &plaintext,
const Memory::MemoryViewRead &signature,
EHashType method) override;
Aurora::Hashing::EHashType method) override;
bool AsPublicECC(Memory::ByteBuffer &out) override;
@ -29,6 +29,6 @@ namespace Aurora::Crypto::ECC
private:
curve25519_key key_;
bool isX25519_;
bool bIsX25519_;
};
}

View File

@ -33,7 +33,7 @@ namespace Aurora::Crypto::ECC
}
bool PrivateECCImpl::Sign(const AuMemoryViewRead &plainText,
EHashType method,
AuHashing::EHashType method,
AuByteBuffer &out)
{
const int salt = 0;
@ -44,7 +44,7 @@ namespace Aurora::Crypto::ECC
return {};
}
int hash = HashMethodToId(method);
int hash = ::Crypto::HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);

View File

@ -8,7 +8,7 @@ namespace Aurora::Crypto::ECC
~PrivateECCImpl();
bool Sign(const Memory::MemoryViewRead &plainText,
EHashType method,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out) override;
bool Sign(const Memory::MemoryViewRead &hash,

View File

@ -62,7 +62,7 @@ namespace Aurora::Crypto::ECC
bool PublicECCImpl::Verify(const AuMemoryViewRead &plaintext,
const AuMemoryViewRead &signature,
EHashType method)
AuHashing::EHashType method)
{
if (!plaintext.HasMemory())
{
@ -76,7 +76,7 @@ namespace Aurora::Crypto::ECC
return {};
}
int hash = HashMethodToId(method);
int hash = ::Crypto::HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);

View File

@ -12,7 +12,7 @@ namespace Aurora::Crypto::ECC
bool Verify(const Memory::MemoryViewRead &plaintext,
const Memory::MemoryViewRead &signature,
EHashType method) override;
Aurora::Hashing::EHashType method) override;
bool AsPublicECC(Memory::ByteBuffer &out) override;

View File

@ -11,7 +11,7 @@
namespace Aurora::Crypto::HMAC
{
HMACContext::HMACContext(EHashType type) :
HMACContext::HMACContext(AuHashing::EHashType type) :
type_(type)
{
}
@ -45,7 +45,7 @@ namespace Aurora::Crypto::HMAC
{
int iRet {};
int hash = HashMethodToId(this->type_);
int hash = ::Crypto::HashMethodToId(this->type_);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", this->type_);
@ -63,7 +63,7 @@ namespace Aurora::Crypto::HMAC
return true;
}
AUKN_SYM IHMACContext *HMACNew(EHashType algorithm,
AUKN_SYM IHMACContext *HMACNew(AuHashing::EHashType algorithm,
const Memory::MemoryViewRead &sharedSecret)
{
auto pContext = _new HMACContext(algorithm);

View File

@ -11,7 +11,7 @@ namespace Aurora::Crypto::HMAC
{
struct HMACContext : IHMACContext
{
HMACContext(EHashType type);
HMACContext(AuHashing::EHashType type);
void Ingest(const Memory::MemoryViewRead &input) override;
@ -24,7 +24,7 @@ namespace Aurora::Crypto::HMAC
AuUInt8 buffer_[64] {};
hmac_state state_ {};
hmac_state referenceState_ {};
EHashType type_ {};
AuHashing::EHashType type_ {};
bool bFinished_ {};
};
}

View File

@ -46,9 +46,9 @@ namespace Aurora::Crypto::RSA
{
int flags{};
if (rsakey.meta.type == ERSAKeyType::eCert)
if (rsakey.meta.encoding == ERSAKeyType::eCert)
{
if (rsakey.meta.side == EKeyType::eKeyPrivate)
if (rsakey.meta.type == EKeyType::eKeyPrivate)
{
SysPushErrorArg("Attempted to import a certificate as a private key.");
return false;
@ -64,12 +64,12 @@ namespace Aurora::Crypto::RSA
return true;
}
if (rsakey.meta.type == ERSAKeyType::eRsaKey)
if (rsakey.meta.encoding == ERSAKeyType::eRsaKey)
{
flags |= kRsaFlagPKCS1;
}
if (rsakey.meta.side == EKeyType::eKeyPublic)
if (rsakey.meta.type == EKeyType::eKeyPublic)
{
flags |= kRsaFlagPublic;
}

View File

@ -24,7 +24,7 @@ namespace Aurora::Crypto::RSA
}
bool PrivateRSA::Sign(const Memory::MemoryViewRead & payload,
EHashType method,
AuHashing::EHashType method,
EPaddingType type,
Memory::ByteBuffer &out)
{
@ -37,14 +37,14 @@ namespace Aurora::Crypto::RSA
return {};
}
int padding = PaddingToType(type);
int padding = ::Crypto::PaddingToType(type);
if (padding == 0xFF)
{
SysPushErrorCrypt("invalid pad {}", type);
return false;
}
int hash = HashMethodToId(method);
int hash = ::Crypto::HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);
@ -105,7 +105,7 @@ namespace Aurora::Crypto::RSA
return {};
}
int padding = PaddingToType(type);
int padding = ::Crypto::PaddingToType(type);
if (padding == 0xFF)
{
SysPushErrorCrypt("invalid pad {}", type);
@ -171,14 +171,14 @@ namespace Aurora::Crypto::RSA
bool PrivateRSA::ToKey(const RSAMeta &meta, AuByteBuffer &out)
{
return ExportRSAKey(this->key_, meta.side, meta.type, out);
return ExportRSAKey(this->key_, meta.type, meta.encoding, out);
}
AUKN_SYM IRSAPrivate *OpenRSAPrivateNew(const RSAKey &key)
{
rsa_key in {};
if (key.meta.side == EKeyType::eKeyPublic)
if (key.meta.type == EKeyType::eKeyPublic)
{
SysPushErrorArg("Attempted to import a public key as a private key.");
return nullptr;

View File

@ -15,7 +15,7 @@ namespace Aurora::Crypto::RSA
~PrivateRSA();
bool Sign(const Memory::MemoryViewRead & payload,
EHashType method,
AuHashing::EHashType method,
EPaddingType type,
Memory::ByteBuffer &out) override;

View File

@ -24,7 +24,7 @@ namespace Aurora::Crypto::RSA
bool PublicRSA::Verify(const AuMemoryViewRead &payload,
const AuMemoryViewRead &signature,
EHashType method,
AuHashing::EHashType method,
EPaddingType type)
{
@ -40,14 +40,14 @@ namespace Aurora::Crypto::RSA
return {};
}
int padding = PaddingToType(type);
int padding = ::Crypto::PaddingToType(type);
if (padding == 0xFF)
{
SysPushErrorCrypt("invalid pad {}", type);
return false;
}
int hash = HashMethodToId(method);
int hash = ::Crypto::HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);
@ -97,7 +97,7 @@ namespace Aurora::Crypto::RSA
return {};
}
int padding = PaddingToType(type);
int padding = ::Crypto::PaddingToType(type);
if (padding == 0xFF)
{
SysPushErrorCrypt("invalid pad {}", type);

View File

@ -17,7 +17,7 @@ namespace Aurora::Crypto::RSA
bool Verify(const Memory::MemoryViewRead & payload,
const Memory::MemoryViewRead & signature,
EHashType method,
AuHashing::EHashType method,
EPaddingType type) override;
bool Encrypt(const Memory::MemoryViewRead & plainText,