|
root / base / usr / src / common / ccid
ccid Plain Text 1767 lines 41.8 KB
   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
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 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
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 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
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2019, Joyent, Inc.
 * Copyright 2024 Oxide Computer Company
 */

/*
 * ATR parsing routines shared between userland (ccidadm) and the kernel (CCID
 * driver)
 */

#include "atr.h"
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/hexdump.h>

#ifdef	_KERNEL
#include <sys/inttypes.h>
#include <sys/sunddi.h>
#include <sys/kmem.h>
#else
#include <inttypes.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#endif

/*
 * The ATR must have at least 2 bytes and then may have up to 33 bytes. The
 * first byte is always TS and the second required byte is T0.
 */
#define	ATR_TS_IDX	0
#define	ATR_T0_IDX	1

/*
 * There are two valid values for TS. It must either be 0x3F or 0x3B. This is
 * required per ISO/IEC 7816-3:2006 section 8.1.
 */
#define	ATR_TS_INVERSE	0x3F
#define	ATR_TS_DIRECT	0x3B

/*
 * After TS, each word is used to indicate a combination of protocol and the
 * number of bits defined for that protocol. The lower nibble is treated as the
 * protocol. The upper nibble is treated to indicate which of four defined words
 * are present. These are usually referred to as TA, TB, TC, and TD. TD is
 * always used to indicate the next protocol and the number of bytes present for
 * that. T0 works in a similar way, except that it defines the number of
 * historical bytes present in its protocol section and then it refers to a set
 * of pre-defined global bytes that may be present.
 */
#define	ATR_TD_PROT(x)	((x) & 0x0f)
#define	ATR_TD_NBITS(x)	(((x) & 0xf0) >> 4)
#define	ATR_TA_MASK	0x1
#define	ATR_TB_MASK	0x2
#define	ATR_TC_MASK	0x4
#define	ATR_TD_MASK	0x8

#define	ATR_TA1_FTABLE(x)	(((x) & 0xf0) >> 4)
#define	ATR_TA1_DITABLE(x)	((x) & 0x0f)

#define	ATR_TA2_CANCHANGE(x)	(((x) & 0x80) == 0)
#define	ATR_TA2_HONORTA1(x)	(((x) & 0x10) == 0)
#define	ATR_TA2_PROTOCOL(x)	((x) & 0x0f)

/*
 * When the checksum is required in the ATR, each byte must XOR to zero.
 */
#define	ATR_CKSUM_TARGET	0

/*
 * Maximum number of historic ATR bytes. This is limited by the fact that it's a
 * 4-bit nibble.
 */
#define	ATR_HISTORICAL_MAX	15

/*
 * The maximum number of TA, TB, TC, and TD levels that can be encountered in a
 * given structure. In the best case, there are 30 bytes available (TS, T0, and
 * TCK use the others). Given that each one of these needs 4 bytes to be
 * represented, the maximum number of layers that can fit is seven.
 */
#define	ATR_TI_MAX	7

/*
 * Defined protocol values. See ISO/IEC 7816-3:2006 8.2.3 for this list.
 * Reserved values are noted but not defined.
 */
#define	ATR_PROTOCOL_T0		0
#define	ATR_PROTOCOL_T1		1

#define	ATR_T1_TB0_CWI(x)	((x) & 0x0f)
#define	ATR_T1_TB0_BWI(x)	(((x) & 0xf0) >> 4)
#define	ATR_T1_TC0_CRC(x)	(((x) & 0x01) != 0)

/*
 * T=2 and T=3 are reserved for future full-duplex operation.
 * T=4 is reserved for enhanced half-duplex character transmission.
 * T=5-13 are reserved for future use by ISO/IEC JTC 1/SC 17.
 * T=14 is for protocols not standardized by ISO/IEC JTC 1/SC 17.
 */
#define	ATR_PROTOCOL_T15	15

#define	ATR_T15_TA0_CLOCK(x)	(((x) & 0xc0) >> 6)
#define	ATR_T15_TA0_VOLTAGE(x)	((x) & 0x3f)

#define	ATR_T15_TB0_SPU_STANDARD(x)	(((x & 0x80)) != 0)

/*
 * Various definitions for the configuration of historical data. This comes from
 * ISO/IEC 7816-4:2013 Section 12.1.1.
 */

/*
 * The first historical byte is used to indicate the encoding of the data. Only
 * values 0x00, 0x80-0x8f are defined. All others are proprietary. 0x81-0x8f are
 * reserved for future use.
 */
#define	ATR_HIST_CAT_MAND_STATUS	0x00
#define	ATR_HIST_CAT_TLV_STATUS		0x80
#define	ATR_HIST_CAT_RFU_MIN		0x81
#define	ATR_HIST_CAT_RFU_MAX		0x8f

/*
 * From ISO/IEC 7816-3:2006 Section 8.3.
 *
 * The default value for Fi is 372 which is table entry 1. The default value for
 * Di is 1, which is table entry 1.
 */
#define	ATR_FI_DEFAULT_INDEX	1
#define	ATR_DI_DEFAULT_INDEX	1
#define	ATR_EXTRA_GUARDTIME_DEFAULT	0

/*
 * From ISO/IEC 7816-3:2006 Section 10.2.
 */
#define	ATR_T0_WI_DEFAULT	10

/*
 * From ISO/IEC 7816-3:2006 Section 11.4.3.
 */
#define	ATR_T1_CWI_DEFAULT	13

/*
 * From ISO/IEC 7816-3:2006 Section 11.4.3.
 */
#define	ATR_T1_BWI_DEFAULT	4

/*
 * From ISO/IEC 7816-3:2006 Section 11.4.2.
 */
#define	ATR_T1_IFSC_DEFAULT	32

/*
 * From ISO/IEC 7816-3:2006 Section 11.4.4
 */
#define	ATR_T1_CHECKSUM_DEFAULT	ATR_T1_CHECKSUM_LRC

/*
 * Definitions for PPS construction. These are derived from ISO/IEC 7816-3:2006
 * section 9, Protocol and parameters selection.
 */
#define	PPS_LEN_MIN	3	/* PPSS, PPS0, PCK */
#define	PPS_LEN_MAX	PPS_BUFFER_MAX
#define	PPS_PPSS_INDEX	0
#define	PPS_PPSS_VAL	0xff
#define	PPS_PPS0_INDEX	0x01
#define	PPS_PPS0_PROT(x)	((x) & 0x0f)
#define	PPS_PPS0_PPS1		(1 << 4)
#define	PPS_PPS0_PPS2		(1 << 5)
#define	PPS_PPS0_PPS3		(1 << 6)
#define	PPS_PPS1_SETVAL(f, d)	((((f) & 0x0f) << 4) | ((d) & 0x0f))

/*
 * This enum and subsequent structure is used to represent a single level of
 * 'T'. This includes the possibility for all three values to be set and records
 * the protocol.
 */
