moved code for vacrd from flash to loadables

This commit is contained in:
schneider 2011-08-03 10:38:43 +02:00
parent e5a4cdf427
commit 7539f4e6f7
4 changed files with 250 additions and 301 deletions

View File

@ -18,9 +18,12 @@ void forLoadables(int i){
if(i){ if(i){
lcdSetPixel(0,0); lcdSetPixel(0,0);
font=&Font_Invaders; font=&Font_Invaders;
filetransfer_send(); //filetransfer_send();
ECIES_encyptkeygen(); ECIES_encyptkeygen();
filetransfer_receive(); //filetransfer_receive();
//rftransfer_receive();
//rftransfer_send();
systickGetTicks();
ECIES_decryptkeygen(); ECIES_decryptkeygen();
bitstr_parse_export(); bitstr_parse_export();
nrf_rcv_pkt_time_encr(); nrf_rcv_pkt_time_encr();

View File

@ -1,299 +0,0 @@
#include <sysinit.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/allfonts.h"
#include "basic/ecc.h"
#include "funk/nrf24l01p.h"
#include "filesystem/ff.h"
#include "filesystem/diskio.h"
#include "funk/filetransfer.h"
#include "lcd/print.h"
uint8_t mac[5] = {1,2,3,2,1};
void sendPublicKey(void)
{
uint8_t exp[2 + 4*NUMWORDS + 2];
char buf[42];
UINT readbytes;
FIL file;
if( f_open(&file, "pubx.key", FA_OPEN_EXISTING|FA_READ) ){
return;
}
if( f_read(&file, buf, 41, &readbytes) || readbytes != 41 ){
return;
}
f_close(&file);
buf[41] = 0;
exp[0] = 'P';
bitstr_parse_export((char*)exp+2, buf);
exp[1] = 'X';
nrf_snd_pkt_crc(32, exp);
delayms(10);
if( f_open(&file, "puby.key", FA_OPEN_EXISTING|FA_READ) ){
return;
}
if( f_read(&file, buf, 41, &readbytes) || readbytes != 41 ){
return;
}
f_close(&file);
buf[41] = 0;
exp[1] = 'Y';
bitstr_parse_export((char*)exp+2, buf);
nrf_snd_pkt_crc(32, exp);
delayms(10);
}
void sendR(uint8_t *rx, uint8_t *ry)
{
uint8_t exp[2 + 4*NUMWORDS + 2];
exp[0] = 'R';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = rx[i];
exp[1] = 'X';
nrf_snd_pkt_crc(32, exp);
delayms(10);
exp[1] = 'Y';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = ry[i];
nrf_snd_pkt_crc(32, exp);
delayms(10);
}
int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
{
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(1000, 32, buf);
if( n == 32 && buf[0] == type && buf[1] == 'X' ){
for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2];
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] ==type && buf[1] == 'Y' ){
for(int i=0; i<NUMWORDS*4; i++)
y[i] = buf[i+2];
return 0;
}
}
return -1;
}
int receivePublicKey(uint8_t *px, uint8_t *py)
{
return receiveKey('P',px,py);
}
int receiveR(uint8_t *rx, uint8_t *ry)
{
return receiveKey('R',rx,ry);
}
void sendMac(void)
{
uint8_t buf[32];
buf[0] = 'M';
buf[1] = 'C';
buf[2] = mac[0];
buf[3] = mac[1];
buf[4] = mac[2];
buf[5] = mac[3];
buf[6] = mac[4];
nrf_snd_pkt_crc(32, buf);
delayms(10);
}
int receiveMac(uint8_t *mac)
{
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] == 'M' && buf[1] == 'C' ){
for(int i=0; i<5; i++)
mac[i] = buf[i+2];
return 0;
}
return -1;
}
int sendKeys(void)
{
uint8_t done = 0;
char key;
while( !done ){
lcdClear();
lcdPrintln("Sending PUBKEY");lcdRefresh();
sendPublicKey();
sendMac();
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}else if( key == BTN_DOWN ){
return -1;
}
}
}
return 0;
}
int receiveKeys(uint8_t *px, uint8_t *py, uint8_t *mac)
{
uint8_t done = 0;
char key;
while( !done ){
lcdClear();
lcdPrintln("Recv. PUBKEY");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
delayms(20);
if( key == BTN_DOWN ){
return -1;
}
if( receivePublicKey(px,py) )
continue;
if( receiveMac(mac) )
continue;
lcdClear();
lcdPrintln("Got PUBKEY"); lcdRefresh();
lcdPrintln("Hash:");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}else if( key == BTN_DOWN ){
return -1;
}
}
}
return 0;
}
void receiveFile(void)
{
if( sendKeys() )
return;
char priv[42];
UINT readbytes;
FIL file;
if( f_open(&file, "priv.key", FA_OPEN_EXISTING|FA_READ) ){
return;
}
if( f_read(&file, priv, 41, &readbytes) || readbytes != 41 ){
return;
}
f_close(&file);
priv[41] = 0;
uint8_t done = 0;
uint8_t key;
uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS];
while( !done ){
lcdClear();
lcdPrintln("Receiving file");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
delayms(20);
if( key == BTN_DOWN ){
return -1;
}
if( receiveR(rx,ry) )
continue;
lcdPrintln("Creating key");
lcdRefresh();
ECIES_decryptkeygen(rx, ry, k1, k2, priv);
if( filetransfer_receive(mac,(uint32_t*)k1) < 0 )
continue;
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}else if( key == BTN_DOWN ){
return -1;
}
}
}
}
void sendFile(char *filename)
{
uint8_t px[4*NUMWORDS];
uint8_t py[4*NUMWORDS];
uint8_t mac[5];
if( receiveKeys(px, py, mac) )
return;
uint8_t done = 0;
uint8_t key;
uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS];
lcdClear();
lcdPrintln("Creating key"); lcdRefresh();
ECIES_encyptkeygen(px, py, k1, k2, rx, ry);
while( !done ){
lcdPrintln("Sending file");lcdRefresh();
sendR(rx,ry);
delayms(3000);
filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k1);
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}
}
lcdClear();
}
}

