1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
23
26
27
30
31
32
33
34
43
44
45
48
52
58
59
60
69
70
71
72
73
74
105
123
137
141
145
146
147
148
149
155
166
167
168
169
170
171
172
173
174
175
179
180
185
189
190
195
196
197
198
199
200
201
202
203
204
205
206
208
209
210
211
212
213
214
215
216
217
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
239
240
241
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
270
271
272
273
274
275
282
283
284
285
286
287
288
289
290
294
295
296
297
298
300
301
302
308
309
314
319
320
321
322
323
324
325
326
327
329
330
336
347
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
391
392
393
397
398
399
400
401
402
403
404
405
406
407
414
415
416
417
418
419
420
421
422
427
431
432
433
434
435
436
437
438
442
446
447
448
451
457
460
461
462
463
468
469
470
471
472
473
474
475
476
477
481
482
486
487
491
492
496
500
501
502
503
507
511
512
513
514
515
519
520
524
525
526
527
531
532
533
534
535
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
606
607
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
663
667
668
671
672
/* ... */
/* ... */
/* ... */
#include "smartcard.h"
Includes
SC_ATR SC_A2R;
uint8_t SC_ATR_Table[40] = {0};
static __IO uint8_t SCData = 0;
static uint32_t F_Table[16] = {372, 372, 558, 744, 1116, 1488, 1860, 0,
0, 512, 768, 1024, 1536, 2048, 0, 0...};
static uint32_t D_Table[16] = {0, 1, 2, 4, 8, 16, 32, 0, 12, 20, 0, 0, 0, 0, 0, 0};
SMARTCARD_HandleTypeDef SCHandle;
Global variables definition and initialization
static void SC_SendData(SC_ADPU_Commands *SC_ADPU, SC_ADPU_Response *SC_ResponseStatus);
--------------APDU
static void SC_AnswerReq(SC_State *SCState, uint8_t *card, uint8_t length);
static uint8_t SC_decode_Answer2reset(uint8_t *card);
------------ ATR
static void SC_Init(void);
static void SC_DeInit(void);
static void SC_VoltageConfig(uint32_t SC_Voltage);
static uint8_t SC_Detect(void);
Physical Port Layer
/* ... */
void SC_Handler(SC_State *SCState, SC_ADPU_Commands *SC_ADPU, SC_ADPU_Response *SC_Response)
{
uint32_t i = 0;
switch(*SCState)
{
case SC_POWER_ON:
if (SC_ADPU->Header.INS == SC_GET_A2R)
{
SC_Init();
Smartcard initialization
for (i = 0; i < 40; i++)
{
SC_ATR_Table[i] = 0;
}for (i = 0; i < 40; i++) { ... }
Reset Data from SC buffer
SC_A2R.TS = 0;
SC_A2R.T0 = 0;
for (i = 0; i < SETUP_LENGTH; i++)
{
SC_A2R.T[i] = 0;
}for (i = 0; i < SETUP_LENGTH; i++) { ... }
for (i = 0; i < HIST_LENGTH; i++)
{
SC_A2R.H[i] = 0;
}for (i = 0; i < HIST_LENGTH; i++) { ... }
SC_A2R.Tlength = 0;
SC_A2R.Hlength = 0;
Reset SC_A2R Structure
*SCState = SC_RESET_LOW;
}if (SC_ADPU->Header.INS == SC_GET_A2R) { ... }
break;
case SC_POWER_ON:
case SC_RESET_LOW:
if(SC_ADPU->Header.INS == SC_GET_A2R)
{
if (SC_Detect())
{
while(((*SCState) != SC_POWER_OFF) && ((*SCState) != SC_ACTIVE))
{
SC_AnswerReq(SCState, (uint8_t *)&SC_ATR_Table, 40);
}while (((*SCState) != SC_POWER_OFF) && ((*SCState) != SC_ACTIVE)) { ... }
}if (SC_Detect()) { ... }
else
{
(*SCState) = SC_POWER_OFF;
}else { ... }
}if (SC_ADPU->Header.INS == SC_GET_A2R) { ... }
break;
case SC_RESET_LOW:
case SC_ACTIVE:
if (SC_ADPU->Header.INS == SC_GET_A2R)
{
if(SC_decode_Answer2reset(&SC_ATR_Table[0]) == T0_PROTOCOL)
{
(*SCState) = SC_ACTIVE_ON_T0;
}if (SC_decode_Answer2reset(&SC_ATR_Table[0]) == T0_PROTOCOL) { ... }
else
{
(*SCState) = SC_POWER_OFF;
}else { ... }
}if (SC_ADPU->Header.INS == SC_GET_A2R) { ... }
break;
case SC_ACTIVE:
case SC_ACTIVE_ON_T0:
SC_SendData(SC_ADPU, SC_Response);
break;
case SC_ACTIVE_ON_T0:
case SC_POWER_OFF:
SC_DeInit();
break;
case SC_POWER_OFF:
default: (*SCState) = SC_POWER_OFF;
}switch (*SCState) { ... }
}{ ... }
/* ... */
void SC_PowerCmd(FunctionalState NewState)
{
if(NewState != DISABLE)
{
HAL_GPIO_WritePin(SC_CMDVCC_GPIO_PORT, SC_CMDVCC_PIN, GPIO_PIN_RESET);
}if (NewState != DISABLE) { ... }
else
{
HAL_GPIO_WritePin(SC_CMDVCC_GPIO_PORT, SC_CMDVCC_PIN, GPIO_PIN_SET);
}else { ... }
}{ ... }
/* ... */
void SC_Reset(GPIO_PinState ResetState)
{
HAL_GPIO_WritePin(SC_RESET_GPIO_PORT, SC_RESET_PIN, ResetState);
}{ ... }
/* ... */
void SC_ParityErrorHandler(void)
{
HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)&SCData, 1, SC_TRANSMIT_TIMEOUT);
}{ ... }
/* ... */
void SC_PTSConfig(void)
{
uint32_t workingbaudrate = 0, apbclock = 0;
uint8_t PPSConfirmStatus = 1;
uint8_t ptscmd[4] = {0x00};
uint8_t ptsanswer[4] = {0x00};
apbclock = HAL_RCC_GetPCLK2Freq();
apbclock /= ((SC_USART->GTPR & (uint16_t)0x00FF) * 2);
/* ... */
__HAL_SMARTCARD_DMA_REQUEST_ENABLE(&SCHandle, SMARTCARD_DMAREQ_RX);
if((SC_A2R.T0 & (uint8_t)0x10) == 0x10)
{
if(SC_A2R.T[0] != 0x11)
{
ptscmd[0] = 0xFF;
/* ... */
ptscmd[1] = 0x10;
ptscmd[2] = SC_A2R.T[0];
ptscmd[3] = (uint8_t)0xFF^(uint8_t)0x10^(uint8_t)SC_A2R.T[0];
HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)ptsanswer, 4);
HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)ptscmd, 4, SC_TRANSMIT_TIMEOUT);
__HAL_SMARTCARD_DMA_REQUEST_DISABLE(&SCHandle, SMARTCARD_DMAREQ_RX);
while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY)
{}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... }
if((ptsanswer[0] != 0xFF) || (ptsanswer[1] != 0x10) || (ptsanswer[2] != SC_A2R.T[0]) || (ptsanswer[3] != ((uint8_t)0xFF^(uint8_t)0x10^(uint8_t)SC_A2R.T[0])))
{
PPSConfirmStatus = 0x00;
}if ((ptsanswer[0] != 0xFF) || (ptsanswer[1] != 0x10) || (ptsanswer[2] != SC_A2R.T[0]) || (ptsanswer[3] != ((uint8_t)0xFF^(uint8_t)0x10^(uint8_t)SC_A2R.T[0]))) { ... }
if(PPSConfirmStatus == 0x01)
{
workingbaudrate = apbclock * D_Table[(SC_A2R.T[0] & (uint8_t)0x0F)];
workingbaudrate /= F_Table[((SC_A2R.T[0] >> 4) & (uint8_t)0x0F)];
SCHandle.Init.BaudRate = workingbaudrate;
SCHandle.Init.WordLength = SMARTCARD_WORDLENGTH_9B;
SCHandle.Init.StopBits = SMARTCARD_STOPBITS_1_5;
SCHandle.Init.Parity = SMARTCARD_PARITY_EVEN;
SCHandle.Init.Mode = SMARTCARD_MODE_TX_RX;
SCHandle.Init.CLKPolarity = SMARTCARD_POLARITY_LOW;
SCHandle.Init.CLKPhase = SMARTCARD_PHASE_1EDGE;
SCHandle.Init.CLKLastBit = SMARTCARD_LASTBIT_ENABLE;
SCHandle.Init.Prescaler = 12;
SCHandle.Init.GuardTime = 16;
SCHandle.Init.NACKState = SMARTCARD_NACK_ENABLE;
SCHandle.Instance = SC_USART;
if(HAL_SMARTCARD_Init(&SCHandle) != HAL_OK)
{
while (1);
}if (HAL_SMARTCARD_Init(&SCHandle) != HAL_OK) { ... }
}if (PPSConfirmStatus == 0x01) { ... }
}if (SC_A2R.T[0] != 0x11) { ... }
}if ((SC_A2R.T0 & (uint8_t)0x10) == 0x10) { ... }
}{ ... }
/* ... */
static void SC_SendData(SC_ADPU_Commands *SC_ADPU, SC_ADPU_Response *SC_ResponseStatus)
{
uint32_t i = 0;
uint8_t command[5] = {0x00};
uint8_t answer[40] = {0x00};
for(i = 0; i < LC_MAX; i++)
{
SC_ResponseStatus->Data[i] = 0;
}for (i = 0; i < LC_MAX; i++) { ... }
SC_ResponseStatus->SW1 = 0;
SC_ResponseStatus->SW2 = 0;
/* ... */
__HAL_SMARTCARD_DMA_REQUEST_ENABLE(&SCHandle, SMARTCARD_DMAREQ_RX);
Reset response buffer
command[0] = SC_ADPU->Header.CLA;
command[1] = SC_ADPU->Header.INS;
command[2] = SC_ADPU->Header.P1;
command[3] = SC_ADPU->Header.P2;
Send header
if(SC_ADPU->Body.LC)
{
command[4] = SC_ADPU->Body.LC;
HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)command, 5, SC_TRANSMIT_TIMEOUT);
}if (SC_ADPU->Body.LC) { ... }
else if(SC_ADPU->Body.LE)
{
command[4] = SC_ADPU->Body.LE;
HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)command, 5, SC_TRANSMIT_TIMEOUT);
}else if (SC_ADPU->Body.LE) { ... }
__HAL_SMARTCARD_FLUSH_DRREGISTER(&SCHandle);
HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[0], 1);
while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY)
{}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... }
/* ... */
if(((answer[0] & (uint8_t)0xF0) == 0x60) || ((answer[0] & (uint8_t)0xF0) == 0x90))
{
SC_ResponseStatus->SW1 = answer[0];
if(HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[1], 1) == HAL_OK)
{
SC_ResponseStatus->SW2 = answer[1];
}if (HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[1], 1) == HAL_OK) { ... }
}if (((answer[0] & (uint8_t)0xF0) == 0x60) || ((answer[0] & (uint8_t)0xF0) == 0x90)) { ... }
else if(((answer[0] & (uint8_t)0xFE) == (((uint8_t)~(SC_ADPU->Header.INS)) & (uint8_t)0xFE)) || \
((answer[0] & (uint8_t)0xFE) == (SC_ADPU->Header.INS & (uint8_t)0xFE)))
{
SC_ResponseStatus->Data[0] = answer[0];
}else if (((answer[0] & (uint8_t)0xFE) == (((uint8_t)~(SC_ADPU->Header.INS)) & (uint8_t)0xFE)) || \ ((answer[0] & (uint8_t)0xFE) == (SC_ADPU->Header.INS & (uint8_t)0xFE))) { ... }
Send body length to/from SC
if(SC_ResponseStatus->SW1 == 0x00)
{
if(SC_ADPU->Body.LC)
{
HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)&SC_ADPU->Body.Data[0], SC_ADPU->Body.LC, SC_TRANSMIT_TIMEOUT);
__HAL_SMARTCARD_FLUSH_DRREGISTER(&SCHandle);
__HAL_SMARTCARD_DMA_REQUEST_DISABLE(&SCHandle, SMARTCARD_DMAREQ_RX);
HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[0], 2);
while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY)
{}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... }
SC_ResponseStatus->SW1 = answer[0];
SC_ResponseStatus->SW2 = answer[1];
}if (SC_ADPU->Body.LC) { ... }
Send body data to SC
else if(SC_ADPU->Body.LE)
{
HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[0], (SC_ADPU->Body.LE +2));
while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY)
{}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... }
for(i = 0; i < SC_ADPU->Body.LE; i++)
{
SC_ResponseStatus->Data[i] = answer[i];
}for (i = 0; i < SC_ADPU->Body.LE; i++) { ... }
SC_ResponseStatus->SW1 = answer[SC_ADPU->Body.LE];
SC_ResponseStatus->SW2 = answer[SC_ADPU->Body.LE +1];
}else if (SC_ADPU->Body.LE) { ... }
}if (SC_ResponseStatus->SW1 == 0x00) { ... }
}{ ... }
/* ... */
static void SC_AnswerReq(SC_State *SCState, uint8_t *card, uint8_t length)
{
switch(*SCState)
{
case SC_RESET_LOW:
HAL_SMARTCARD_Receive(&SCHandle, card, length, SC_RECEIVE_TIMEOUT);
if(card[0] != 0x00)
{
(*SCState) = SC_ACTIVE;
SC_Reset(GPIO_PIN_SET);
}if (card[0] != 0x00) { ... }
else
{
(*SCState) = SC_RESET_HIGH;
}else { ... }
break;
case SC_RESET_LOW:
case SC_RESET_HIGH:
SC_Reset(GPIO_PIN_SET);
HAL_SMARTCARD_Receive(&SCHandle, card, length, SC_RECEIVE_TIMEOUT);
if(card[0])
{
(*SCState) = SC_ACTIVE;
}if (card[0]) { ... }
else
{
(*SCState) = SC_POWER_OFF;
}else { ... }
break;
case SC_RESET_HIGH:
case SC_ACTIVE:
break;
case SC_ACTIVE:
case SC_POWER_OFF:
SC_Reset(GPIO_PIN_SET);
SC_PowerCmd(DISABLE);
break;
case SC_POWER_OFF:
default:
(*SCState) = SC_RESET_LOW;
break;default
}switch (*SCState) { ... }
}{ ... }
/* ... */
static uint8_t SC_decode_Answer2reset(uint8_t *card)
{
uint32_t i = 0, flag = 0, buf = 0, protocol = 0;
SC_A2R.TS = card[0];
SC_A2R.T0 = card[1];
SC_A2R.Hlength = SC_A2R.T0 & (uint8_t)0x0F;
if ((SC_A2R.T0 & (uint8_t)0x80) == 0x80)
{
flag = 1;
}if ((SC_A2R.T0 & (uint8_t)0x80) == 0x80) { ... }
for (i = 0; i < 4; i++)
{
SC_A2R.Tlength = SC_A2R.Tlength + (((SC_A2R.T0 & (uint8_t)0xF0) >> (4 + i)) & (uint8_t)0x1);
}for (i = 0; i < 4; i++) { ... }
for (i = 0; i < SC_A2R.Tlength; i++)
{
SC_A2R.T[i] = card[i + 2];
}for (i = 0; i < SC_A2R.Tlength; i++) { ... }
if ((SC_A2R.T0 & (uint8_t)0x80) == 0x00)
{
protocol = 0;
}if ((SC_A2R.T0 & (uint8_t)0x80) == 0x00) { ... }
else
{
protocol = SC_A2R.T[SC_A2R.Tlength - 1] & (uint8_t)0x0F;
}else { ... }
while (flag)
{
if ((SC_A2R.T[SC_A2R.Tlength - 1] & (uint8_t)0x80) == 0x80)
{
flag = 1;
}if ((SC_A2R.T[SC_A2R.Tlength - 1] & (uint8_t)0x80) == 0x80) { ... }
else
{
flag = 0;
}else { ... }
buf = SC_A2R.Tlength;
SC_A2R.Tlength = 0;
for (i = 0; i < 4; i++)
{
SC_A2R.Tlength = SC_A2R.Tlength + (((SC_A2R.T[buf - 1] & (uint8_t)0xF0) >> (4 + i)) & (uint8_t)0x1);
}for (i = 0; i < 4; i++) { ... }
for (i = 0;i < SC_A2R.Tlength; i++)
{
SC_A2R.T[buf + i] = card[i + 2 + buf];
}for (i = 0;i < SC_A2R.Tlength; i++) { ... }
SC_A2R.Tlength += (uint8_t)buf;
}while (flag) { ... }
for (i = 0; i < SC_A2R.Hlength; i++)
{
SC_A2R.H[i] = card[i + 2 + SC_A2R.Tlength];
}for (i = 0; i < SC_A2R.Hlength; i++) { ... }
return (uint8_t)protocol;
}{ ... }
/* ... */
static void SC_Init(void)
{
/* ... */
SCHandle.Instance = SC_USART;
SCHandle.Init.BaudRate = 9408;
SCHandle.Init.WordLength = SMARTCARD_WORDLENGTH_9B;
SCHandle.Init.StopBits = SMARTCARD_STOPBITS_1_5;
SCHandle.Init.Parity = SMARTCARD_PARITY_EVEN;
SCHandle.Init.Mode = SMARTCARD_MODE_TX_RX;
SCHandle.Init.CLKPolarity = SMARTCARD_POLARITY_LOW;
SCHandle.Init.CLKPhase = SMARTCARD_PHASE_1EDGE;
SCHandle.Init.CLKLastBit = SMARTCARD_LASTBIT_ENABLE;
SCHandle.Init.Prescaler = 12;
SCHandle.Init.GuardTime = 16;
SCHandle.Init.NACKState = SMARTCARD_NACK_ENABLE;
HAL_SMARTCARD_Init(&SCHandle);
SC_Reset(GPIO_PIN_SET);
}{ ... }
/* ... */
static void SC_DeInit(void)
{
SC_PowerCmd(DISABLE);
HAL_SMARTCARD_DeInit(&SCHandle);
}{ ... }
/* ... */
static void SC_VoltageConfig(uint32_t SC_Voltage)
{
if(SC_Voltage == SC_VOLTAGE_5V)
{
HAL_GPIO_WritePin(SC_3_5V_GPIO_PORT, SC_3_5V_PIN, GPIO_PIN_SET);
}if (SC_Voltage == SC_VOLTAGE_5V) { ... }
else
{
HAL_GPIO_WritePin(SC_3_5V_GPIO_PORT, SC_3_5V_PIN, GPIO_PIN_RESET);
}else { ... }
}{ ... }
/* ... */
void SC_IOConfig(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SC_3_5V_CLK_ENABLE();
SC_RESET_CLK_ENABLE();
SC_CMDVCC_CLK_ENABLE();
SC_OFF_CLK_ENABLE();
GPIO_InitStruct.Pin = SC_CMDVCC_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(SC_CMDVCC_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SC_RESET_PIN;
HAL_GPIO_Init(SC_RESET_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SC_3_5V_PIN;
HAL_GPIO_Init(SC_3_5V_GPIO_PORT, &GPIO_InitStruct);
SC_VoltageConfig(SC_VOLTAGE_5V);
SC_PowerCmd(DISABLE);
SC_Reset(GPIO_PIN_SET);
GPIO_InitStruct.Pin = SC_OFF_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(SC_OFF_GPIO_PORT, &GPIO_InitStruct);
HAL_NVIC_SetPriority(SC_OFF_EXTI_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SC_OFF_EXTI_IRQn);
}{ ... }
/* ... */
static uint8_t SC_Detect(void)
{
return HAL_GPIO_ReadPin(SC_OFF_GPIO_PORT, SC_OFF_PIN);
}{ ... }
/* ... */
/* ... */