#include "mxkCrypt.h"

#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/hmac.h>

// These three should be initialised to null, and populated by mxkCryptInit, but we already know
// their values and aren't trying to hide them from anyone! mxkCryptInit is still fully implemented,
// but is a no-op unless MXK_CRYPT_HAS_INIT is manually set to FALSE.
static unsigned char KEY_R[16] = {
    0x75, 0x6f, 0x72, 0x61, 0x74, 0x6e, 0x65, 0x6b, 0x61, 0x6d, 0x69, 0x68, 0x73, 0x75, 0x6b, 0x75,
};
static unsigned char KEY_S[16] = {
    0x66, 0x6E, 0x65, 0x6B, 0x65, 0x72, 0x61, 0x77, 0x64, 0x72, 0x61, 0x68, 0x61, 0x67, 0x65, 0x73,
};
static unsigned char TRACEDATA_DES_KEY[16] = { 0x4d, 0x77, 0xf1, 0x74, 0x8d, 0x6d, 0x10, 0x94 };
BOOL MXK_CRYPT_HAS_INIT = TRUE;

unsigned char BILLING_PRIVKEY[] =
    ("-----BEGIN PRIVATE KEY-----\n"
     "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAML2GPUuzv2N4bYC\n"
     "xtc5bZSzolHFWdCUbP+whjr3K98FOLnYeoi7mtUSUUYOW8wIqy6WM3c4c0Bp7FcQ\n"
     "LnZ0zWMm1TfLGHZzZmk5n7Iv6HDPr3ehDgbWLnOpRrVqZDxpAGD2vQb4p2DW4I2x\n"
     "GUqnqDa++C8dH/0lXqE6cqwGXNGtAgMBAAECgYEAizgPhG4Dk55QkpeTBDfXH3vT\n"
     "Ko9B3qdO2ptkjxDX/C8PXe7POXq2SvcEoIE6Xg3Gp8LMR5NBAbth8J32f9JSov3P\n"
     "SiGCGno4k2i2s3jRuVg76FGLDsZH/N1dt4h78VnW0VlInwaM6bQv3zp0u8rXVk/P\n"
     "wpYh9AGmquBJS3VYUcECQQD0PDRe28SrhollygGZSO321rYbYhoTIstDXZWyQ/y/\n"
     "PWKNwNHcYTHIVGmTrJx2AJUyr1tJhwjiOwlsI5Y1Q4/9AkEAzFpFPcs1r/xgSFxB\n"
     "eYrcNseWYbVajtVxG9t57sayaEQbH2UMNA2vqSYK/nU6oJhj5eLRIsPHlA5ZbIiZ\n"
     "rvc/cQJAKS0RQ0DX+ncXKQMSm+4wuGHgl+NFNB60mCnp+AEAVpmZyP5OI1J7myOo\n"
     "HQ6H3lkgzkfEIzRR6ho773BcfaRjXQJAfS4nEE11G9ML4AezjBLGB0CIHF6NlMWn\n"
     "PhtaPCy3iSt/OeIacaCYpJNLVMjXGx1+xIoG9rbbgRSxLs0W55lJ4QJBALOUVcNw\n"
     "GKEJdxhIkA8iuUlEyGpKluAgHUNOOKvC3ogRoB0OyH+If/9o8wWDfxgexgM0zGBc\n"
     "u178W9XDW+IijDA=\n"
     "-----END PRIVATE KEY-----\n");
