HelloAurora/Tests/Public/12. Hello Crypto/Main.cpp

396 lines
11 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Main.cpp
Date: 2022-2-18
Author: Reece
***/
#include <AuroraRuntime.hpp>
#include <Aurora/Crypto/HashCash/HashCash.hpp>
#include <Aurora/Crypto/BCrypt/BCrypt.hpp>
#include <gtest/gtest.h>
#include <Aurora/Crypto/HashCash/HashCash.hpp>
static const auto kPassword = "testpassword";
TEST(Crypto, HashCash)
{
}
TEST(BCrypt, RoundTrip)
{
auto salt = AuCrypto::BCrypt::GenSalt(14);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
ASSERT_TRUE(AuCrypto::BCrypt::CheckPassword(kPassword, hash));
}
TEST(BCrypt, KnownGood)
{
// Trust me bro
}
TEST(BCryptSalt, T_10)
{
auto salt = AuCrypto::BCrypt::GenSalt(10);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 10: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
TEST(BCryptSalt, T_11)
{
auto salt = AuCrypto::BCrypt::GenSalt(11);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 11: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
TEST(BCryptSalt, T_12)
{
auto salt = AuCrypto::BCrypt::GenSalt(12);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 12: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
TEST(BCryptSalt, T_13)
{
auto salt = AuCrypto::BCrypt::GenSalt(13);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 13: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
TEST(BCryptSalt, T_14)
{
auto salt = AuCrypto::BCrypt::GenSalt(14);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 14: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
TEST(BCryptSalt, T_15)
{
auto salt = AuCrypto::BCrypt::GenSalt(15);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 15: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
TEST(BCryptSalt, T_16)
{
auto salt = AuCrypto::BCrypt::GenSalt(16);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 16: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
TEST(BCryptSalt, T_17)
{
auto salt = AuCrypto::BCrypt::GenSalt(17);
ASSERT_FALSE(salt.empty());
AuLogDbg("salt 17: {}", salt);
auto hash = AuCrypto::BCrypt::HashPassword(kPassword, salt);
ASSERT_FALSE(hash.empty());
AuLogDbg("password: {}", hash);
}
#define ADD_HASHCASH(n) \
TEST(HashCash, Power_## n) \
{ \
auto token = AuRng::RngU64(); \
auto answer = AuCrypto::HashCash::FindAnswer(token, n); \
ASSERT_TRUE(AuCrypto::HashCash::CheckSalt(token, n, answer)); \
}
ADD_HASHCASH(18) //
ADD_HASHCASH(19) //
ADD_HASHCASH(20) //
ADD_HASHCASH(21) //
ADD_HASHCASH(22) //
ADD_HASHCASH(23) //
ADD_HASHCASH(24) //
ADD_HASHCASH(25) //
ADD_HASHCASH(26) //
ADD_HASHCASH(27) //
//ADD_HASHCASH(28) // 47936
//ADD_HASHCASH(32) //
TEST(RSA, RSARng)
{
auto pRSAPrivate = AuCrypto::RSA::NewRSAKeyShared(4096);
ASSERT_TRUE(bool(pRSAPrivate));
}
TEST(RSACerts, GenerateOne)
{
auto pRSAPrivate = AuCrypto::RSA::NewRSAKeyShared(4096);
ASSERT_TRUE(bool(pRSAPrivate));
AuCrypto::X509::CertRequest request;
request.pSelfSigningRSAKey = pRSAPrivate;
request.pRSAKey = pRSAPrivate->ToPublic();
request.bSubjectKeyId = true;
request.name.commonName = "Aurora Test";
request.name.state = "Shithole";
request.name.countryCode = "GB";
request.name.name = "Firstname Lastname";
request.digest = AuHashing::EHashType::eSHA2_64;
auto cert = AuCrypto::X509::GenerateCertificate(request);
ASSERT_TRUE(bool(cert));
ASSERT_TRUE(AuFS::WriteFile("./SelfSigned_RSA.crt", cert.GetResult()));
}
TEST(ECCCerts, GenerateNist521)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurve521);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPublicECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPublicECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
AuCrypto::X509::CertRequest request;
request.pSelfSigningECCKey = pECCPrivate;
request.pECCKey = pECCPublic;
request.bSubjectKeyId = true;
request.name.commonName = "Aurora Test";
request.name.state = "Shithole";
request.name.countryCode = "GB";
request.name.name = "Firstname Lastname";
request.digest = AuHashing::EHashType::eSHA2_64;
auto cert = AuCrypto::X509::GenerateCertificate(request);
ASSERT_TRUE(bool(cert));
ASSERT_TRUE(AuFS::WriteFile("./SelfSigned_N521.crt", cert.GetResult()));
}
TEST(ECCCerts, GenerateSECP256K1)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveSECP256K1);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPublicECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPublicECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
AuCrypto::X509::CertRequest request;
request.pSelfSigningECCKey = pECCPrivate;
request.pECCKey = pECCPublic;
request.bSubjectKeyId = true;
request.name.commonName = "Aurora Test";
request.name.state = "Shithole";
request.name.countryCode = "GB";
request.name.name = "Firstname Lastname";
request.digest = AuHashing::EHashType::eSHA2_64;
request.usage.push_back(AuCrypto::X509::EExtendedUsage::eCodeSigning);
request.usage.push_back(AuCrypto::X509::EExtendedUsage::eOCSPSigning);
auto cert = AuCrypto::X509::GenerateCertificate(request);
ASSERT_TRUE(bool(cert));
ASSERT_TRUE(AuFS::WriteFile("./SelfSigned_256K1.crt", cert.GetResult()));
}
TEST(ECCReimport, SECP256K1)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveSECP256K1);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPublicECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPublicECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
}
TEST(ECCReimport, SECP256K1Private)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveSECP256K1);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPrivateECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPrivateECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
}
TEST(ECCReimport, x25519)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveX25519);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPublicECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPublicECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
}
TEST(ECCReimport, x25519Private)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveX25519);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPrivateECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPrivateECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
}
TEST(ECCReimport, ed25519)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveEd25519);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPublicECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPublicECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
}
TEST(ECCReimport, ed25519Private)
{
auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveEd25519);
ASSERT_TRUE(bool(pECCPrivate));
AuByteBuffer a;
pECCPrivate->AsPrivateECC(a);
auto pECCPublic = AuCrypto::ECC::OpenPrivateECCShared(a);
ASSERT_TRUE(bool(pECCPublic));
}
//
//TEST(ECCCerts, x25519)
//{
// auto pECCPrivate = AuCrypto::ECC::NewECCShared(AuCrypto::ECC::EECCCurve::eCurveEd25519);
// ASSERT_TRUE(bool(pECCPrivate));
//
// AuByteBuffer a;
// pECCPrivate->AsPublicECC(a);
// auto pECCPublic = AuCrypto::ECC::OpenPublicECCShared(a);
//
// AuCrypto::X509::CertRequest request;
// request.pSelfSigningECCKey = pECCPrivate;
// request.pECCKey = pECCPublic;
// request.bSubjectKeyId = true;
// request.name.commonName = "Aurora Test";
// request.name.state = "Shithole";
// request.name.countryCode = "GB";
// request.name.name = "Firstname Lastname";
//
// auto cert = AuCrypto::X509::GenerateCertificate(request);
// ASSERT_TRUE(bool(cert));
//
// ASSERT_TRUE(AuFS::WriteFile("./SelfSigned_X25519.crt", cert.GetResult()));
//}
TEST(RSA, RSAExportImport)
{
}
TEST(RSA, RSAEncrypt)
{
}
TEST(RSA, RSAVerify)
{
}
TEST(ECC, ECCVerify)
{
}
TEST(ECC, ECCECDH)
{
}
TEST(PEM, A)
{
}
TEST(CBC, AES)
{
char IV[16];
char key[24];
AuRng::RngFillArray(IV);
AuRng::RngFillArray(key);
auto pContext = AuCrypto::CBC::NewContextUnique();
auto pContext2 = AuCrypto::CBC::NewContextUnique();
ASSERT_TRUE(pContext);
ASSERT_TRUE(pContext2);
ASSERT_TRUE(pContext->Initialize(key, IV));
ASSERT_TRUE(pContext2->Initialize(key, IV));
char textBuffer[] { "The quick brown fox jumps over the lazy dog!!!!" };
char textBuffer2[] { "The quick brown fox jumps over the lazy dog!!!!" };
ASSERT_TRUE(AuCrypto::AES::CBCEncrypt(AuUnsafeRaiiToShared(pContext), textBuffer));
EXPECT_NE(AuMemcmp(textBuffer, textBuffer2, sizeof(textBuffer2)), 0);
ASSERT_TRUE(AuCrypto::AES::CBCDecrypt(AuUnsafeRaiiToShared(pContext2), textBuffer));
EXPECT_EQ(AuMemcmp(textBuffer, textBuffer2, sizeof(textBuffer2)), 0);
}
TEST(CBC, TDES)
{
char IV[16];
char key[24];
AuRng::RngFillArray(IV);
AuRng::RngFillArray(key);
auto pContext = AuCrypto::CBC::NewContextUnique();
auto pContext2 = AuCrypto::CBC::NewContextUnique();
ASSERT_TRUE(pContext);
ASSERT_TRUE(pContext2);
ASSERT_TRUE(pContext->Initialize(key, IV));
ASSERT_TRUE(pContext2->Initialize(key, IV));
char textBuffer[] { "The quick brown fox jumps over the lazy dog!!!!" };
char textBuffer2[] { "The quick brown fox jumps over the lazy dog!!!!" };
ASSERT_TRUE(AuCrypto::TDES::CBCEncrypt(AuUnsafeRaiiToShared(pContext), textBuffer));
EXPECT_NE(AuMemcmp(textBuffer, textBuffer2, sizeof(textBuffer2)), 0);
ASSERT_TRUE(AuCrypto::TDES::CBCDecrypt(AuUnsafeRaiiToShared(pContext2), textBuffer));
EXPECT_EQ(AuMemcmp(textBuffer, textBuffer2, sizeof(textBuffer2)), 0);
}
void RunTests()
{
Aurora::RuntimeStartInfo info;
2023-07-28 11:58:08 +00:00
info.console.fio.bEnableLogging = false;
Aurora::RuntimeStart(info);
}