LED blink

This commit is contained in:
NIIBE Yutaka
2010-12-09 10:12:54 +09:00
parent 8d7abb6108
commit e9e0b0f571
11 changed files with 174 additions and 59 deletions

View File

@@ -1,3 +1,19 @@
2010-12-09 NIIBE Yutaka <gniibe@fsij.org>
* src/usb-icc.c (icc_power_off): Set icc_data_size = 0 to specify
no command APDU. Signal GPGThread.
(icc_handle_data, USBthread): Don't signal main thread any more.
* src/openpgp.c (GPGthread): Only process the command APDU, if any.
* src/openpgp-do.c (do_tag_to_nr): Don't call fatal.
* src/main.c (fatal_code): New.
(main): Implemented 1-bit LED status display.
(fatal): Added argument CODE.
* src/flash.c (flash_data_pool_allocate): Supply argument FATAL_FLASH.
* src/random.c (random_bytes_get): Supply argument FATAL_RANDOM.
* src/ac.c (auth_status): Added volatile, and remove static.
2010-12-08 NIIBE Yutaka <gniibe@fsij.org> 2010-12-08 NIIBE Yutaka <gniibe@fsij.org>
* src/gnuk.h (AC_OTHER_AUTHORIZED): Renamed (was: * src/gnuk.h (AC_OTHER_AUTHORIZED): Renamed (was:

11
NEWS
View File

@@ -4,12 +4,19 @@ Gnuk NEWS - User visible changes
Released 2010-12-XX, by NIIBE Yutaka Released 2010-12-XX, by NIIBE Yutaka
** New board support "STM8S Discovery". ** LED blink
LED blink now shows status output the card. It shows the status of
CHV3, CHV2, and CHV1 when GPG is accessing the card.
** New board support "STM8S Discovery"
ST-Link part (with STM32F103C8T6) of STM8S Discovery board is now supported. ST-Link part (with STM32F103C8T6) of STM8S Discovery board is now supported.
** Digital signing for SHA224/SHA256/SHA384/SHA512 digestInfo is now possible. ** Digital signing for SHA224/SHA256/SHA384/SHA512 digestInfo is now possible.
** Fixes for password management. ** Fixes for password management
Now, you can allow card to do digital signing multiple times with
single authentication. You can use "forcesig" subcommand in card-edit
of GnuPG to enable the feature.
** Key management changes ** Key management changes
If you remove all keys, it is possible to import keys again. If you remove all keys, it is possible to import keys again.

1
THANKS
View File

@@ -5,6 +5,7 @@ Gnuk was originally written by NIIBE Yutaka. People contributed by
encouraging the development, testing the implementation, suggesting encouraging the development, testing the implementation, suggesting
improvements, or fixing bugs. Here is a list of those people. improvements, or fixing bugs. Here is a list of those people.
Andre Zepezauer andre.zepezauer@student.uni-halle.de
Hironobu SUZUKI hironobu@h2np.net Hironobu SUZUKI hironobu@h2np.net
Jan Suhr jan@suhr.info Jan Suhr jan@suhr.info
Kaz Kojima kkojima@rr.iij4u.or.jp Kaz Kojima kkojima@rr.iij4u.or.jp

View File

@@ -28,7 +28,7 @@
#include "polarssl/config.h" #include "polarssl/config.h"
#include "polarssl/sha1.h" #include "polarssl/sha1.h"
static uint8_t auth_status = AC_NONE_AUTHORIZED; uint8_t volatile auth_status = AC_NONE_AUTHORIZED;
int int
ac_check_status (uint8_t ac_flag) ac_check_status (uint8_t ac_flag)

View File

@@ -280,7 +280,7 @@ flash_data_pool_allocate (size_t size)
if (is_data_pool_full (size)) if (is_data_pool_full (size))
if (flash_copying_gc () < 0 || /*still*/ is_data_pool_full (size)) if (flash_copying_gc () < 0 || /*still*/ is_data_pool_full (size))
fatal (); fatal (FATAL_FLASH);
p = last_p; p = last_p;
last_p += size; last_p += size;

View File

@@ -1,7 +1,3 @@
extern Thread *blinker_thread;
#define EV_LED_ON ((eventmask_t)1)
#define EV_LED_OFF ((eventmask_t)2)
extern Thread *stdout_thread; extern Thread *stdout_thread;
#define EV_TX_READY ((eventmask_t)1) #define EV_TX_READY ((eventmask_t)1)
@@ -51,6 +47,20 @@ extern int res_APDU_size;
/ USB_LL_BUF_SIZE * USB_LL_BUF_SIZE) / USB_LL_BUF_SIZE * USB_LL_BUF_SIZE)
extern uint8_t icc_buffer[USB_BUF_SIZE]; extern uint8_t icc_buffer[USB_BUF_SIZE];
enum icc_state
{
ICC_STATE_START, /* Initial */
ICC_STATE_WAIT, /* Waiting APDU */
/* Busy1, Busy2, Busy3, Busy5 */
ICC_STATE_EXECUTE, /* Busy4 */
ICC_STATE_RECEIVE, /* APDU Received Partially */
/* Not used */
ICC_STATE_SEND, /* APDU Sent Partially */
};
extern volatile enum icc_state icc_state;
extern volatile uint8_t auth_status;
#define AC_NONE_AUTHORIZED 0x00 #define AC_NONE_AUTHORIZED 0x00
#define AC_PSO_CDS_AUTHORIZED 0x01 /* PW1 with 0x81 verified */ #define AC_PSO_CDS_AUTHORIZED 0x01 /* PW1 with 0x81 verified */
#define AC_OTHER_AUTHORIZED 0x02 /* PW1 with 0x82 verified */ #define AC_OTHER_AUTHORIZED 0x02 /* PW1 with 0x82 verified */
@@ -185,7 +195,9 @@ extern void gpg_increment_digital_signature_counter (void);
extern void gpg_set_pw3 (const uint8_t *newpw, int newpw_len); extern void gpg_set_pw3 (const uint8_t *newpw, int newpw_len);
extern void fatal (void) __attribute__ ((noreturn)); extern void fatal (uint8_t code) __attribute__ ((noreturn));
#define FATAL_FLASH 1
#define FATAL_RANDOM 2
extern uint8_t keystring_md_pw3[KEYSTRING_MD_SIZE]; extern uint8_t keystring_md_pw3[KEYSTRING_MD_SIZE];