View File

@ -180,4 +180,141 @@ int sendKeys(void)
} }
return 0; return 0;
} }
#include <string.h>
#include "funk/nrf24l01p.h"
#include "funk/filetransfer.h"
#include "funk/rftransfer.h"
#include "basic/basic.h"
#include "basic/xxtea.h"
#include "filesystem/ff.h"
#include "lcd/print.h"
int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
{
uint8_t buf[MAXSIZE+1];
uint16_t size;
uint8_t n;
UINT written = 0;
FIL file;
FRESULT res;
//uint8_t macbuf[5];
//nrf_get_rx_max(0,5,macbuf);
uint8_t metadata[32];
//nrf_set_rx_mac(0, 32, 5, mac);
n = nrf_rcv_pkt_time_encr(3000, 32, metadata, k);
if( n != 32 )
return 1; //timeout
//nrf_set_rx_mac(0, 32, 5, macbuf);
//lcdPrintln("got meta"); lcdRefresh();
metadata[19] = 0; //enforce termination
size = (metadata[20] << 8) | metadata[21];
if( size > MAXSIZE ) {lcdPrintln("too big"); lcdRefresh(); while(1);}
if( size > MAXSIZE ) return 1; //file to big
//if(fileexists(metadata)) return 1; //file already exists
//lcdPrint("open"); lcdPrintln((const char*)metadata); lcdRefresh();
res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE);
//lcdPrintln("file opened"); lcdRefresh();
if( res ) {lcdPrintln("res"); lcdPrint(f_get_rc_string(res)); lcdRefresh(); while(1);}
if( res )
return res;
uint16_t wordcount = (size+3)/4;
//nrf_set_rx_mac(0, 32, 5, mac);
//lcdPrintln("get file"); lcdRefresh();
int fres = rftransfer_receive(buf, wordcount*4, 1000);
if( fres == -1 ){
lcdPrintln("checksum wrong");
}else if( fres == -2 ){
lcdPrintln("timeout");
}else{
//lcdPrintln("got file");
}
lcdRefresh();
if( fres < 0 )
return 1;
//nrf_set_rx_mac(0, 32, 5, macbuf);
xxtea_decode_words((uint32_t *)buf, wordcount, k);
res = f_write(&file, buf, size, &written);
f_close(&file);
if( res )
return res;
if( written != size )
return 1; //error while writing
lcdClear();
lcdPrintln("Received"); lcdPrintln((const char*)metadata); lcdRefresh();
return 0;
}
#define MAXPACKET 32
int16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
{
uint8_t buf[MAXPACKET];
uint8_t state = 0;
uint16_t pos = 0, seq = 0, size = 0, rand = 0, crc = 0;
int n,i;
unsigned int currentTick = systickGetTicks();
unsigned int startTick = currentTick;
while(systickGetTicks() < (startTick+timeout) ){//this fails if either overflows
n = nrf_rcv_pkt_time(1000, MAXPACKET, buf);
switch(state){
case 0:
if( n == 32 && buf[0] == 'L' ){
size = (buf[1] << 8) | buf[2];
rand = (buf[3] << 8) | buf[4];
seq = 0;
pos = 0;
if( size <= maxlen ){
//lcdClear();
//lcdPrint("got l="); lcdPrintInt(size);
//lcdPrintln(""); lcdRefresh();
state = 1;
}
}
break;
case 1:
if( n == 32 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){
//lcdPrint("got d"); lcdRefresh();
if( seq == ((buf[1]<<8)|buf[2]) ){
//lcdPrintln(" in seq"); lcdRefresh();
for(i=5; i<n-2 && pos<size; i++,pos++){
buffer[pos] = buf[i];
}
seq++;
}
}
if( pos == size ){
//lcdPrintln("got all"); lcdRefresh();
crc = crc16(buffer, size);
state = 2;
}
break;
case 2:
if( n == 32 && buf[0] == 'C' && ((buf[3]<<8)|buf[4])==rand){
//lcdPrint("got crc"); lcdRefresh();
if( crc == ((buf[1]<<8)|buf[2]) ){
//lcdPrintln(" ok"); lcdRefresh();
return size;
}else{
//lcdPrintln(" nok"); lcdRefresh();
return -1;
}
}
break;
};
}
//lcdPrintln("Timeout"); lcdRefresh();
return -2;
}

