-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathISO7816_Segger_UART_7816_converter.c
335 lines (283 loc) · 9.04 KB
/
ISO7816_Segger_UART_7816_converter.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "stdint.h"
#include "SEGGER_RTT.h"
#include "Universal.h"
#include "ISO7816.h"
/*
SEGGER ADAPTER ADAPTOR CONVERTER
SEGGET TO UART TO ISO7816
*/
uint8_t ready_to_send=0;
uint8_t Segger_recieve_buffer[255];
uint8_t HEX_String[255];
#define CONFG 'G'
#define HELP1 'H'
#define HELP2 '?'
#define RESET 'R'
#define ACTIV 'T'
#define DEACT 't'
#define EXIT 'X'
#define TESTK 's'
#define C_REC 'V'
#define C_SEN 'v'
// example inputs to segger rtt viewer
/*
R
05
00 00 01 fF fe
08
00 00 01 00 a4 00 00 a5
0a
00 00 06 80 10 01 02 01 FF FF
05
00 00 00 00 00
05
00 a4 04 00 00
0x60h 0x61h 0x07h
05
00 c0 00 00 07
0xc0h 0xa0h 0x00h 0x00h 0x00h 0x03h 0x00h 0x00h 0x90h 0x00h
0d
80F0800F08 A0000000 03000000
09
000005 00a4040000ff 1010
0101
05
00 20 00 00 00
05
00 f2 60 00 00
08
00 80 F2 20 00 02 4F 00
0d
00 20 00 00 08 00 00 00 00 00 00 00 00
*/
void Print_Help() {
// //##############################################\n\n
Segger_write_string(
"\
##############################################\n\
######## HELP ########\n\
Usage through Segger RTT viewer console:\n \
\t ? or H -help\n\n\
\t X -Exit Converter \n\
\t R -cold reset Smart Card\n\
\t T -Activate Smart Card\n\
\t t -Deactivate Cards VCC, CLK, RESET, IO\n\
\t G -CONFG G0/1 Set protocol \n\
\t V -continual ISO7816 RecieVe/send until Segger recieve \n\
\t\t GM BruteForceLocating Card Manager\n\
\t\t Gm Locating Card Manager\n\
\t\t GC Locate Classes\n\
\t\t GI Locate Instructiions\n\
\t\t GN Send Negotiation Byte Protocol\n\
\t\t GS Try to Get Status Info\n\
\t\t GR Try read records\n\
\t\t Gc Try CLCD\n\
\t\t DD Try to get Data info\n\
\t\t DB0xXXXXXXXX UART Baud rate set\n\
\t\t Db set UART Baud rate to CLK/372\n\
\t\t DP toggle uart parity\n\
\t\t Gt Try applet response\n");
Card_wait();
Segger_write_string(
"\
Message sending to card:\n \
\t First send how many data, then the actual message\n\
\t\t03\n\
\t\tXX XX XX\n\
\t\tor\n\
\t\tXXXXXX \n\
where X is [0-9, a-f, A-F]\n\
Actual message can be on multiple lines\n\n\n\
Set Segger options as:\n\
\tINPUT->Sending...->Send on Enter\n\
\tINPUT->Sending...->Block if FIFO full\n\
##############################################\n\n");
// TODO! if you add next line, segger wont be able to send that long message
//##############################################\n
Card_wait();
}
uint8_t String_To_Dec(uint8_t value, uint8_t position) {
uint8_t return_value=0;
//Segger_write_string_value(" Value:", value);
if(value>='0' && value<='9') {
return_value=value-'0';
}
if(value>='a' && value<='f') {
return_value=10+value-'a';
}
if(value>='A' && value<='F') {
return_value=10+value-'A';
}
//Segger_write_string_value(" Converted:", return_value);
if(position==0) {
return return_value<<4;
}
return return_value;
}
uint32_t Hex_UINT(uint8_t offset, uint8_t* Segger_recieve_buffer, uint8_t recieved) {
uint32_t value=0;
uint8_t count=offset;
while(count<=recieved && (count-offset)<9) {
value=value << 4;
if( !(Segger_recieve_buffer[count]==' ' || Segger_recieve_buffer[count]=='\t' || Segger_recieve_buffer[count]=='\n')) {
value |= String_To_Dec(Segger_recieve_buffer[count], 1);
}
count++;
}
return value;
}
uint8_t Convert_To_Hex_String(uint8_t recieved, uint8_t * Segger_recieve_buffer) {
uint8_t count=0;
uint8_t lenght=0;
SC_APDU[lenght+ready_to_send]=0;
for(uint8_t i=0; i<recieved; i++) {
if(Segger_recieve_buffer[i]==' ' || Segger_recieve_buffer[i]=='\t' || Segger_recieve_buffer[i]=='\n') {
;
}
else {
SC_APDU[lenght+ready_to_send]|=String_To_Dec(Segger_recieve_buffer[i], count++);
if(count>=2) {
count=0;
lenght++;
SC_APDU[lenght+ready_to_send]=0;
}
}
}
return lenght;
}
void Debug_Mode(void) {
Print_Help();
init_ISO7816_pins();
uint8_t recieved=0;
ready_to_send=0;
uint8_t lenght=0;
uint8_t FLAG_run=1;
while(FLAG_run) {
uint8_t ready_to_recieve = SEGGER_RTT_HasData(DEFAULT_SEGGER_JLINK_RTT_VIEWER_CONSOLE);
if(ready_to_recieve) {
//Segger_write_string("Data ready to be recieved\n");
recieved = SEGGER_RTT_Read(DEFAULT_SEGGER_JLINK_RTT_VIEWER_CONSOLE, Segger_recieve_buffer, 255);
Segger_write_string_value("D: Data ready:", recieved);
switch(Segger_recieve_buffer[0]) {
case C_REC:
if(!Is_Card_Active()) {
Segger_write_string("D: Card is not activated!");
break;
}
while(true) {
if(SEGGER_RTT_HasData(DEFAULT_SEGGER_JLINK_RTT_VIEWER_CONSOLE)) {
SEGGER_RTT_Read(DEFAULT_SEGGER_JLINK_RTT_VIEWER_CONSOLE, Segger_recieve_buffer, 255);
break;
}
uint8_t success=0;
Recieve_UART_timeout(45000 * 100, &success);
if(success==0) {
Segger_write_string("\n");
}
}
break;
case C_SEN:
if(!Is_Card_Active()) {
Segger_write_string("D: Card is not activated!");
break;
}
while(true) {
if(SEGGER_RTT_HasData(DEFAULT_SEGGER_JLINK_RTT_VIEWER_CONSOLE)) {
SEGGER_RTT_Read(DEFAULT_SEGGER_JLINK_RTT_VIEWER_CONSOLE, Segger_recieve_buffer, 255);
break;
}
SC_APDU[0]=0xaa;
Send_UART(1);
}
break;
case RESET:
lenght=0;
ready_to_send=0;
Card_Deactivate();
Set_Comm_Baudrate(ISO7816_CLK/372);
Card_Cold_Reset();
SC_Recieve_ATR_And_Config();
break;
case HELP1:
case HELP2:
Print_Help();
break;
case ACTIV:
Card_Activate();
SC_Recieve_ATR_And_Config();
break;
case DEACT:
Card_Deactivate();
break;
case EXIT:
Card_Deactivate();
FLAG_run=0;
break;
case CONFG:
if(recieved>1) {
switch(Segger_recieve_buffer[1]) {
case '0': SC_ATR_Set_Protocol_Type(0x00); Segger_write_string("D: Messages will be sent as APDU\n"); break;
case '1': SC_ATR_Set_Protocol_Type(0x01); Segger_write_string("D: Messages will be encapsulated in block\n"); break;
case 'M': Try_Locating_Card_Manager_Brute(); break;
case 'm': Try_Locating_Card_Manager(); Get_Response(7); break;
case 'C': Try_Locating_Classes(); break;
case 'I': Try_Locating_Instructions(); break;
case 'N': Send_Negotiate_Block_Protocol_Alone(); Recieve_And_Check_Response(); SC_ATR_Set_Protocol_Type(0x00); break; //TODO 0x00
case 'S': Try_STATUS(); break;
case 'D': Try_DATA(); break;
case 'R': Try_RECORDS(); break;
case 'c': Try_Card_Production_Life_Cycle_Data(); break;
case 'B': if(recieved==0x0d) {
//Segger_write_one_hex_value_32( Hex_UINT(3, Segger_recieve_buffer, recieved));
Set_Comm_Baudrate(Hex_UINT(3, Segger_recieve_buffer, recieved));
reconfigure_UART();
//strtol( (char*)(Segger_recieve_buffer+2), NULL, 32) );
}
break;
case 'b': Set_Comm_Baudrate(ISO7816_CLK/372); reconfigure_UART(); break;
case 't': Select_Applet_Wrapper(); break;
case 'P': Toggle_Parity(); reconfigure_UART(); break;
case 'L': Toggle_APDU_EXTRA_LRC(); break;
/*case 'e': //uint8_t message[16];
HW_AES_Decode(HEX_String);
Print_Array(12, HEX_String);
HW_AES_Encode(HEX_String, 0xaa);
Print_Array(12, HEX_String);
break;*/
default: break;
}
}
break;
//case TESTK:
// test_Card();
// break;
default:
if(lenght==0) {
Convert_To_Hex_String(2, Segger_recieve_buffer);
lenght = SC_APDU[0];
//Segger_write_string_value("D: Waiting for additional bytes:", lenght);
}
else {
ready_to_send += Convert_To_Hex_String(recieved, Segger_recieve_buffer);
if(ready_to_send>=lenght) {
if(SC_ATR_Get_Protocol_Type()==0) {
/*!!!!SC_Send_Message(lenght);
Recieve_And_Check_Response();*/
Send_And_Recieve(lenght);
}
else {
uint8_t count = Prepare_Standard_Block(lenght, SC_APDU);
/*!!!!SC_Send_Message(count);
Recieve_And_Check_Response();*/
Send_And_Recieve(count);
}
lenght=0;
ready_to_send=0;
}
}
Segger_write_string_value("D: Waiting for additional bytes:", lenght-ready_to_send);
break;
}
}
}
}