vcard: small clean up

This commit is contained in:
schneider 2011-07-23 17:58:56 +02:00
parent ec956a3263
commit 76a84b354f
4 changed files with 47 additions and 93 deletions

View File

@ -58,7 +58,6 @@ int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
uint8_t n; uint8_t n;
n = nrf_rcv_pkt_time(1000, 32, buf); n = nrf_rcv_pkt_time(1000, 32, buf);
lcdPrint("pkt:"); lcdPrintInt(n);lcdPrintln(""); lcdRefresh();
if( n == 32 && 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];
@ -116,7 +115,7 @@ int sendKeys(void)
char key; char key;
while( !done ){ while( !done ){
lcdClear(); lcdClear();
lcdPrintln("Sending key");lcdRefresh(); lcdPrintln("Sending PUBKEY");lcdRefresh();
sendPublicKey(Px,Py); sendPublicKey(Px,Py);
sendMac(); sendMac();
lcdPrintln("Done"); lcdPrintln("Done");
@ -146,7 +145,7 @@ int receiveKeys(uint8_t *px, uint8_t *py, uint8_t *mac)
char key; char key;
while( !done ){ while( !done ){
lcdClear(); lcdClear();
lcdPrintln("Receiving key"); lcdPrintln("Recv. PUBKEY");
lcdPrintln("Down=Abort"); lcdPrintln("Down=Abort");
lcdRefresh(); lcdRefresh();
key = getInput(); key = getInput();
@ -156,11 +155,11 @@ int receiveKeys(uint8_t *px, uint8_t *py, uint8_t *mac)
} }
if( receivePublicKey(px,py) ) if( receivePublicKey(px,py) )
continue; continue;
lcdPrintln("Got PUBKEY");
lcdRefresh();
if( receiveMac(mac) ) if( receiveMac(mac) )
continue; continue;
lcdPrintln("Done"); lcdClear();
lcdPrintln("Got PUBKEY"); lcdRefresh();
lcdPrintln("Hash:");
lcdPrintln("Right=OK"); lcdPrintln("Right=OK");
lcdPrintln("Left=Retry"); lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort"); lcdPrintln("Down=Abort");
@ -203,29 +202,16 @@ void receiveFile(void)
} }
if( receiveR(rx,ry) ) if( receiveR(rx,ry) )
continue; continue;
lcdPrintln("Got R"); lcdPrintln("Creating key");
lcdRefresh(); lcdRefresh();
ECIES_decryptkeygen(rx, ry, k1, k2, Priv); ECIES_decryptkeygen(rx, ry, k1, k2, Priv);
#if 0 if( filetransfer_receive(mac,(uint32_t*)k1) < 0 )
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};
//k[0] = 0; k[1] = 0; k[2] = 0; k[3] = 0;
if( filetransfer_receive(mac,(uint32_t*)k1) )
continue; continue;
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);
@ -239,7 +225,6 @@ void receiveFile(void)
} }
} }
} }
} }
void sendFile(char *filename) void sendFile(char *filename)
@ -254,32 +239,20 @@ void sendFile(char *filename)
uint8_t key; uint8_t key;
uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS]; 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); ECIES_encyptkeygen(px, py, k1, k2, rx, ry);
while( !done ){ while( !done ){
lcdClear();
lcdPrintln("Sending file");lcdRefresh(); lcdPrintln("Sending file");lcdRefresh();
sendR(rx,ry); sendR(rx,ry);
lcdPrintln("Sent R");
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);
uint32_t k[4] = {0xffff,0xffff,0xffff,0xffff};
k[0] = 0; k[1] = 0; k[2] = 0; k[3] = 0;
filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k1); filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k1);
lcdPrintln("Done"); lcdPrintln("Done");
lcdPrintln("Right=OK"); lcdPrintln("Right=OK");
lcdPrintln("Left=Retry"); lcdPrintln("Left=Retry");
#endif
lcdRefresh(); lcdRefresh();
while(1){ while(1){
key = getInput(); key = getInput();
delayms(20); delayms(20);
@ -289,7 +262,8 @@ void sendFile(char *filename)
done = 1; done = 1;
break; break;
} }
} }
lcdClear();
} }
} }
@ -310,38 +284,17 @@ void main_vcard(void) {
while (1) { while (1) {
key= getInput(); key= getInput();
delayms(20); delayms(20);
// Easy flashing // Easy flashing
if(key==BTN_LEFT){ if(key==BTN_LEFT){
DoString(0,8,"Enter ISP!"); DoString(0,8,"Enter ISP!");
lcdDisplay(); lcdDisplay();
ISPandReset(); ISPandReset();
}else if(key==BTN_UP){ }else if(key==BTN_UP){
//lcdClear();
//lcdPrintln("Generating...");
//lcdRefresh();
char file[13]; char file[13];
selectFile(file,"TXT"); selectFile(file,"TXT");
sendFile(file); sendFile(file);
//uint8_t k1[16], k2[16], Rx[4*NUMWORDS], Ry[4*NUMWORDS];
//ECIES_encyptkeygen("1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7",
// "45f46eb303edf2e62f74bd68368d979e265ee3c03",
// k1, k2, Rx, Ry);
//nrf_snd_pkt_crc(30, k1);
lcdPrintln("Done");
lcdRefresh();
}else if(key==BTN_DOWN){ }else if(key==BTN_DOWN){
lcdClear();
lcdPrintln("Generating...");
lcdRefresh();
receiveFile(); receiveFile();
//uint8_t k1[16], k2[16], Rx[4*NUMWORDS], Ry[4*NUMWORDS];
//ECIES_encyptkeygen("1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7",
// "45f46eb303edf2e62f74bd68368d979e265ee3c03",
// k1, k2, Rx, Ry);
//nrf_snd_pkt_crc(30, k1);
lcdPrintln("Done");
lcdRefresh();
}else if(key==BTN_RIGHT){ }else if(key==BTN_RIGHT){
DoString(0,8,"MSC Enabled."); DoString(0,8,"MSC Enabled.");
lcdDisplay(); lcdDisplay();
@ -349,13 +302,7 @@ void main_vcard(void) {
while(!getInputRaw())delayms(10); while(!getInputRaw())delayms(10);
DoString(0,16,"MSC Disabled."); DoString(0,16,"MSC Disabled.");
usbMSCOff(); usbMSCOff();
} }
//encryption_decryption_demo("This is encrypted",
// "1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7",
// "45f46eb303edf2e62f74bd68368d979e265ee3c03",
// "0e10e787036941e6c78daf8a0e8e1dbfac68e26d2");
} }
} }

