AuroraRuntime/Source/Crypto/AES/Aes.cpp

371 lines
10 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Aes.cpp
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "../Crypto.hpp"
#include "Aes.hpp"
#include <tomcrypt.h>
#include <Crypto.hpp>
namespace Aurora::Crypto::AES
{
AUKN_SYM AuUInt GetSafeCipherPadding(const Memory::MemoryViewRead &parameters)
{
auto tptr = reinterpret_cast<const unsigned char *>(parameters.ptr);
auto overhang = parameters.length & (128 - 1);
auto primaryLength = parameters.length - overhang;
if (overhang == 0)
{
auto lastbyte = tptr[primaryLength - 1];
if (lastbyte <= 128)
{
return parameters.length + 128;
}
else
{
return parameters.length;
}
}
else
{
return 128 - overhang;
}
}
static bool EncryptCoolCodePadding(const void *plainText, AuUInt plainTextLength,
const void *iv, void *outIv, AuUInt ivLength,
const void *key, AuUInt keyLength,
AuList<AuUInt8> &out,
symmetric_CBC &cbc)
{
auto overhang = plainTextLength & (128 - 1) /* || fast_rng_bool */;
auto padding = 128 - overhang;
auto primaryLength = plainTextLength - overhang;
int ret;
auto tptr = reinterpret_cast<const unsigned char *>(plainText);
unsigned char pad[128] =
{
0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE,
0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00,
0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE,
0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00,
0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE,
0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00,
0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE,
0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00,
0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE,
0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00,
0xC0, 0x01, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x80
};
if (overhang == 0)
{
auto lastbyte = tptr[primaryLength - 1];
if (lastbyte <= 128)
{
if (!AuTryResize(out, primaryLength + 128))
{
SysPushErrorMem();
return false;
}
// encrypt plain text
ret = cbc_encrypt(tptr, out.data(), primaryLength, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypto("{}", ret);
return false;
}
// encrypt another 128 bytes of trash :( 50/50 chance
ret = cbc_encrypt(pad, out.data() + primaryLength, 128, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypto("{}", ret);
return false;
}
}
else
{
if (!AuTryResize(out, primaryLength))
{
SysPushErrorMem();
return false;
}
ret = cbc_encrypt(tptr, out.data(), primaryLength, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypto("{}", ret);
return false;
}
}
}
else
{
if (!AuTryResize(out, primaryLength + 128))
{
SysPushErrorMem();
return false;
}
ret = cbc_encrypt(tptr, out.data(), primaryLength, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypto("{}", ret);
return false;
}
std::memcpy(pad, tptr + primaryLength, overhang);
pad[127] = padding;
ret = cbc_encrypt(pad, out.data() + primaryLength, 128, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypto("{}", ret);
return false;
}
}
return true;
}
AUKN_SYM bool Encrypt(const Memory::MemoryViewRead &plainText,
const Memory::MemoryViewRead &inIv,
const Memory::MemoryViewWrite & outIv,
const Memory::MemoryViewRead &inKey,
AuList<AuUInt8> &out,
bool auCoolCodePadding)
{
symmetric_CBC cbc;
if (!plainText.length)
{
return false;
}
if (!plainText.ptr)
{
return false;
}
if (!inIv.length)
{
return false;
}
if (!inIv.ptr)
{
return false;
}
if (!inKey.length)
{
return false;
}
if (!inKey.ptr)
{
return false;
}
if (inIv.length != 16)
{
SysPushErrorCrypt("{}", inIv.length);
return false;
}
if (outIv.length)
{
if (inIv.length != outIv.length)
{
SysPushErrorCrypt("{}", outIv.length);
return false;
}
}
if ((inKey.length != 16) &&
(inKey.length != 24) &&
(inKey.length != 32))
{
SysPushErrorCrypt("{}", inKey.length);
return false;
}
auto ret = cbc_start(::Crypto::gAesCipher,
reinterpret_cast<const unsigned char *>(inIv.ptr),
reinterpret_cast<const unsigned char *>(inKey.ptr),
inKey.length,
0,
&cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
auto tptr = reinterpret_cast<const unsigned char *>(plainText.ptr);
if (auCoolCodePadding)
{
if (!EncryptCoolCodePadding(tptr, plainText.length, inIv.ptr, outIv.ptr, inIv.length, inKey.ptr, inKey.length, out, cbc))
{
return false;
}
}
else
{
if ((plainText.length & (16 - 1)) != 0)
{
SysPushErrorArg("{}", plainText.length);
return false;
}
ret = cbc_encrypt(tptr, out.data(), plainText.length, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
}
if (outIv.ptr)
{
unsigned long ivLength = 16;
ret = cbc_getiv(reinterpret_cast<unsigned char *>(outIv.ptr), &ivLength, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
}
return true;
}
AUKN_SYM bool Decrypt(const Memory::MemoryViewRead &cipherText,
const Memory::MemoryViewRead &inIv,
const Memory::MemoryViewWrite &outIv,
const Memory::MemoryViewRead &inKey,
AuList<AuUInt8> &plainText,
bool safe)
{
symmetric_CBC cbc;
if (!cipherText.length)
{
return false;
}
if (!cipherText.ptr)
{
return false;
}
if (!inIv.length)
{
return false;
}
if (!inIv.ptr)
{
return false;
}
if (!inKey.length)
{
return false;
}
if (!inKey.ptr)
{
return false;
}
if (inIv.length != 16)
{
SysPushErrorCrypt("{}", inIv.length);
return false;
}
if (outIv.length)
{
if (inIv.length != outIv.length)
{
SysPushErrorCrypt("{}", outIv.length);
return false;
}
}
if ((inKey.length != 16) &&
(inKey.length != 24) &&
(inKey.length != 32))
{
SysPushErrorCrypt("{}", inKey.length);
return false;
}
auto ret = cbc_start(::Crypto::gAesCipher,
reinterpret_cast<const unsigned char *>(inIv.ptr),
reinterpret_cast<const unsigned char *>(inKey.ptr),
inKey.length,
0,
&cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
auto tptr = reinterpret_cast<const unsigned char *>(cipherText.ptr);
if (!AuTryResize(plainText, cipherText.length))
{
SysPushErrorMem();
return false;
}
ret = cbc_decrypt(tptr, plainText.data(), cipherText.length, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
if (safe)
{
auto magic = plainText[cipherText.length - 1];
if (magic <= 128)
{
plainText.resize(cipherText.length - magic);
}
}
if (outIv.ptr)
{
unsigned long ivLength = 16;
ret = cbc_getiv(reinterpret_cast<unsigned char *>(outIv.ptr), &ivLength, &cbc);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
}
return true;
}
}