unsigned char BILLING_PUBKEY[] = {
    0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
    0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xc2,
    0xf6, 0x18, 0xf5, 0x2e, 0xce, 0xfd, 0x8d, 0xe1, 0xb6, 0x02, 0xc6, 0xd7, 0x39, 0x6d, 0x94,
    0xb3, 0xa2, 0x51, 0xc5, 0x59, 0xd0, 0x94, 0x6c, 0xff, 0xb0, 0x86, 0x3a, 0xf7, 0x2b, 0xdf,
    0x05, 0x38, 0xb9, 0xd8, 0x7a, 0x88, 0xbb, 0x9a, 0xd5, 0x12, 0x51, 0x46, 0x0e, 0x5b, 0xcc,
    0x08, 0xab, 0x2e, 0x96, 0x33, 0x77, 0x38, 0x73, 0x40, 0x69, 0xec, 0x57, 0x10, 0x2e, 0x76,
    0x74, 0xcd, 0x63, 0x26, 0xd5, 0x37, 0xcb, 0x18, 0x76, 0x73, 0x66, 0x69, 0x39, 0x9f, 0xb2,
    0x2f, 0xe8, 0x70, 0xcf, 0xaf, 0x77, 0xa1, 0x0e, 0x06, 0xd6, 0x2e, 0x73, 0xa9, 0x46, 0xb5,
    0x6a, 0x64, 0x3c, 0x69, 0x00, 0x60, 0xf6, 0xbd, 0x06, 0xf8, 0xa7, 0x60, 0xd6, 0xe0, 0x8d,
    0xb1, 0x19, 0x4a, 0xa7, 0xa8, 0x36, 0xbe, 0xf8, 0x2f, 0x1d, 0x1f, 0xfd, 0x25, 0x5e, 0xa1,
    0x3a, 0x72, 0xac, 0x06, 0x5c, 0xd1, 0xad, 0x02, 0x03, 0x01, 0x00, 0x01,
};