View File

@@ -155,30 +155,31 @@ _write (const char *s, int size)
static WORKING_AREA(waUSBthread, 128); static WORKING_AREA(waUSBthread, 128);
extern msg_t USBthread (void *arg); extern msg_t USBthread (void *arg);
Thread *blinker_thread;
/* /*
* LEDs blinks. * main thread does 1-bit LED display output
* When GPGthread execute some command, LED stop blinking, but always ON.
*/ */
#define LED_BLINKER_TIMEOUT MS2ST(200) #define LED_TIMEOUT_INTERVAL MS2ST(100)
#define LED_TIMEOUT_ZERO MS2ST(50)
#define LED_TIMEOUT_ONE MS2ST(200)
#define LED_TIMEOUT_STOP MS2ST(500)
static volatile uint8_t fatal_code;
/* /*
* Entry point, note, the main() function is already a thread in the system * Entry point.
* on entry. *
* NOTE: the main function is already a thread in the system on entry.
* See the hwinit1_common function.
*/ */
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
eventmask_t m;
uint8_t led_state = 0;
int count = 0; int count = 0;
(void)argc; (void)argc;
(void)argv; (void)argv;
blinker_thread = chThdSelf ();
usb_lld_init (); usb_lld_init ();
USB_Init(); USB_Init();
@@ -199,26 +200,108 @@ main (int argc, char **argv)
{ {
count++; count++;
m = chEvtWaitOneTimeout (ALL_EVENTS, LED_BLINKER_TIMEOUT); if (fatal_code != 0)
if (m == EV_LED_ON)
led_state = 1;
else if (m == EV_LED_OFF)
led_state = 0;
if (led_state)
set_led (1);
else
{ {
if ((count & 1)) set_led (1);
set_led (1); chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_STOP);
set_led (1);
if (fatal_code & 1)
chThdSleep (LED_TIMEOUT_ONE);
else else
set_led (0); chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
if (fatal_code & 2)
chThdSleep (LED_TIMEOUT_ONE);
else
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_STOP);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
}
if (bDeviceState != CONFIGURED)
{
set_led (1);
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_STOP * 3);
} }
else
/* Device configured */
if (icc_state == ICC_STATE_START)
{
set_led (1);
chThdSleep (LED_TIMEOUT_ONE);
set_led (0);
chThdSleep (LED_TIMEOUT_STOP * 3);
}
else
/* GPGthread running */
{
set_led (1);
if ((auth_status & AC_ADMIN_AUTHORIZED) != 0)
chThdSleep (LED_TIMEOUT_ONE);
else
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
if ((auth_status & AC_OTHER_AUTHORIZED) != 0)
chThdSleep (LED_TIMEOUT_ONE);
else
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
if ((auth_status & AC_PSO_CDS_AUTHORIZED) != 0)
chThdSleep (LED_TIMEOUT_ONE);
else
chThdSleep (LED_TIMEOUT_ZERO);
if (icc_state == ICC_STATE_WAIT)
{
set_led (0);
chThdSleep (LED_TIMEOUT_STOP * 2);
}
else if (icc_state == ICC_STATE_RECEIVE)
{
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_ONE);
set_led (0);
chThdSleep (LED_TIMEOUT_STOP);
}
else
{
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_STOP);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
}
}
#ifdef DEBUG_MORE #ifdef DEBUG_MORE
if (bDeviceState == CONFIGURED && (count % 100) == 0) if (bDeviceState == CONFIGURED && (count % 10) == 0)
{ {
DEBUG_SHORT (count / 100); DEBUG_SHORT (count / 10);
_write ("\r\nThis is ChibiOS 2.0.8 on STM32.\r\n" _write ("\r\nThis is ChibiOS 2.0.8 on STM32.\r\n"
"Testing USB driver.\n\n" "Testing USB driver.\n\n"
"Hello world\r\n\r\n", 35+21+15); "Hello world\r\n\r\n", 35+21+15);
@@ -230,8 +313,9 @@ main (int argc, char **argv)
} }
void void
fatal (void) fatal (uint8_t code)
{ {
fatal_code = code;
_write ("fatal\r\n", 7); _write ("fatal\r\n", 7);
for (;;); for (;;);
} }