View File

@ -63,6 +63,7 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
{ {
uint8_t buf[MAXSIZE+1]; uint8_t buf[MAXSIZE+1];
uint16_t size; uint16_t size;
uint8_t n;
UINT written = 0; UINT written = 0;
FIL file; FIL file;
@ -73,7 +74,9 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
uint8_t metadata[32]; uint8_t metadata[32];
//nrf_set_rx_mac(0, 32, 5, mac); //nrf_set_rx_mac(0, 32, 5, mac);
nrf_rcv_pkt_time_encr(3000, 32, metadata, k); n = nrf_rcv_pkt_time_encr(3000, 32, metadata, k);
if( n != 32 )
return 1; //timeout
//nrf_set_rx_mac(0, 32, 5, macbuf); //nrf_set_rx_mac(0, 32, 5, macbuf);
//lcdPrintln("got meta"); lcdRefresh(); //lcdPrintln("got meta"); lcdRefresh();
metadata[19] = 0; //enforce termination metadata[19] = 0; //enforce termination
@ -83,7 +86,7 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
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((const char*)metadata); lcdRefresh(); //lcdPrint("open"); lcdPrintln((const char*)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(); //lcdPrintln("file opened"); lcdRefresh();
@ -94,9 +97,18 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
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(); //lcdPrintln("get file"); lcdRefresh();
rftransfer_receive(buf, wordcount*4, 1000); int fres = rftransfer_receive(buf, wordcount*4, 1000);
lcdPrintln("got file"); lcdRefresh(); if( fres == -1 ){
lcdPrintln("checksum wrong");
}else if( fres == -2 ){
lcdPrintln("timeout");
}else{
//lcdPrintln("got file");
}
lcdRefresh();
if( fres < 0 )
return;
//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);
@ -107,6 +119,8 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
return res; return res;
if( written != size ) if( written != size )
return 1; //error while writing return 1; //error while writing
lcdClear();
lcdPrintln("Received"); lcdPrintln((const char*)metadata); lcdRefresh();
return 0; return 0;
} }

