Merge branch 'master' of github.com:r0ket/r0ket

This commit is contained in:
Stefan `Sec` Zehl 2011-07-17 16:50:29 +02:00
commit 06ea1d11f3
8 changed files with 175 additions and 77 deletions

View File

@ -24,46 +24,46 @@ char *Priv ="0e10e787036941e6c78daf8a0e8e1dbfac68e26d2";
void sendPublicKey(char *px, char *py) void sendPublicKey(char *px, char *py)
{ {
uint8_t exp[2 + 4*NUMWORDS]; uint8_t exp[2 + 4*NUMWORDS + 2];
exp[0] = 'P'; exp[0] = 'P';
bitstr_parse_export((char*)exp+2, px); bitstr_parse_export((char*)exp+2, px);
exp[1] = 'X'; exp[1] = 'X';
nrf_snd_pkt_crc(30, exp); nrf_snd_pkt_crc(32, exp);
delayms(10); delayms(10);
exp[1] = 'Y'; exp[1] = 'Y';
bitstr_parse_export((char*)exp+2, py); bitstr_parse_export((char*)exp+2, py);
nrf_snd_pkt_crc(30, exp); nrf_snd_pkt_crc(32, exp);
delayms(10); delayms(10);
} }
void sendR(uint8_t *rx, uint8_t *ry) void sendR(uint8_t *rx, uint8_t *ry)
{ {
uint8_t exp[2 + 4*NUMWORDS]; uint8_t exp[2 + 4*NUMWORDS + 2];
exp[0] = 'R'; exp[0] = 'R';
for(int i=0; i<4*NUMWORDS; i++) for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = rx[i]; exp[2+i] = rx[i];
exp[1] = 'X'; exp[1] = 'X';
nrf_snd_pkt_crc(30, exp); nrf_snd_pkt_crc(32, exp);
delayms(10); delayms(10);
exp[1] = 'Y'; exp[1] = 'Y';
for(int i=0; i<4*NUMWORDS; i++) for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = ry[i]; exp[2+i] = ry[i];
nrf_snd_pkt_crc(30, exp); nrf_snd_pkt_crc(32, exp);
delayms(10); delayms(10);
} }
int receiveKey(uint8_t type, uint8_t *x, uint8_t *y) int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
{ {
uint8_t buf[30]; uint8_t buf[32];
uint8_t n; uint8_t n;
n = nrf_rcv_pkt_time(1000, 30, buf); n = nrf_rcv_pkt_time(1000, 32, buf);
lcdPrint("pkt:"); lcdPrintInt(n);lcdPrintln(""); lcdRefresh(); lcdPrint("pkt:"); lcdPrintInt(n);lcdPrintln(""); lcdRefresh();
if( n == 30 && buf[0] == type && buf[1] == 'X' ){ if( n == 32 && buf[0] == type && buf[1] == 'X' ){
for(int i=0; i<NUMWORDS*4; i++) for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2]; x[i] = buf[i+2];
n = nrf_rcv_pkt_time(100, 30, buf); n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 30 && buf[0] ==type && buf[1] == 'Y' ){ if( n == 32 && buf[0] ==type && buf[1] == 'Y' ){
for(int i=0; i<NUMWORDS*4; i++) for(int i=0; i<NUMWORDS*4; i++)
y[i] = buf[i+2]; y[i] = buf[i+2];
return 0; return 0;
@ -84,7 +84,7 @@ int receiveR(uint8_t *rx, uint8_t *ry)
void sendMac(void) void sendMac(void)
{ {
uint8_t buf[7]; uint8_t buf[32];
buf[0] = 'M'; buf[0] = 'M';
buf[1] = 'C'; buf[1] = 'C';
buf[2] = mac[0]; buf[2] = mac[0];
@ -92,17 +92,17 @@ void sendMac(void)
buf[4] = mac[2]; buf[4] = mac[2];
buf[5] = mac[3]; buf[5] = mac[3];
buf[6] = mac[4]; buf[6] = mac[4];
nrf_snd_pkt_crc(30, buf); nrf_snd_pkt_crc(32, buf);
delayms(10); delayms(10);
} }
int receiveMac(uint8_t *mac) int receiveMac(uint8_t *mac)
{ {
uint8_t buf[30]; uint8_t buf[32];
uint8_t n; uint8_t n;
n = nrf_rcv_pkt_time(100, 30, buf); n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 30 && buf[0] == 'M' && buf[1] == 'C' ){ if( n == 32 && buf[0] == 'M' && buf[1] == 'C' ){
for(int i=0; i<5; i++) for(int i=0; i<5; i++)
mac[i] = buf[i+2]; mac[i] = buf[i+2];
return 0; return 0;
@ -206,13 +206,24 @@ void receiveFile(void)
lcdPrintln("Got R"); lcdPrintln("Got R");
lcdRefresh(); lcdRefresh();
ECIES_decryptkeygen(rx, ry, k1, k2, Priv); ECIES_decryptkeygen(rx, ry, k1, k2, Priv);
//if( filetransfer_receive(mac,k1) ) #if 0
// continue; lcdClear();
for(int i=0; i<16; i++){
lcdPrintCharHex(k1[i]);
if((i+1)%4==0)
lcdPrintln("");
}
lcdRefresh();
#else
uint32_t k[4] = {0xffff,0xffff,0xffff,0xffff};
if( filetransfer_receive(mac,(uint32_t*)k) )
continue;
lcdPrintln("Done"); lcdPrintln("Done");
lcdPrintln("Right=OK"); lcdPrintln("Right=OK");
lcdPrintln("Left=Retry"); lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort"); lcdPrintln("Down=Abort");
lcdRefresh(); lcdRefresh();
#endif
while(1){ while(1){
key = getInput(); key = getInput();
delayms(20); delayms(20);
@ -249,11 +260,21 @@ void sendFile(char *filename)
sendR(rx,ry); sendR(rx,ry);
lcdPrintln("Sent R"); lcdPrintln("Sent R");
lcdRefresh(); lcdRefresh();
#if 0
lcdClear();
for(int i=0; i<16; i++){
lcdPrintCharHex(k1[i]);
if((i+1)%4==0)
lcdPrintln("");
}
#else
delayms(3000); delayms(3000);
//filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k1); uint32_t k[4] = {0xffff,0xffff,0xffff,0xffff};
filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k);
lcdPrintln("Done"); lcdPrintln("Done");
lcdPrintln("Right=OK"); lcdPrintln("Right=OK");
lcdPrintln("Left=Retry"); lcdPrintln("Left=Retry");
#endif
lcdRefresh(); lcdRefresh();
while(1){ while(1){
@ -273,7 +294,15 @@ void main_vcard(void) {
char key; char key;
nrf_init(); nrf_init();
f_mount(0, &FatFs[0]); f_mount(0, &FatFs[0]);
nrf_set_rx_mac(0, 32, 5, mac);
struct NRF_CFG config = {
.channel= 81,
.txmac= "\x1\x2\x3\x2\x1",
.nrmacs=1,
.mac0= "\x1\x2\x3\x2\x1",
.maclen ="\x20",
};
nrf_config_set(&config);
while (1) { while (1) {
key= getInput(); key= getInput();
@ -285,10 +314,12 @@ void main_vcard(void) {
lcdDisplay(0); lcdDisplay(0);
ISPandReset(5); ISPandReset(5);
}else if(key==BTN_UP){ }else if(key==BTN_UP){
lcdClear(); //lcdClear();
lcdPrintln("Generating..."); //lcdPrintln("Generating...");
lcdRefresh(); //lcdRefresh();
sendFile("foobar.txt"); char file[13];
selectFile(file,"TXT");
sendFile(file);
//uint8_t k1[16], k2[16], Rx[4*NUMWORDS], Ry[4*NUMWORDS]; //uint8_t k1[16], k2[16], Rx[4*NUMWORDS], Ry[4*NUMWORDS];
//ECIES_encyptkeygen("1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7", //ECIES_encyptkeygen("1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7",
// "45f46eb303edf2e62f74bd68368d979e265ee3c03", // "45f46eb303edf2e62f74bd68368d979e265ee3c03",
@ -308,6 +339,7 @@ void main_vcard(void) {
//nrf_snd_pkt_crc(30, k1); //nrf_snd_pkt_crc(30, k1);
lcdPrintln("Done"); lcdPrintln("Done");
lcdRefresh(); lcdRefresh();
}else if(key==BTN_RIGHT){
} }
//encryption_decryption_demo("This is encrypted", //encryption_decryption_demo("This is encrypted",

View File

@ -7,6 +7,7 @@ OBJS =
OBJS += nrf24l01p.o OBJS += nrf24l01p.o
OBJS += rftransfer.o OBJS += rftransfer.o
OBJS += filetransfer.o OBJS += filetransfer.o
OBJS += openbeacon.h
LIBNAME=funk LIBNAME=funk

View File

@ -34,7 +34,7 @@ int filetransfer_send(uint8_t *filename, uint16_t size,
uint16_t wordcount = (size+3)/4; uint16_t wordcount = (size+3)/4;
uint8_t macbuf[5]; uint8_t macbuf[5];
uint8_t metadata[30]; uint8_t metadata[32];
if( strlen((char*)filename) < 20 ) if( strlen((char*)filename) < 20 )
strcpy((char*)metadata, (char*)filename); strcpy((char*)metadata, (char*)filename);
else else
@ -44,14 +44,34 @@ int filetransfer_send(uint8_t *filename, uint16_t size,
//nrf_get_tx_max(5,macbuf); //nrf_get_tx_max(5,macbuf);
nrf_set_tx_mac(5, mac); //nrf_set_tx_mac(5, mac);
nrf_snd_pkt_xxtea(30, metadata, k); nrf_snd_pkt_crc_encr(32, metadata, k);
delayms(20);
xxtea_encode_words((uint32_t *)buf, wordcount, k); xxtea_encode_words((uint32_t *)buf, wordcount, k);
rftransfer_send(wordcount*4, buf); rftransfer_send(wordcount*4, buf);
nrf_set_tx_mac(5, macbuf); //nrf_set_tx_mac(5, macbuf);
return 0; return 0;
} }
void put_rc_y (FRESULT rc, int y) {
const TCHAR *p =
_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
_T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
_T("NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
_T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0");
FRESULT i;
for (i = 0; i != rc && *p; i++) {
while(*p++) ;
}
DoString(0,y,p);
}
void put_rc (FRESULT rc){
put_rc_y(rc,0);
};
int filetransfer_receive(uint8_t *mac, uint32_t const k[4]) int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
{ {
uint8_t buf[MAXSIZE+1]; uint8_t buf[MAXSIZE+1];
@ -63,31 +83,39 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
uint8_t macbuf[5]; uint8_t macbuf[5];
//nrf_get_rx_max(0,5,macbuf); //nrf_get_rx_max(0,5,macbuf);
uint8_t metadata[30]; uint8_t metadata[32];
nrf_set_rx_mac(0, 32, 5, mac); //nrf_set_rx_mac(0, 32, 5, mac);
nrf_rcv_pkt_time_xxtea(1000, 30, metadata, k); nrf_rcv_pkt_time_encr(3000, 32, metadata, k);
//nrf_set_rx_mac(0, 32, 5, macbuf); //nrf_set_rx_mac(0, 32, 5, macbuf);
//lcdPrintln("got meta"); lcdRefresh();
metadata[19] = 0; //enforce termination metadata[19] = 0; //enforce termination
size = (metadata[20] << 8) | metadata[21]; size = (metadata[20] << 8) | metadata[21];
if( size > MAXSIZE ) {lcdPrintln("too big"); lcdRefresh(); while(1);}
if( size > MAXSIZE ) return 1; //file to big if( size > MAXSIZE ) return 1; //file to big
//if(fileexists(metadata)) return 1; //file already exists //if(fileexists(metadata)) return 1; //file already exists
lcdPrint("open"); lcdPrintln(metadata); lcdRefresh();
res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE); res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE);
//lcdPrintln("file opened"); lcdRefresh();
if( res ) {lcdPrintln("res"); put_rc(res); lcdRefresh(); while(1);}
if( res ) if( res )
return res; return res;
uint16_t wordcount = (size+3)/4; uint16_t wordcount = (size+3)/4;
nrf_set_rx_mac(0, 32, 5, mac); //nrf_set_rx_mac(0, 32, 5, mac);
lcdPrintln("get file"); lcdRefresh();
rftransfer_receive(buf, wordcount*4, 1000); rftransfer_receive(buf, wordcount*4, 1000);
lcdPrintln("got file"); lcdRefresh();
//nrf_set_rx_mac(0, 32, 5, macbuf); //nrf_set_rx_mac(0, 32, 5, macbuf);
xxtea_decode_words((uint32_t *)buf, wordcount, k); xxtea_decode_words((uint32_t *)buf, wordcount, k);
res = f_write(&file, buf, size, &written); res = f_write(&file, buf, size, &written);
f_close(&file);
if( res ) if( res )
return res; return res;
if( written != size ) if( written != size )

View File

@ -92,24 +92,6 @@ void nrf_write_long(const uint8_t cmd, int len, uint8_t* data){
nrf_write_long(C_W_REGISTER|(reg), len, data) nrf_write_long(C_W_REGISTER|(reg), len, data)
// High-Level: // High-Level:
int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
uint8_t * pkt, uint32_t const k[4])
{
int n = nrf_rcv_pkt_time(maxtime, maxsize, pkt);
if( n ){
if( n < 2 )
return -4;
xxtea_decode(pkt, n, k);
uint16_t crc=crc16(pkt,n-2);
if( pkt[n-2] == ((crc>>8)&0xFF) && pkt[n-1] == (crc&0xFF) ){
return n;
}else{
return -5;
}
}
return n;
}
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){ int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){
uint8_t len; uint8_t len;
uint8_t status=0; uint8_t status=0;
@ -203,12 +185,6 @@ char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){
return nrf_cmd_status(C_NOP); return nrf_cmd_status(C_NOP);
}; };
char nrf_snd_pkt_xxtea(int size, uint8_t * pkt, uint32_t const k[4])
{
xxtea_encode(pkt, size, k);
return nrf_snd_pkt_crc(size, pkt);
}
void nrf_set_rx_mac(int pipe, int rxlen, int maclen, uint8_t * mac){ void nrf_set_rx_mac(int pipe, int rxlen, int maclen, uint8_t * mac){
#ifdef SAFE #ifdef SAFE
assert(maclen>=1 || maclen<=5); assert(maclen>=1 || maclen<=5);

View File

@ -137,10 +137,6 @@ int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t cons
nrf_snd_pkt_crc_encr(size, pkt, NULL) nrf_snd_pkt_crc_encr(size, pkt, NULL)
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const k[4]); char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const k[4]);
// Schneider-stuff?
int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
uint8_t * pkt, uint32_t const k[4]);
char nrf_snd_pkt_xxtea(int size, uint8_t * pkt, uint32_t const k[4]);
void nrf_init() ; void nrf_init() ;
void nrf_cmd(uint8_t cmd); void nrf_cmd(uint8_t cmd);

View File

@ -0,0 +1,46 @@
#include "openbeacon.h"
#include "nrf24l01p.h"
#include "basic/byteorder.h"
const uint32_t key = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
const uint8_t enctoggle = 0;
uint32_t mid = 0;
uint32_t mctr = 0;
uint8_t strength = 0;
void openbeaconSetup(uint32_t id, uint32_t ctr)
{
mid = id;
mctr = ctr;
strength = 0;
}
void openbeaconSendPacket(uint32_t id, uint32_t ctr, uint8_t flags, uint8_t strength)
{
uint8_t buf[32];
int status;
buf[0]=0x10; // Length: 16 bytes
buf[1]=0x17; // Proto - fixed at 0x17?
buf[2]=flags;
buf[3]=strength*85; // Send intensity
uint322uint8p(ctr, buf+4);
uint322uint8p(id, buf+8);
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
status=nrf_snd_pkt_crc_encr(16,buf,enctoggle?testkey:NULL);
}
void openbeaconSend(void)
{
//uint8_t tmp = nrfgetstrength();
//nrfsetstrength(strength);
openbeaconSendPacket(mid, ctr++, 0xFF, strength++);
if( strength == 4 )
strength = 0;
//maybe this produces timing problems?
//nrfsetstrength(tmp);
}

View File

@ -0,0 +1,10 @@
#ifndef _OPENBEACON_H_
#define _OPENBEACON_H_
#include <stdint.h>
void openbeaconSetup(uint32_t id, uint32_t ctr);
void openbeaconSendPacket(uint32_t id, uint32_t ctr,
uint8_t flags, uint8_t strength);
void openbeaconSend(void);
#endif

View File

@ -3,7 +3,7 @@
#include <basic/basic.h> #include <basic/basic.h>
#include <core/systick/systick.h> #include <core/systick/systick.h>
#define MAXPACKET 30 #define MAXPACKET 32
void rftransfer_send(uint16_t size, uint8_t *data) void rftransfer_send(uint16_t size, uint8_t *data)
{ {
uint8_t buf[MAXPACKET]; uint8_t buf[MAXPACKET];
@ -16,8 +16,8 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[4] = rand & 0xFF; buf[4] = rand & 0xFF;
//nrf_snd_pkt_crc(5,buf); //setup packet //nrf_snd_pkt_crc(5,buf); //setup packet
nrf_snd_pkt_crc(30,buf); //setup packet nrf_snd_pkt_crc(32,buf); //setup packet
delayms(10); delayms(20);
uint16_t index = 0; uint16_t index = 0;
uint8_t i; uint8_t i;
uint16_t crc = crc16(data,size); uint16_t crc = crc16(data,size);
@ -32,9 +32,8 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[i] = *data++; buf[i] = *data++;
} }
index++; index++;
//nrf_snd_pkt_crc(i,buf); //data packet nrf_snd_pkt_crc(32,buf); //data packet
nrf_snd_pkt_crc(30,buf); //setup packet delayms(20);
delayms(10);
} }
buf[0] = 'C'; buf[0] = 'C';
@ -43,8 +42,8 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[3] = rand >> 8; buf[3] = rand >> 8;
buf[4] = rand & 0xFF; buf[4] = rand & 0xFF;
//nrf_snd_pkt_crc(5,buf); //crc packet //nrf_snd_pkt_crc(5,buf); //crc packet
nrf_snd_pkt_crc(30,buf); //setup packet nrf_snd_pkt_crc(32,buf); //setup packet
delayms(10); delayms(20);
} }
uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout) uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
@ -57,36 +56,46 @@ uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
unsigned int startTick = currentTick; unsigned int startTick = currentTick;
while(currentTick < (startTick+timeout) ){//this fails if either overflows while(currentTick < (startTick+timeout) ){//this fails if either overflows
n = nrf_rcv_pkt_time(10, MAXPACKET, buf); n = nrf_rcv_pkt_time(1000, MAXPACKET, buf);
switch(state){ switch(state){
case 0: case 0:
if( n == 5 && buf[0] == 'L' ){ if( n == 32 && buf[0] == 'L' ){
size = (buf[1] << 8) | buf[2]; size = (buf[1] << 8) | buf[2];
rand = (buf[3] << 8) | buf[4]; rand = (buf[3] << 8) | buf[4];
seq = 0; seq = 0;
pos = 0; pos = 0;
if( size <= maxlen ) if( size <= maxlen ){
lcdClear();
lcdPrintln("got l"); lcdRefresh();
state = 1; state = 1;
} }
}
break; break;
case 1: case 1:
if( n > 5 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){ if( n == 32 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){
lcdPrint("got d"); lcdRefresh();
if( seq == ((buf[1]<<8)|buf[2]) ){ if( seq == ((buf[1]<<8)|buf[2]) ){
if( (pos + n - 5)<maxlen ){ lcdPrintln(" in seq"); lcdRefresh();
for(i=5; i<n; i++,pos++){ //if( (pos + n - 5)<maxlen ){
//for(i=5; i<n; i++,pos++){
for(i=5; i<n && pos<size; i++,pos++){
buffer[pos] = buf[i]; buffer[pos] = buf[i];
} }
} seq++;
//}
} }
} }
if( pos == size ){ if( pos == size ){
lcdPrintln("got all"); lcdRefresh();
crc = crc16(buffer, size); crc = crc16(buffer, size);
state = 2; state = 2;
} }
break; break;
case 2: case 2:
if( n == 5 && buf[0] == 'C' && ((buf[3]<<8)|buf[4])==rand){ if( n == 32 && buf[0] == 'C' && ((buf[3]<<8)|buf[4])==rand){
lcdPrint("got crc"); lcdRefresh();
if( crc == ((buf[1]<<8)|buf[2]) ){ if( crc == ((buf[1]<<8)|buf[2]) ){
lcdPrintln(" ok"); lcdRefresh();
return size; return size;
}else{ }else{
state = 0; state = 0;