typedef enum atr_ti_flags {
	ATR_TI_HAVE_TA	= 1 << 0,
	ATR_TI_HAVE_TB	= 1 << 1,
	ATR_TI_HAVE_TC	= 1 << 2,
	ATR_TI_HAVE_TD	= 1 << 3
} atr_ti_flags_t;

typedef struct atr_ti {
	uint8_t		atrti_protocol;
	uint8_t		atrti_ti_val;
	uint8_t		atrti_td_idx;
	atr_ti_flags_t	atrti_flags;
	uint8_t		atrti_ta;
	uint8_t		atrti_tb;
	uint8_t		atrti_tc;
	uint8_t		atrti_td;
} atr_ti_t;

typedef enum atr_flags {
	ATR_F_USES_DIRECT	= 1 << 0,
	ATR_F_USES_INVERSE	= 1 << 1,
	ATR_F_HAS_CHECKSUM	= 1 << 2,
	ATR_F_VALID		= 1 << 3
} atr_flags_t;


struct atr_data {
	atr_flags_t	atr_flags;
	uint8_t		atr_nti;
	atr_ti_t	atr_ti[ATR_TI_MAX];
	uint8_t		atr_nhistoric;
	uint8_t		atr_historic[ATR_HISTORICAL_MAX];
	uint8_t		atr_cksum;
	uint8_t		atr_raw[ATR_LEN_MAX];
	uint8_t		atr_nraw;
};

/*
 * These tables maps the bit values for Fi from 7816-3:2006 section 8.3 Table 7.
 */
static uint_t atr_fi_valtable[16] = {
	372,		/* 0000 */
	372,		/* 0001 */
	558,		/* 0010 */
	744,		/* 0011 */
	1116,		/* 0100 */
	1488,		/* 0101 */
	1860,		/* 0110 */
	0,		/* 0111 */
	0,		/* 1000 */
	512,		/* 1001 */
	768,		/* 1010 */
	1024,		/* 1011 */
	1536,		/* 1100 */
	2048,		/* 1101 */
	0,		/* 1110 */
	0		/* 1111 */
};

static const char *atr_fi_table[16] = {
	"372",		/* 0000 */
	"372",		/* 0001 */
	"558",		/* 0010 */
	"744",		/* 0011 */
	"1116",		/* 0100 */
	"1488",		/* 0101 */
	"1860",		/* 0110 */
	"RFU",		/* 0111 */
	"RFU",		/* 1000 */
	"512",		/* 1001 */
	"768",		/* 1010 */
	"1024",		/* 1011 */
	"1536",		/* 1100 */
	"2048",		/* 1101 */
	"RFU",		/* 1110 */
	"RFU",		/* 1111 */
};

/*
 * This table maps the bit values for f(max) from 7816-3:2006 section 8.3
 * Table 7.
 */
static const char *atr_fmax_table[16] = {
	"4",		/* 0000 */
	"5",		/* 0001 */
	"6",		/* 0010 */
	"8",		/* 0011 */
	"12",		/* 0100 */
	"16",		/* 0101 */
	"20",		/* 0110 */
	"-",		/* 0111 */
	"-",		/* 1000 */
	"5",		/* 1001 */
	"7.5",		/* 1010 */
	"10",		/* 1011 */
	"15",		/* 1100 */
	"20",		/* 1101 */
	"-",		/* 1110 */
	"-",		/* 1111 */
};

/*
 * This table maps the bit values for Di from 7816-3:2006 section 8.3 Table 8.
 */
static uint_t atr_di_valtable[16] = {
	0,		/* 0000 */
	1,		/* 0001 */
	2,		/* 0010 */
	4,		/* 0011 */
	8,		/* 0100 */
	16,		/* 0101 */
	32,		/* 0110 */
	64,		/* 0111 */
	12,		/* 1000 */
	20,		/* 1001 */
	0,		/* 1010 */
	0,		/* 1011 */
	0,		/* 1100 */
	0,		/* 1101 */
	0,		/* 1110 */
	0		/* 1111 */
};

static const char *atr_di_table[16] = {
	"RFU",		/* 0000 */
	"1",		/* 0001 */
	"2",		/* 0010 */
	"4",		/* 0011 */
	"8",		/* 0100 */
	"16",		/* 0101 */
	"32",		/* 0110 */
	"64",		/* 0111 */
	"12",		/* 1000 */
	"20",		/* 1001 */
	"RFU",		/* 1010 */
	"RFU",		/* 1011 */
	"RFU",		/* 1100 */
	"RFU",		/* 1101 */
	"RFU",		/* 1110 */
	"RFU",		/* 1111 */
};

/*
 * This table maps the bit values for the clock stop indicator from 7816-3:2006
 * section 8.3 Table 9.
 */
static const char *atr_clock_table[4] = {
	"disallowed",		/* 00 */
	"signal low",		/* 01 */
	"signal high",		/* 10 */
	"signal low or high"	/* 11 */
};

uint_t
atr_fi_index_to_value(uint8_t val)
{
	if (val >= ARRAY_SIZE(atr_fi_valtable)) {
		return (0);
	}

	return (atr_fi_valtable[val]);
}

const char *
atr_fi_index_to_string(uint8_t val)
{
	if (val >= ARRAY_SIZE(atr_fi_table)) {
		return ("<invalid>");
	}

	return (atr_fi_table[val]);
}

const char *
atr_fmax_index_to_string(uint8_t val)
{
	if (val >= ARRAY_SIZE(atr_fmax_table)) {
		return ("<invalid>");
	}

	return (atr_fmax_table[val]);
}

uint_t
atr_di_index_to_value(uint8_t val)
{
	if (val >= ARRAY_SIZE(atr_di_valtable)) {
		return (0);
	}

	return (atr_di_valtable[val]);
}
const char *
atr_di_index_to_string(uint8_t val)
{
	if (val >= ARRAY_SIZE(atr_di_table)) {
		return ("<invalid>");
	}

	return (atr_di_table[val]);
}

const char *
atr_clock_stop_to_string(atr_clock_stop_t val)
{
	if (val >= ARRAY_SIZE(atr_clock_table)) {
		return ("<invalid>");
	}

	return (atr_clock_table[val]);
}

const char *
atr_protocol_to_string(atr_protocol_t prot)
{
	if (prot == ATR_P_NONE) {
		return ("none");
	}

	if ((prot & ATR_P_T0) == ATR_P_T0) {
		return ("T=0");
	} else if ((prot & ATR_P_T1) == ATR_P_T1) {
		return ("T=1");
	} else {
		return ("T=0, T=1");
	}
}

const char *
atr_convention_to_string(atr_convention_t conv)
{
	if (conv == ATR_CONVENTION_DIRECT) {
		return ("direct");
	} else if (conv == ATR_CONVENTION_INVERSE) {
		return ("inverse");
	} else {
		return ("<invalid convention>");
	}
}