View File

@@ -282,7 +282,7 @@ do_tag_to_nr (uint16_t tag)
case GPG_DO_LANGUAGE: case GPG_DO_LANGUAGE:
return NR_DO_LANGUAGE; return NR_DO_LANGUAGE;
default: default:
fatal (); return NR_NONE;
} }
} }
@@ -1231,11 +1231,16 @@ gpg_do_put_data (uint16_t tag, const uint8_t *data, int len)
{ {
uint8_t nr = do_tag_to_nr (tag); uint8_t nr = do_tag_to_nr (tag);
*do_data_p = flash_do_write (nr, data, len); if (nr == NR_NONE)
if (*do_data_p)
GPG_SUCCESS ();
else
GPG_MEMORY_FAILURE (); GPG_MEMORY_FAILURE ();
else
{
*do_data_p = flash_do_write (nr, data, len);
if (*do_data_p)
GPG_SUCCESS ();
else
GPG_MEMORY_FAILURE ();
}
} }
break; break;
} }

View File

@@ -751,9 +751,11 @@ GPGthread (void *arg)
DEBUG_INFO ("GPG!: "); DEBUG_INFO ("GPG!: ");
DEBUG_WORD ((uint32_t)&m); DEBUG_WORD ((uint32_t)&m);
process_command_apdu (); if (icc_data_size != 0)
{
chEvtSignal (icc_thread, EV_EXEC_FINISHED); process_command_apdu ();
chEvtSignal (icc_thread, EV_EXEC_FINISHED);
}
} }
gpg_fini (); gpg_fini ();