View File

@ -174,3 +174,111 @@ void sendFile(char *filename)
} }
} }
#include <string.h>
#include "funk/nrf24l01p.h"
#include "funk/filetransfer.h"
#include "funk/rftransfer.h"
#include "basic/basic.h"
#include "basic/xxtea.h"
#include "filesystem/ff.h"
#include "lcd/print.h"
//TODO: use a proper MAC to sign the message
int filetransfer_send(uint8_t *filename, uint16_t size,
uint8_t *mac, uint32_t const k[4])
{
uint8_t buf[MAXSIZE];
FIL file;
FRESULT res;
UINT readbytes;
if( size > MAXSIZE )
return 1; //File to big
res=f_open(&file, (const char*)filename, FA_OPEN_EXISTING|FA_READ);
if( res )
return res;
//res = f_read(&file, (char *)buf, size, &readbytes);
for(uint16_t i=0; i<MAXSIZE; i++)
buf[i] = 0;
res = f_read(&file, (char *)buf, MAXSIZE, &readbytes);
size = readbytes;
if( res )
return res;
if( size != readbytes)
return 1; //Error while reading
uint16_t wordcount = (size+3)/4;
//uint8_t macbuf[5];
uint8_t metadata[32];
if( strlen((char*)filename) < 20 )
strcpy((char*)metadata, (char*)filename);
else
return 1; //File name too long
metadata[20] = size >> 8;
metadata[21] = size & 0xFF;
//nrf_get_tx_max(5,macbuf);
//nrf_set_tx_mac(5, mac);
nrf_snd_pkt_crc_encr(32, metadata, k);
delayms(20);
xxtea_encode_words((uint32_t *)buf, wordcount, k);
rftransfer_send(wordcount*4, buf);
//nrf_set_tx_mac(5, macbuf);
return 0;
}
#include "funk/rftransfer.h"
#include "funk/nrf24l01p.h"
#include <basic/basic.h>
#include <basic/random.h>
#include <core/systick/systick.h>
#include <lcd/print.h>
#define MAXPACKET 32
void rftransfer_send(uint16_t size, uint8_t *data)
{
uint8_t buf[MAXPACKET];
buf[0] = 'L';
buf[1] = size >> 8;
buf[2] = size & 0xFF;
uint16_t rand = getRandom() & 0xFFFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
uint16_t index = 0;
uint8_t i;
uint16_t crc = crc16(data,size);
while(size){
buf[0] = 'D';
buf[1] = index >> 8;
buf[2] = index & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
for(i=5; i<MAXPACKET-2 && size>0; i++,size--){
buf[i] = *data++;
}
index++;
nrf_snd_pkt_crc(32,buf); //data packet
delayms(20);
}
buf[0] = 'C';
buf[1] = crc >> 8;
buf[2] = crc & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
}