const char *
atr_strerror(atr_parsecode_t code)
{
	switch (code) {
	case ATR_CODE_OK:
		return ("ATR parsed successfully");
	case ATR_CODE_TOO_SHORT:
		return ("Specified buffer too short");
	case ATR_CODE_TOO_LONG:
		return ("Specified buffer too long");
	case ATR_CODE_INVALID_TS:
		return ("ATR has invalid TS byte value");
	case ATR_CODE_OVERRUN:
		return ("ATR data requires more bytes than provided");
	case ATR_CODE_UNDERRUN:
		return ("ATR data did not use all provided bytes");
	case ATR_CODE_CHECKSUM_ERROR:
		return ("ATR data did not checksum correctly");
	case ATR_CODE_INVALID_TD1:
		return ("ATR data has invalid protocol in TD1");
	default:
		return ("Unknown Parse Code");
	}
}

static uint_t
atr_count_cbits(uint8_t x)
{
	uint_t ret = 0;

	if (x & ATR_TA_MASK)
		ret++;
	if (x & ATR_TB_MASK)
		ret++;
	if (x & ATR_TC_MASK)
		ret++;
	if (x & ATR_TD_MASK)
		ret++;
	return (ret);
}

/*
 * Parse out ATR values. Focus on only parsing it and not interpreting it.
 * Interpretation should be done in other functions that can walk over the data
 * and be more protocol-aware.
 */
atr_parsecode_t
atr_parse(const uint8_t *buf, size_t len, atr_data_t *data)
{
	uint_t nhist, cbits, ncbits, idx, Ti, prot;
	uint_t ncksum = 0;
	atr_ti_t *atp;

	/*
	 * Zero out data in case someone's come back around for another loop on
	 * the same data.
	 */
	bzero(data, sizeof (atr_data_t));

	if (len < ATR_LEN_MIN) {
		return (ATR_CODE_TOO_SHORT);
	}

	if (len > ATR_LEN_MAX) {
		return (ATR_CODE_TOO_LONG);
	}

	if (buf[ATR_TS_IDX] != ATR_TS_INVERSE &&
	    buf[ATR_TS_IDX] != ATR_TS_DIRECT) {
		return (ATR_CODE_INVALID_TS);
	}

	bcopy(buf, data->atr_raw, len);
	data->atr_nraw = len;

	if (buf[ATR_TS_IDX] == ATR_TS_DIRECT) {
		data->atr_flags |= ATR_F_USES_DIRECT;
	} else {
		data->atr_flags |= ATR_F_USES_INVERSE;
	}

	/*
	 * The protocol of T0 is the number of historical bits present.
	 */
	nhist = ATR_TD_PROT(buf[ATR_T0_IDX]);
	cbits = ATR_TD_NBITS(buf[ATR_T0_IDX]);
	idx = ATR_T0_IDX + 1;
	ncbits = atr_count_cbits(cbits);

	/*
	 * Ti is used to track the current iteration of T[A,B,C,D] that we are
	 * on, as the ISO/IEC standard suggests. The way that values are
	 * interpreted depends on the value of Ti.
	 *
	 * When Ti is one, TA, TB, and TC represent global properties. TD's
	 * protocol represents the preferred protocol.
	 *
	 * When Ti is two, TA, TB, and TC also represent global properties.
	 * However, TC only has meaning if the protocol is T=0.
	 *
	 * When Ti is 15, it indicates more global properties.
	 *
	 * For all other values of Ti, the meaning depends on the protocol in
	 * question and they are all properties specific to that protocol.
	 */
	Ti = 1;
	/*
	 * Initialize prot to an invalid protocol to help us deal with the
	 * normal workflow and make sure that we don't mistakenly do anything.
	 */
	prot = UINT32_MAX;
	for (;;) {
		atp = &data->atr_ti[data->atr_nti];
		data->atr_nti++;
		ASSERT3U(data->atr_nti, <=, ATR_TI_MAX);

		/*
		 * Make sure that we have enough space to read all the cbits.
		 * idx points to the first cbit, which could also potentially be
		 * over the length of the buffer. This is why we subtract one
		 * from idx when doing the calculation.
		 */
		if (idx - 1 + ncbits >= len) {
			return (ATR_CODE_OVERRUN);
		}

		ASSERT3U(Ti, !=, 0);

		/*
		 * At the moment we opt to ignore reserved protocols.
		 */
		atp->atrti_protocol = prot;
		atp->atrti_ti_val = Ti;
		atp->atrti_td_idx = idx - 1;

		if (cbits & ATR_TA_MASK) {
			atp->atrti_flags |= ATR_TI_HAVE_TA;
			atp->atrti_ta = buf[idx];
			idx++;
		}

		if (cbits & ATR_TB_MASK) {
			atp->atrti_flags |= ATR_TI_HAVE_TB;
			atp->atrti_tb = buf[idx];
			idx++;
		}

		if (cbits & ATR_TC_MASK) {
			atp->atrti_flags |= ATR_TI_HAVE_TC;
			atp->atrti_tc = buf[idx];
			idx++;
		}

		if (cbits & ATR_TD_MASK) {
			atp->atrti_flags |= ATR_TI_HAVE_TD;
			atp->atrti_td = buf[idx];
			cbits = ATR_TD_NBITS(buf[idx]);
			prot = ATR_TD_PROT(buf[idx]);
			ncbits = atr_count_cbits(cbits);
			if (prot != 0)
				ncksum = 1;

			/*
			 * T=15 is not allowed in TD1 per 8.2.3.
			 */
			if (Ti == 1 && prot == 0xf)
				return (ATR_CODE_INVALID_TD1);

			idx++;
			/*
			 * Encountering TD means that once we take the next loop
			 * and we need to increment Ti.
			 */
			Ti++;
		} else {
			break;
		}
	}

	/*
	 * We've parsed all of the cbits. At this point, we should take into
	 * account all of the historical bits and potentially the checksum.
	 */
	if (idx - 1 + nhist + ncksum >= len) {
		return (ATR_CODE_OVERRUN);
	}

	if (idx + nhist + ncksum != len) {
		return (ATR_CODE_UNDERRUN);
	}

	if (nhist > 0) {
		data->atr_nhistoric = nhist;
		bcopy(&buf[idx], data->atr_historic, nhist);
	}

	if (ncksum > 0) {
		size_t i;
		uint8_t val;

		/*
		 * Per ISO/IEC 7816-3:2006 Section 8.2.5 the checksum is all
		 * bytes excluding TS. Therefore, we must start at byte 1.
		 */
		for (val = 0, i = 1; i < len; i++) {
			val ^= buf[i];
		}

		if (val != ATR_CKSUM_TARGET) {
			return (ATR_CODE_CHECKSUM_ERROR);
		}
		data->atr_flags |= ATR_F_HAS_CHECKSUM;
		data->atr_cksum = buf[len - 1];
	}

	data->atr_flags |= ATR_F_VALID;
	return (ATR_CODE_OK);
}