View File

@ -16,7 +16,6 @@ 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); //setup packet
nrf_snd_pkt_crc(32,buf); //setup packet nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20); delayms(20);
uint16_t index = 0; uint16_t index = 0;
@ -42,13 +41,11 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[2] = crc & 0xFF; buf[2] = crc & 0xFF;
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(32,buf); //setup packet nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20); delayms(20);
lcdPrint("crc="); lcdPrintIntHex(crc);lcdPrintln("");lcdRefresh();
} }
uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout) int16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
{ {
uint8_t buf[MAXPACKET]; uint8_t buf[MAXPACKET];
uint8_t state = 0; uint8_t state = 0;
@ -57,7 +54,7 @@ uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
unsigned int currentTick = systickGetTicks(); unsigned int currentTick = systickGetTicks();
unsigned int startTick = currentTick; unsigned int startTick = currentTick;
while(currentTick < (startTick+timeout) ){//this fails if either overflows while(systickGetTicks() < (startTick+timeout) ){//this fails if either overflows
n = nrf_rcv_pkt_time(1000, MAXPACKET, buf); n = nrf_rcv_pkt_time(1000, MAXPACKET, buf);
switch(state){ switch(state){
case 0: case 0:
@ -67,49 +64,45 @@ uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
seq = 0; seq = 0;
pos = 0; pos = 0;
if( size <= maxlen ){ if( size <= maxlen ){
lcdClear(); //lcdClear();
lcdPrint("got l="); lcdPrintInt(size); //lcdPrint("got l="); lcdPrintInt(size);
lcdPrintln(""); lcdRefresh(); //lcdPrintln(""); lcdRefresh();
state = 1; state = 1;
} }
} }
break; break;
case 1: case 1:
if( n == 32 && 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(); //lcdPrint("got d"); lcdRefresh();
if( seq == ((buf[1]<<8)|buf[2]) ){ if( seq == ((buf[1]<<8)|buf[2]) ){
lcdPrintln(" in seq"); lcdRefresh(); //lcdPrintln(" in seq"); lcdRefresh();
//if( (pos + n - 5)<maxlen ){
//for(i=5; i<n; i++,pos++){
for(i=5; i<n-2 && pos<size; i++,pos++){ for(i=5; i<n-2 && pos<size; i++,pos++){
buffer[pos] = buf[i]; buffer[pos] = buf[i];
} }
seq++; seq++;
//}
} }
} }
if( pos == size ){ if( pos == size ){
lcdPrintln("got all"); lcdRefresh(); //lcdPrintln("got all"); lcdRefresh();
crc = crc16(buffer, size); crc = crc16(buffer, size);
lcdPrint("crc="); lcdPrintIntHex(crc);
lcdPrintln("");lcdRefresh();
state = 2; state = 2;
} }
break; break;
case 2: case 2:
if( n == 32 && 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(); //lcdPrint("got crc"); lcdRefresh();
if( crc == ((buf[1]<<8)|buf[2]) ){ if( crc == ((buf[1]<<8)|buf[2]) ){
lcdPrintln(" ok"); lcdRefresh(); //lcdPrintln(" ok"); lcdRefresh();
return size; return size;
}else{ }else{
lcdPrintln(" nok"); lcdRefresh(); //lcdPrintln(" nok"); lcdRefresh();
state = 0; return -1;
} }
} }
break; break;
}; };
} }
return 0; //lcdPrintln("Timeout"); lcdRefresh();
return -2;
} }

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
void rftransfer_send(uint16_t size, uint8_t *data); void rftransfer_send(uint16_t size, uint8_t *data);
uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout); int16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout);
#endif #endif