summaryrefslogtreecommitdiff
path: root/lib/libsectok/scrw.c
blob: d0d85d2e12cbabb12c665f6257b7f6879e5469e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* $Id: scrw.c,v 1.6 2001/07/20 15:51:45 rees Exp $ */

/*
copyright 1997, 1999, 2000
the regents of the university of michigan
all rights reserved

permission is granted to use, copy, create derivative works
and redistribute this software and such derivative works
for any purpose, so long as the name of the university of
michigan is not used in any advertising or publicity
pertaining to the use or distribution of this software
without specific, written prior authorization.  if the
above copyright notice or any other identification of the
university of michigan is included in any copy of any
portion of this software, then the disclaimer below must
also be included.

this software is provided as is, without representation
from the university of michigan as to its fitness for any
purpose, and without warranty by the university of
michigan of any kind, either express or implied, including
without limitation the implied warranties of
merchantability and fitness for a particular purpose. the
regents of the university of michigan shall not be liable
for any damages, including special, indirect, incidental, or
consequential damages, with respect to any claim arising
out of or in connection with the use of the software, even
if it has been or is hereafter advised of the possibility of
such damages.
*/

/*
 * OS independent part
 *
 * Jim Rees, University of Michigan, October 1997
 */

#ifdef __palmos__
#include <Common.h>
#include <System/SysAll.h>
#include <System/Unix/unix_stdio.h>
#include <System/Unix/unix_stdlib.h>
#include <System/Unix/unix_string.h>
#include <UI/UIAll.h>
#include "field.h"
#else
#include <stdio.h>
#include <string.h>
#endif
#ifdef SCPERF
#define SCPERF_FIRST_APPEARANCE
#endif /* SCPERF */
#include "sectok.h"
#include "sc7816.h"
#include "todos_scrw.h"

/* external variable */
#ifdef BYTECOUNT
extern int num_getc, num_putc;
#endif /* BYTECOUNT */

struct scparam scparam[4];

/* reset the card, and return answer to reset (atr) */

int
todos_scxreset(int ttyn, int flags, unsigned char *atr, int *ep)
{
    unsigned char buf[33];
    int n, err;

    if (ep)
	*ep = SCEOK;
    else
	ep = &err;

    if (!todos_sccardpresent(ttyn)) {
	*ep = SCENOCARD;
	return 0;
    }

    if (!atr)
	atr = buf;

    if (!(flags & SCRTODOS) && (todos_scsetflags(ttyn, 0, 0) & SCOXDTR))
	flags |= SCRTODOS;

    todos_scsetspeed(ttyn, 9600);
    todos_scsetflags(ttyn, 0, SCOINVRT);

    if (todos_scdtr(ttyn, (flags & SCRTODOS)) < 0) {
	*ep = SCENOCARD;
	return 0;
    }

    /* 7816-3 sec 5.2 says >= 40000 clock cycles, ~=12 msec */
    scsleep(20);

    todos_scdtr(ttyn, !(flags & SCRTODOS));

    n = todos_get_atr(ttyn, flags, atr, &scparam[ttyn]);
    if (!n && ep)
	*ep = SCESLAG;
    if (scparam[ttyn].t < 0 && ep)
	*ep = SCENOSUPP;

    return n;
}

static int
todos_scioproc(int ttyn, int io, unsigned char *cp)
{
    int code;

    /* Wait extra guard time if needed */
    if (!io && scparam[ttyn].n)
	scsleep(((scparam[ttyn].n * scparam[ttyn].etu) + 999) / 1000);

    code = (!io ? scputc(ttyn, *cp) : scgetc(ttyn, cp, scparam[ttyn].cwt));

    return code;
}

/* This does the real work of transferring data to or from the card.  Since the ack
   handling is the same for both send and receive, we do it all here. */