uint8_t
atr_fi_default_index(void)
{
	return (ATR_FI_DEFAULT_INDEX);
}

uint8_t
atr_di_default_index(void)
{
	return (ATR_DI_DEFAULT_INDEX);
}

/*
 * Parse the data to determine which protocols are supported in this atr data.
 * Based on this, users can come and ask us to fill in protocol information.
 */
atr_protocol_t
atr_supported_protocols(atr_data_t *data)
{
	uint_t i;
	atr_protocol_t prot;

	if ((data->atr_flags & ATR_F_VALID) == 0)
		return (ATR_P_NONE);

	/*
	 * Based on 8.2.3 of ISO/IEC 7816-3:2006, if TD1 is present, then that
	 * indicates the first protocol. However, if it is not present, then
	 * that implies that T=0 is the only supported protocol. Otherwise, all
	 * protocols are referenced in ascending order. The first entry in
	 * atr_ti refers to data from T0, so the protocol in the second entry
	 * would have the TD1 data.
	 */
	if (data->atr_nti < 2) {
		return (ATR_P_T0);
	}

	prot = ATR_P_NONE;
	for (i = 0; i < data->atr_nti; i++) {
		switch (data->atr_ti[i].atrti_protocol) {
		case ATR_PROTOCOL_T0:
			prot |= ATR_P_T0;
			break;
		case ATR_PROTOCOL_T1:
			prot |= ATR_P_T1;
			break;
		default:
			/*
			 * T=15 is not a protocol, and all other protocol values
			 * are currently reserved for future use.
			 */
			continue;
		}
	}

	/*
	 * It's possible we've found nothing specific in the above loop (for
	 * example, only T=15 global bits were found). In that case, the card
	 * defaults to T=0.
	 */
	if (prot == ATR_P_NONE)
		prot = ATR_P_T0;

	return (prot);
}

boolean_t
atr_params_negotiable(atr_data_t *data)
{
	/* If for some reason we're called with invalid data, assume it's not */
	if ((data->atr_flags & ATR_F_VALID) == 0)
		return (B_FALSE);


	/*
	 * Whether or not we're negotiable is in the second global page, so atr
	 * index 1. If TA2 is missing, then the card always is negotiable.
	 */
	if (data->atr_nti < 2 ||
	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) == 0) {
		return (B_TRUE);
	}

	if (ATR_TA2_CANCHANGE(data->atr_ti[1].atrti_ta)) {
		return (B_TRUE);
	}

	return (B_FALSE);
}

atr_protocol_t
atr_default_protocol(atr_data_t *data)
{
	uint8_t prot;

	if ((data->atr_flags & ATR_F_VALID) == 0)
		return (ATR_P_NONE);
	/*
	 * If we don't have an TA2 byte, then the system defaults to T=0.
	 */
	if (data->atr_nti < 2) {
		return (ATR_P_T0);
	}

	/*
	 * If TA2 is present, then it encodes the default protocol. Otherwise,
	 * we have to grab the protocol value from TD1, which is called the
	 * 'first offered protocol'.
	 */
	if ((data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) {
		prot = ATR_TA2_PROTOCOL(data->atr_ti[1].atrti_ta);
	} else {
		prot = data->atr_ti[1].atrti_protocol;
	}

	switch (prot) {
	case ATR_PROTOCOL_T0:
		return (ATR_P_T0);
	case ATR_PROTOCOL_T1:
		return (ATR_P_T1);
	default:
		return (ATR_P_NONE);
	}
}

uint8_t
atr_fi_index(atr_data_t *data)
{
	if (data->atr_nti < 1) {
		return (ATR_FI_DEFAULT_INDEX);
	}

	/*
	 * If TA is specified, it is present in TA1. TA2 may override its
	 * presence, so if it is here, check that first to determine whether or
	 * not we should check TA1.
	 */
	if (data->atr_nti >= 2 &&
	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) {
		if (!ATR_TA2_HONORTA1(data->atr_ti[1].atrti_ta)) {
			return (ATR_FI_DEFAULT_INDEX);
		}
	}

	if ((data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TA) != 0) {
		return (ATR_TA1_FTABLE(data->atr_ti[0].atrti_ta));
	}

	return (ATR_FI_DEFAULT_INDEX);
}

uint8_t
atr_di_index(atr_data_t *data)
{
	if (data->atr_nti < 1) {
		return (ATR_DI_DEFAULT_INDEX);
	}

	/*
	 * If TA is specified, it is present in TA1. TA2 may override its
	 * presence, so if it is here, check that first to determine whether or
	 * not we should check TA1.
	 */
	if (data->atr_nti >= 2 &&
	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) {
		if (!ATR_TA2_HONORTA1(data->atr_ti[1].atrti_ta)) {
			return (ATR_DI_DEFAULT_INDEX);
		}
	}

	if ((data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TA) != 0) {
		return (ATR_TA1_DITABLE(data->atr_ti[0].atrti_ta));
	}

	return (ATR_DI_DEFAULT_INDEX);
}

atr_convention_t
atr_convention(atr_data_t *data)
{
	if ((data->atr_flags & ATR_F_USES_DIRECT) != 0) {
		return (ATR_CONVENTION_DIRECT);
	}
	return (ATR_CONVENTION_INVERSE);
}

uint8_t
atr_extra_guardtime(atr_data_t *data)
{
	if ((data->atr_flags & ATR_F_VALID) == 0)
		return (ATR_EXTRA_GUARDTIME_DEFAULT);

	if (data->atr_nti >= 1 &&
	    (data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TC) != 0) {
		return (data->atr_ti[0].atrti_tc);
	}

	return (ATR_EXTRA_GUARDTIME_DEFAULT);
}

uint8_t
atr_t0_wi(atr_data_t *data)
{
	if ((data->atr_flags & ATR_F_VALID) == 0)
		return (ATR_T0_WI_DEFAULT);

	/*
	 * This is stored in the optional global byte in TC2; however, it only
	 * applies to T=0.
	 */
	if (data->atr_nti >= 2 &&
	    data->atr_ti[1].atrti_protocol == ATR_PROTOCOL_T0 &&
	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TC) != 0) {
		return (data->atr_ti[1].atrti_tc);
	}

	return (ATR_T0_WI_DEFAULT);
}

uint8_t
atr_t1_cwi(atr_data_t *data)
{
	uint8_t i;

	if (data->atr_nti <= 2) {
		return (ATR_T1_CWI_DEFAULT);
	}

	for (i = 2; i < data->atr_nti; i++) {
		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TB) !=
			    0) {
				uint8_t tb = data->atr_ti[i].atrti_tb;
				return (ATR_T1_TB0_CWI(tb));
			}

			return (ATR_T1_CWI_DEFAULT);
		}
	}

	return (ATR_T1_CWI_DEFAULT);
}

