mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-08-06 08:01:47 +02:00
Untested update to the new plugin API.
Various other changes to the plugin as well.
This commit is contained in:
160
source/Library/Crypt/AES.cpp
Normal file
160
source/Library/Crypt/AES.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Crypt/AES.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
#include "Base/Stack.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger AES256::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
static SQChar name[] = _SC("SqAES256");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
AES256::AES256()
|
||||
: m_Context(), m_Buffer()
|
||||
{
|
||||
aes256_done(&m_Context);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
AES256::AES256(CSStr key)
|
||||
: m_Context(), m_Buffer()
|
||||
{
|
||||
Init(key);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 AES256::Cmp(const AES256 & o) const
|
||||
{
|
||||
return std::memcmp(m_Buffer, o.m_Buffer, sizeof(m_Buffer));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr AES256::ToString() const
|
||||
{
|
||||
return ToStrF("%s", m_Buffer);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr AES256::GetKey() const
|
||||
{
|
||||
return reinterpret_cast< CSStr >(m_Buffer);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool AES256::Init(CSStr key)
|
||||
{
|
||||
// Clear current key, if any
|
||||
aes256_done(&m_Context);
|
||||
// Is the specified key empty?
|
||||
if (!key || *key == '\0')
|
||||
{
|
||||
return false; // Leave the context with an empty key
|
||||
}
|
||||
// Obtain the specified key size
|
||||
const Uint32 size = (std::strlen(key) * sizeof(SQChar));
|
||||
// See if the key size is accepted
|
||||
if (size > sizeof(m_Buffer))
|
||||
{
|
||||
STHROWF("The specified key is out of bounds: %u > %u", size, sizeof(m_Buffer));
|
||||
}
|
||||
// Initialize the key buffer to 0
|
||||
std::memset(m_Buffer, 0, sizeof(m_Buffer));
|
||||
// Copy the key into the key buffer
|
||||
std::memcpy(m_Buffer, key, size);
|
||||
// Initialize the context with the specified key
|
||||
aes256_init(&m_Context, m_Buffer);
|
||||
// This context was successfully initialized
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void AES256::Done()
|
||||
{
|
||||
aes256_done(&m_Context);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
String AES256::Encrypt(CSStr data)
|
||||
{
|
||||
// Is there any data to encrypt?
|
||||
if (!data || *data == 0)
|
||||
{
|
||||
return String();
|
||||
}
|
||||
// Copy the data into an editable string
|
||||
String str(data);
|
||||
// Make sure that we have a size with a multiple of 16
|
||||
if ((str.size() % 16) != 0)
|
||||
{
|
||||
str.resize(str.size() - (str.size() % 16) + 16);
|
||||
}
|
||||
// Encrypt in chunks of 16 characters
|
||||
for (Uint32 n = 0; n < str.size(); n += 16)
|
||||
{
|
||||
aes256_encrypt_ecb(&m_Context, reinterpret_cast< Uint8 * >(&str[n]));
|
||||
}
|
||||
// Return ownership of the encrypted string
|
||||
return std::move(str);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
String AES256::Decrypt(CSStr data)
|
||||
{
|
||||
// Is there any data to decrypt?
|
||||
if (!data || *data == 0)
|
||||
{
|
||||
return String();
|
||||
}
|
||||
// Copy the data into an editable string
|
||||
String str(data);
|
||||
// Make sure that we have a size with a multiple of 16
|
||||
if ((str.size() % 16) != 0)
|
||||
{
|
||||
str.resize(str.size() - (str.size() % 16) + 16);
|
||||
}
|
||||
// Decrypt inc chunks of 16 characters
|
||||
for (Uint32 n = 0; n < str.size(); n += 16)
|
||||
{
|
||||
aes256_decrypt_ecb(&m_Context, reinterpret_cast< Uint8 * >(&str[n]));
|
||||
}
|
||||
// Remove null characters in case the string was not a multiple of 16 when encrypted
|
||||
while (!str.empty() && str.back() == 0)
|
||||
{
|
||||
str.pop_back();
|
||||
}
|
||||
// Return ownership of the encrypted string
|
||||
return std::move(str);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_AES(HSQUIRRELVM vm)
|
||||
{
|
||||
RootTable(vm).Bind("SqAES256", Class< AES256 >(vm, "SqAES256")
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< CSStr >()
|
||||
// Metamethods
|
||||
.Func(_SC("_cmp"), &AES256::Cmp)
|
||||
.SquirrelFunc(_SC("_typename"), &AES256::Typename)
|
||||
.Func(_SC("_tostring"), &AES256::ToString)
|
||||
/* Properties */
|
||||
.Prop(_SC("Key"), &AES256::GetKey)
|
||||
/* Functions */
|
||||
.Func(_SC("Init"), &AES256::Init)
|
||||
.Func(_SC("Done"), &AES256::Done)
|
||||
.Func(_SC("Encrypt"), &AES256::Encrypt)
|
||||
.Func(_SC("Decrypt"), &AES256::Decrypt)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
110
source/Library/Crypt/AES.hpp
Normal file
110
source/Library/Crypt/AES.hpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#ifndef _LIBRARY_CRYPT_AES_HPP_
|
||||
#define _LIBRARY_CRYPT_AES_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "SqBase.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <aes256.h>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Simple wrapper around a the AES encryption context.
|
||||
*/
|
||||
class AES256
|
||||
{
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
AES256();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct with an explicit key.
|
||||
*/
|
||||
AES256(CSStr key);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
AES256(const AES256 & o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
AES256(AES256 && o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~AES256() = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
AES256 & operator = (const AES256 & o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
AES256 & operator = (AES256 && o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to compare two instances of this type.
|
||||
*/
|
||||
Int32 Cmp(const AES256 & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to convert an instance of this type to a string.
|
||||
*/
|
||||
CSStr ToString() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the associated key.
|
||||
*/
|
||||
CSStr GetKey() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Initialize the context key.
|
||||
*/
|
||||
bool Init(CSStr key);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reset the associated context.
|
||||
*/
|
||||
void Done();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Encrypt the specified string.
|
||||
*/
|
||||
String Encrypt(CSStr data);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Decrypt the specified string.
|
||||
*/
|
||||
String Decrypt(CSStr data);
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The managed encryption context.
|
||||
*/
|
||||
aes256_context m_Context;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The key used to encrypt data.
|
||||
*/
|
||||
Uint8 m_Buffer[32]{0};
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_CRYPT_AES_HPP_
|
94
source/Library/Crypt/Hash.cpp
Normal file
94
source/Library/Crypt/Hash.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Crypt/Hash.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
#include "Base/Stack.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <crc32.h>
|
||||
#include <keccak.h>
|
||||
#include <md5.h>
|
||||
#include <sha1.h>
|
||||
#include <sha256.h>
|
||||
#include <sha3.h>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Utility to avoid creating encoder instances for each call.
|
||||
*/
|
||||
template < class T > struct BaseHash
|
||||
{
|
||||
static T Algo;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template < class T > T BaseHash< T >::Algo;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Hash the specified value or the result of a formatted string.
|
||||
*/
|
||||
template < class T > static SQInteger HashF(HSQUIRRELVM vm)
|
||||
{
|
||||
// Attempt to retrieve the value from the stack as a string
|
||||
StackStrF val(vm, 2);
|
||||
// Have we failed to retrieve the string?
|
||||
if (SQ_FAILED(val.mRes))
|
||||
{
|
||||
return val.mRes; // Propagate the error!
|
||||
}
|
||||
// Forward the call to the actual implementation and store the string
|
||||
String str(BaseHash< T >::Algo(val.mPtr));
|
||||
// Push the string on the stack
|
||||
sq_pushstring(vm, str.data(), str.size());
|
||||
// At this point we have a valid string on the stack
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
template < class T > static void RegisterWrapper(Table & hashns, CCStr cname)
|
||||
{
|
||||
typedef HashWrapper< T > Hash;
|
||||
hashns.Bind(cname, Class< Hash >(hashns.GetVM(), cname)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
// Metamethods
|
||||
.Func(_SC("_tostring"), &Hash::ToString)
|
||||
// Properties
|
||||
.Prop(_SC("Hash"), &Hash::GetHash)
|
||||
// Functions
|
||||
.Func(_SC("Reset"), &Hash::Reset)
|
||||
.Func(_SC("Compute"), &Hash::Compute)
|
||||
.Func(_SC("GetHash"), &Hash::GetHash)
|
||||
.Func(_SC("Add"), &Hash::AddStr)
|
||||
.Func(_SC("AddStr"), &Hash::AddStr)
|
||||
);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_Hash(HSQUIRRELVM vm)
|
||||
{
|
||||
Table hashns(vm);
|
||||
|
||||
RegisterWrapper< CRC32 >(hashns, _SC("CRC32"));
|
||||
RegisterWrapper< Keccak >(hashns, _SC("Keccak"));
|
||||
RegisterWrapper< MD5 >(hashns, _SC("MD5"));
|
||||
RegisterWrapper< SHA1 >(hashns, _SC("SHA1"));
|
||||
RegisterWrapper< SHA256 >(hashns, _SC("SHA256"));
|
||||
RegisterWrapper< SHA3 >(hashns, _SC("SHA3"));
|
||||
|
||||
hashns.SquirrelFunc(_SC("GetCRC32"), &HashF< CRC32 >);
|
||||
hashns.SquirrelFunc(_SC("GetKeccak"), &HashF< Keccak >);
|
||||
hashns.SquirrelFunc(_SC("GetMD5"), &HashF< MD5 >);
|
||||
hashns.SquirrelFunc(_SC("GetSHA1"), &HashF< SHA1 >);
|
||||
hashns.SquirrelFunc(_SC("GetSHA256"), &HashF< SHA256 >);
|
||||
hashns.SquirrelFunc(_SC("GetSHA3"), &HashF< SHA3 >);
|
||||
|
||||
RootTable(vm).Bind(_SC("SqHash"), hashns);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
102
source/Library/Crypt/Hash.hpp
Normal file
102
source/Library/Crypt/Hash.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#ifndef _LIBRARY_CRYPT_HASH_HPP_
|
||||
#define _LIBRARY_CRYPT_HASH_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "SqBase.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Simple class to maintain the state of an encoder.
|
||||
*/
|
||||
template < class T > class HashWrapper
|
||||
{
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
HashWrapper()
|
||||
: m_Encoder()
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy operator.
|
||||
*/
|
||||
HashWrapper(const HashWrapper & o)
|
||||
: m_Encoder(o.m_Encoder)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~HashWrapper()
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
HashWrapper & operator = (const HashWrapper & o)
|
||||
{
|
||||
m_Encoder = o.m_Encoder;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to convert an instance of this type to a string.
|
||||
*/
|
||||
String ToString()
|
||||
{
|
||||
return m_Encoder.getHash();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reset the encoder state.
|
||||
*/
|
||||
void Reset()
|
||||
{
|
||||
m_Encoder.reset();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Compute the hash of the specified string.
|
||||
*/
|
||||
String Compute(const String & str)
|
||||
{
|
||||
return m_Encoder(str);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the hash value of the data hashed so far.
|
||||
*/
|
||||
String GetHash()
|
||||
{
|
||||
return m_Encoder.getHash();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Add a string value to be hashed.
|
||||
*/
|
||||
void AddStr(const String & str)
|
||||
{
|
||||
m_Encoder.add(str.data(), str.length() * sizeof(String::value_type));
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The managed encoder state.
|
||||
*/
|
||||
T m_Encoder;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_CRYPT_HASH_HPP_
|
Reference in New Issue
Block a user