static int
todos_scioT0(int ttyn, int io, int cla, int ins, int p1, int p2, int p3, unsigned char *buf, int *sw1p, int *sw2p)
{
    int n = 0, ack, ackxins;
    unsigned char *bp = buf, c, apdu[5];
#ifdef BYTECOUNT
    int tmp_num_getc, tmp_num_putc;
#endif /* BYTECOUNT */
	
#ifdef BYTECOUNT
    tmp_num_getc = num_getc;
    tmp_num_putc = num_putc;
#endif /* BYTECOUNT */

#ifdef SCPERF
    SetTime("SCIO starts");
#endif /* SCPERF */

#ifdef DEBUG
    printf("scioT0 %3s %02x %02x %02x %02x %02x\n", io ? "out" : "in", cla, ins, p1, p2, p3);
#endif

    apdu[0] = cla;
    apdu[1] = ins;
    apdu[2] = p1;
    apdu[3] = p2;
    apdu[4] = p3;
    scputblk(ttyn, apdu, 5);

#ifdef SCPERF
    SetTime("Finish sending APDU");
#endif /* SCPERF */

    while (1) {
	/* read ack byte; see 7816-3 8.2.2 */
	if (scgetc(ttyn, &c, scparam[ttyn].cwt) != SCEOK) {
#ifdef DEBUG
	    printf("%d ms timeout reading ack\n", scparam[ttyn].cwt);
#endif
	    return -1;
	}
	ack = c;

	if (ack == 0x60) {
	    /* null (no-op but resets timer) */
#ifdef DEBUG
	    printf("got 0x60 (null) ack; reset timer\n");
#endif
	    continue;
	}

	if ((ack & 0xf0) == 0x60 || (ack & 0xf0) == 0x90) {
	    /* SW1; get SW2 and return */
	    *sw1p = ack;
	    if (scgetc(ttyn, &c, scparam[ttyn].cwt) != SCEOK) {
#ifdef DEBUG
		printf("%d ms timeout reading sw2\n", scparam[ttyn].cwt);
#endif
		return -1;
	    }
	    *sw2p = (c & 0xff);
	    break;
	}

	/* we would set VPP here if the interface supported it */

	ackxins = (ack ^ ins) & 0xfe;
	if (ackxins == 0xfe) {
	    if (n < p3) {
		/* xfer next data byte */
		if (todos_scioproc(ttyn, io, bp++) != SCEOK) {
#ifdef DEBUG
		    printf("%d ms timeout reading next data byte\n", scparam[ttyn].cwt);
#endif
		    return -1;
		}
		n++;
	    }

	} else if (ackxins == 0) {
	    /* xfer all remaining data bytes */
	    while (n < p3) {
		if (todos_scioproc(ttyn, io, bp++) != SCEOK) {
#ifdef DEBUG
		    printf("%d ms timeout reading all remaining data bytes\n", scparam[ttyn].cwt);
#endif
		    return -1;
		}
		n++;
	    }
#ifdef SCPERF
	    SetTime("Finish sending or receiving DATA");
#endif /* SCPERF */
	} else {
	    /* ?? unknown ack byte */
#ifdef DEBUG
	    printf("unknown ack %x\n", ack);
	    continue;
#else
	    return -1;
#endif
	}
    }

#ifdef SCPERF
    SetTime("Finish scio");
#endif /* SCPERF */

#ifdef BYTECOUNT
    tmp_num_putc = num_putc - tmp_num_putc;
    tmp_num_getc = num_getc - tmp_num_getc - tmp_num_putc;
    MESSAGE3("#getc=%d, #putc=%d\n", tmp_num_getc, tmp_num_putc);
#endif /* BYTECOUNT */

    return n;
}

int
todos_scrw(int ttyn, int cla, int ins, int p1, int p2, int ilen, unsigned char *ibuf, int olen, unsigned char *obuf, int *sw1p, int *sw2p)
{
    int r;

    if (scparam[ttyn].t == 0) {
	if (ilen > 0 && ibuf) {
	    /* Send "in" data */
	    r = (todos_scioT0(ttyn, 0, cla, ins, p1, p2, ilen, ibuf, sw1p, sw2p) >= 0) ? 0 : -1;
	    if (r >= 0 && *sw1p == 0x61 && olen >= *sw2p && obuf) {
		/* Response available; get it */
		r = todos_scioT0(ttyn, 1, cla, 0xc0, 0, 0, *sw2p, obuf, sw1p, sw2p);
	    }
	} else {
	    /* Get "out" data */
	    r = todos_scioT0(ttyn, 1, cla, ins, p1, p2, olen, obuf, sw1p, sw2p);
	}
#ifndef NO_T_EQ_1
    } else if (scparam[ttyn].t == 1) {
	r = scioT1(ttyn, cla, ins, p1, p2, ilen, ibuf, olen, obuf, sw1p, sw2p);
#endif
    } else
	r = -1;

    return r;
}