1 /*****************************************************************************
2  * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
3  *
4  * This copy is licensed under the Apache License, Version 2.0 (the "License");
5  * You may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and limitations under the License.
11 
12  ****************************************************************************/
13 /**
14  * @file
15  * $Author$
16  * $Revision$
17  * $Date$
18  *
19  * ISO7816 T=1 interface.
20  *
21  */
22 
23 #ifndef ISO7816_T1_H
24 #define ISO7816_T1_H
25 
26 #include <stdint.h>
27 
28 struct t1_state {
29     int spi_fd; /* File descriptor for transport */
30 
31     struct {
32         /* Ordered by decreasing priority */
33         unsigned halt    : 1;   /* Halt dispatch loop             */
34         unsigned request : 1;   /* Build a SBLOCK request         */
35         unsigned reqresp : 1;   /* Build a SBLOCK response        */
36         unsigned badcrc  : 1;   /* Build a RBLOCK for CRC error   */
37         unsigned timeout : 1;   /* Resend S-BLOCK or send R-BLOCK */
38         unsigned aborted : 1;   /* Abort was requested            */
39     } state;
40 
41     uint8_t ifsc; /* IFS for card        */
42     uint8_t ifsd; /* IFS for device      */
43     uint8_t nad;  /* NAD byte for device */
44     uint8_t nadc; /* NAD byte for card   */
45     uint8_t wtx;  /* Read timeout scaler */
46 
47     unsigned bwt; /* Block Waiting Timeout */
48 
49     uint8_t chk_algo; /* One of CHECKSUM_LRC or CHECKSUM_CRC                */
50     uint8_t retries;  /* Remaining retries in case of incorrect block       */
51     uint8_t request;  /* Current pending request, valid only during request */
52 
53     int wtx_rounds;     /* Limit number of WTX round from card    */
54     int wtx_max_rounds; /* Maximum number of WTX rounds from card */
55     int wtx_max_value;  /* Maximum value of WTX supported by host */
56 
57     uint8_t need_reset; /* Need to send a reset on first start            */
58     uint8_t need_resync; /* Need to send a reset on first start            */
59 	uint8_t need_ifsd_sync; /* Need to send a IFSD request after RESET            */
60     uint8_t atr[32];    /* ISO7816 defines ATR with a maximum of 32 bytes */
61     uint8_t atr_length; /* Never over 32                                  */
62 
63     /* Emission window */
64     struct t1_send {
65         const uint8_t *start;
66         const uint8_t *end;
67         uint8_t        next; /* N(S) */
68     } send;
69 
70     /* Reception window */
71     struct t1_recv {
72         uint8_t *start;
73         uint8_t *end;
74         uint8_t  next; /* N(R) */
75         size_t   size; /* Maximum window size */
76     } recv;
77 
78     size_t recv_max;  /* Maximum number of expected bytes on reception */
79     size_t recv_size; /* Received number of bytes so far */
80 
81     /* Max size is:
82      *  - 3 bytes header,
83      *  - 254 bytes data,
84      *  - 2 bytes CRC
85      *
86      * Use 255 bytes data in case of invalid block length of 255.
87      */
88     uint8_t buf[3 + 255 + 2];
89 };
90 
91 enum { CHECKSUM_LRC, CHECKSUM_CRC };
92 
93 void isot1_init(struct t1_state *t1);
94 void isot1_release(struct t1_state *t1);
95 void isot1_bind(struct t1_state *t1, int src, int dst);
96 int isot1_transceive(struct t1_state *t1, const void *snd_buf,
97                      size_t snd_len, void *rcv_buf, size_t rcv_len);
98 int isot1_negotiate_ifsd(struct t1_state *t1, int ifsd);
99 int isot1_reset(struct t1_state *t1);
100 int isot1_resync(struct t1_state *t1);
101 int isot1_get_atr(struct t1_state *t1, void *atr, size_t n);
102 
103 /* for check.c */
104 typedef struct t1_state t1_state_t;
105 
106 #ifndef EREMOTEIO
107 # define EREMOTEIO EIO
108 #endif
109 
110 #endif /* ISO7816_T1_H */
111