unsigned char BILLING_CACERT[] = {
    0x30, 0x82, 0x03, 0x2d, 0x30, 0x82, 0x02, 0x15, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x43,
    0xae, 0xdc, 0x17, 0xc6, 0xea, 0xde, 0x6f, 0x69, 0x71, 0x6a, 0xba, 0xcc, 0x97, 0x99, 0x45, 0xcf,
    0x0d, 0xeb, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
    0x05, 0x00, 0x30, 0x25, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x07, 0x44,
    0x75, 0x6d, 0x6d, 0x79, 0x43, 0x41, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
    0x08, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x50, 0x4b, 0x49, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x38, 0x31,
    0x30, 0x31, 0x35, 0x31, 0x39, 0x33, 0x30, 0x35, 0x31, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x31, 0x38,
    0x31, 0x30, 0x31, 0x35, 0x31, 0x39, 0x33, 0x30, 0x35, 0x31, 0x5a, 0x30, 0x25, 0x31, 0x10, 0x30,
    0x0e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x07, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x43, 0x41, 0x31,
    0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x50,
    0x4b, 0x49, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
    0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
    0x01, 0x01, 0x00, 0xde, 0x1c, 0x42, 0x76, 0x2f, 0xe1, 0x57, 0xad, 0xc7, 0x56, 0x1b, 0xc2, 0xcd,
    0x67, 0xf5, 0xa4, 0x9d, 0x3c, 0xc4, 0xf6, 0x5b, 0x01, 0xe0, 0x8d, 0x46, 0x6f, 0xb2, 0xdc, 0xad,
    0x78, 0xbf, 0x62, 0xaf, 0xab, 0x12, 0x71, 0x7c, 0x8a, 0xbb, 0xb6, 0x87, 0x88, 0xf5, 0x89, 0xc9,
    0x83, 0x4a, 0xec, 0x0b, 0x05, 0x2f, 0xff, 0xa8, 0x43, 0x4e, 0xd8, 0xb7, 0xc4, 0xfa, 0x25, 0x35,
    0xce, 0x6b, 0x65, 0x86, 0x30, 0x1f, 0x1a, 0xff, 0x12, 0x61, 0x25, 0xe0, 0xd6, 0xcb, 0x00, 0x6c,
    0x1e, 0x92, 0xbf, 0x8b, 0xef, 0xcf, 0x45, 0x34, 0xa4, 0xd0, 0x80, 0xbe, 0x72, 0x7f, 0x2b, 0xc5,
    0x0d, 0xee, 0x40, 0x17, 0x1c, 0x1d, 0x6b, 0xc3, 0x97, 0x51, 0xbc, 0xee, 0x3b, 0x03, 0xe3, 0x35,
    0x34, 0xef, 0x12, 0xea, 0x96, 0x13, 0x2f, 0x98, 0xc9, 0x84, 0xaf, 0x81, 0x68, 0xd5, 0x66, 0x3f,
    0xac, 0x3d, 0x78, 0xe4, 0x38, 0x73, 0xed, 0x19, 0xbc, 0xa1, 0x4e, 0x11, 0x7d, 0x04, 0xfe, 0xec,
    0xb5, 0xa9, 0xc5, 0x2e, 0x4b, 0x7f, 0x3a, 0x65, 0xc3, 0xc5, 0x47, 0x14, 0x45, 0xbc, 0x4a, 0xd4,
    0xa3, 0xc5, 0xa3, 0xc8, 0xc0, 0x30, 0x57, 0xd1, 0xd9, 0xa1, 0x56, 0xb7, 0x87, 0x7b, 0x40, 0xcb,
    0x02, 0x9f, 0x0e, 0xb6, 0x23, 0x17, 0xc0, 0xed, 0x74, 0xd1, 0xbd, 0xdc, 0x6d, 0xe2, 0x83, 0x3a,
    0x6f, 0xe0, 0x8a, 0xe8, 0xe0, 0x0b, 0x84, 0x4a, 0xb3, 0xc4, 0xde, 0xde, 0x31, 0xe6, 0xe8, 0x8e,
    0xe1, 0x4c, 0x70, 0x50, 0xe6, 0xe8, 0x96, 0x45, 0x6c, 0x98, 0xe0, 0x33, 0xa3, 0xb8, 0x35, 0x42,
    0x21, 0x22, 0xd3, 0x09, 0x47, 0x4d, 0x05, 0xf5, 0x64, 0x7d, 0xdd, 0x5e, 0x06, 0x91, 0x00, 0x5e,
    0x6f, 0xbf, 0xf5, 0x74, 0xd9, 0x90, 0x27, 0x98, 0x8e, 0x64, 0x79, 0x77, 0xcf, 0x0c, 0x32, 0x2a,
    0x51, 0x17, 0xf7, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03,
    0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xc4, 0x77, 0x5b, 0x5d, 0x0e, 0xdd, 0xa8, 0x5a, 0xb0,
    0x8f, 0x3c, 0xe5, 0xd3, 0x62, 0xfe, 0x51, 0xe7, 0xc4, 0xc0, 0xd8, 0x30, 0x1f, 0x06, 0x03, 0x55,
    0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xc4, 0x77, 0x5b, 0x5d, 0x0e, 0xdd, 0xa8, 0x5a,
    0xb0, 0x8f, 0x3c, 0xe5, 0xd3, 0x62, 0xfe, 0x51, 0xe7, 0xc4, 0xc0, 0xd8, 0x30, 0x0f, 0x06, 0x03,
    0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06,
    0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01,
    0x00, 0xa5, 0xc5, 0x86, 0xb3, 0xe0, 0x34, 0xe8, 0x06, 0xaa, 0xaf, 0xd5, 0x40, 0xc6, 0x10, 0xe9,
    0x8b, 0x8b, 0xd2, 0x57, 0x42, 0xbe, 0xfb, 0xdd, 0x76, 0x20, 0x95, 0x91, 0xbf, 0xaa, 0xd1, 0x47,
    0x2e, 0xc7, 0xcf, 0x86, 0xfb, 0x3a, 0xfe, 0x54, 0x39, 0x9e, 0xc0, 0x1e, 0xc8, 0x67, 0x40, 0xd5,
    0x90, 0xbd, 0xdf, 0x50, 0xa7, 0xe7, 0xa1, 0x84, 0xda, 0x0b, 0x8c, 0xbe, 0x63, 0xa1, 0x69, 0x3d,
    0x06, 0x6c, 0x0c, 0x3c, 0x60, 0x0f, 0x84, 0x10, 0x2f, 0x89, 0xd0, 0xe1, 0xf1, 0x05, 0x0b, 0x50,
    0x02, 0x03, 0x25, 0x60, 0xc6, 0x17, 0xc0, 0xad, 0x0c, 0x36, 0x96, 0xf2, 0x81, 0xd8, 0x8e, 0xc0,
    0x53, 0x05, 0x25, 0x63, 0x2a, 0xed, 0x4c, 0x6b, 0xf3, 0x95, 0x31, 0xcf, 0x6f, 0x17, 0x41, 0x23,
    0x31, 0x46, 0xf7, 0x2e, 0xd9, 0x4a, 0x66, 0x43, 0x54, 0xaa, 0x65, 0x02, 0x8e, 0x51, 0x3f, 0x57,
    0x66, 0xd8, 0x62, 0x3a, 0x95, 0x2c, 0xd4, 0xe8, 0x82, 0xb5, 0xce, 0xe0, 0xac, 0x57, 0xca, 0xa7,
    0xc1, 0x15, 0xd5, 0x33, 0x4c, 0x20, 0x7f, 0xa9, 0x7e, 0xd5, 0xf8, 0x73, 0x67, 0xb3, 0x30, 0xea,
    0x20, 0x51, 0xc5, 0xfb, 0x0c, 0x93, 0x92, 0x53, 0x12, 0x3b, 0x04, 0xd4, 0x22, 0xbb, 0xd6, 0xe1,
    0x1e, 0xb3, 0xa2, 0x35, 0xcb, 0xd3, 0x08, 0xd1, 0x76, 0xfb, 0x41, 0xa9, 0x74, 0xaf, 0x3c, 0x5a,
    0xb9, 0x1d, 0x62, 0x4f, 0x76, 0x0d, 0x1d, 0x7d, 0x5c, 0xb2, 0x54, 0x36, 0x6e, 0xef, 0x23, 0xce,
    0xde, 0x03, 0x09, 0x2e, 0xa9, 0x03, 0x90, 0x7b, 0x1a, 0x60, 0xbb, 0x36, 0x20, 0xa1, 0xd4, 0x5e,
    0xa2, 0x42, 0x6c, 0x70, 0x89, 0xff, 0x08, 0x69, 0x31, 0x63, 0x58, 0x62, 0x8c, 0xc3, 0x14, 0x6a,
    0xe1, 0x7a, 0xd3, 0x52, 0xbe, 0x8d, 0xec, 0x2a, 0x1b, 0x7d, 0xc2, 0xf4, 0x0a, 0xd3, 0xc9, 0x9c,
    0xca,
};