View File

@@ -45,7 +45,7 @@ random_bytes_get (void)
addr = ((uint32_t)&_binary_random_bits_start); addr = ((uint32_t)&_binary_random_bits_start);
if (addr == addr0) if (addr == addr0)
fatal (); fatal (FATAL_RANDOM);
} }
return (const uint8_t *)addr; return (const uint8_t *)addr;

View File

@@ -223,18 +223,7 @@ EP2_OUT_Callback (void)
} }
} }
enum icc_state volatile enum icc_state icc_state;
{
ICC_STATE_START, /* Initial */
ICC_STATE_WAIT, /* Waiting APDU */
/* Busy1, Busy2, Busy3, Busy5 */
ICC_STATE_EXECUTE, /* Busy4 */
ICC_STATE_RECEIVE, /* APDU Received Partially */
/* Not used */
ICC_STATE_SEND, /* APDU Sent Partially */
};
static enum icc_state icc_state;
/* /*
* ATR (Answer To Reset) string * ATR (Answer To Reset) string
@@ -368,9 +357,12 @@ icc_send_status (void)
enum icc_state enum icc_state
icc_power_off (void) icc_power_off (void)
{ {
icc_data_size = 0;
if (gpg_thread) if (gpg_thread)
{ {
chThdTerminate (gpg_thread); chThdTerminate (gpg_thread);
chEvtSignal (gpg_thread, (eventmask_t)1);
chThdWait (gpg_thread); chThdWait (gpg_thread);
gpg_thread = NULL; gpg_thread = NULL;
} }
@@ -515,7 +507,6 @@ icc_handle_data (void)
{ /* Give this message to GPG thread */ { /* Give this message to GPG thread */
chEvtSignal (gpg_thread, (eventmask_t)1); chEvtSignal (gpg_thread, (eventmask_t)1);
next_state = ICC_STATE_EXECUTE; next_state = ICC_STATE_EXECUTE;
chEvtSignal (blinker_thread, EV_LED_ON);
} }
else if (icc_header->param == 1) else if (icc_header->param == 1)
{ {
@@ -558,7 +549,6 @@ icc_handle_data (void)
icc_data_size = icc_next_p - icc_buffer - ICC_MSG_HEADER_SIZE; icc_data_size = icc_next_p - icc_buffer - ICC_MSG_HEADER_SIZE;
icc_chain_p = NULL; icc_chain_p = NULL;
next_state = ICC_STATE_EXECUTE; next_state = ICC_STATE_EXECUTE;
chEvtSignal (blinker_thread, EV_LED_ON);
chEvtSignal (gpg_thread, (eventmask_t)1); chEvtSignal (gpg_thread, (eventmask_t)1);
} }
else /* icc_header->param == 3 is not supported. */ else /* icc_header->param == 3 is not supported. */
@@ -639,8 +629,6 @@ USBthread (void *arg)
{ {
if (icc_state == ICC_STATE_EXECUTE) if (icc_state == ICC_STATE_EXECUTE)
{ {
chEvtSignal (blinker_thread, EV_LED_OFF);
icc_send_data_block_filling_header (res_APDU_size); icc_send_data_block_filling_header (res_APDU_size);
icc_state = ICC_STATE_WAIT; icc_state = ICC_STATE_WAIT;
} }