#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include "libcryptprov.h"
#include "testcrypt.h"
#include "util.h"

// Decrypts (allocates/deallocates) and prints to screen
#define DECRYPT_AND_PRINT(x) decrypt_and_print(ctx, x, sizeof(x) / sizeof(uint8_t))
static uint32_t decrypt_and_print(struct crypt_context *ctx, const uint8_t *buf, uint32_t buf_size);

static const uint8_t key[] = { 
    0xc1, 0xab, 0xe5, 0xec, 0x1e, 0x7a 
};
static const uint8_t key_size = sizeof(key) / sizeof(uint8_t);

static const uint8_t coded1[] = { 
    0x85, 0xc9, 0x84, 0x80, 0x46, 0x16, 0xaf, 0xca,
    0xc9, 0x81, 0x43, 0xe1, 0xac, 0xdd, 0xcb, 0x81,
    0x45, 0xa9, 0xa3, 0xca, 0xcd, 0x9b, 0x41, 0xfc,
    0xb3, 0xd5, 0x8c, 0x8f, 0x1c, 0x99
};

static const uint8_t coded2[] = { 
    0x92, 0xc5, 0x90, 0x8a, 0x43, 0xeb, 0xe1, 0xc1,
    0x9b, 0x6e, 0x4f, 0xf1, 0xa5, 0x93, 0x97, 0x61,
    0x1e, 0xc9, 0xa4, 0xc4, 0x83, 0x2b, 0x62, 0xd4,
    0xae, 0x95, 0x9d, 0x63, 0x20, 0xca, 0xa4, 0xc4,
    0x9e, 0x63, 0x3e, 0x91, 0xa2, 0xd8, 0x99, 0x75,
    0x6e, 0xdb, 0xa8, 0xdf, 0x97, 0x67, 0x72, 0xc2,
    0xa8, 0xdc, 0x6d, 0x72, 0x76, 0xb3, 0xa0, 0xd7,
    0x66, 0x39, 0x3e, 0xa0, 0xa2, 0xd4, 0x61, 0x79,
    0x3a, 0xea, 0xb2, 0xc8, 0x75, 0x76, 0x0c, 0xa8,
    0xef, 0xb7
};

static const uint8_t coded3[] = { 
    0x5a, 0x56, 0x07, 0xa0, 0xb4, 0xcd, 0x2b, 0x56,
    0x02, 0xb6, 0xb4, 0xd3, 0x69, 0x08, 0x0c, 0xbb,
    0xe1, 0xab, 0x6a, 0x5b, 0x06, 0xcf, 0xe1, 0xb2,
    0x7e, 0x0e, 0x12, 0x81, 0xa7, 0xa4, 0x76, 0x43,
    0x1f, 0x83, 0xb5, 0xe3, 0x76, 0x5b, 0x1a, 0x97,
    0xe1, 0xa9, 0x7e, 0x50, 0xea, 0x83, 0xe1, 0xbc,
    0x70, 0x5f, 0xea, 0x98, 0xe1, 0xb5, 0x7a, 0x50,
    0xef, 0x21, 0xa5, 0xa2, 0x7e, 0x2f, 0xea, 0x63,
    0xa5, 0xe8, 0x6c, 0x37, 0xe0, 0x62, 0xaf, 0xae,
    0x0f, 0x4c
};

static const uint8_t coded4[] = { 
    0xd7 
};

static const uint8_t coded5[] = { 
    0x1a, 0x8c, 0xbf, 0x50, 0x3d, 0xba, 0x62, 0xae,
    0xb9, 0x4e, 0x6c, 0xf8, 0x75, 0xb3, 0xec, 0x54,
    0x26, 0xcc, 0x78, 0xad, 0xa8, 0x09, 0x31, 0xce,
    0x45, 0xb3, 0xaf, 0x48, 0x21, 0xcf, 0x5b, 0xb2,
    0xef, 0x4c, 0x2b, 0x8e, 0x59, 0xa4, 0xbc, 0x43,
    0x75, 0xb8
};

int main(void)
{
    DEBUG_INFO("Starting testcrypt, test application");
    DEBUG_INFO("Key size: %d", key_size);

    struct crypt_context *ctx = NULL;

    int32_t status = crypt_alloc_context(&ctx, key, sizeof(key) / sizeof(uint8_t));
    if (status != CRYPT_ERROR_OK) {
        debug(true, "Failed to create crypt_context: 0x%08x", status);
        return status;
    }

    DECRYPT_AND_PRINT(coded1);
    DECRYPT_AND_PRINT(coded2);
    DECRYPT_AND_PRINT(coded3);
    DECRYPT_AND_PRINT(coded4);
    DECRYPT_AND_PRINT(coded5);

    crypt_free_context(ctx);
    ctx = NULL;

    return 0;
}

static uint32_t decrypt_and_print(struct crypt_context *ctx, const uint8_t *buf, uint32_t buf_size)
{
    if (!ctx || !buf || buf_size == 0) {
        return 0;
    }

    DEBUG_INFO("Decrypting %d bytes...", buf_size);

    //todo: can we do this all in one buffer, but since the globals are const arrays, we
    //  must allocate for the output buffer
    uint8_t *output_buf = (uint8_t *)malloc(buf_size);

    uint32_t res = crypt_buffer(ctx, output_buf, buf, buf_size);
    if (res != buf_size) {
        DEBUG_ERR("crypt_buffer failed. Returned: %d, expected: %d", res, buf_size);
        return 0;
    }

    DEBUG_INFO("crypt_buffer success, decrypted %d bytes. Output:\n", res);

    fflush(stdout);
    fwrite(output_buf, 1, buf_size, stdout);

    printf("\n");

    memset(output_buf, 0x00, buf_size);
    free(output_buf);

    return buf_size;
}
