diff --git a/Include/Aurora/Crypto/Crypto.hpp b/Include/Aurora/Crypto/Crypto.hpp index ede3932f..9c786b61 100644 --- a/Include/Aurora/Crypto/Crypto.hpp +++ b/Include/Aurora/Crypto/Crypto.hpp @@ -10,31 +10,19 @@ namespace Aurora::Crypto { using DerBuffer = AuList; - using PrivateRSAKey = DerBuffer; // a/k/a "RSA", OpenSSL, private key in mbedtls, PKCS1 - using PublicRSAKey = DerBuffer; - using PrivateECCKey = DerBuffer; - using PublicECCKey = DerBuffer; - using PrivateKey = DerBuffer; // PKCS8 - using PublicKey = DerBuffer; namespace X509 { using Certificate = AuList; } - - struct RSAPair - { - X509::Certificate pub; - PrivateRSAKey priv; - }; } #include "EHashType.hpp" +#include "EKeyType.hpp" #include "EPaddingType.hpp" #include "AES/AES.hpp" #include "X509/X509.hpp" #include "CA/CA.hpp" -#include "ECC25519/ECC25519.hpp" -#include "ECCNIST/ECCNIST.hpp" +#include "ECC/ECC.hpp" #include "PEM/PEM.hpp" #include "RSA/RSA.hpp" diff --git a/Include/Aurora/Crypto/ECC25519/ECC25519.hpp b/Include/Aurora/Crypto/ECC/25519/25519.hpp similarity index 88% rename from Include/Aurora/Crypto/ECC25519/ECC25519.hpp rename to Include/Aurora/Crypto/ECC/25519/25519.hpp index aedf31e2..bffafa5e 100644 --- a/Include/Aurora/Crypto/ECC25519/ECC25519.hpp +++ b/Include/Aurora/Crypto/ECC/25519/25519.hpp @@ -1,7 +1,7 @@ /*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - File: ECC25519.hpp + File: 25519.hpp Date: 2021-6-11 Author: Reece ***/ diff --git a/Source/Crypto/ECC25519/EccX25519Private.cpp b/Include/Aurora/Crypto/ECC/ECC.hpp similarity index 50% rename from Source/Crypto/ECC25519/EccX25519Private.cpp rename to Include/Aurora/Crypto/ECC/ECC.hpp index ddc0307f..bb34234e 100644 --- a/Source/Crypto/ECC25519/EccX25519Private.cpp +++ b/Include/Aurora/Crypto/ECC/ECC.hpp @@ -1,7 +1,11 @@ /*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - File: EccX25519Private.cpp - Date: 2021-6-12 + File: ECC.hpp + Date: 2021-7-1 Author: Reece ***/ +#pragma once + +#include "25519/25519.hpp" +#include "NIST/NIST.hpp" \ No newline at end of file diff --git a/Include/Aurora/Crypto/ECCNIST/ECCNIST.hpp b/Include/Aurora/Crypto/ECC/NIST/NIST.hpp similarity index 88% rename from Include/Aurora/Crypto/ECCNIST/ECCNIST.hpp rename to Include/Aurora/Crypto/ECC/NIST/NIST.hpp index 5d420c30..9fba724f 100644 --- a/Include/Aurora/Crypto/ECCNIST/ECCNIST.hpp +++ b/Include/Aurora/Crypto/ECC/NIST/NIST.hpp @@ -1,7 +1,7 @@ /*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - File: ECCNIST.hpp + File: NIST.hpp Date: 2021-6-11 Author: Reece ***/ diff --git a/Include/Aurora/Crypto/EKeyType.hpp b/Include/Aurora/Crypto/EKeyType.hpp new file mode 100644 index 00000000..43a214e2 --- /dev/null +++ b/Include/Aurora/Crypto/EKeyType.hpp @@ -0,0 +1,17 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: EKeyType.hpp + Date: 2021-7-1 + Author: Reece +***/ +#pragma once + +namespace Aurora::Crypto +{ + enum class EKeyType + { + eKeyPublic, + eKeyPrivate + }; +} \ No newline at end of file diff --git a/Include/Aurora/Crypto/PEM/PEM.hpp b/Include/Aurora/Crypto/PEM/PEM.hpp index b128202d..f245b06f 100644 --- a/Include/Aurora/Crypto/PEM/PEM.hpp +++ b/Include/Aurora/Crypto/PEM/PEM.hpp @@ -10,10 +10,10 @@ namespace Aurora::Crypto::PEM { AUKN_SYM AuString ToString(const Aurora::Crypto::X509::Certificate &in); - AUKN_SYM AuString PublicToString(const PublicKey &in); - AUKN_SYM AuString PrivateToString(const PrivateKey &in); - AUKN_SYM AuString PublicRSAToString(const PrivateRSAKey &in); - AUKN_SYM AuString PrivateRSAToString(const PublicRSAKey &in); + AUKN_SYM AuString PublicToString(const DerBuffer &in); + AUKN_SYM AuString PrivateToString(const DerBuffer &in); + AUKN_SYM AuString PublicRSAToString(const DerBuffer &in); + AUKN_SYM AuString PrivateRSAToString(const DerBuffer &in); AUKN_SYM bool FromString(const AuString &in, Aurora::Crypto::X509::Certificate &out); AUKN_SYM bool PublicFromString(const AuString &in, PublicKey &out); diff --git a/Include/Aurora/Crypto/RSA/ERSAKeyType.hpp b/Include/Aurora/Crypto/RSA/ERSAKeyType.hpp new file mode 100644 index 00000000..34b06ac2 --- /dev/null +++ b/Include/Aurora/Crypto/RSA/ERSAKeyType.hpp @@ -0,0 +1,21 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: ERSAKeyType.hpp + Date: 2021-7-1 + Author: Reece +***/ +#pragma once + +namespace Aurora::Crypto::RSA +{ + enum class ERSAKeyType + { + /// pkcs1 + eRsaKey, + /// pkcs8 + eKey, + /// x509 + eCert + }; +} \ No newline at end of file diff --git a/Include/Aurora/Crypto/RSA/IRSAPrivate.hpp b/Include/Aurora/Crypto/RSA/IRSAPrivate.hpp new file mode 100644 index 00000000..805a8bde --- /dev/null +++ b/Include/Aurora/Crypto/RSA/IRSAPrivate.hpp @@ -0,0 +1,30 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: IRSAPrivate.hpp + Date: 2021-7-1 + Author: Reece +***/ +#pragma once + +namespace Aurora::Crypto::RSA +{ + // Remember: there is no such thing as private encryption + class IRSAPrivate + { + public: + virtual bool Sign(const void *buffer, AuUInt length, + EHashType method, EPaddingType type, + AuList &out) = 0; + virtual bool Sign(const AuList &in, + EHashType method, EPaddingType type, + AuList &out) = 0; + + virtual bool Decrypt(const void *buffer, AuUInt length, EPaddingType type, AuList &out) = 0; + virtual bool Decrypt(const AuList &in, EPaddingType type, AuList &out) = 0; + + virtual AuSPtr ToPublic() = 0; + + virtual bool ToKey(const RSAMeta &meta, AuList &out) = 0; + }; +} \ No newline at end of file diff --git a/Include/Aurora/Crypto/RSA/IRSAPublic.hpp b/Include/Aurora/Crypto/RSA/IRSAPublic.hpp new file mode 100644 index 00000000..9af4b76e --- /dev/null +++ b/Include/Aurora/Crypto/RSA/IRSAPublic.hpp @@ -0,0 +1,29 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: IRSAPublic.hpp + Date: 2021-7-1 + Author: Reece +***/ +#pragma once + +namespace Aurora::Crypto::RSA +{ + // Rememeber: there is no such thing as public decryption + class IRSAPublic + { + public: + virtual bool Verify(const void *buffer, AuUInt length, + const void *sigBuffer, AuUInt sigLength, + EHashType method, + EPaddingType type) = 0; + + virtual bool Verify(const AuList &buffer, const AuList &sig, + EHashType method, EPaddingType type) = 0; + + virtual bool Encrypt(const void *buffer, AuUInt length, EPaddingType type, AuList &out) = 0; + virtual bool Encrypt(const AuList &in, EPaddingType type, AuList &out) = 0; + + virtual bool ToKey(ERSAKeyType type, AuList &out) = 0; + }; +} \ No newline at end of file diff --git a/Include/Aurora/Crypto/RSA/RSA.hpp b/Include/Aurora/Crypto/RSA/RSA.hpp index 9796fd0a..a4286a9f 100644 --- a/Include/Aurora/Crypto/RSA/RSA.hpp +++ b/Include/Aurora/Crypto/RSA/RSA.hpp @@ -7,7 +7,15 @@ ***/ #pragma once +#include "ERSAKeyType.hpp" +#include "RSAMeta.hpp" +#include "RSAKey.hpp" +#include "IRSAPublic.hpp" +#include "IRSAPrivate.hpp" + namespace Aurora::Crypto::RSA { - + AUKN_SHARED_API(OpenRSAPublic, IRSAPublic, const RSAKey &key); + AUKN_SHARED_API(OpenRSAPrivate, IRSAPrivate, const RSAKey &key); + AUKN_SHARED_API(NewRSAKey, IRSAPrivate, AuUInt16 keySize); } \ No newline at end of file diff --git a/Include/Aurora/Crypto/RSA/RSAKey.hpp b/Include/Aurora/Crypto/RSA/RSAKey.hpp new file mode 100644 index 00000000..1ec1db50 --- /dev/null +++ b/Include/Aurora/Crypto/RSA/RSAKey.hpp @@ -0,0 +1,17 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: RSAKey.hpp + Date: 2021-7-1 + Author: Reece +***/ +#pragma once + +namespace Aurora::Crypto::RSA +{ + struct RSAKey + { + RSAMeta meta; + DerBuffer blob; + }; +} \ No newline at end of file diff --git a/Include/Aurora/Crypto/RSA/RSAMeta.hpp b/Include/Aurora/Crypto/RSA/RSAMeta.hpp new file mode 100644 index 00000000..e5ea6bf0 --- /dev/null +++ b/Include/Aurora/Crypto/RSA/RSAMeta.hpp @@ -0,0 +1,17 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: RSAMeta.hpp + Date: 2021-7-1 + Author: Reece +***/ +#pragma once + +namespace Aurora::Crypto::RSA +{ + struct RSAMeta + { + ERSAKeyType type; + EKeyType side; + }; +} \ No newline at end of file diff --git a/Source/Async/Async.cpp b/Source/Async/Async.cpp index 002077c8..36336e13 100644 --- a/Source/Async/Async.cpp +++ b/Source/Async/Async.cpp @@ -16,7 +16,7 @@ namespace Aurora::Async InitSched(); } - void ShutdownSync() + void ShutdownAsync() { ShutdownSched(); } diff --git a/Source/Async/Async.hpp b/Source/Async/Async.hpp index 0d4fbf9c..01a3e8ad 100644 --- a/Source/Async/Async.hpp +++ b/Source/Async/Async.hpp @@ -43,5 +43,5 @@ namespace Aurora::Async }; void InitAsync(); - void ShutdownSync(); + void ShutdownAsync(); } \ No newline at end of file diff --git a/Source/Crypto/ECC25519/EccX25519Private.hpp b/Source/Crypto/ECC25519/EccX25519Private.hpp deleted file mode 100644 index d30fd959..00000000 --- a/Source/Crypto/ECC25519/EccX25519Private.hpp +++ /dev/null @@ -1,7 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: EccX25519Private.hpp - Date: 2021-6-12 - Author: Reece -***/ diff --git a/Source/Crypto/ECC25519/EccX25519Public.cpp b/Source/Crypto/ECC25519/EccX25519Public.cpp deleted file mode 100644 index 09e601ea..00000000 --- a/Source/Crypto/ECC25519/EccX25519Public.cpp +++ /dev/null @@ -1,7 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: EccX25519Public.cpp - Date: 2021-6-12 - Author: Reece -***/ diff --git a/Source/Crypto/ECC25519/EccX25519Public.hpp b/Source/Crypto/ECC25519/EccX25519Public.hpp deleted file mode 100644 index a06511ab..00000000 --- a/Source/Crypto/ECC25519/EccX25519Public.hpp +++ /dev/null @@ -1,7 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: EccX25519Public.hpp - Date: 2021-6-12 - Author: Reece -***/ diff --git a/Source/Crypto/ECCNIST/EccNISTPrivate.cpp b/Source/Crypto/ECCNIST/EccNISTPrivate.cpp deleted file mode 100644 index 7029cf86..00000000 --- a/Source/Crypto/ECCNIST/EccNISTPrivate.cpp +++ /dev/null @@ -1,7 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: EccNISTPrivate.cpp - Date: 2021-6-12 - Author: Reece -***/ diff --git a/Source/Crypto/ECCNIST/EccNISTPrivate.hpp b/Source/Crypto/ECCNIST/EccNISTPrivate.hpp deleted file mode 100644 index 9cb3bb43..00000000 --- a/Source/Crypto/ECCNIST/EccNISTPrivate.hpp +++ /dev/null @@ -1,7 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: EccNISTPrivate.hpp - Date: 2021-6-12 - Author: Reece -***/ diff --git a/Source/Crypto/ECCNIST/EccNISTPublic.cpp b/Source/Crypto/ECCNIST/EccNISTPublic.cpp deleted file mode 100644 index 87913e98..00000000 --- a/Source/Crypto/ECCNIST/EccNISTPublic.cpp +++ /dev/null @@ -1,7 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: EccNISTPublic.cpp - Date: 2021-6-12 - Author: Reece -***/ diff --git a/Source/Crypto/ECCNIST/EccNISTPublic.hpp b/Source/Crypto/ECCNIST/EccNISTPublic.hpp deleted file mode 100644 index da3cc4c1..00000000 --- a/Source/Crypto/ECCNIST/EccNISTPublic.hpp +++ /dev/null @@ -1,7 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: EccNISTPublic.hpp - Date: 2021-6-12 - Author: Reece -***/ diff --git a/Source/Crypto/RSA/RSA.hpp b/Source/Crypto/RSA/RSA.hpp new file mode 100644 index 00000000..266c51ee --- /dev/null +++ b/Source/Crypto/RSA/RSA.hpp @@ -0,0 +1,124 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: RSA.hpp + Date: 2021-7-1 + Author: Reece +***/ +#pragma once +#include +#include +#include + +namespace Aurora::Crypto::RSA +{ + static bool ExportRSAKey(const rsa_key &key, EKeyType side, ERSAKeyType type, AuList &out) + { + int flags = 0; + + if (type == ERSAKeyType::eRsaKey) + { + flags |= kRsaFlagPKCS1; + } + + if (side == EKeyType::eKeyPublic) + { + flags |= kRsaFlagPublic; + } + + if (!TryResize(out, 4096)) + { + return false; + } + + unsigned long actualSize = out.size(); + auto ret = rsa_pkcs8_export(out.data(), &actualSize, &key, flags); + + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + out.resize(actualSize); + return true; + } + + static bool ImportRSAKey(rsa_key &in, const RSAKey &rsakey) + { + int flags{}; + + if (rsakey.meta.type == ERSAKeyType::eCert) + { + if (rsakey.meta.side == EKeyType::eKeyPrivate) + { + SysPushErrorArg("Attempted to import a certificate as a private key."); + return false; + } + + auto ret = rsa_import_x509(rsakey.blob.data(), rsakey.blob.size(), &in); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + return true; + } + + if (rsakey.meta.type == ERSAKeyType::eRsaKey) + { + flags |= kRsaFlagPKCS1; + } + + if (rsakey.meta.side == EKeyType::eKeyPublic) + { + flags |= kRsaFlagPublic; + } + + auto ret = rsa_import_ex(rsakey.blob.data(), rsakey.blob.size(), &in, flags); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + return true; + } + + 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_64_512: + return ::Crypto::gHashSha512; + default: + return 0xFF; + } + } +} diff --git a/Source/Crypto/RSA/RSAPrivate.cpp b/Source/Crypto/RSA/RSAPrivate.cpp index 3b51c9e3..3b8a15d2 100644 --- a/Source/Crypto/RSA/RSAPrivate.cpp +++ b/Source/Crypto/RSA/RSAPrivate.cpp @@ -7,4 +7,205 @@ ***/ #include #include "../Crypto.hpp" +#include "RSA.hpp" #include "RSAPrivate.hpp" +#include "RSAPublic.hpp" + +namespace Aurora::Crypto::RSA +{ + PrivateRSA::PrivateRSA(rsa_key &key) : key_(key) + { + + } + + PrivateRSA::~PrivateRSA() + { + rsa_free(&key_); + } + + bool PrivateRSA::Sign(const void *buffer, AuUInt length, + EHashType method, EPaddingType type, + AuList &out) + { + prng_state yarrow_prng; + const int salt = 0; + + int padding = PaddingToType(type); + if (padding == 0xFF) + { + SysPushErrorCrypt("invalid pad {}", type); + return false; + } + + int hash = HashMethodToId(method); + if (hash == 0xFF) + { + SysPushErrorCrypt("invalid hash {}", method); + return false; + } + + if (!TryResize(out, 1024)) + { + SysPushErrorMem(); + return false; + } + + AuList hashVec; + + if (!TryResize(hashVec, 128)) + { + SysPushErrorMem(); + return false; + } + + unsigned long hashSize = hashVec.size(); + auto ret = hash_memory(hash, + reinterpret_cast(buffer), length, + reinterpret_cast(hashVec.data()), &hashSize); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + unsigned long len = out.size(); + ret = rsa_sign_hash_ex(reinterpret_cast(hashVec.data()), hashSize, + out.data(), &len, + padding, + &yarrow_prng, + ::Crypto::gPrngYarrow, + hash, + salt, + &key_); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + out.resize(len); + return true; + } + + bool PrivateRSA::Sign(const AuList &in, + EHashType method, EPaddingType type, + AuList &out) + { + return Sign(in.data(), in.size(), method, type, out); + } + + bool PrivateRSA::Decrypt(const void *buffer, AuUInt length, EPaddingType type, AuList &out) + { + int padding = PaddingToType(type); + if (padding == 0xFF) + { + SysPushErrorCrypt("invalid pad {}", type); + return false; + } + + const int prng_idx = padding == LTC_PKCS_1_PSS ? ::Crypto::gPrngYarrow : 0; + if (prng_idx < 0) + { + SysPushErrorCrypt("{}", prng_idx); + return false; + } + + if (!TryResize(out, length)) + { + SysPushErrorMem(); + return false; + } + + unsigned long len = out.size(); + + int stat = 0; + auto ret = rsa_decrypt_key_ex(reinterpret_cast(buffer), length, + out.data(), &len, + NULL, 0, + 0, // hash? excuse me? + padding, + &stat, + &key_); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + out.resize(len); + return stat == 1; + } + + bool PrivateRSA::Decrypt(const AuList &in, EPaddingType type, AuList &out) + { + return Decrypt(in.data(), in.size(), type, out); + } + + AuSPtr PrivateRSA::ToPublic() + { + return std::make_shared(key_); + } + + bool PrivateRSA::ToKey(const RSAMeta &meta, AuList &out) + { + return ExportRSAKey(key_, meta.side, meta.type, out); + } + + AUKN_SYM IRSAPrivate *OpenRSAPrivateNew(const RSAKey &key) + { + rsa_key in {}; + + if (key.meta.side == EKeyType::eKeyPublic) + { + SysPushErrorArg("Attempted to import a public key as a private key."); + return false; + } + + if (!ImportRSAKey(in, key)) + { + return nullptr; + } + + auto ret = _new PrivateRSA(in); + if (!ret) + { + rsa_free(&in); + return nullptr; + } + + return ret; + } + + AUKN_SYM void OpenRSAPrivateRelease(IRSAPrivate *re) + { + SafeDelete(re); + } + + AUKN_SYM IRSAPrivate *NewRSAKeyNew(AuUInt16 keySize) + { + prng_state yarrow_prng {}; + rsa_key key {}; + const int prng_idx = register_prng(&sprng_desc); + + auto error = rsa_make_key(NULL, prng_idx, keySize / 8, 65537, &key); + if (error != CRYPT_OK) + { + SysPushErrorCrypt("{}", error); + return nullptr; + } + + auto ret = _new PrivateRSA(key); + if (!ret) + { + rsa_free(&key); + return nullptr; + } + + return ret; + } + + AUKN_SYM void NewRSAKeyRelease(IRSAPrivate *re) + { + return OpenRSAPrivateRelease(re); + } +} \ No newline at end of file diff --git a/Source/Crypto/RSA/RSAPrivate.hpp b/Source/Crypto/RSA/RSAPrivate.hpp index 9df37cc7..9ad88212 100644 --- a/Source/Crypto/RSA/RSAPrivate.hpp +++ b/Source/Crypto/RSA/RSAPrivate.hpp @@ -6,3 +6,33 @@ Author: Reece ***/ #pragma once + +namespace Aurora::Crypto::RSA +{ + class PrivateRSA : public IRSAPrivate + { + public: + PrivateRSA(rsa_key &key); + ~PrivateRSA(); + + bool Sign(const void *buffer, AuUInt length, + EHashType method, EPaddingType type, + AuList &out) override; + + + bool Sign(const AuList &in, + EHashType method, EPaddingType type, + AuList &out) override; + + bool Decrypt(const void *buffer, AuUInt length, EPaddingType type, AuList &out) override; + + bool Decrypt(const AuList &in, EPaddingType type, AuList &out) override; + + AuSPtr ToPublic() override; + + bool ToKey(const RSAMeta &meta, AuList &out) override; + + private: + rsa_key key_; + }; +} \ No newline at end of file diff --git a/Source/Crypto/RSA/RSAPublic.cpp b/Source/Crypto/RSA/RSAPublic.cpp index 10366707..1046d0c2 100644 --- a/Source/Crypto/RSA/RSAPublic.cpp +++ b/Source/Crypto/RSA/RSAPublic.cpp @@ -7,4 +7,151 @@ ***/ #include #include "../Crypto.hpp" +#include "RSA.hpp" #include "RSAPublic.hpp" + +namespace Aurora::Crypto::RSA +{ + PublicRSA::PublicRSA(rsa_key &key) : key_(key) + { + + } + + PublicRSA::~PublicRSA() + { + + } + + bool PublicRSA::Verify(const void *buffer, AuUInt length, + const void *sigBuffer, AuUInt sigLength, + EHashType method, + EPaddingType type) + { + int padding = PaddingToType(type); + if (padding == 0xFF) + { + SysPushErrorCrypt("invalid pad {}", type); + return false; + } + + int hash = HashMethodToId(method); + if (hash == 0xFF) + { + SysPushErrorCrypt("invalid hash {}", method); + return false; + } + + AuList hashVec; + if (!TryResize(hashVec, 128)) + { + SysPushErrorMem(); + return false; + } + + unsigned long hashSize = hashVec.size(); + auto ret = hash_memory(hash, + reinterpret_cast(buffer), length, + reinterpret_cast(hashVec.data()), &hashSize); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + + int ok = 0; + ret = rsa_verify_hash_ex(reinterpret_cast(sigBuffer), sigLength, + reinterpret_cast(hashVec.data()), hashSize, + padding, hash, 0, &ok, &key_); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + return ok == 1; + } + + bool PublicRSA::Verify(const AuList &buffer, const AuList &sig, + EHashType method, EPaddingType type) + { + return Verify(buffer.data(), buffer.size(), sig.data(), sig.size(), method, type); + } + + bool PublicRSA::Encrypt(const void *buffer, AuUInt length, EPaddingType type, AuList &out) + { + prng_state yarrow_prng; + + int padding = PaddingToType(type); + if (padding == 0xFF) + { + SysPushErrorCrypt("invalid pad {}", type); + return false; + } + + const int prng_idx = padding == LTC_PKCS_1_PSS ? ::Crypto::gPrngYarrow : 0; + if (prng_idx < 0) + { + SysPushErrorCrypt("{}", prng_idx); + return false; + } + + if (!TryResize(out, length + 1024)) + { + SysPushErrorMem(); + return false; + } + + unsigned long len = out.size(); + + auto ret = rsa_encrypt_key_ex(reinterpret_cast(buffer), length, + out.data(), &len, + NULL, 0, + &yarrow_prng, prng_idx, + 0, + padding, + &key_); + if (ret != CRYPT_OK) + { + SysPushErrorCrypt("{}", ret); + return false; + } + + out.resize(len); + return true; + } + + bool PublicRSA::Encrypt(const AuList &in, EPaddingType type, AuList &out) + { + return Encrypt(in.data(), in.size(), type, out); + } + + bool PublicRSA::ToKey(ERSAKeyType type, AuList &out) + { + return ExportRSAKey(key_, EKeyType::eKeyPublic, type, out); + } + + AUKN_SYM IRSAPublic *OpenRSAPublicNew(const RSAKey &key) + { + rsa_key in {}; + + if (!ImportRSAKey(in, key)) + { + return nullptr; + } + + auto ret = _new PublicRSA(in); + if (!ret) + { + rsa_free(&in); + return nullptr; + } + + return ret; + } + + AUKN_SYM void OpenRSAPublicRelease(IRSAPublic *re) + { + SafeDelete(re); + } +} \ No newline at end of file diff --git a/Source/Crypto/RSA/RSAPublic.hpp b/Source/Crypto/RSA/RSAPublic.hpp index a2a62e2b..80cf2e34 100644 --- a/Source/Crypto/RSA/RSAPublic.hpp +++ b/Source/Crypto/RSA/RSAPublic.hpp @@ -7,3 +7,29 @@ ***/ #pragma once + +namespace Aurora::Crypto::RSA +{ + class PublicRSA : public IRSAPublic + { + public: + PublicRSA(rsa_key &key); + ~PublicRSA(); + + bool Verify(const void *buffer, AuUInt length, + const void *sigBuffer, AuUInt sigLength, + EHashType method, + EPaddingType type) override; + + bool Verify(const AuList &buffer, const AuList &sig, + EHashType method, EPaddingType type) override; + + bool Encrypt(const void *buffer, AuUInt length, EPaddingType type, AuList &out) override; + bool Encrypt(const AuList &in, EPaddingType type, AuList &out) override; + + bool ToKey(ERSAKeyType type, AuList &out) override; + + private: + rsa_key key_; + }; +} \ No newline at end of file diff --git a/Source/Entrypoint.cpp b/Source/Entrypoint.cpp index 77133f3f..9a876bc3 100644 --- a/Source/Entrypoint.cpp +++ b/Source/Entrypoint.cpp @@ -18,7 +18,6 @@ #include "Debug/Debug.hpp" #include "Async/Async.hpp" - static void Init() { Crypto::InitCrypto(); @@ -42,7 +41,7 @@ static void Deinit() { Aurora::RNG::Release(); Aurora::Console::Exit(); - Aurora::Async::ShutdownSync(); + Aurora::Async::ShutdownAsync(); } namespace Aurora