AuroraRuntime/Source/AuCrypto.cpp
Reece Wilson 033f7e2453 [+] Aurora::Crypto::X509::CertRequest
[+] Aurora::Crypto::X509::GenerateCertificate
[*] Fix lazily copied gen1 RSA code
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP256R1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP256K1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP384R1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP521R1
[*] Unfuck ECC interop
[*] Tls pinning: use mbedtls_ssl_conf_verify for tls1.3 (when mbedtls is in a better state)
2022-11-18 21:03:11 +00:00

133 lines
4.1 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Crypto.cpp
Date: 2021-6-17
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuCrypto.hpp"
#include <tomcrypt.h>
#include <mbedtls/psa_util.h>
namespace Crypto
{
static prng_state gPrng;
static void TomCryptInit()
{
crypt_mp_init("ltm");
yarrow_start(&gPrng);
gHashTiger = register_hash(&tiger_desc);
register_hash(&md5_desc);
gHashSha1 = register_hash(&sha1_desc);
gHashSha256 = register_hash(&sha256_desc);
gHashSha384 = register_hash(&sha384_desc);
gHashSha512 = register_hash(&sha512_desc);
gHashSha3_224 = register_hash(&sha3_224_desc);
gHashSha3_256 = register_hash(&sha3_256_desc);
gHashSha3_384 = register_hash(&sha3_384_desc);
gHashSha3_512 = register_hash(&sha3_512_desc);
gHashMD5 = register_hash(&md5_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);
gDesCipher = register_cipher(&des3_desc);
}
static void MBedTlsInit()
{
psa_crypto_init();
}
void InitCrypto()
{
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;
}
}
mbedtls_md_type_t TypeToMbed(Aurora::Hashing::EHashType type)
{
switch (type)
{
case AuHashing::EHashType::eMD5:
return mbedtls_md_type_t::MBEDTLS_MD_MD5;
case AuHashing::EHashType::eRMD160:
return mbedtls_md_type_t::MBEDTLS_MD_RIPEMD160;
case AuHashing::EHashType::eSHA1:
return mbedtls_md_type_t::MBEDTLS_MD_SHA1;
case AuHashing::EHashType::eSHA2_32:
return mbedtls_md_type_t::MBEDTLS_MD_SHA256;
case AuHashing::EHashType::eSHA2_64:
return mbedtls_md_type_t::MBEDTLS_MD_SHA512;
case AuHashing::EHashType::eSHA2_48:
return mbedtls_md_type_t::MBEDTLS_MD_SHA384;
default:
return mbedtls_md_type_t::MBEDTLS_MD_NONE;
}
}
}