atr_clock_stop_t
atr_clock_stop(atr_data_t *data)
{
	uint8_t i;

	for (i = 0; i < data->atr_nti; i++) {
		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T15) {
			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TA) !=
			    0) {
				uint8_t ta = data->atr_ti[i].atrti_ta;
				return (ATR_T15_TA0_CLOCK(ta));
			}

			return (ATR_CLOCK_STOP_NONE);
		}
	}

	return (ATR_CLOCK_STOP_NONE);
}

atr_t1_checksum_t
atr_t1_checksum(atr_data_t *data)
{
	uint8_t i;

	if (data->atr_nti <= 2) {
		return (ATR_T1_CHECKSUM_DEFAULT);
	}

	for (i = 2; i < data->atr_nti; i++) {
		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TC) !=
			    0) {
				if (ATR_T1_TC0_CRC(data->atr_ti[i].atrti_tc)) {
					return (ATR_T1_CHECKSUM_CRC);
				} else {
					return (ATR_T1_CHECKSUM_LRC);
				}
			}

			return (ATR_T1_CHECKSUM_DEFAULT);
		}
	}

	return (ATR_T1_CHECKSUM_DEFAULT);

}

uint8_t
atr_t1_bwi(atr_data_t *data)
{
	uint8_t i;

	if (data->atr_nti <= 2) {
		return (ATR_T1_BWI_DEFAULT);
	}

	for (i = 2; i < data->atr_nti; i++) {
		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TB) !=
			    0) {
				uint8_t tb = data->atr_ti[i].atrti_tb;
				return (ATR_T1_TB0_BWI(tb));
			}

			return (ATR_T1_BWI_DEFAULT);
		}
	}

	return (ATR_T1_BWI_DEFAULT);
}

uint8_t
atr_t1_ifsc(atr_data_t *data)
{
	uint8_t i;

	if (data->atr_nti <= 2) {
		return (ATR_T1_IFSC_DEFAULT);
	}

	for (i = 2; i < data->atr_nti; i++) {
		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TA) !=
			    0) {
				return (data->atr_ti[i].atrti_ta);
			}

			return (ATR_T1_IFSC_DEFAULT);
		}
	}

	return (ATR_T1_IFSC_DEFAULT);
}

/*
 * Attempt to determine which set of data rates we should be able to use for a
 * given class of protocol. Here we want to do the calculation based on the CCID
 * specification, section 9.4.x. To use these higher rates we need:
 *
 * + Reader's data rate > frequency * Di / Fi.
 *
 * To determine which rate and frequency we use, we look at the reader's
 * features. If the reader supports both the Automatic baud rate and automatic
 * ICC clock frequency change, then we use the _maximum_ rate. Otherwise we will
 * indicate that we can use the ATR's properties, but will require changing the
 * default data rate.
 *
 * Now, some ICC devices are not negotiable. In those cases, we'll see if we can
 * fit it in with either the default or maximum data rates. If not, then we'll
 * not be able to support this card.
 *
 * There are two wrinkles that exist in this. The first is supported frequencies
 * and data rates. If there are no additional data rates supported, then all of
 * the data rates between the default and max are supported. If not, then only
 * those specified in the data rates array are supported.
 *
 * The second hurdle is that we need to do this division and try and avoid the
 * pitfalls of floating point arithmetic, as floating point is not allowed in
 * the kernel (and this is shared). Importantly that means only integers are
 * allowed here.
 */
atr_data_rate_choice_t
atr_data_rate(atr_data_t *data, ccid_class_descr_t *class, uint32_t *rates,
    uint_t nrates, uint32_t *dataratep)
{
	uint_t nfeats = CCID_CLASS_F_AUTO_ICC_CLOCK | CCID_CLASS_F_AUTO_BAUD;
	uint8_t di, fi;
	uint_t dival, fival;
	boolean_t autospeed, negotiable, exprates;
	uint64_t maxval, defval;

	if ((data->atr_flags & ATR_F_VALID) == 0)
		return (ATR_RATE_UNSUPPORTED);

	di = atr_di_index(data);
	fi = atr_fi_index(data);
	dival = atr_di_index_to_value(di);
	fival = atr_fi_index_to_value(fi);
	autospeed = (class->ccd_dwFeatures & nfeats) == nfeats;
	exprates = class->ccd_bNumDataRatesSupported != 0;
	negotiable = atr_params_negotiable(data);

	/*
	 * We don't support cards with fixed rates at this time as it's not
	 * clear what that rate should be. If it's negotiable, we'll let them
	 * run at the default. Otherwise, we have to fail the request until
	 * we implement the logic to search their data rates.
	 */
	if (exprates) {
		if (negotiable) {
			return (ATR_RATE_USEDEFAULT);
		}
		return (ATR_RATE_UNSUPPORTED);
	}

	/*
	 * This indicates that the card gave us values that were reserved for
	 * future use. If we could negotiate it, then just stick with the
	 * default paramters. Otherwise, return that we can't support this ICC.
	 */
	if (dival == 0 || fival == 0) {
		if (negotiable)
			return (ATR_RATE_USEDEFAULT);
		return (ATR_RATE_UNSUPPORTED);
	}

	/*
	 * Calculate the maximum and default values.
	 */
	maxval = class->ccd_dwMaximumClock * 1000;
	maxval *= dival;
	maxval /= fival;

	defval = class->ccd_dwDefaultClock * 1000;
	defval *= dival;
	defval /= fival;

	/*
	 * We're allowed any set of data rates between the default and the
	 * maximum. Check if the maximum data rate will work for either the
	 * default or maximum clock. If so, then we can use the cards rates.
	 *
	 * To account for the fact that we may have had a fractional value,
	 * we require a strict greater than comparison.
	 */
	if ((uint64_t)class->ccd_dwMaxDataRate > maxval ||
	    (uint64_t)class->ccd_dwMaxDataRate > defval) {
		if (autospeed) {
			return (ATR_RATE_USEATR);
		}
	}

	/*
	 * If the CCID reader can't handle the ICC's proposed rates, then fall
	 * back to the defaults if we're allowed to negotiate. Otherwise, we're
	 * not able to use this ICC.
	 */
	if (negotiable) {
		return (ATR_RATE_USEDEFAULT);
	}

	return (ATR_RATE_UNSUPPORTED);
}

void
atr_data_reset(atr_data_t *data)
{
	bzero(data, sizeof (*data));
}

#ifdef	_KERNEL
atr_data_t *
atr_data_alloc(void)
{
	return (kmem_zalloc(sizeof (atr_data_t), KM_SLEEP));
}

void
atr_data_free(atr_data_t *data)
{
	kmem_free(data, sizeof (atr_data_t));
}

/*
 * Make sure that the response we got from the ICC is valid. It must pass
 * checksum and have the PPSS value set correctly. The protocol must match
 * what we requested; however, the PPS1-3 bits are a bit different. They may
 * only be set in the response if we set them in the request. However, they
 * do not have to be set in the response.
 */