MXK_STATUS mxkCryptInit(void) {
    static unsigned char KEY_HIDDEN[4][16] = { { 0xd8, 0x4f, 0x03, 0x08, 0x48, 0x92, 0xcb, 0x4a,
                                                 0xfc, 0x47, 0x75, 0x2b, 0xf9, 0xb1, 0x4f, 0x97 },
                                               { 0xbe, 0x21, 0x66, 0x63, 0x2d, 0xe0, 0xaa, 0x3d,
                                                 0x98, 0x35, 0x14, 0x43, 0x98, 0xd6, 0x2a, 0xe4 },
                                               { 0x07, 0xca, 0x77, 0x01, 0x63, 0x2b, 0x36, 0x60,
                                                 0xb5, 0x2d, 0x8f, 0x77, 0x41, 0x4e, 0x18, 0x5a },
                                               { 0x72, 0xa5, 0x05, 0x60, 0x17, 0x45, 0x53, 0x0b,
                                                 0xd4, 0x40, 0xe6, 0x1f, 0x32, 0x3b, 0x73, 0x2f } };

    if (MXK_CRYPT_HAS_INIT) return 0;

    /**
     * SEGA's loose attempt at hiding the real encryption keys
     *
     * static unsigned char KEY_R[16] = {
     *     0x75, 0x6f, 0x72, 0x61, 0x74, 0x6e, 0x65, 0x6b,
     *     0x61, 0x6d, 0x69, 0x68, 0x73, 0x75, 0x6b, 0x75,
     * };
     * static unsigned char KEY_S[16] = {
     *     0x66, 0x6E, 0x65, 0x6B, 0x65, 0x72, 0x61, 0x77,
     *     0x64, 0x72, 0x61, 0x68, 0x61, 0x67, 0x65, 0x73,
     * };
     */
    for (int i = 0; i < 16; i++) {
        KEY_R[i] = KEY_HIDDEN[0][i] ^ KEY_HIDDEN[1][i];
        KEY_S[i] = KEY_HIDDEN[2][i] ^ KEY_HIDDEN[3][i];
    }

    TRACEDATA_DES_KEY[0] = 0x4d;
    TRACEDATA_DES_KEY[1] = 0x77;
    TRACEDATA_DES_KEY[2] = 0xf1;
    TRACEDATA_DES_KEY[3] = 0x74;
    TRACEDATA_DES_KEY[4] = 0x8d;
    TRACEDATA_DES_KEY[5] = 0x6d;
    TRACEDATA_DES_KEY[6] = 0x10;
    TRACEDATA_DES_KEY[7] = 0x94;

    MXK_CRYPT_HAS_INIT = TRUE;
    return 0;
}

