crashtest-r0ket/firmware/applications/vcard.c

366 lines
8.9 KiB
C
Raw Normal View History

2011-07-16 17:12:35 +00:00
#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"
FATFS FatFs[_VOLUMES]; /* File system object for logical drive */
/**************************************************************************/
uint8_t mac[5] = {1,2,3,2,1};
char *Px = "1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7";
char *Py = "45f46eb303edf2e62f74bd68368d979e265ee3c03";
char *Priv ="0e10e787036941e6c78daf8a0e8e1dbfac68e26d2";
void sendPublicKey(char *px, char *py)
{
2011-07-17 10:46:33 +00:00
uint8_t exp[2 + 4*NUMWORDS + 2];
2011-07-16 17:12:35 +00:00
exp[0] = 'P';
bitstr_parse_export((char*)exp+2, px);
exp[1] = 'X';
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32, exp);
2011-07-16 17:12:35 +00:00
delayms(10);
exp[1] = 'Y';
bitstr_parse_export((char*)exp+2, py);
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32, exp);
2011-07-16 17:12:35 +00:00
delayms(10);
}
void sendR(uint8_t *rx, uint8_t *ry)
{
2011-07-17 10:46:33 +00:00
uint8_t exp[2 + 4*NUMWORDS + 2];
2011-07-16 17:12:35 +00:00
exp[0] = 'R';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = rx[i];
exp[1] = 'X';
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32, exp);
2011-07-16 17:12:35 +00:00
delayms(10);
exp[1] = 'Y';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = ry[i];
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32, exp);
2011-07-16 17:12:35 +00:00
delayms(10);
}
int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
{
2011-07-17 10:46:33 +00:00
uint8_t buf[32];
2011-07-16 17:12:35 +00:00
uint8_t n;
2011-07-17 10:46:33 +00:00
n = nrf_rcv_pkt_time(1000, 32, buf);
2011-07-16 18:17:46 +00:00
lcdPrint("pkt:"); lcdPrintInt(n);lcdPrintln(""); lcdRefresh();
2011-07-17 10:46:33 +00:00
if( n == 32 && buf[0] == type && buf[1] == 'X' ){
2011-07-16 17:12:35 +00:00
for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2];
2011-07-17 10:46:33 +00:00
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] ==type && buf[1] == 'Y' ){
2011-07-16 17:12:35 +00:00
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)
{
2011-07-17 10:46:33 +00:00
uint8_t buf[32];
2011-07-16 17:12:35 +00:00
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];
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32, buf);
2011-07-16 17:12:35 +00:00
delayms(10);
}
int receiveMac(uint8_t *mac)
{
2011-07-17 10:46:33 +00:00
uint8_t buf[32];
2011-07-16 17:12:35 +00:00
uint8_t n;
2011-07-17 10:46:33 +00:00
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] == 'M' && buf[1] == 'C' ){
2011-07-16 17:12:35 +00:00
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 key");lcdRefresh();
sendPublicKey(Px,Py);
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("Receiving key");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
delayms(20);
if( key == BTN_DOWN ){
return -1;
}
if( receivePublicKey(px,py) )
continue;
2011-07-16 18:17:46 +00:00
lcdPrintln("Got PUBKEY");
lcdRefresh();
2011-07-16 17:12:35 +00:00
if( receiveMac(mac) )
continue;
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;
}
void receiveFile(void)
{
if( sendKeys() )
return;
uint8_t done = 0;
uint8_t key;
uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS];
while( !done ){
lcdClear();
2011-07-16 18:17:46 +00:00
lcdSetCrsr(0,0);
2011-07-16 17:12:35 +00:00
lcdPrintln("Receiving file");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
delayms(20);
if( key == BTN_DOWN ){
return -1;
}
if( receiveR(rx,ry) )
continue;
2011-07-16 18:17:46 +00:00
lcdPrintln("Got R");
lcdRefresh();
2011-07-16 17:12:35 +00:00
ECIES_decryptkeygen(rx, ry, k1, k2, Priv);
2011-07-17 09:26:52 +00:00
#if 0
lcdClear();
for(int i=0; i<16; i++){
lcdPrintCharHex(k1[i]);
if((i+1)%4==0)
lcdPrintln("");
}
lcdRefresh();
#else
2011-07-23 13:35:00 +00:00
//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) )
2011-07-17 09:26:52 +00:00
continue;
2011-07-16 17:12:35 +00:00
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
2011-07-17 09:26:52 +00:00
#endif
2011-07-16 17:12:35 +00:00
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];
ECIES_encyptkeygen(px, py, k1, k2, rx, ry);
while( !done ){
lcdClear();
lcdPrintln("Sending file");lcdRefresh();
sendR(rx,ry);
2011-07-16 18:17:46 +00:00
lcdPrintln("Sent R");
lcdRefresh();
2011-07-17 09:26:52 +00:00
#if 0
lcdClear();
for(int i=0; i<16; i++){
lcdPrintCharHex(k1[i]);
if((i+1)%4==0)
lcdPrintln("");
}
#else
2011-07-16 18:17:46 +00:00
delayms(3000);
2011-07-17 09:26:52 +00:00
uint32_t k[4] = {0xffff,0xffff,0xffff,0xffff};
2011-07-23 13:35:00 +00:00
k[0] = 0; k[1] = 0; k[2] = 0; k[3] = 0;
filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k1);
2011-07-16 17:12:35 +00:00
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
2011-07-17 09:26:52 +00:00
#endif
2011-07-16 17:12:35 +00:00
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}
}
}
}
void main_vcard(void) {
char key;
nrf_init();
f_mount(0, &FatFs[0]);
2011-07-17 09:26:52 +00:00
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);
2011-07-16 17:12:35 +00:00
while (1) {
key= getInput();
2011-07-16 18:17:46 +00:00
delayms(20);
2011-07-16 17:12:35 +00:00
// Easy flashing
if(key==BTN_LEFT){
DoString(0,8,"Enter ISP!");
lcdDisplay();
ISPandReset();
2011-07-16 17:12:35 +00:00
}else if(key==BTN_UP){
2011-07-17 09:26:52 +00:00
//lcdClear();
//lcdPrintln("Generating...");
//lcdRefresh();
char file[13];
selectFile(file,"TXT");
sendFile(file);
2011-07-16 17:12:35 +00:00
//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();
2011-07-17 09:26:52 +00:00
}else if(key==BTN_RIGHT){
2011-07-23 13:35:00 +00:00
DoString(0,8,"MSC Enabled.");
lcdDisplay();
usbMSCInit();
while(!getInputRaw())delayms(10);
DoString(0,16,"MSC Disabled.");
usbMSCOff();
2011-07-16 17:12:35 +00:00
}
//encryption_decryption_demo("This is encrypted",
// "1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7",
// "45f46eb303edf2e62f74bd68368d979e265ee3c03",
// "0e10e787036941e6c78daf8a0e8e1dbfac68e26d2");
}
}
void tick_vcard(void){
return;
};