boolean_t
atr_pps_valid(void *reqbuf, size_t reqlen, void *respbuf, size_t resplen)
{
	uint8_t val, i, reqidx, respidx;
	uint8_t *req = reqbuf, *resp = respbuf;

	if (resplen > PPS_LEN_MAX || resplen < PPS_LEN_MIN)
		return (B_FALSE);

	/*
	 * Before we validate the data, make sure the checksum is valid.
	 */
	for (i = 0, val = 0; i < resplen; i++) {
		val ^= resp[i];
	}

	/* Checksum failure */
	if (val != 0) {
		return (B_FALSE);
	}

	/*
	 * We should always have PPSS echoed back as we set it.
	 */
	if (resp[PPS_PPSS_INDEX] != PPS_PPSS_VAL) {
		return (B_FALSE);
	}

	/*
	 * Go through and make sure the number of bytes present makes sense for
	 * the number of bits set in PPS1.
	 */
	val = PPS_LEN_MIN;
	if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1)
		val++;
	if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS2)
		val++;
	if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS3)
		val++;
	if (val != resplen)
		return (B_FALSE);

	/*
	 * Now we've finally verified that the response is syntactically valid.
	 * We must go through and make sure that it is semantically valid.
	 */
	if (PPS_PPS0_PROT(req[PPS_PPS0_INDEX]) !=
	    PPS_PPS0_PROT(resp[PPS_PPS0_INDEX])) {
		return (B_FALSE);
	}

	/*
	 * When checking the PPS bit and extensions, we first check in the
	 * response as a bit in the request is allowed to not be in the
	 * response. But not the opposite way around. We also have to keep track
	 * of the fact that the index for values will vary.
	 */
	reqidx = respidx = PPS_PPS0_INDEX + 1;
	if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0) {
		if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) == 0) {
			return (B_FALSE);
		}

		if (req[reqidx] != resp[respidx]) {
			return (B_FALSE);
		}

		reqidx++;
		respidx++;
	} else if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0) {
		reqidx++;
	}

	if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) != 0) {
		if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) == 0) {
			return (B_FALSE);
		}

		if (req[reqidx] != resp[respidx]) {
			return (B_FALSE);
		}

		reqidx++;
		respidx++;
	} else if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) != 0) {
		reqidx++;
	}

	if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS3) != 0) {
		/*
		 * At this time, we never specify PPS3 in a request. Therefore
		 * if it is present in the response, treat this as an invalid
		 * request.
		 */
		return (B_FALSE);
	}

	return (B_TRUE);
}

uint_t
atr_pps_generate(uint8_t *buf, size_t buflen, atr_protocol_t prot,
    boolean_t pps1, uint8_t fi, uint8_t di, boolean_t pps2, uint8_t spu)
{
	uint8_t protval, cksum, i;
	uint_t len = 0;

	if (buflen < PPS_BUFFER_MAX)
		return (0);

	buf[PPS_PPSS_INDEX] = PPS_PPSS_VAL;
	switch (prot) {
	case ATR_P_T0:
		protval = 0;
		break;
	case ATR_P_T1:
		protval = 1;
		break;
	default:
		return (0);
	}

	buf[PPS_PPS0_INDEX] = PPS_PPS0_PROT(protval);
	len = 2;
	if (pps1) {
		buf[PPS_PPS0_INDEX] |= PPS_PPS0_PPS1;
		buf[len++] = PPS_PPS1_SETVAL(fi, di);
	}

	if (pps2) {
		buf[PPS_PPS0_INDEX] |= PPS_PPS0_PPS2;
		buf[len++] = spu;
	}

	/*
	 * The checksum must xor to zero.
	 */
	for (i = 0, cksum = 0; i < len; i++) {
		cksum ^= buf[i];
	}
	buf[len++] = cksum;
	return (len);
}

/*
 * The caller of this wants to know if the Fi/Di values that they proposed were
 * accepted. The caller must have already called atr_pps_valid(). At this point,
 * we can say that the value was accepted if the PPS1 bit is set.
 */
boolean_t
atr_pps_fidi_accepted(void *respbuf, size_t len)
{
	uint8_t *resp = respbuf;
	return ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0);
}

#else	/* !_KERNEL */
atr_data_t *
atr_data_alloc(void)
{
	return (calloc(1, sizeof (atr_data_t)));
}

void
atr_data_free(atr_data_t *data)
{
	if (data == NULL)
		return;
	free(data);
}

/*
 * This table maps the bit values for Fi from 7816-3:2006 section 8.3 Table 9.
 * The table is up to 6 bits wide. Entries not present are RFU. We use NULL as a
 * sentinel to indicate that.
 */
static const char *atr_voltage_table[64] = {
	NULL,			/* 00 0000 */
	"5V",			/* 00 0001 */
	"3V",			/* 00 0010 */
	"5V, 3V",		/* 00 0011 */
	"1.5V",			/* 00 0100 */
	NULL,			/* 00 0101 */
	"3V, 1.5V",		/* 00 0110 */
	"5V, 3V, 1.5V"		/* 00 0111 */
};

static void
atr_data_dump_ta(atr_ti_t *atp, FILE *out, uint_t level)
{
	uint8_t ta;

	if (!(atp->atrti_flags & ATR_TI_HAVE_TA)) {
		return;
	}

	ta = atp->atrti_ta;
	(void) fprintf(out, "   %c%c%c+-> TA%u 0x%02x",
	    atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ',
	    atp->atrti_flags & ATR_TI_HAVE_TC ? '|' : ' ',
	    atp->atrti_flags & ATR_TI_HAVE_TB ? '|' : ' ',
	    atp->atrti_ti_val, ta);
	switch (atp->atrti_ti_val) {
	case 1:
		(void) fprintf(out, "; Fi: %s, F(max): %s MHz, Di: %s",
		    atr_fi_table[ATR_TA1_FTABLE(ta)],
		    atr_fmax_table[ATR_TA1_FTABLE(ta)],
		    atr_di_table[ATR_TA1_DITABLE(ta)]);
		break;
	case 2:
		(void) fprintf(out, "; ICC in %s mode; %shonoring TA1; default "
		    "T=%u",
		    ATR_TA2_CANCHANGE(ta) ? "negotiable" : "specific",
		    ATR_TA2_HONORTA1(ta) ? "" : "not ",
		    ATR_TA2_PROTOCOL(ta));
		break;
	default:
		switch (atp->atrti_protocol) {
		case ATR_PROTOCOL_T1:
			if (level != 0)
				break;
			if (ta == 0 || ta == 0xff) {
				(void) fprintf(out, "; IFSC: RFU");
			} else {
				(void) fprintf(out, "; IFSC: %u", ta);
			}
			break;
		case ATR_PROTOCOL_T15:
			if (level != 0)
				break;
			(void) fprintf(out, "; Clock stop: %s, Supported "
			    "Voltage: %s",
			    atr_clock_table[ATR_T15_TA0_CLOCK(ta)],
			    atr_voltage_table[ATR_T15_TA0_VOLTAGE(ta)] != NULL ?
			    atr_voltage_table[ATR_T15_TA0_VOLTAGE(ta)] : "RFU");
			break;
		default:
			break;
		}
	}
	(void) fprintf(out, "\n");
}

