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);
226 volatile enum icc_state icc_state;
229 * ATR (Answer To Reset) string
231 * TS = 0x3B: Direct conversion
232 * T0 = 0x94: TA1 and TD1 follow, 4 historical bytes
233 * TA1 = 0x11: FI=1, DI=1
234 * TD1 = 0x81: TD2 follows, T=1
235 * TD2 = 0x31: TA3 and TB3 follow, T=1
236 * TA3 = 0xFE: IFSC = 254 bytes
237 * TB3 = 0x55: BWI = 5, CWI = 5 (BWT timeout 3.2 sec)
238 * Historical bytes: "FSIJ"
242 static const char ATR[] = {
243 0x3B, 0x94, 0x11, 0x81, 0x31, 0xFE, 0x55,
245 (0x94^0x11^0x81^0x31^0xFE^0x55^'F'^'S'^'I'^'J')
252 icc_error (int offset)
257 icc_reply = icc_chain_p;
259 icc_reply = icc_buffer;
261 icc_reply[0] = ICC_SLOT_STATUS_RET; /* Any value should be OK */
266 icc_reply[5] = 0x00; /* Slot */
267 icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
268 if (icc_state == ICC_STATE_START)
269 /* 1: ICC present but not activated 2: No ICC present */
270 icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
272 /* An ICC is present and active */
273 icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
274 icc_reply[ICC_MSG_STATUS_OFFSET] |= ICC_CMD_STATUS_ERROR; /* Failed */
275 icc_reply[ICC_MSG_ERROR_OFFSET] = offset;
276 icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
278 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
279 icc_tx_size = ICC_MSG_HEADER_SIZE;
280 USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size);
281 SetEPTxValid (ENDP1);
284 static Thread *gpg_thread;
285 static WORKING_AREA(waGPGthread, 128*16);
286 extern msg_t GPGthread (void *arg);
289 /* Send back ATR (Answer To Reset) */
295 if (gpg_thread == NULL)
296 gpg_thread = chThdCreateStatic (waGPGthread, sizeof(waGPGthread),
297 NORMALPRIO, GPGthread, (void *)icc_thread);
299 size_atr = sizeof (ATR);
300 icc_buffer[0] = ICC_DATA_BLOCK_RET;
301 icc_buffer[1] = size_atr;
302 icc_buffer[2] = 0x00;
303 icc_buffer[3] = 0x00;
304 icc_buffer[4] = 0x00;
305 icc_buffer[5] = 0x00; /* Slot */
306 icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
307 icc_buffer[ICC_MSG_STATUS_OFFSET] = 0x00;
308 icc_buffer[ICC_MSG_ERROR_OFFSET] = 0x00;
309 icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0x00;
310 memcpy (&icc_buffer[ICC_MSG_DATA_OFFSET], ATR, size_atr);
312 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
313 icc_tx_size = ICC_MSG_HEADER_SIZE + size_atr;
314 USB_SIL_Write (EP1_IN, icc_buffer, icc_tx_size);
315 SetEPTxValid (ENDP1);
316 DEBUG_INFO ("ON\r\n");
318 return ICC_STATE_WAIT;
322 icc_send_status (void)
327 icc_reply = icc_chain_p;
329 icc_reply = icc_buffer;
331 icc_reply[0] = ICC_SLOT_STATUS_RET;
336 icc_reply[5] = 0x00; /* Slot */
337 icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
338 if (icc_state == ICC_STATE_START)
339 /* 1: ICC present but not activated 2: No ICC present */
340 icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
342 /* An ICC is present and active */
343 icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
344 icc_reply[ICC_MSG_ERROR_OFFSET] = 0x00;
345 icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
347 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
348 icc_tx_size = ICC_MSG_HEADER_SIZE;
349 USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size);
350 SetEPTxValid (ENDP1);
353 DEBUG_INFO ("St\r\n");
364 chThdTerminate (gpg_thread);
365 chEvtSignal (gpg_thread, (eventmask_t)1);
366 chThdWait (gpg_thread);
371 DEBUG_INFO ("OFF\r\n");
372 return ICC_STATE_START;
378 icc_send_data_block_filling_header (int len)
380 int tx_size = USB_LL_BUF_SIZE;
381 uint8_t *p = icc_buffer;
383 icc_buffer[0] = ICC_DATA_BLOCK_RET;
384 icc_buffer[1] = len & 0xFF;
385 icc_buffer[2] = (len >> 8)& 0xFF;
386 icc_buffer[3] = (len >> 16)& 0xFF;
387 icc_buffer[4] = (len >> 24)& 0xFF;
388 icc_buffer[5] = 0x00; /* Slot */
389 icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
390 icc_buffer[ICC_MSG_STATUS_OFFSET] = 0;
391 icc_buffer[ICC_MSG_ERROR_OFFSET] = 0;
392 icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0;
394 icc_tx_size = ICC_MSG_HEADER_SIZE + len;
395 icc_next_p = icc_buffer + USB_LL_BUF_SIZE;
396 if (icc_next_p > &icc_buffer[icc_tx_size])
399 tx_size = &icc_buffer[icc_tx_size] - p;
402 USB_SIL_Write (EP1_IN, p, tx_size);
403 SetEPTxValid (ENDP1);
405 DEBUG_INFO ("DATA\r\n");
410 icc_send_data_block (uint8_t status, uint8_t chain)
415 icc_reply = icc_chain_p;
417 icc_reply = icc_buffer;
419 icc_reply[0] = ICC_DATA_BLOCK_RET;
424 icc_reply[5] = 0x00; /* Slot */
425 icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
426 icc_reply[ICC_MSG_STATUS_OFFSET] = status;
427 icc_reply[ICC_MSG_ERROR_OFFSET] = 0x00;
428 icc_reply[ICC_MSG_CHAIN_OFFSET] = chain;
430 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
431 icc_tx_size = ICC_MSG_HEADER_SIZE;
432 USB_SIL_Write (EP1_IN, icc_reply, icc_tx_size);
433 SetEPTxValid (ENDP1);
435 DEBUG_INFO ("DATA\r\n");
441 icc_send_params (void)
443 icc_buffer[0] = ICC_PARAMS_RET;
444 icc_buffer[1] = 0x07; /* Length = 0x00000007 */
448 icc_buffer[5] = 0x00; /* Slot */
449 icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
450 icc_buffer[ICC_MSG_STATUS_OFFSET] = 0;
451 icc_buffer[ICC_MSG_ERROR_OFFSET] = 0;
452 icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0x01; /* ProtocolNum: T=1 */
453 icc_buffer[ICC_MSG_DATA_OFFSET] = 0x11; /* bmFindexDindex */
454 icc_buffer[ICC_MSG_DATA_OFFSET+1] = 0x11; /* bmTCCKST1 */
455 icc_buffer[ICC_MSG_DATA_OFFSET+2] = 0xFE; /* bGuardTimeT1 */
456 icc_buffer[ICC_MSG_DATA_OFFSET+3] = 0x55; /* bmWaitingIntegersT1 */
457 icc_buffer[ICC_MSG_DATA_OFFSET+4] = 0x03; /* bClockStop */
458 icc_buffer[ICC_MSG_DATA_OFFSET+5] = 0xFE; /* bIFSC */
459 icc_buffer[ICC_MSG_DATA_OFFSET+6] = 0; /* bNadValue */
461 icc_next_p = NULL; /* This is a single transaction Bulk-IN */
462 icc_tx_size = ICC_MSG_HEADER_SIZE + 7;
463 USB_SIL_Write (EP1_IN, icc_buffer, icc_tx_size);
464 SetEPTxValid (ENDP1);
466 DEBUG_INFO ("DATA\r\n");
470 static enum icc_state
471 icc_handle_data (void)
473 enum icc_state next_state = icc_state;
474 struct icc_header *icc_header;
477 icc_header = (struct icc_header *)icc_chain_p;
479 icc_header = (struct icc_header *)icc_buffer;
483 case ICC_STATE_START:
484 if (icc_header->msg_type == ICC_POWER_ON)
485 next_state = icc_power_on ();
486 else if (icc_header->msg_type == ICC_POWER_OFF)
487 next_state = icc_power_off ();
488 else if (icc_header->msg_type == ICC_SLOT_STATUS)
492 DEBUG_INFO ("ERR01\r\n");
493 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
497 if (icc_header->msg_type == ICC_POWER_ON)
498 /* Not in the spec., but pcscd/libccid */
499 next_state = icc_power_on ();
500 else if (icc_header->msg_type == ICC_POWER_OFF)
501 next_state = icc_power_off ();
502 else if (icc_header->msg_type == ICC_SLOT_STATUS)
504 else if (icc_header->msg_type == ICC_XFR_BLOCK)
506 if (icc_header->param == 0)
507 { /* Give this message to GPG thread */
508 chEvtSignal (gpg_thread, (eventmask_t)1);
509 next_state = ICC_STATE_EXECUTE;
511 else if (icc_header->param == 1)
513 icc_chain_p = icc_next_p;
514 icc_send_data_block (0, 0x10);
515 next_state = ICC_STATE_RECEIVE;
519 DEBUG_INFO ("ERR02\r\n");
520 icc_error (ICC_OFFSET_PARAM);
523 else if (icc_header->msg_type == ICC_SET_PARAMS
524 || icc_header->msg_type == ICC_GET_PARAMS)
528 DEBUG_INFO ("ERR03\r\n");
529 DEBUG_BYTE (icc_header->msg_type);
530 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
533 case ICC_STATE_RECEIVE:
534 if (icc_header->msg_type == ICC_POWER_OFF)
537 next_state = icc_power_off ();
539 else if (icc_header->msg_type == ICC_SLOT_STATUS)
541 else if (icc_header->msg_type == ICC_XFR_BLOCK)
543 if (icc_header->param == 2) /* Got the final block */
544 { /* Give this message to GPG thread */
545 int len = icc_next_p - icc_chain_p - ICC_MSG_HEADER_SIZE;
547 memmove (icc_chain_p, icc_chain_p + ICC_MSG_HEADER_SIZE, len);
548 icc_next_p -= ICC_MSG_HEADER_SIZE;
549 icc_data_size = icc_next_p - icc_buffer - ICC_MSG_HEADER_SIZE;
551 next_state = ICC_STATE_EXECUTE;
552 chEvtSignal (gpg_thread, (eventmask_t)1);
554 else /* icc_header->param == 3 is not supported. */
556 DEBUG_INFO ("ERR08\r\n");
557 icc_error (ICC_OFFSET_PARAM);
562 DEBUG_INFO ("ERR05\r\n");
563 DEBUG_BYTE (icc_header->msg_type);
565 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
566 next_state = ICC_STATE_WAIT;
569 case ICC_STATE_EXECUTE:
570 if (icc_header->msg_type == ICC_POWER_OFF)
571 next_state = icc_power_off ();
572 else if (icc_header->msg_type == ICC_SLOT_STATUS)
576 DEBUG_INFO ("ERR04\r\n");
577 DEBUG_BYTE (icc_header->msg_type);
578 icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
582 next_state = ICC_STATE_START;
583 DEBUG_INFO ("ERR10\r\n");
590 static enum icc_state
591 icc_handle_timeout (void)
593 enum icc_state next_state = icc_state;
597 case ICC_STATE_EXECUTE:
598 icc_send_data_block (ICC_CMD_STATUS_TIMEEXT, 0);
607 #define USB_ICC_TIMEOUT MS2ST(1950)
610 USBthread (void *arg)
614 icc_thread = chThdSelf ();
615 chEvtClear (ALL_EVENTS);
617 icc_state = ICC_STATE_START;
619 icc_prepare_receive (0);
624 m = chEvtWaitOneTimeout (ALL_EVENTS, USB_ICC_TIMEOUT);
626 if (m == EV_RX_DATA_READY)
627 icc_state = icc_handle_data ();
628 else if (m == EV_EXEC_FINISHED)
630 if (icc_state == ICC_STATE_EXECUTE)
632 icc_send_data_block_filling_header (res_APDU_size);
633 icc_state = ICC_STATE_WAIT;
637 DEBUG_INFO ("ERR07\r\n");
640 else if (m == EV_TX_FINISHED)
642 if (icc_state == ICC_STATE_START || icc_state == ICC_STATE_WAIT)
643 icc_prepare_receive (0);
644 else if (icc_state == ICC_STATE_RECEIVE)
645 icc_prepare_receive (1);
648 icc_state = icc_handle_timeout ();