main.ino 3.18 KB
Newer Older
Enrico Pozzobon committed
1 2
#include "crypto_aead.h"
#include "api.h"
3
#include "uartp.h"
Enrico Pozzobon committed
4

5
#define MAX_BYTES 100
Enrico Pozzobon committed
6

7
//#define DEBUG
Enrico Pozzobon committed
8

9 10 11 12 13 14 15 16 17
uint8_t npub[CRYPTO_NPUBBYTES];
uint8_t nsec[CRYPTO_NSECBYTES];
uint8_t k[CRYPTO_KEYBYTES];
uint8_t ad[MAX_BYTES];
unsigned long long int adlen;
uint8_t m[MAX_BYTES];
unsigned long long int mlen;
uint8_t c[MAX_BYTES];
unsigned long long int clen;
Enrico Pozzobon committed
18

19 20 21 22
#ifdef __cplusplus
extern "C" {
#endif

23 24 25 26 27 28 29 30 31 32 33 34
void uart_write(uint8_t x) {
    Serial.write((uint8_t) (x));
}

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

35
#ifdef __cplusplus
36
}
37
#endif
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#ifdef DEBUG
#define DEBUG_BUF_LEN 80
int dbg_printf(const char *format, ...) {
    char printbuf[DEBUG_BUF_LEN+2];

    va_list vargs;
    va_start(vargs, format);
    int r = vsnprintf(printbuf+1, DEBUG_BUF_LEN, format, vargs);
    va_end(vargs);

    if (r < 0) {
        memcpy(printbuf+1, "DEBUG ERROR\r\n", 13);
        r = 13;
    }

    r = r > DEBUG_BUF_LEN ? DEBUG_BUF_LEN : r;
    printbuf[0] = 0xde;  // Debug messages should start with "\xde"
    uartp_send(printbuf, r+1);
    return r;
}
#else
60
#define dbg_printf(...) (0)
61 62 63 64 65 66 67 68 69
#endif

void assert(bool b) {
    if (b)
        return;
    dbg_printf("Assertion failed\r\n");
    for(;;)
        yield();
}
Enrico Pozzobon committed
70 71

void setup() {
72 73 74 75
    Serial.begin(115200);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
76
    Serial.print("Hello, World!");
Enrico Pozzobon committed
77 78
}

79

Enrico Pozzobon committed
80
void loop() {
81 82 83 84 85 86 87 88
    static uint8_t buf[256];
    int res;
    uint16_t len = uartp_recv(buf, 255);
    uint8_t action = buf[0];
    if (len == 0 || len > 255)
        return;

    uint16_t l = len - 1;
89
    uint16_t rl = 0;
90 91 92 93 94 95 96 97 98
    uint8_t *var = buf+1;
    switch (action) {
        case 'm': assert(l <= MAX_BYTES);        memcpy(m,    var, l); mlen = l; break;
        case 'c': assert(l <= MAX_BYTES);        memcpy(c,    var, l); clen = l; break;
        case 'a': assert(l <= MAX_BYTES);        memcpy(ad,   var, l); adlen = l; break;
        case 'k': assert(l == CRYPTO_KEYBYTES);  memcpy(k,    var, l); break;
        case 'p': assert(l == CRYPTO_NPUBBYTES); memcpy(npub, var, l); break;
        case 's': assert(l == CRYPTO_NSECBYTES); memcpy(nsec, var, l); break;
        case 'e':
99 100
			noInterrupts();
			asm("nop");
101 102 103
            digitalWrite(LED_BUILTIN, LOW);
            res = crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k);
            digitalWrite(LED_BUILTIN, HIGH);
104 105
			asm("nop");
			interrupts();
106 107
            break;
        case 'd':
108 109
			noInterrupts();
			asm("nop");
110 111 112
            digitalWrite(LED_BUILTIN, LOW);
            res = crypto_aead_decrypt(m, &mlen, nsec, c, clen, ad, adlen, npub, k);
            digitalWrite(LED_BUILTIN, HIGH);
113 114
			asm("nop");
			interrupts();
115
            break;
116 117 118 119 120 121 122 123 124
        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;
        default:
            dbg_printf("Unknown command\r\n");
            assert(false);
125 126
    }
    buf[0] = action;
127 128
    memcpy(buf+1, var, rl);
    uartp_send(buf, rl+1);
Enrico Pozzobon committed
129
}