static void
atr_data_dump_tb(atr_ti_t *atp, FILE *out, uint_t level)
{
	uint8_t tb;

	if (!(atp->atrti_flags & ATR_TI_HAVE_TB)) {
		return;
	}

	tb = atp->atrti_tb;
	(void) fprintf(out, "   %c%c+--> TB%u 0x%02x",
	    atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ',
	    atp->atrti_flags & ATR_TI_HAVE_TC ? '|' : ' ',
	    atp->atrti_ti_val, tb);
	switch (atp->atrti_ti_val) {
	case 1:
	case 2:
		(void) fprintf(out, "; deprecated");
		break;
	default:
		switch (atp->atrti_protocol) {
		case ATR_PROTOCOL_T1:
			if (level != 0)
				break;
			(void) fprintf(out, "; CWI: %u, BWI: %u\n",
			    ATR_T1_TB0_CWI(tb),
			    ATR_T1_TB0_BWI(tb));
			break;
		case ATR_PROTOCOL_T15:
			if (level != 0)
				break;
			(void) fprintf(out, "; SPU: %s", tb == 0 ? "not used" :
			    ATR_T15_TB0_SPU_STANDARD(tb) ? "standard" :
			    "proprietary");
			break;
		default:
			break;
		}
	}
	(void) fprintf(out, "\n");
}

static void
atr_data_dump_tc(atr_ti_t *atp, FILE *out, uint_t level)
{
	uint8_t tc;

	if (!(atp->atrti_flags & ATR_TI_HAVE_TC)) {
		return;
	}

	tc = atp->atrti_tc;
	(void) fprintf(out, "   %c+---> TC%u 0x%02x",
	    atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ',
	    atp->atrti_ti_val, tc);

	switch (atp->atrti_ti_val) {
	case 1:
		(void) fprintf(out, "; Extra Guard Time Integer: %u", tc);
		break;
	case 2:
		if (atp->atrti_protocol != ATR_PROTOCOL_T0) {
			(void) fprintf(out, "; illegal value -- only valid for "
			    "T=0");
		} else {
			(void) fprintf(out, "; Waiting Time Integer: %u", tc);
		}
		break;
	default:
		switch (atp->atrti_protocol) {
		case ATR_PROTOCOL_T1:
			if (level != 0)
				break;
			(void) fprintf(out, "; Error Detection Code: %s",
			    ATR_T1_TC0_CRC(tc) ? "CRC" : "LRC");
			break;
		default:
			break;
		}
	}
	(void) fprintf(out, "\n");
}

void
atr_data_hexdump(const uint8_t *buf, size_t nbytes, FILE *out)
{
	hexdump_t h;

	hexdump_init(&h);
	hexdump_set_grouping(&h, 4);
	(void) hexdump_fileh(&h, buf, nbytes, HDF_DEFAULT, out);
	hexdump_fini(&h);
}

static void
atr_data_hexdump_historical(atr_data_t *data, FILE *out)
{
	(void) fprintf(out, "Dumping raw historical bytes\n");

	atr_data_hexdump(data->atr_historic, data->atr_nhistoric, out);
}

static void
atr_data_dump_historical(atr_data_t *data, FILE *out)
{
	uint8_t cat;

	(void) fprintf(out, "Historic Data: %u bytes", data->atr_nhistoric);
	if (data->atr_nhistoric == 0) {
		(void) fprintf(out, "\n");
		return;
	}

	cat = data->atr_historic[0];
	(void) fprintf(out, "; format (0x%02x) ", cat);
	if (cat == ATR_HIST_CAT_MAND_STATUS) {
		(void) fprintf(out, "card status, not shown");
	} else if (cat == ATR_HIST_CAT_TLV_STATUS) {
		(void) fprintf(out, "COMPACT-TLV, not shown");
	} else if (cat >= ATR_HIST_CAT_RFU_MIN && cat <= ATR_HIST_CAT_RFU_MAX) {
		(void) fprintf(out, "reserved\n");
		atr_data_hexdump_historical(data, out);
		return;
	} else {
		(void) fprintf(out, "proprietary\n");
		atr_data_hexdump_historical(data, out);
		return;
	}
}

void
atr_data_dump(atr_data_t *data, FILE *out)
{
	uint8_t i, level;
	if ((data->atr_flags & ATR_F_VALID) == 0)
		return;

	(void) fprintf(out, "TS  0x%02u - ", data->atr_raw[0]);
	if (data->atr_flags & ATR_F_USES_DIRECT) {
		(void) fprintf(out, "direct convention\n");
	} else {
		(void) fprintf(out, "inverse convention\n");
	}

	level = 0;
	for (i = 0; i < data->atr_nti; i++) {
		atr_ti_t *atp = &data->atr_ti[i];

		/*
		 * Various protocols may appear multiple times, indicating
		 * different sets of bits each time. When dealing with T0 and
		 * TD1, the protocol doesn't matter. Otherwise if we have the
		 * same value, we should increment this.
		 */
		if (i <= 2) {
			level = 0;
		} else if (atp->atrti_protocol ==
		    data->atr_ti[i - 1].atrti_protocol) {
			level++;
		} else {
			level = 0;
		}

		if (i == 0) {
			(void) fprintf(out, "T0  ");
		} else {
			(void) fprintf(out, "TD%u ", i);
		}
		(void) fprintf(out, "0x%02x\n",
		    data->atr_raw[atp->atrti_td_idx]);
		(void) fprintf(out, "      |+-> ");
		if (i == 0) {
			(void) fprintf(out, "%u historical bytes\n",
			    data->atr_nhistoric);
		} else {
			(void) fprintf(out, "protocol T=%u\n",
			    atp->atrti_protocol);
		}
		(void) fprintf(out, "      v\n");
		(void) fprintf(out, " 0r%u%u%u%u\n",
		    atp->atrti_flags & ATR_TI_HAVE_TD ? 1 : 0,
		    atp->atrti_flags & ATR_TI_HAVE_TC ? 1 : 0,
		    atp->atrti_flags & ATR_TI_HAVE_TB ? 1 : 0,
		    atp->atrti_flags & ATR_TI_HAVE_TA ? 1 : 0);

		atr_data_dump_ta(atp, out, level);
		atr_data_dump_tb(atp, out, level);
		atr_data_dump_tc(atp, out, level);
		if (atp->atrti_flags & ATR_TI_HAVE_TD) {
			(void) fprintf(out, "   v\n");
		}
	}

	atr_data_dump_historical(data, out);

	if (data->atr_flags & ATR_F_HAS_CHECKSUM) {
		(void) fprintf(out, "TCK  0x%02x\n", data->atr_cksum);
	} else {
		(void) fprintf(out, "TCK  ----; Checksum not present\n");
	}

}
#endif	/* _KERNEL */
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2019, Joyent, Inc.
 */

