AuroraRuntime/Source/AuCrypto.cpp

159 lines
5.3 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);
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);
gHashWhirlpool = register_hash(&whirlpool_desc);
gHashBlake2S_32 = register_hash(&blake2s_256_desc);
gHashBlake2S_28 = register_hash(&blake2s_224_desc);
gHashBlake2S_20 = register_hash(&blake2s_160_desc);
gHashBlake2S_16 = register_hash(&blake2s_128_desc);
gHashBlake2B_64 = register_hash(&blake2b_512_desc);
gHashBlake2B_48 = register_hash(&blake2b_384_desc);
gHashBlake2B_32 = register_hash(&blake2b_256_desc);
gHashBlake2B_20 = register_hash(&blake2b_160_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;
case AuHashing::EHashType::eWhirlpool:
return gHashWhirlpool;
case AuHashing::EHashType::eBlake2S_32:
return gHashBlake2S_32;
case AuHashing::EHashType::eBlake2S_28:
return gHashBlake2S_28;
case AuHashing::EHashType::eBlake2S_20:
return gHashBlake2S_20;
case AuHashing::EHashType::eBlake2S_16:
return gHashBlake2S_16;
case AuHashing::EHashType::eBlake2B_64:
return gHashBlake2B_64;
case AuHashing::EHashType::eBlake2B_48:
return gHashBlake2B_48;
case AuHashing::EHashType::eBlake2B_32:
return gHashBlake2B_32;
case AuHashing::EHashType::eBlake2B_20:
return gHashBlake2B_20;
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
}