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

5 6
#define MAX_BYTES 172
#define CMDBUF_LEN 200
7
static uint8_t cmdbuf[CMDBUF_LEN];
Enrico Pozzobon committed
8

9
#define CRYPTO_BUSY 12
10
#define SerialOut Serial
Enrico Pozzobon committed
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];
uint8_t m[MAX_BYTES];
uint8_t c[MAX_BYTES];
18 19 20 21 22 23 24
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();
Enrico Pozzobon committed
25

26 27 28 29
#ifdef __cplusplus
extern "C" {
#endif

30 31
void uart_wbyte(uint8_t x) {
    SerialOut.write((uint8_t) (x));
32 33
}

34
uint8_t uart_rbyte() {
35 36
    int r;
    do {
37
        r = SerialOut.read();
38 39 40 41
    } while (r == -1);
    return (uint8_t) (0xff & r);
}

42
#ifdef __cplusplus
43
}
44
#endif
45

46
void my_assert(bool b) {
47 48 49 50 51
    if (b)
        return;
    for(;;)
        yield();
}
Enrico Pozzobon committed
52 53

void setup() {
54
    SerialOut.begin(115200);
55 56
    pinMode(CRYPTO_BUSY, OUTPUT);
    digitalWrite(CRYPTO_BUSY, HIGH);
57
    delay(100);
58 59 60 61 62 63 64
    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);
    SerialOut.print("Hello, World!");
Enrico Pozzobon committed
65 66 67
}

void loop() {
68
    int res;
69 70 71
    uint16_t len = uartp_recv(cmdbuf, CMDBUF_LEN - 1);
    uint8_t action = cmdbuf[0];
    if (len == 0 || len > CMDBUF_LEN - 1)
72 73 74
        return;

    uint16_t l = len - 1;
75
    uint16_t rl = 0;
76
    uint8_t *var = cmdbuf+1;
77
    switch (action) {
78 79 80 81 82 83
        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;
84
        case 'e':
85 86
			noInterrupts();
			asm("nop");
87
            digitalWrite(CRYPTO_BUSY, LOW);
88
            res = crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k);
89
            digitalWrite(CRYPTO_BUSY, HIGH);
90 91
			asm("nop");
			interrupts();
92 93
            break;
        case 'd':
94 95
			noInterrupts();
			asm("nop");
96
            digitalWrite(CRYPTO_BUSY, LOW);
97
            res = crypto_aead_decrypt(m, &mlen, nsec, c, clen, ad, adlen, npub, k);
98
            digitalWrite(CRYPTO_BUSY, HIGH);
99 100
			asm("nop");
			interrupts();
101
            break;
102 103 104 105 106 107
        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;
108
        case'R': var = (uint8_t *) &res; rl = sizeof(res); break;
109
        default:
110
            my_assert(false);
111
    }
112 113 114
    cmdbuf[0] = action;
    memcpy(cmdbuf+1, var, rl);
    uartp_send(cmdbuf, rl+1);
Enrico Pozzobon committed
115
}