#ifndef _ATR_H
#define	_ATR_H

/*
 * Parse Answer-To-Reset values. This header file is private to illumos and
 * should not be shipped or used by applications.
 *
 * This is based on ISO/IEC 7816-3:2006. It has been designed such that if newer
 * revisions come out that define reserved values, they will be ignored until
 * this code is updated.
 */

#include <sys/types.h>
#include <sys/usb/clients/ccid/ccid.h>
#ifndef	_KERNEL
#include <stdio.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

/*
 * The ATR must have at least 2 bytes and then may have up to 33 bytes.
 */
#define	ATR_LEN_MIN	2
#define	ATR_LEN_MAX	33

typedef enum atr_parsecode {
	ATR_CODE_OK	= 0,
	ATR_CODE_TOO_SHORT,
	ATR_CODE_TOO_LONG,
	ATR_CODE_INVALID_TS,
	ATR_CODE_OVERRUN,
	ATR_CODE_UNDERRUN,
	ATR_CODE_CHECKSUM_ERROR,
	ATR_CODE_INVALID_TD1
} atr_parsecode_t;

typedef enum atr_protocol {
	ATR_P_NONE	= 0,
	ATR_P_T0	= 1 << 0,
	ATR_P_T1	= 1 << 1
} atr_protocol_t;

typedef enum atr_convention {
	ATR_CONVENTION_DIRECT	= 0x00,
	ATR_CONVENTION_INVERSE	= 0x01
} atr_convention_t;

typedef enum atr_clock_stop {
	ATR_CLOCK_STOP_NONE	= 0x00,
	ATR_CLOCK_STOP_LOW	= 0x01,
	ATR_CLOCK_STOP_HI	= 0x02,
	ATR_CLOCK_STOP_BOTH	= 0x03
} atr_clock_stop_t;

typedef enum atr_data_rate_choice {
	/*
	 * Indicates that the reader cannot support the data rate needed for the
	 * ICC.
	 */
	ATR_RATE_UNSUPPORTED	= 0x00,
	/*
	 * Indicates that the reader supports the ICC present, but must run at
	 * the protocol's default rate (Di index = Fi index = 1)
	 */
	ATR_RATE_USEDEFAULT	= 0x01,
	/*
	 * The reader supports the Di/Fi values that the ICC proposed in its ATR
	 * and no action beyond setting the parameters of the reader is required
	 * (this may be automatic depending on the reader's dwFeatures).
	 */
	ATR_RATE_USEATR		= 0x02,
	/*
	 * The reader can use the features of the ATR specified. However, it
	 * must change the data rate or frequency that the card is running at to
	 * proceed.
	 */
	ATR_RATE_USEATR_SETRATE	= 0x03
} atr_data_rate_choice_t;

typedef enum atr_t1_checksum {
	ATR_T1_CHECKSUM_LRC	= 0x00,
	ATR_T1_CHECKSUM_CRC	= 0x01
} atr_t1_checksum_t;

typedef struct atr_data atr_data_t;

/*
 * Allocate and free ATR data.
 */
extern atr_data_t *atr_data_alloc(void);
extern void atr_data_free(atr_data_t *);

/*
 * Reset an allocated ATR data to be ready to parse something else.
 */
extern void atr_data_reset(atr_data_t *);

/*
 * Parse the ATR data into an opaque structure that organizes the data and
 * allows for various queries to be made on it later.
 */
extern atr_parsecode_t atr_parse(const uint8_t *, size_t, atr_data_t *data);
extern const char *atr_strerror(atr_parsecode_t);

/*
 * Get an eumeration of supported protocols in this ATR data. Note that if a
 * reserved protocol is encountered, we may not report it as we don't know of it
 * at this time.
 */
extern atr_protocol_t atr_supported_protocols(atr_data_t *);

/*
 * Based on the ATR determine what the default protocol is and whether or not it
 * supports negotiation. When a ICC is not negotiable, it will always start up
 * with a specific protocol and parameters based on the ATR and be ready to use.
 * Otherwise, the card will be in a negotiable mode and be set to a default set
 * of parameters.
 */
extern boolean_t atr_params_negotiable(atr_data_t *);
extern atr_protocol_t atr_default_protocol(atr_data_t *);

/*
 * Protocol default values.
 */
extern uint8_t atr_fi_default_index(void);
extern uint8_t atr_di_default_index(void);

/*
 * Obtain the table indexes that should be used by the device.
 */
extern uint8_t atr_fi_index(atr_data_t *);
extern uint8_t atr_di_index(atr_data_t *);
extern atr_convention_t atr_convention(atr_data_t *);
extern uint8_t atr_extra_guardtime(atr_data_t *);
extern uint8_t atr_t0_wi(atr_data_t *);
extern atr_t1_checksum_t atr_t1_checksum(atr_data_t *);
extern uint8_t atr_t1_bwi(atr_data_t *);
extern uint8_t atr_t1_cwi(atr_data_t *);
extern atr_clock_stop_t atr_clock_stop(atr_data_t *);
extern uint8_t atr_t1_ifsc(atr_data_t *);

/*
 * Use this function to determine what set of Di and Fi values should be used by
 * a reader, based on the parameters from the ATR and the reader's cclass.
 */
extern atr_data_rate_choice_t atr_data_rate(atr_data_t *, ccid_class_descr_t *,
    uint32_t *, uint_t, uint32_t *);

#ifndef	_KERNEL
extern void atr_data_hexdump(const uint8_t *, size_t, FILE *);
extern void atr_data_dump(atr_data_t *, FILE *);
#endif

/*
 * String and table index values.
 */
extern const char *atr_protocol_to_string(atr_protocol_t);
extern uint_t atr_fi_index_to_value(uint8_t);
extern const char *atr_fi_index_to_string(uint8_t);
extern const char *atr_fmax_index_to_string(uint8_t);
extern uint_t atr_di_index_to_value(uint8_t);
extern const char *atr_di_index_to_string(uint8_t);
extern const char *atr_clock_stop_to_string(atr_clock_stop_t);
extern const char *atr_convention_to_string(atr_convention_t);

/*
 * Functions for generating and testing PPS values. Before calling
 * atr_pps_fidi_accepted(), one must call atr_pps_valid().
 */
#define	PPS_BUFFER_MAX	6
extern uint_t atr_pps_generate(uint8_t *, size_t, atr_protocol_t, boolean_t,
    uint8_t, uint8_t, boolean_t, uint8_t);
extern boolean_t atr_pps_valid(void *, size_t, void *, size_t);
extern boolean_t atr_pps_fidi_accepted(void *, size_t);

#ifdef __cplusplus
}
#endif

#endif /* _ATR_H */