#include "crypto_aead.h"
#include "api.h"
#include "uartp.h"

#define MAX_BYTES 100
#define CMDBUF_LEN 72
static uint8_t cmdbuf[CMDBUF_LEN];

#define CRYPTO_BUSY 12
#define SerialOut Serial

uint8_t npub[CRYPTO_NPUBBYTES];
uint8_t nsec[CRYPTO_NSECBYTES];
uint8_t k[CRYPTO_KEYBYTES];
uint8_t ad[MAX_BYTES];
uint8_t m[MAX_BYTES];
uint8_t c[MAX_BYTES];
unsigned long long int adlen = 0;
unsigned long long int mlen = 0;
unsigned long long int clen = 0;
int res = 0;

void setup();
void loop();

#ifdef __cplusplus
extern "C" {
#endif

void uart_wbyte(uint8_t x) {
    SerialOut.write((uint8_t) (x));
}

uint8_t uart_rbyte() {
    int r;
    do {
        r = SerialOut.read();
    } while (r == -1);
    return (uint8_t) (0xff & r);
}

#ifdef __cplusplus
}
#endif

void my_assert(bool b) {
    if (b)
        return;
    for(;;)
        yield();
}

void setup() {
    SerialOut.begin(500000);
    pinMode(CRYPTO_BUSY, OUTPUT);
    digitalWrite(CRYPTO_BUSY, HIGH);
    delay(100);
    memset(npub, 0, CRYPTO_NPUBBYTES);
    memset(nsec, 0, CRYPTO_NSECBYTES);
    memset(k, 0, CRYPTO_KEYBYTES);
    memset(ad, 0, MAX_BYTES);
    memset(m, 0, MAX_BYTES);
    memset(c, 0, MAX_BYTES);
    mlen = 8;
    adlen = 0;
    crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k);
    crypto_aead_decrypt(m, &mlen, nsec, c, clen, ad, adlen, npub, k);
    SerialOut.print("Hello, World!");
}

void loop() {
    int res;
    portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
    uint16_t len = uartp_recv(cmdbuf, CMDBUF_LEN - 1);
    uint8_t action = cmdbuf[0];
    if (len == 0 || len > CMDBUF_LEN - 1)
        return;

    uint16_t l = len - 1;
    uint16_t rl = 0;
    uint8_t *var = cmdbuf+1;
    switch (action) {
        case 'm': my_assert(l <= MAX_BYTES);        memcpy(m,    var, l); mlen = l; break;
        case 'c': my_assert(l <= MAX_BYTES);        memcpy(c,    var, l); clen = l; break;
        case 'a': my_assert(l <= MAX_BYTES);        memcpy(ad,   var, l); adlen = l; break;
        case 'k': my_assert(l == CRYPTO_KEYBYTES);  memcpy(k,    var, l); break;
        case 'p': my_assert(l == CRYPTO_NPUBBYTES); memcpy(npub, var, l); break;
        case 's': my_assert(l == CRYPTO_NSECBYTES); memcpy(nsec, var, l); break;
        case 'e':
            portENTER_CRITICAL(&mux);
            digitalWrite(CRYPTO_BUSY, LOW);
            res = crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k);
            digitalWrite(CRYPTO_BUSY, HIGH);
            portEXIT_CRITICAL(&mux);
            break;
        case 'd':
            portENTER_CRITICAL(&mux);
            digitalWrite(CRYPTO_BUSY, LOW);
            res = crypto_aead_decrypt(m, &mlen, nsec, c, clen, ad, adlen, npub, k);
            digitalWrite(CRYPTO_BUSY, HIGH);
            portEXIT_CRITICAL(&mux);
            break;
        case'M': var = m;    rl = mlen; break;
        case'C': var = c;    rl = clen; break;
        case'A': var = ad;   rl = adlen; break;
        case'K': var = k;    rl = CRYPTO_KEYBYTES; break;
        case'P': var = npub; rl = CRYPTO_NPUBBYTES; break;
        case'S': var = nsec; rl = CRYPTO_NSECBYTES; break;
        case'R': var = (uint8_t *) &res; rl = sizeof(res); break;
        default:
            my_assert(false);
    }
    cmdbuf[0] = action;
    memcpy(cmdbuf+1, var, rl);
    uartp_send(cmdbuf, rl+1);
}
