diff options
| -rw-r--r-- | app/crypto.c | 31 | ||||
| -rw-r--r-- | app/main.c | 1 | ||||
| -rw-r--r-- | app/project.h | 3 | ||||
| -rw-r--r-- | app/prototypes.h | 23 | ||||
| -rw-r--r-- | app/state.c | 10 | ||||
| -rw-r--r-- | app/ticker.c | 7 | ||||
| -rw-r--r-- | app/usb.c | 24 | ||||
| -rw-r--r-- | common/vendor_req.h | 5 | ||||
| -rw-r--r-- | host/Makefile | 19 | ||||
| -rw-r--r-- | host/cryptopad.c | 122 | ||||
| -rw-r--r-- | host/get.c | 103 | ||||
| -rw-r--r-- | host/main.c | 104 | ||||
| -rw-r--r-- | host/project.h | 1 | ||||
| -rw-r--r-- | host/set.c | 103 | 
14 files changed, 201 insertions, 355 deletions
diff --git a/app/crypto.c b/app/crypto.c index 494e5ab..a725def 100644 --- a/app/crypto.c +++ b/app/crypto.c @@ -2,6 +2,9 @@  #include "../common/vendor_req.h" +static int key_send_timer = 0; +static int start_key_send = 0; +  int  crypto_control (uint8_t request, uint16_t value, uint16_t index, void *buf,                  size_t len) @@ -18,10 +21,15 @@ crypto_control (uint8_t request, uint16_t value, uint16_t index, void *buf,        send_ascii (value);        return 1;      case VENDOR_REQ_SET_KEY: +      state_init ();        key_set (buf, len);        return 1;      case VENDOR_REQ_SEND_KEY: -      key_send (); + +      if (!value) +        start_key_send = 1; +      else +        key_send_timer = value;        return 1;      case VENDOR_REQ_CLEAR_KEY:        key_wipe (); @@ -46,3 +54,24 @@ vendor_control_request (usbd_device * usbd_dev, struct usb_setup_data *req,    return crypto_control (req->bRequest, req->wValue, req->wIndex, *buf, *len);  } + +void +crypto_tick (void) +{ +  if (!key_send_timer) +    return; +  key_send_timer--; + +  if (!key_send_timer) +    start_key_send = 1; +} + +void +crypto_poll (void) +{ +  if (start_key_send) +    { +      start_key_send = 0; +      key_send (); +    } +} @@ -20,6 +20,7 @@ main (void)    rcc_periph_clock_enable (RCC_GPIOC);    rcc_periph_clock_enable (RCC_AFIO); +    ticker_init ();    led_init ();  #ifndef SLIM diff --git a/app/project.h b/app/project.h index a8b773b..6fccdc5 100644 --- a/app/project.h +++ b/app/project.h @@ -30,6 +30,9 @@  #define POWER_RETENTION_TIME 10  #define USB_RETENTION_TIME 60 +#define POWER_LOCK_TIME 500 +#define USB_LOCK_TIME 500 +  #ifndef USB_REQ_TYPE_OUT  #define USB_REQ_TYPE_OUT 0x0  #endif diff --git a/app/prototypes.h b/app/prototypes.h index 34d7863..2e78350 100644 --- a/app/prototypes.h +++ b/app/prototypes.h @@ -28,26 +28,7 @@ extern void delay_ms(uint32_t d);  extern int timed_out(uint32_t then, unsigned int ms);  extern void ticker_init(void);  /* i2c.c */ -extern int i2c_bb(int scl, int sda); -extern void i2c_bb_start(void); -extern void i2c_bb_stop(void); -extern int i2c_bb_send_data(uint8_t v); -extern int i2c_bb_start_transaction(uint8_t a, int wnr); -extern void i2c_bb_init(void);  /* lcd.c */ -extern uint8_t fb[2][16]; -extern uint8_t shadow[2][16]; -extern void lcd_refresh(void); -extern void lcd_tick(void); -extern void lcd_write_char(uint8_t c, int x, int y); -extern void lcd_erase(int x, int y, int w); -extern void lcd_erase_line(int w, int y); -extern void lcd_erase_all(void); -extern void lcd_write(char *c, int x, int y); -extern void lcd_backlight(int i); -extern void lcd_reset(void); -extern void lcd_init(void); -extern void lcd_shutdown(void);  /* adc.c */  extern int host_has_power;  extern void adc_tick(void); @@ -55,6 +36,8 @@ extern void adc_init(void);  /* state.c */  extern uint32_t up_time;  extern uint32_t down_time; +extern uint32_t usb_up_time; +extern uint32_t usb_down_time;  extern int locked;  extern void state_show(void);  extern void state_tick(void); @@ -73,6 +56,8 @@ extern void keyboard_keypress(int m, int k);  /* crypto.c */  extern int crypto_control(uint8_t request, uint16_t value, uint16_t index, void *buf, size_t len);  extern int vendor_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, int (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); +extern void crypto_tick(void); +extern void crypto_poll(void);  /* map.c */  extern void send_ascii(unsigned char a);  extern void send_str(const char *c); diff --git a/app/state.c b/app/state.c index 0c23260..eb033ac 100644 --- a/app/state.c +++ b/app/state.c @@ -1,6 +1,6 @@  #include "project.h" -uint32_t up_time, down_time,usb_up_time,usb_down_time; +uint32_t up_time, down_time, usb_up_time, usb_down_time;  int locked; @@ -77,6 +77,11 @@ state_tick (void)        key_wipe ();      } +  if ((usb_up_time > USB_LOCK_TIME) && !locked) +    locked = 1; +  if ((up_time > POWER_LOCK_TIME) && !locked) +    locked = 1; +    state_show ();  } @@ -87,5 +92,8 @@ state_init (void)  {    down_time = 0;    up_time = 0; +  usb_down_time = 0; +  usb_up_time = 0; +  up_time = 0;    locked = 0;  } diff --git a/app/ticker.c b/app/ticker.c index 3e25c12..6ef8a5f 100644 --- a/app/ticker.c +++ b/app/ticker.c @@ -22,6 +22,7 @@ s_tick_handler (void)  {    adc_tick ();    state_tick (); +  crypto_tick ();  }  void @@ -39,18 +40,14 @@ sys_tick_handler (void)    lcd_tick ();  #endif -  usb_tick(); +  usb_tick (); -#if 1    s_count++;    if (s_count >= 1000)      {        s_count = 0;        s_tick_handler ();      } -#else -      s_tick_handler (); -#endif  } @@ -58,22 +58,25 @@ usbd_device *usbd_dev;  int usb_is_suspended = 0; -int usb_running =0; +int usb_running = 0;  static int time_since_sof; -void usb_tick(void) +void +usb_tick (void)  { -time_since_sof++; -if (time_since_sof>3000) usb_running=0; +  time_since_sof++; +  if (time_since_sof > 3000) +    usb_running = 0;  } -static void usb_sof(void) +static void +usb_sof (void)  { -usb_running=1; -time_since_sof=0; +  usb_running = 1; +  time_since_sof = 0;  }  static void @@ -184,7 +187,7 @@ usb_init (void)    usbd_register_set_config_callback (usbd_dev, usb_set_config);    usbd_register_suspend_callback (usbd_dev, usb_suspended);    usbd_register_resume_callback (usbd_dev, usb_resumed); -  usbd_register_sof_callback(usbd_dev, usb_sof); +  usbd_register_sof_callback (usbd_dev, usb_sof);  } @@ -192,5 +195,8 @@ void  usb_run (void)  {    for (;;) -    usbd_poll (usbd_dev); +    { +      usbd_poll (usbd_dev); +      crypto_poll (); +    }  } diff --git a/common/vendor_req.h b/common/vendor_req.h new file mode 100644 index 0000000..50e3e36 --- /dev/null +++ b/common/vendor_req.h @@ -0,0 +1,5 @@ +#define VENDOR_REQ_KEYPRESS 1 +#define VENDOR_REQ_SEND_KEY 2 +#define VENDOR_REQ_SET_KEY 3 +#define VENDOR_REQ_CLEAR_KEY 4 + diff --git a/host/Makefile b/host/Makefile index 317bc59..2013f89 100644 --- a/host/Makefile +++ b/host/Makefile @@ -2,23 +2,22 @@ INCLUDES=$(shell pkg-config --cflags libusb-1.0)  LIBS=-Bstatic $(shell pkg-config --libs libusb-1.0) -lpthread -Bdynamic -ludev  LDFLAGS=-s -PROGS=main set get -LIBSRCS=hexdump.c +PROG=cryptopad +SRCS=hexdump.c cryptopad.c -LIBOBJS=${LIBSRCS:%.c=%.o} +OBJS=${SRCS:%.c=%.o}  CFLAGS=${OPT}   CPPFLAGS=${INCLUDES} ${DEFINES} -default:${PROGS}  +default:${PROG}  -${PROGS}: %:%.o ${LIBOBJS} -	${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $@.o ${OBJS} ${LIBS} - -#${PROG}:${OBJS} -#	${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS} ${LIBS} +${PROG}:${OBJS} +	${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS} ${LIBS} +install: ${PROG} +	install -c -m 755 ${PROG} /sbin/${PROG}  clean: -	/bin/rm -f *~ *.d ${PROGS} ${OBJS} +	/bin/rm -f *~ *.d ${PROG} ${OBJS} diff --git a/host/cryptopad.c b/host/cryptopad.c new file mode 100644 index 0000000..42b1d36 --- /dev/null +++ b/host/cryptopad.c @@ -0,0 +1,122 @@ +#include "project.h" +#include "../common/vendor_req.h" + +#define TIMEOUT 4000 + +struct libusb_device * find_device(libusb_context *ctx) +{ +        libusb_device **list; +        ssize_t num_devs; +        ssize_t i; + +        num_devs = libusb_get_device_list(ctx, &list); +        for (i = 0; i < num_devs; ++i) { +                struct libusb_device_descriptor desc; +                struct libusb_device *dev = list[i]; + +                if (libusb_get_device_descriptor(dev, &desc)) +                        continue; + + +		if (desc.idVendor!=0x1d6b) continue; +		if (desc.idProduct!=0x1932) continue; +		 +		return dev; + +        } +        libusb_free_device_list(list, 0); + +	return NULL; +} + + +static void usage(char *n) +{ +fprintf(stderr,"Usage:\n" +	"%s [-g [-n secs]] [-s [-p pwd]] [-h]\n" +	"        -h       display this message\n" +	"        -g       ask cryptopad to type password\n" +	"        -n secs  wait secs before typing password\n" +	"        -s       set password in cryptopad\n" +	"        -p pwd   password to set, prompts otherwise\n" +	"        -w       wipe password from cryptopad\n", +	n); + +exit(1); +} + + + + +int main(int argc,char *argv[]) +{ +        libusb_context *ctx; +	libusb_device *dev; +	libusb_device_handle *devh; +	int wflag=0,gflag=0,sflag=0; +	char *pass=NULL; +        int c,ret; +        int delay=0; + + +        while((c=getopt(argc,argv,"hwgsp:n:"))!=-1) { +	switch(c) { +		case 'n': +		if (optarg) delay=atoi(optarg); +		break; +		case 'g': +		gflag++; +		break; +		case 's': +		sflag++; +		break; +		case 'w': +		wflag++; +		break; +		case 'p': +		pass=optarg; +		break; +		default: +		usage(argv[0]); +	} +	} + +        if ((ret=libusb_init(&ctx))) +                errx(EX_IOERR, "unable to initialize libusb: %i", ret); + +	dev=find_device(ctx); + +	if (!dev) err(1,"no cryptopad found"); + +	if ((ret=libusb_open(dev, &devh))) err(1,"unable to open usb device: %i",ret); + + +	if (sflag) { +		char *pwd; +		if (!pass) pass=getpass("Enter password:"); + +		pwd=malloc(strlen(pass)+2); +		strcpy(pwd,pass); +		strcat(pwd,"\n"); + +		libusb_control_transfer( devh, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, VENDOR_REQ_SET_KEY, 0, 0, pwd, strlen(pwd), TIMEOUT ); +	} + + +	if (gflag)  +		libusb_control_transfer( devh, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, VENDOR_REQ_SEND_KEY, delay, 0, NULL, 0, TIMEOUT ); + +	if (wflag)  +		libusb_control_transfer( devh, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, VENDOR_REQ_CLEAR_KEY, 0, 0, NULL, 0, TIMEOUT ); + + +	libusb_close(devh); + +	return 0; +} + + +	 + + + diff --git a/host/get.c b/host/get.c deleted file mode 100644 index 79a6a40..0000000 --- a/host/get.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "project.h" -#include "../common/vendor_req.h" - - - -static void poke(libusb_device_handle *devh) -{ -uint32_t timeout=4000; -int len; - - - -len=    libusb_control_transfer( devh, -        /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, -        /* bRequest      */ VENDOR_REQ_SEND_KEY, -        /* wValue        */ 0x23, -        /* wIndex        */ 0, -        /* Data          */ NULL, -        /* wLength       */ 0, timeout ); - - - - -//if (len>=0) -//hexdump(">",buf,len); - - -} - - - -static void poke_device(libusb_device *dev, struct libusb_device_descriptor *desc) -{ -int ret; -libusb_device_handle *devh; - -ret=libusb_open(dev, &devh); - -if (ret) { -	warn("unable to open device: %i",ret); -	return; -} - -printf("poke\n"); - -poke(devh); - -libusb_close(devh); - -} - -void probe_devices(libusb_context *ctx) -{ -        libusb_device **list; -        ssize_t num_devs; -        ssize_t i; - -        num_devs = libusb_get_device_list(ctx, &list); -        for (i = 0; i < num_devs; ++i) { -                struct libusb_device_descriptor desc; -                struct libusb_device *dev = list[i]; - -                if (libusb_get_device_descriptor(dev, &desc)) -                        continue; - - -		if (desc.idVendor!=0x1d6b) continue; -		if (desc.idProduct!=0x1932) continue; - -		poke_device(dev,&desc); - -        } -        libusb_free_device_list(list, 0); -} - - - - - -int main(int argc,char *argv) -{ -	int ret; - -        libusb_context *ctx; - - -        ret = libusb_init(&ctx); -        if (ret)  -                errx(EX_IOERR, "unable to initialize libusb: %i", ret); - - -	//libusb_set_debug(ctx, 255); - -        probe_devices(ctx); - -	return 0; -} - - -	 - - - diff --git a/host/main.c b/host/main.c deleted file mode 100644 index 5a61963..0000000 --- a/host/main.c +++ /dev/null @@ -1,104 +0,0 @@ -#include "project.h" -#include "../common/vendor_req.h" - - - -static void poke(libusb_device_handle *devh) -{ -uint32_t timeout=4000; -char buf[128]; -int len; - - - -len=    libusb_control_transfer( devh, -        /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, -        /* bRequest      */ VENDOR_REQ_KEYPRESS, -        /* wValue        */ 0x23, -        /* wIndex        */ 0, -        /* Data          */ buf, -        /* wLength       */ sizeof(buf), timeout ); - - - - -//if (len>=0) -//hexdump(">",buf,len); - - -} - - - -static void poke_device(libusb_device *dev, struct libusb_device_descriptor *desc) -{ -int ret; -libusb_device_handle *devh; - -ret=libusb_open(dev, &devh); - -if (ret) { -	warn("unable to open device: %i",ret); -	return; -} - -printf("poke\n"); - -poke(devh); - -libusb_close(devh); - -} - -void probe_devices(libusb_context *ctx) -{ -        libusb_device **list; -        ssize_t num_devs; -        ssize_t i; - -        num_devs = libusb_get_device_list(ctx, &list); -        for (i = 0; i < num_devs; ++i) { -                struct libusb_device_descriptor desc; -                struct libusb_device *dev = list[i]; - -                if (libusb_get_device_descriptor(dev, &desc)) -                        continue; - - -		if (desc.idVendor!=0x1d6b) continue; -		if (desc.idProduct!=0x1932) continue; - -		poke_device(dev,&desc); - -        } -        libusb_free_device_list(list, 0); -} - - - - - -int main(int argc,char *argv) -{ -	int ret; - -        libusb_context *ctx; - - -        ret = libusb_init(&ctx); -        if (ret)  -                errx(EX_IOERR, "unable to initialize libusb: %i", ret); - - -	//libusb_set_debug(ctx, 255); - -        probe_devices(ctx); - -	return 0; -} - - -	 - - - diff --git a/host/project.h b/host/project.h index 2302570..cf932b6 100644 --- a/host/project.h +++ b/host/project.h @@ -5,6 +5,7 @@  #include <unistd.h>  #include <stdlib.h>  #include <libusb.h> +#include <stdarg.h>  #ifdef HAVE_ERR  # include <err.h> diff --git a/host/set.c b/host/set.c deleted file mode 100644 index e004c4f..0000000 --- a/host/set.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "project.h" -#include "../common/vendor_req.h" - - - -static void poke(libusb_device_handle *devh) -{ -uint32_t timeout=4000; -//char buf[]="The quick brown fox jumps over the lazy dog\n"; -char buf[]="fishsoup\n"; -int len; - - - -len=    libusb_control_transfer( devh, -        /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, -        /* bRequest      */ VENDOR_REQ_SET_KEY, -        /* wValue        */ 0x0, -        /* wIndex        */ 0x0, -        /* Data          */ buf, -        /* wLength       */ sizeof(buf)-1, timeout ); - - -//if (len>=0) -//hexdump(">",buf,len); - - -} - - - -static void poke_device(libusb_device *dev, struct libusb_device_descriptor *desc) -{ -int ret; -libusb_device_handle *devh; - -ret=libusb_open(dev, &devh); - -if (ret) { -	warn("unable to open device: %i",ret); -	return; -} - -printf("poke\n"); - -poke(devh); - -libusb_close(devh); - -} - -void probe_devices(libusb_context *ctx) -{ -        libusb_device **list; -        ssize_t num_devs; -        ssize_t i; - -        num_devs = libusb_get_device_list(ctx, &list); -        for (i = 0; i < num_devs; ++i) { -                struct libusb_device_descriptor desc; -                struct libusb_device *dev = list[i]; - -                if (libusb_get_device_descriptor(dev, &desc)) -                        continue; - - -		if (desc.idVendor!=0x1d6b) continue; -		if (desc.idProduct!=0x1932) continue; - -		poke_device(dev,&desc); - -        } -        libusb_free_device_list(list, 0); -} - - - - - -int main(int argc,char *argv) -{ -	int ret; - -        libusb_context *ctx; - - -        ret = libusb_init(&ctx); -        if (ret)  -                errx(EX_IOERR, "unable to initialize libusb: %i", ret); - - -	//libusb_set_debug(ctx, 255); - -        probe_devices(ctx); - -	return 0; -} - - -	 - - -  | 
