2 * usb-icc.c -- USB CCID/ICCD protocol handling
4 * Copyright (C) 2010 Free Software Initiative of Japan
5 * Author: NIIBE Yutaka <gniibe@fsij.org>
7 * This file is a part of Gnuk, a GnuPG USB Token implementation.
9 * Gnuk is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * Gnuk is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 * License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "hw_config.h"
34 extern void *memmove(void *dest, const void *src, size_t n);
36 #define ICC_SET_PARAMS 0x61 /* non-ICCD command */
37 #define ICC_POWER_ON 0x62
38 #define ICC_POWER_OFF 0x63
39 #define ICC_SLOT_STATUS 0x65 /* non-ICCD command */
40 #define ICC_GET_PARAMS 0x6C /* non-ICCD command */
41 #define ICC_XFR_BLOCK 0x6F
42 #define ICC_DATA_BLOCK_RET 0x80
43 #define ICC_SLOT_STATUS_RET 0x81 /* non-ICCD result */
44 #define ICC_PARAMS_RET 0x82 /* non-ICCD result */
46 #define ICC_MSG_SEQ_OFFSET 6
47 #define ICC_MSG_STATUS_OFFSET 7
48 #define ICC_MSG_ERROR_OFFSET 8
49 #define ICC_MSG_CHAIN_OFFSET 9
50 #define ICC_MSG_DATA_OFFSET 10 /* == ICC_MSG_HEADER_SIZE */
51 #define ICC_MAX_MSG_DATA_SIZE (USB_BUF_SIZE - ICC_MSG_HEADER_SIZE)
53 #define ICC_STATUS_RUN 0x00
54 #define ICC_STATUS_PRESENT 0x01
55 #define ICC_STATUS_NOTPRESENT 0x02
56 #define ICC_CMD_STATUS_OK 0x00
57 #define ICC_CMD_STATUS_ERROR 0x40
58 #define ICC_CMD_STATUS_TIMEEXT 0x80
60 #define ICC_ERROR_XFR_OVERRUN 0xFC
63 * Since command-byte is at offset 0,
64 * error with offset 0 means "command not supported".
66 #define ICC_OFFSET_CMD_NOT_SUPPORTED 0
67 #define ICC_OFFSET_PARAM 8
76 } __attribute__((packed));
81 * USB-ICC communication could be considered "half duplex".
83 * While the device is sending something, there is no possibility for
84 * the device to receive anything.
86 * While the device is receiving something, there is no possibility
87 * for the device to send anything.
89 * Thus, the buffer can be shared for RX and TX.
93 * Buffer of USB communication: for both of RX and TX
95 * The buffer will be filled by multiple RX transactions (Bulk-OUT)
96 * or will be used for multiple TX transactions (Bulk-IN)
98 uint8_t icc_buffer[USB_BUF_SIZE];
102 * Pointer to ICC_BUFFER
104 static uint8_t *icc_next_p;
107 * Chain pointer: This implementation support two packets in chain (not more)
109 static uint8_t *icc_chain_p;
112 * Whole size of TX transfer (Bulk-IN transactions)
114 static int icc_tx_size;
116 static Thread *icc_thread;
118 #define EV_RX_DATA_READY (eventmask_t)1 /* USB Rx data available */
119 /* EV_EXEC_FINISHED == 2 */
120 #define EV_TX_FINISHED (eventmask_t)4 /* USB Tx finished */
126 EP1_IN_Callback (void)
128 if (icc_next_p == NULL)
129 /* The sequence of Bulk-IN transactions finished */
130 chEvtSignalI (icc_thread, EV_TX_FINISHED);
131 else if (icc_next_p == &icc_buffer[icc_tx_size])
132 /* It was multiple of USB_LL_BUF_SIZE */
134 /* Send the last 0-DATA transcation of Bulk-IN in the transactions */
136 USB_SIL_Write (EP1_IN, icc_buffer, 0);
137 SetEPTxValid (ENDP1);
141 int tx_size = USB_LL_BUF_SIZE;
142 uint8_t *p = icc_next_p;
144 icc_next_p += USB_LL_BUF_SIZE;
145 if (icc_next_p > &icc_buffer[icc_tx_size])
148 tx_size = &icc_buffer[icc_tx_size] - p;
151 USB_SIL_Write (EP1_IN, p, tx_size);
152 SetEPTxValid (ENDP1);
157 icc_prepare_receive (int chain)
160 icc_next_p = icc_chain_p;
162 icc_next_p = icc_buffer;
164 SetEPRxValid (ENDP2);
171 EP2_OUT_Callback (void)
175 len = USB_SIL_Read (EP2_OUT, icc_next_p);
177 if (len == USB_LL_BUF_SIZE) /* The sequence of transactions continues */
179 icc_next_p += USB_LL_BUF_SIZE;
180 SetEPRxValid (ENDP2);
181 if ((icc_next_p - icc_buffer) >= USB_BUF_SIZE)
182 /* No room to receive any more */
184 DEBUG_INFO ("ERR0F\r\n");
185 icc_next_p -= USB_LL_BUF_SIZE; /* Just for not overrun the buffer */
187 * Receive until the end of the sequence
188 * (and discard the whole block)
194 struct icc_header *icc_header;
200 icc_header = (struct icc_header *)icc_chain_p;
201 icc_data_size = (icc_next_p - icc_chain_p) - ICC_MSG_HEADER_SIZE;
205 icc_header = (struct icc_header *)icc_buffer;
206 icc_data_size = (icc_next_p - icc_buffer) - ICC_MSG_HEADER_SIZE;
209 /* NOTE: We're little endian, nothing to convert */
210 data_len = icc_header->data_len;
211 icc_seq = icc_header->seq;
213 if (icc_data_size != data_len)
215 DEBUG_INFO ("ERR0E\r\n");
216 /* Ignore the whole block */
218 icc_prepare_receive (0);
221 /* Notify icc_thread */
222 chEvtSignalI (icc_thread, EV_RX_DATA_READY);
228 ICC_STATE_START, /* Initial */
229 ICC_STATE_WAIT, /* Waiting APDU */
230 /* Busy1, Busy2, Busy3, Busy5 */
231 ICC_STATE_EXECUTE, /* Busy4 */
232 ICC_STATE_RECEIVE, /* APDU Received Partially */
234 ICC_STATE_SEND, /* APDU Sent Partially */
237 static enum icc_state icc_state;
240 * ATR (Answer To Reset) string
242 * TS = 0x3B: Direct conversion
243 * T0 = 0x94: TA1 and TD1 follow, 4 historical bytes
244 * TA1 = 0x11: FI=1, DI=1
245 * TD1 = 0x81: TD2 follows, T=1
246 * TD2 = 0x31: TA3 and TB3 follow, T=1
247 * TA3 = 0xFE: IFSC = 254 bytes
248 * TB3 = 0x55: BWI = 5, CWI = 5 (BWT timeout 3.2 sec)
249 * Historical bytes: "FSIJ"
253 static const char ATR[] = {
254 0x3B, 0x94, 0x11, 0x81, 0x31, 0xFE, 0x55,
256 (0x94^0x11^0x81^0x31^0xFE^0x55^'F'^'S'^'I'^'J')
263 icc_error (int offset)
268 icc_reply = icc_chain_p;
270 icc_reply = icc_buffer;
272 icc_reply[0] = ICC_SLOT_STATUS_RET; /* Any value should be OK */
277 icc_reply[5] = 0x00; /* Slot */
278 icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
279 if (icc_state == ICC_STATE_START)
280 /* 1: ICC present but not activated 2: No ICC present */
281 icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
283 /* An ICC is present and active */
284 icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
285 icc_reply[ICC_MSG_STATUS_OFFSET] |= ICC_CMD_STATUS_ERROR; /* Failed */
286 icc_reply[ICC_MSG_ERROR_OFFSET] = offset;
287 icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
289 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
290 icc_tx_size = ICC_MSG_HEADER_SIZE;
291 USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size);
292 SetEPTxValid (ENDP1);
295 static Thread *gpg_thread;
296 static WORKING_AREA(waGPGthread, 128*16);
297 extern msg_t GPGthread (void *arg);
300 /* Send back ATR (Answer To Reset) */
306 if (gpg_thread == NULL)
307 gpg_thread = chThdCreateStatic (waGPGthread, sizeof(waGPGthread),
308 NORMALPRIO, GPGthread, (void *)icc_thread);
310 size_atr = sizeof (ATR);
311 icc_buffer[0] = ICC_DATA_BLOCK_RET;
312 icc_buffer[1] = size_atr;
313 icc_buffer[2] = 0x00;
314 icc_buffer[3] = 0x00;
315 icc_buffer[4] = 0x00;
316 icc_buffer[5] = 0x00; /* Slot */
317 icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
318 icc_buffer[ICC_MSG_STATUS_OFFSET] = 0x00;
319 icc_buffer[ICC_MSG_ERROR_OFFSET] = 0x00;
320 icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0x00;
321 memcpy (&icc_buffer[ICC_MSG_DATA_OFFSET], ATR, size_atr);
323 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
324 icc_tx_size = ICC_MSG_HEADER_SIZE + size_atr;
325 USB_SIL_Write (EP1_IN, icc_buffer, icc_tx_size);
326 SetEPTxValid (ENDP1);
327 DEBUG_INFO ("ON\r\n");
329 return ICC_STATE_WAIT;
333 icc_send_status (void)
338 icc_reply = icc_chain_p;
340 icc_reply = icc_buffer;
342 icc_reply[0] = ICC_SLOT_STATUS_RET;
347 icc_reply[5] = 0x00; /* Slot */
348 icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
349 if (icc_state == ICC_STATE_START)
350 /* 1: ICC present but not activated 2: No ICC present */
351 icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
353 /* An ICC is present and active */
354 icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
355 icc_reply[ICC_MSG_ERROR_OFFSET] = 0x00;
356 icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
358 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
359 icc_tx_size = ICC_MSG_HEADER_SIZE;
360 USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size);
361 SetEPTxValid (ENDP1);
364 DEBUG_INFO ("St\r\n");
373 chThdTerminate (gpg_thread);
374 chThdWait (gpg_thread);
379 DEBUG_INFO ("OFF\r\n");
380 return ICC_STATE_START;
386 icc_send_data_block_filling_header (int len)
388 int tx_size = USB_LL_BUF_SIZE;
389 uint8_t *p = icc_buffer;
391 icc_buffer[0] = ICC_DATA_BLOCK_RET;
392 icc_buffer[1] = len & 0xFF;
393 icc_buffer[2] = (len >> 8)& 0xFF;
394 icc_buffer[3] = (len >> 16)& 0xFF;
395 icc_buffer[4] = (len >> 24)& 0xFF;
396 icc_buffer[5] = 0x00; /* Slot */
397 icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
398 icc_buffer[ICC_MSG_STATUS_OFFSET] = 0;
399 icc_buffer[ICC_MSG_ERROR_OFFSET] = 0;
400 icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0;
402 icc_tx_size = ICC_MSG_HEADER_SIZE + len;
403 icc_next_p = icc_buffer + USB_LL_BUF_SIZE;
404 if (icc_next_p > &icc_buffer[icc_tx_size])
407 tx_size = &icc_buffer[icc_tx_size] - p;
410 USB_SIL_Write (EP1_IN, p, tx_size);
411 SetEPTxValid (ENDP1);
413 DEBUG_INFO ("DATA\r\n");
418 icc_send_data_block (uint8_t status, uint8_t chain)
423 icc_reply = icc_chain_p;
425 icc_reply = icc_buffer;
427 icc_reply[0] = ICC_DATA_BLOCK_RET;
432 icc_reply[5] = 0x00; /* Slot */
433 icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
434 icc_reply[ICC_MSG_STATUS_OFFSET] = status;
435 icc_reply[ICC_MSG_ERROR_OFFSET] = 0x00;
436 icc_reply[ICC_MSG_CHAIN_OFFSET] = chain;
438 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
439 icc_tx_size = ICC_MSG_HEADER_SIZE;
440 USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size);
441 SetEPTxValid (ENDP1);
443 DEBUG_INFO ("DATA\r\n");
449 icc_send_params (void)
451 icc_buffer[0] = ICC_PARAMS_RET;
452 icc_buffer[1] = 0x07; /* Length = 0x00000007 */
456 icc_buffer[5] = 0x00; /* Slot */
457 icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
458 icc_buffer[ICC_MSG_STATUS_OFFSET] = 0;
459 icc_buffer[ICC_MSG_ERROR_OFFSET] = 0;
460 icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0x01; /* ProtocolNum: T=1 */
461 icc_buffer[ICC_MSG_DATA_OFFSET] = 0x11; /* bmFindexDindex */
462 icc_buffer[ICC_MSG_DATA_OFFSET+1] = 0x11; /* bmTCCKST1 */
463 icc_buffer[ICC_MSG_DATA_OFFSET+2] = 0xFE; /* bGuardTimeT1 */
464 icc_buffer[ICC_MSG_DATA_OFFSET+3] = 0x55; /* bmWaitingIntegersT1 */
465 icc_buffer[ICC_MSG_DATA_OFFSET+4] = 0x03; /* bClockStop */
466 icc_buffer[ICC_MSG_DATA_OFFSET+5] = 0xFE; /* bIFSC */
467 icc_buffer[ICC_MSG_DATA_OFFSET+6] = 0; /* bNadValue */
469 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
470 icc_tx_size = ICC_MSG_HEADER_SIZE + 7;
471 USB_SIL_Write (EP1_IN, icc_buffer, icc_tx_size);
472 SetEPTxValid (ENDP1);
474 DEBUG_INFO ("DATA\r\n");
478 static enum icc_state
479 icc_handle_data (void)
481 enum icc_state next_state = icc_state;
482 struct icc_header *icc_header;
485 icc_header = (struct icc_header *)icc_chain_p;
487 icc_header = (struct icc_header *)icc_buffer;
491 case ICC_STATE_START:
492 if (icc_header->msg_type == ICC_POWER_ON)
493 next_state = icc_power_on ();
494 else if (icc_header->msg_type == ICC_POWER_OFF)
495 next_state = icc_power_off ();
496 else if (icc_header->msg_type == ICC_SLOT_STATUS)
500 DEBUG_INFO ("ERR01\r\n");
501 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
505 if (icc_header->msg_type == ICC_POWER_ON)
506 /* Not in the spec., but pcscd/libccid */
507 next_state = icc_power_on ();
508 else if (icc_header->msg_type == ICC_POWER_OFF)
509 next_state = icc_power_off ();
510 else if (icc_header->msg_type == ICC_SLOT_STATUS)
512 else if (icc_header->msg_type == ICC_XFR_BLOCK)
514 if (icc_header->param == 0)
515 { /* Give this message to GPG thread */
516 chEvtSignal (gpg_thread, (eventmask_t)1);
517 next_state = ICC_STATE_EXECUTE;
518 chEvtSignal (blinker_thread, EV_LED_ON);
520 else if (icc_header->param == 1)
522 icc_chain_p = icc_next_p;
523 icc_send_data_block (0, 0x10);
524 next_state = ICC_STATE_RECEIVE;
528 DEBUG_INFO ("ERR02\r\n");
529 icc_error (ICC_OFFSET_PARAM);
532 else if (icc_header->msg_type == ICC_SET_PARAMS
533 || icc_header->msg_type == ICC_GET_PARAMS)
537 DEBUG_INFO ("ERR03\r\n");
538 DEBUG_BYTE (icc_header->msg_type);
539 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
542 case ICC_STATE_RECEIVE:
543 if (icc_header->msg_type == ICC_POWER_OFF)
546 next_state = icc_power_off ();
548 else if (icc_header->msg_type == ICC_SLOT_STATUS)
550 else if (icc_header->msg_type == ICC_XFR_BLOCK)
552 if (icc_header->param == 2) /* Got the final block */
553 { /* Give this message to GPG thread */
554 int len = icc_next_p - icc_chain_p - ICC_MSG_HEADER_SIZE;
556 memmove (icc_chain_p, icc_chain_p + ICC_MSG_HEADER_SIZE, len);
557 icc_next_p -= ICC_MSG_HEADER_SIZE;
558 icc_data_size = icc_next_p - icc_buffer - ICC_MSG_HEADER_SIZE;
560 next_state = ICC_STATE_EXECUTE;
561 chEvtSignal (blinker_thread, EV_LED_ON);
562 chEvtSignal (gpg_thread, (eventmask_t)1);
564 else /* icc_header->param == 3 is not supported. */
566 DEBUG_INFO ("ERR08\r\n");
567 icc_error (ICC_OFFSET_PARAM);
572 DEBUG_INFO ("ERR05\r\n");
573 DEBUG_BYTE (icc_header->msg_type);
575 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
576 next_state = ICC_STATE_WAIT;
579 case ICC_STATE_EXECUTE:
580 if (icc_header->msg_type == ICC_POWER_OFF)
581 next_state = icc_power_off ();
582 else if (icc_header->msg_type == ICC_SLOT_STATUS)
586 DEBUG_INFO ("ERR04\r\n");
587 DEBUG_BYTE (icc_header->msg_type);
588 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
592 next_state = ICC_STATE_START;
593 DEBUG_INFO ("ERR10\r\n");
600 static enum icc_state
601 icc_handle_timeout (void)
603 enum icc_state next_state = icc_state;
607 case ICC_STATE_EXECUTE:
608 icc_send_data_block (ICC_CMD_STATUS_TIMEEXT, 0);
617 #define USB_ICC_TIMEOUT MS2ST(1950)
620 USBthread (void *arg)
624 icc_thread = chThdSelf ();
625 chEvtClear (ALL_EVENTS);
627 icc_state = ICC_STATE_START;
629 icc_prepare_receive (0);
634 m = chEvtWaitOneTimeout (ALL_EVENTS, USB_ICC_TIMEOUT);
636 if (m == EV_RX_DATA_READY)
637 icc_state = icc_handle_data ();
638 else if (m == EV_EXEC_FINISHED)
640 if (icc_state == ICC_STATE_EXECUTE)
642 chEvtSignal (blinker_thread, EV_LED_OFF);
644 icc_send_data_block_filling_header (res_APDU_size);
645 icc_state = ICC_STATE_WAIT;
649 DEBUG_INFO ("ERR07\r\n");
652 else if (m == EV_TX_FINISHED)
654 if (icc_state == ICC_STATE_START || icc_state == ICC_STATE_WAIT)
655 icc_prepare_receive (0);
656 else if (icc_state == ICC_STATE_RECEIVE)
657 icc_prepare_receive (1);
660 icc_state = icc_handle_timeout ();