// dllmain.cpp : Defines the entry point for the DLL application.
#include <windows.h>
#include "md5.h"
#include "gm6.h"

#define GMSTR  __declspec(dllexport) char* __cdecl
#define GMREAL __declspec(dllexport) double __cdecl

static char md5str[33];
static BYTE guid[0x1028];
static char gm6key[31];

GMSTR GetMachineGuid(void)
{
    HKEY hk;
    DWORD dwType = REG_SZ;
    memset(guid, 0x00, 0x1028);
    DWORD sz = 0x1028;
    DWORD error = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hk);
    error = RegQueryValueExA(hk, "MachineGuid", NULL, &dwType, guid, &sz);
    RegCloseKey(hk);
    return (char*)guid;
}

GMSTR GenerateGM6Key(char* name) {
    uint8_t out_key[KEY_LEN];
    keygen_gm6(name, out_key);
    key_to_string(out_key, gm6key, sizeof(gm6key));
    return gm6key;
}

uint8_t* datahex(char* string) {

    if (string == NULL)
        return NULL;

    size_t slength = strlen(string);
    if ((slength % 2) != 0) // must be even
        return NULL;

    size_t dlength = slength / 2;

    uint8_t* data = malloc(dlength);
    memset(data, 0, dlength);

    size_t index = 0;
    while (index < slength) {
        char c = string[index];
        int value = 0;
        if (c >= '0' && c <= '9')
            value = (c - '0');
        else if (c >= 'A' && c <= 'F')
            value = (10 + (c - 'A'));
        else if (c >= 'a' && c <= 'f')
            value = (10 + (c - 'a'));
        else {
            free(data);
            return NULL;
        }

        data[(index / 2)] += value << (((index + 1) % 2) * 4);

        index++;
    }

    return data;
}

GMREAL SetBinKey(char* path, char* key, char* hexstr)
{
    BYTE* binData = (BYTE*)datahex(hexstr);
    HKEY hk;
    DWORD dwType = REG_BINARY;
    DWORD sz = strlen(hexstr) / 2;
    DWORD error = RegOpenKeyExA(HKEY_CURRENT_USER, path, 0, KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_WOW64_64KEY, &hk);
    error = RegSetValueExA(hk, key, NULL, dwType, binData, sz);
    RegCloseKey(hk);
    return (double)error;
}

GMSTR MD5(char* input)
{
    MD5Context ctx;
    md5Init(&ctx);
    md5Update(&ctx, input, strlen(input));
    md5Finalize(&ctx);

    MD5Context ctx2;
    md5Init(&ctx2);
    md5Update(&ctx2, ctx.digest, 16);
    md5Finalize(&ctx2);

    memset(md5str, 0x00, 33);
    snprintf(md5str, 33, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", ctx2.digest[0], ctx2.digest[1], ctx2.digest[2], ctx2.digest[3], ctx2.digest[4], ctx2.digest[5], ctx2.digest[6], ctx2.digest[7], ctx2.digest[8], ctx2.digest[9], ctx2.digest[10], ctx2.digest[11], ctx2.digest[12], ctx2.digest[13], ctx2.digest[14], ctx2.digest[15]);
    return (char*)md5str;
}

BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved )
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
        srand(time(0));
        break;
    }
    return TRUE;
}

