AuroraRuntime/Source/AuCrypto.cpp

133 lines
4.1 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Crypto.cpp
Date: 2021-6-17
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
#include "AuCrypto.hpp"
2021-06-27 21:25:29 +00:00
#include <tomcrypt.h>
#include <mbedtls/psa_util.h>
2021-06-27 21:25:29 +00:00
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);
2021-06-27 21:25:29 +00:00
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);
2021-06-27 21:25:29 +00:00
gPrngYarrow = register_prng(&yarrow_desc);
gAesCipher = register_cipher(&aes_desc);
gDesCipher = register_cipher(&des3_desc);
2021-06-27 21:25:29 +00:00
}
static void MBedTlsInit()
{
psa_crypto_init();
2021-06-27 21:25:29 +00:00
}
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;
}
}
2021-06-27 21:25:29 +00:00
}