main.ino 4.04 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4 5 6 7 8 9
#include <Arduino.h>
#include "crypto_aead.h"
#include "api.h"
#include "uartp.h"

#define MAX_BYTES 172
#define CMDBUF_LEN 200
static uint8_t cmdbuf[CMDBUF_LEN];

Enrico Pozzobon committed
10
#define CRYPTO_BUSY PB9
Enrico Pozzobon committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#define noInterrupts() eclic_global_interrupt_disable()
#define interrupts() eclic_global_interrupt_disable()
#define yield() do {} while(0)

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

static void init_uart0(void)
{
    // enable USART0 clock 
    rcu_periph_clock_enable(RCU_USART0);  
    // configure USART0
    usart_deinit(USART0);
    usart_baudrate_set(USART0, 4*115200);
    usart_word_length_set(USART0, USART_WL_8BIT);
    usart_stop_bit_set(USART0, USART_STB_1BIT);
    usart_parity_config(USART0, USART_PM_NONE);
    usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
    usart_enable(USART0);
}

void uart_wbyte(uint8_t x) {
    while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET);
    usart_data_transmit(USART0, x);
    while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET);
}

uint8_t uart_rbyte() {
    while (usart_flag_get(USART0, USART_FLAG_RBNE) == RESET);
    return (uint8_t) usart_data_receive(USART0);
}

void uart_wstr(const char *str) {
    char x;
    while ((x = *(str++)) != 0) {
        uart_wbyte((uint8_t) x);
    }
}

#ifdef __cplusplus
}
#endif

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

void setup() {
Enrico Pozzobon committed
80 81
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
Enrico Pozzobon committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    init_uart0();
    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);
    uart_wstr("Hello, World!");
}

void loop() {
    int res;
    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':
			noInterrupts();
			asm("nop");
            digitalWrite(CRYPTO_BUSY, LOW);
            res = crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k);
            digitalWrite(CRYPTO_BUSY, HIGH);
			asm("nop");
			interrupts();
            break;
        case 'd':
			noInterrupts();
			asm("nop");
            digitalWrite(CRYPTO_BUSY, LOW);
            res = crypto_aead_decrypt(m, &mlen, nsec, c, clen, ad, adlen, npub, k);
            digitalWrite(CRYPTO_BUSY, HIGH);
			asm("nop");
			interrupts();
            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);
}