1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-11-02 23:37:18 +01:00

Rename source to module.

This commit is contained in:
Sandu Liviu Catalin
2020-03-21 23:02:27 +02:00
parent a5c87bae5e
commit c0fd374404
237 changed files with 0 additions and 272718 deletions

View File

@@ -0,0 +1,155 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Crypt/AES.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("SqAES256"))
// ------------------------------------------------------------------------------------------------
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(Typename::Str,
Class< AES256 >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< CSStr >()
// Meta-methods
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &AES256::ToString)
.Func(_SC("cmp"), &AES256::Cmp)
/* 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

View File

@@ -0,0 +1,105 @@
#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;
/* --------------------------------------------------------------------------------------------
* 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_

View File

@@ -0,0 +1,192 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Crypt/Hash.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <crc32.h>
#include <keccak.h>
#include <md5.h>
#include <sha1.h>
#include <sha256.h>
#include <sha3.h>
#include <b64.h>
#include <whirlpool.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.Proc(true)))
{
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;
}
/* ------------------------------------------------------------------------------------------------
* Hash the specified value or the result of a formatted string with whirlpool algorithm.
*/
static SQInteger WhirlpoolF(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.Proc(true)))
{
return val.mRes; // Propagate the error!
}
// Prepare a whirlpool hashing context
whirlpool_ctx ctx;
// Initialize the hashing context
rhash_whirlpool_init(&ctx);
// Update the context with the given string
rhash_whirlpool_update(&ctx, reinterpret_cast< const unsigned char * >(val.mPtr),
val.mLen < 0 ? 0 : static_cast< size_t >(val.mLen));
// Reserve space for the result in binary form
unsigned char raw_hash[whirlpool_block_size];
// Finalize hashing and obtain the result
rhash_whirlpool_final(&ctx, raw_hash);
// Reserve space for the hexadecimal string
char hex_hash[whirlpool_block_size * 2];
// Convert from binary form to hex string
for (int i = 0, p = 0; i < whirlpool_block_size; ++i)
{
static const char dec2hex[16+1] = "0123456789abcdef";
hex_hash[p++] = dec2hex[(raw_hash[i] >> 4) & 15];
hex_hash[p++] = dec2hex[ raw_hash[i] & 15];
}
// Push the string on the stack
sq_pushstring(vm, hex_hash, whirlpool_block_size * 2);
// At this point we have a valid string on the stack
return 1;
}
/* ------------------------------------------------------------------------------------------------
* Encode the specified value or the result of a formatted string with base64 algorithm.
*/
static SQInteger EncodeBase64F(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.Proc(true)))
{
return val.mRes; // Propagate the error!
}
// Size of the encoded string
size_t enclen = 0;
// Attempt to encode the resulted string
char * result = b64_encode_ex(reinterpret_cast< const unsigned char * >(val.mPtr),
val.mLen < 0 ? 0 : static_cast< size_t >(val.mLen), &enclen);
// Did we fail to allocate memory for it?
if (!result)
{
return sq_throwerror(vm, _SC("Unable to allocate memory for output"));
}
// Push the string on the stack
sq_pushstring(vm, result, ConvTo< SQInteger >::From(enclen));
// At this point we have a valid string on the stack
return 1;
}
/* ------------------------------------------------------------------------------------------------
* Decode the specified value or the result of a formatted string with base64 algorithm.
*/
static SQInteger DecodeBase64F(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.Proc(true)))
{
return val.mRes; // Propagate the error!
}
// Size of the decoded string
size_t declen = 0;
// Attempt to decode the resulted string
unsigned char * result = b64_decode_ex(val.mPtr, val.mLen < 0 ? 0 : static_cast< size_t >(val.mLen), &declen);
// Did we fail to allocate memory for it?
if (!result)
{
return sq_throwerror(vm, _SC("Unable to allocate memory for output"));
}
// Push the string on the stack
sq_pushstring(vm, reinterpret_cast< CSStr >(result), ConvTo< SQInteger >::From(declen));
// 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()
// Meta-methods
.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 >);
hashns.SquirrelFunc(_SC("GetWhirlpool"), &WhirlpoolF);
hashns.SquirrelFunc(_SC("EncodeBase64"), &EncodeBase64F);
hashns.SquirrelFunc(_SC("DecodeBase64"), &DecodeBase64F);
RootTable(vm).Bind(_SC("SqHash"), hashns);
}
} // Namespace:: SqMod

View 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_