void mxkSetKeyS(unsigned char* key_s) { memcpy(KEY_S, key_s, 16); }
void mxkSetKeyR(unsigned char* key_r) { memcpy(KEY_R, key_r, 16); }
void mxkSwapKeys() {
    unsigned char temp[16];
    memcpy(temp, KEY_R, 16);
    memcpy(KEY_R, KEY_S, 16);
    memcpy(KEY_S, temp, 16);
}

MXK_STATUS mxkCryptEncryptAes128CBC(const unsigned char* key, const unsigned char* iv,
                                    unsigned char* ct, const unsigned char* pt, size_t nbytes) {
    if (key == NULL || iv == NULL || ct == NULL || pt == NULL) {
        amiDebugLog("Error: Invalid param.");
        return MXK_STATUS_INVALID_PARAM;
    }

    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv, 1);

    int outl;
    EVP_EncryptUpdate(ctx, ct, &outl, pt, nbytes);
    unsigned char dump[16];
    EVP_EncryptFinal_ex(ctx, dump, &outl);
    EVP_CIPHER_CTX_free(ctx);

    return MXK_STATUS_OK;
}

MXK_STATUS mxkCryptDecryptAes128CBC(const unsigned char* key, const unsigned char* iv,
                                    const unsigned char* ct, unsigned char* pt, size_t nbytes) {
    if (key == NULL || iv == NULL || ct == NULL || pt == NULL) {
        amiDebugLog("Error: Invalid param.");
        return MXK_STATUS_INVALID_PARAM;
    }

    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv, 0);

    int outl;
    EVP_DecryptUpdate(ctx, pt, &outl, ct, nbytes);
    unsigned char dump[16];
    EVP_EncryptFinal_ex(ctx, dump, &outl);
    EVP_CIPHER_CTX_free(ctx);

    return MXK_STATUS_OK;
}

MXK_STATUS mxkCryptEncryptData(unsigned char* ct, const unsigned char* pt) {
    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, KEY_S, NULL, 1);

    if (!MXK_CRYPT_HAS_INIT) {
        amiDebugLog("Error no init!!!");
        return MXK_STATUS_NO_INIT;
    }
    if (ct == NULL || pt == NULL) {
        amiDebugLog("Error invalid param!!!");
        return MXK_STATUS_INVALID_PARAM;
    }

    int outl;
    EVP_EncryptUpdate(ctx, ct, &outl, pt, 16);
    unsigned char dump[16];
    EVP_EncryptFinal_ex(ctx, dump, &outl);
    EVP_CIPHER_CTX_free(ctx);

    return MXK_STATUS_OK;
}

