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;
n = nrf_rcv_pkt_time(1000, 32, buf);
lcdPrint("pkt:"); lcdPrintInt(n);lcdPrintln(""); lcdRefresh();
if( n == 32 && buf[0] == type && buf[1] == 'X' ){
for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2];
@ -116,7 +115,7 @@ int sendKeys(void)
char key;
while( !done ){
lcdClear();
lcdPrintln("Sending key");lcdRefresh();
lcdPrintln("Sending PUBKEY");lcdRefresh();
sendPublicKey(Px,Py);
sendMac();
lcdPrintln("Done");
@ -146,7 +145,7 @@ int receiveKeys(uint8_t *px, uint8_t *py, uint8_t *mac)
char key;
while( !done ){
lcdClear();
lcdPrintln("Receiving key");
lcdPrintln("Recv. PUBKEY");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
@ -156,11 +155,11 @@ int receiveKeys(uint8_t *px, uint8_t *py, uint8_t *mac)
}
if( receivePublicKey(px,py) )
continue;
lcdPrintln("Got PUBKEY");
lcdRefresh();
if( receiveMac(mac) )
continue;
lcdPrintln("Done");
lcdClear();
lcdPrintln("Got PUBKEY"); lcdRefresh();
lcdPrintln("Hash:");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
@ -203,29 +202,16 @@ void receiveFile(void)
}
if( receiveR(rx,ry) )
continue;
lcdPrintln("Got R");
lcdPrintln("Creating key");
lcdRefresh();
ECIES_decryptkeygen(rx, ry, k1, k2, Priv);
#if 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) )
if( filetransfer_receive(mac,(uint32_t*)k1) < 0 )
continue;
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
#endif
while(1){
key = getInput();
delayms(20);
@ -239,7 +225,6 @@ void receiveFile(void)
}
}
}
}
void sendFile(char *filename)
@ -254,32 +239,20 @@ void sendFile(char *filename)
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 ){
lcdClear();
lcdPrintln("Sending file");lcdRefresh();
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);
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);
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
#endif
lcdRefresh();
while(1){
key = getInput();
delayms(20);
@ -290,6 +263,7 @@ void sendFile(char *filename)
break;
}
}
lcdClear();
}
}
@ -310,38 +284,17 @@ void main_vcard(void) {
while (1) {
key= getInput();
delayms(20);
// Easy flashing
if(key==BTN_LEFT){
DoString(0,8,"Enter ISP!");
lcdDisplay();
ISPandReset();
}else if(key==BTN_UP){
//lcdClear();
//lcdPrintln("Generating...");
//lcdRefresh();
char file[13];
selectFile(file,"TXT");
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){
lcdClear();
lcdPrintln("Generating...");
lcdRefresh();
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){
DoString(0,8,"MSC Enabled.");
lcdDisplay();
@ -349,13 +302,7 @@ void main_vcard(void) {
while(!getInputRaw())delayms(10);
DoString(0,16,"MSC Disabled.");
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];
uint16_t size;
uint8_t n;
UINT written = 0;
FIL file;
@ -73,7 +74,9 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
uint8_t metadata[32];
//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);
//lcdPrintln("got meta"); lcdRefresh();
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(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);
//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;
//nrf_set_rx_mac(0, 32, 5, mac);
lcdPrintln("get file"); lcdRefresh();
rftransfer_receive(buf, wordcount*4, 1000);
lcdPrintln("got file"); lcdRefresh();
//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;
//nrf_set_rx_mac(0, 32, 5, macbuf);
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;
if( written != size )
return 1; //error while writing
lcdClear();
lcdPrintln("Received"); lcdPrintln((const char*)metadata); lcdRefresh();
return 0;
}

View File

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

View File

@ -3,7 +3,7 @@
#include <stdint.h>
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