MXK_STATUS mxkCryptDecryptData(const unsigned char* ct, unsigned char* pt) {
    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, KEY_R, NULL, 0);

    if (!MXK_CRYPT_HAS_INIT) {
        amiDebugLog("Error no init!!!");
        return MXK_STATUS_NO_INIT;
    }
    if (ct == NULL || pt == NULL) {
        amiDebugLog("Error invalid param!!!");
        return MXK_STATUS_INVALID_PARAM;
    }

    int outl;
    EVP_DecryptUpdate(ctx, pt, &outl, ct, 16);
    unsigned char dump[16];
    EVP_EncryptFinal_ex(ctx, dump, &outl);
    EVP_CIPHER_CTX_free(ctx);

    return MXK_STATUS_OK;
}

void mxkSign(void* buffer, size_t nbytes, unsigned char* signature) {
    BIO* bio = BIO_new_mem_buf(BILLING_PRIVKEY, sizeof BILLING_PRIVKEY);
    EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
    BIO_free(bio);

    EVP_MD_CTX* ctx = EVP_MD_CTX_create();
    EVP_DigestSignInit(ctx, NULL, EVP_sha1(), NULL, pkey);
    EVP_DigestSignUpdate(ctx, buffer, nbytes);
    EVP_DigestSignFinal(ctx, NULL, &nbytes);
    EVP_DigestSignFinal(ctx, signature, &nbytes);

    EVP_PKEY_free(pkey);
    EVP_MD_CTX_destroy(ctx);
}

#pragma pack(push, 1)
typedef struct {
    unsigned int value;
    unsigned char key_low[4];
    unsigned char key_high[7];
} sign_payload_t;
#pragma pack(pop)

// TODO: Move this all
unsigned char KEYCHIP_ID[16];
void mxkSignValue(unsigned int value, unsigned char* signature) {
    sign_payload_t hash_data;
    hash_data.value = value;
    memcpy(hash_data.key_low, KEYCHIP_ID, 4);
    memcpy(hash_data.key_high, &KEYCHIP_ID[5], 7);
    mxkSign(&hash_data, sizeof hash_data, signature);
}

int mxkCryptCalcHashWithHmacSha1(void* key, unsigned char* md, size_t* nbuffer,
                                 unsigned char* buffer, size_t nin) {
    if (key == NULL || buffer == NULL || md == NULL || nbuffer == NULL) {
        amiDebugLog("Error: Invalid param.");
        return 3;
    }

    if (*nbuffer < 20) {
        amiDebugLog("Error: Datasize is too small.");
        return 3;
    }

    // int len = 20;
    // /* Load the openssl hasher */
    // EVP_MD *md = FUN_00412e70();
    // FUN_00415cd0(md, key, len, buffer, nin, md, nbuffer);
    // return 0;

    HMAC_CTX hmac_ctx;
    HMAC_CTX_init(&hmac_ctx);
    HMAC_Init_ex(&hmac_ctx, key, 20, EVP_sha1(), NULL);
    HMAC_Update(&hmac_ctx, buffer, nin);
    HMAC_Final(&hmac_ctx, md, nbuffer);
    HMAC_CTX_cleanup(&hmac_ctx);

    return 0;
}

void mxkCryptCalcHashWithSha1(unsigned char* data, size_t nbytes, unsigned char* sum) {
    EVP_MD_CTX* ctx = EVP_MD_CTX_create();
    EVP_DigestInit(ctx, EVP_sha1());
    EVP_DigestUpdate(ctx, data, nbytes);
    unsigned int outlen;
    EVP_DigestFinal_ex(ctx, sum, &outlen);
    EVP_MD_CTX_destroy(ctx);
}