| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /******************************************************************** |
| 2 | FileName: SClib.h |
||
| 3 | Dependencies: See INCLUDES section |
||
| 4 | Processor: PIC18, PIC24 Microcontrollers |
||
| 5 | Hardware: This demo is natively intended to be used on Exp 16, LPC |
||
| 6 | & HPC Exp board. This demo can be modified for use on other hardware |
||
| 7 | platforms. |
||
| 8 | Complier: Microchip C18 (for PIC18), C30 (for PIC24) |
||
| 9 | Company: Microchip Technology, Inc. |
||
| 10 | |||
| 11 | Software License Agreement: |
||
| 12 | |||
| 13 | The software supplied herewith by Microchip Technology Incorporated |
||
| 14 | (the Company) for its PIC® Microcontroller is intended and |
||
| 15 | supplied to you, the Companys customer, for use solely and |
||
| 16 | exclusively on Microchip PIC Microcontroller products. The |
||
| 17 | software is owned by the Company and/or its supplier, and is |
||
| 18 | protected under applicable copyright laws. All rights are reserved. |
||
| 19 | Any use in violation of the foregoing restrictions may subject the |
||
| 20 | user to criminal sanctions under applicable laws, as well as to |
||
| 21 | civil liability for the breach of the terms and conditions of this |
||
| 22 | license. |
||
| 23 | |||
| 24 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
| 25 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
| 26 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
| 27 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
| 28 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
| 29 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
| 30 | |||
| 31 | ******************************************************************** |
||
| 32 | File Description: |
||
| 33 | |||
| 34 | Change History: |
||
| 35 | Rev Description |
||
| 36 | ---- ----------------------------------------- |
||
| 37 | 1.0 Initial release |
||
| 38 | 1.01 Cleaned up unnecessary variables,supported T=1 protocol |
||
| 39 | and improvments in T=0 functions following the coding standards |
||
| 40 | ********************************************************************/ |
||
| 41 | |||
| 42 | // Smart Card Library |
||
| 43 | #ifndef __SC_MCP_LIB__ |
||
| 44 | #define __SC_MCP_LIB__ |
||
| 45 | |||
| 46 | #include "GenericTypeDefs.h" |
||
| 47 | |||
| 48 | // Smart Card error types |
||
| 49 | typedef enum |
||
| 50 | { |
||
| 51 | SC_ERR_NONE, // No Error |
||
| 52 | SC_ERR_CARD_NOT_SUPPORTED, // Card Not Supported |
||
| 53 | SC_ERR_BAR_OR_NO_ATR_RESPONSE, // No ATR Response from the card |
||
| 54 | SC_ERR_CARD_NOT_PRESENT, // Card Not present in the slot |
||
| 55 | SC_ERR_CARD_NO_RESPONSE, // No response from the card |
||
| 56 | SC_ERR_RECEIVE_LRC, // LRC Error in the block recieved from the card |
||
| 57 | SC_ERR_RECEIVE_CRC, // CRC Error in the block recieved from the card |
||
| 58 | SC_CARD_VPP_ERR, // VPP Error recieved from the card |
||
| 59 | SC_ERR_ATR_DATA, // ERROR in ATR data recieved from the card |
||
| 60 | SC_ERR_RSV1 // Smart Card Error 1 (Reserved) - can be used based upon the Application |
||
| 61 | } SC_ERROR; |
||
| 62 | |||
| 63 | // Block types in T=1 protocol |
||
| 64 | typedef enum |
||
| 65 | { |
||
| 66 | I_BLOCK, // I Block |
||
| 67 | R_BLOCK, // R Block |
||
| 68 | S_BLOCK, // S Block |
||
| 69 | INVALID_BLOCK // INVALID BLOCK |
||
| 70 | }T1BLOCK_TYPE; |
||
| 71 | |||
| 72 | // Returns '1' if T=1 protocol is supported & Returns 0 otherwise |
||
| 73 | #define SC_T1ProtocolType() (((scTD1 & 0x0F) == 0x01)?TRUE:FALSE) |
||
| 74 | |||
| 75 | // Returns '1' if T=0 protocol is supported & Returns 0 otherwise |
||
| 76 | #define SC_T0ProtocolType() (((scTD1 & 0x0F) == 0x00)?TRUE:FALSE) |
||
| 77 | |||
| 78 | // Returns '1' if TA1 present & Returns 0 otherwise |
||
| 79 | #define SC_TA1Present() ((scCardATR[1] & 0x10)?TRUE:FALSE) |
||
| 80 | |||
| 81 | // Returns '1' if TB1 present & Returns 0 otherwise |
||
| 82 | #define SC_TB1Present() ((scCardATR[1] & 0x20)?TRUE:FALSE) |
||
| 83 | |||
| 84 | // Returns '1' if TC1 present & Returns 0 otherwise |
||
| 85 | #define SC_TC1Present() ((scCardATR[1] & 0x40)?TRUE:FALSE) |
||
| 86 | |||
| 87 | // Returns '1' if TD1 present & Returns 0 otherwise |
||
| 88 | #define SC_TD1Present() ((scCardATR[1] & 0x80)?TRUE:FALSE) |
||
| 89 | |||
| 90 | // Returns '1' if TA2 present & Returns 0 otherwise |
||
| 91 | #define SC_TA2Present() ((scTD1 & 0x10)?TRUE:FALSE) |
||
| 92 | |||
| 93 | // Returns '1' if TB2 present & Returns 0 otherwise |
||
| 94 | #define SC_TB2Present() ((scTD1 & 0x20)?TRUE:FALSE) |
||
| 95 | |||
| 96 | // Returns '1' if TC2 present & Returns 0 otherwise |
||
| 97 | #define SC_TC2Present() ((scTD1 & 0x40)?TRUE:FALSE) |
||
| 98 | |||
| 99 | // Returns '1' if TD2 present & Returns 0 otherwise |
||
| 100 | #define SC_TD2Present() ((scTD1 & 0x80)?TRUE:FALSE) |
||
| 101 | |||
| 102 | // Smart Card Commands |
||
| 103 | |||
| 104 | // Start Session Command code to the Smart Card |
||
| 105 | #define SC_START_SESSION 0x84 |
||
| 106 | |||
| 107 | // Authenticate Command code to the Smart Card |
||
| 108 | #define SC_AUTHENTICATE 0x82 |
||
| 109 | |||
| 110 | // Get Response Command code to the Smart Card |
||
| 111 | #define SC_GET_RESPONSE 0xC0 |
||
| 112 | |||
| 113 | // Submit Code Command to the Smart Card |
||
| 114 | #define SC_SUBMIT_CODE 0x20 |
||
| 115 | |||
| 116 | // Clear Card Command code to the Smart Card |
||
| 117 | #define SC_CLEAR_CARD 0x30 |
||
| 118 | |||
| 119 | // Select File Command code to the Smart Card |
||
| 120 | #define SC_SELECT_FILE 0xA4 |
||
| 121 | |||
| 122 | // Read Record Command code to the Smart Card |
||
| 123 | #define SC_READ_RECORD 0xB2 |
||
| 124 | |||
| 125 | // Write Record Command code to the Smart Card |
||
| 126 | #define SC_WRITE_RECORD 0xD2 |
||
| 127 | |||
| 128 | // Credit Command code to the Smart Card |
||
| 129 | #define SC_CREDIT 0xE2 |
||
| 130 | |||
| 131 | // Debit Command code to the Smart Card |
||
| 132 | #define SC_DEBIT 0xE6 |
||
| 133 | |||
| 134 | // Revoke Command code to the Smart Card |
||
| 135 | #define SC_REVOKE 0xE8 |
||
| 136 | |||
| 137 | // Inquire Account Command code to the Smart Card |
||
| 138 | #define SC_INQUIRE_ACCT 0xE4 |
||
| 139 | |||
| 140 | // Change Pin Command code to the Smart Card |
||
| 141 | #define SC_CHANGE_PIN 0x24 |
||
| 142 | |||
| 143 | // ATR data sent by smartcard. |
||
| 144 | extern BYTE scCardATR[]; |
||
| 145 | |||
| 146 | // length of ATR data sent by smart card |
||
| 147 | extern BYTE scATRLength; |
||
| 148 | |||
| 149 | // Smart Card Error type is stored in this variable |
||
| 150 | extern SC_ERROR scLastError; |
||
| 151 | |||
| 152 | // TA1 determines the clock-rate conversion factor F & bit-rate-adjustment factor D |
||
| 153 | extern BYTE scTA1; |
||
| 154 | |||
| 155 | // TA2 determines whether the smart card will operate in specific mode or negotiable mode |
||
| 156 | // following the ATR |
||
| 157 | extern BYTE scTA2; |
||
| 158 | |||
| 159 | // TA3 conveys the Information Field Size Integer (IFSI) for the smart card. |
||
| 160 | extern BYTE scTA3; |
||
| 161 | |||
| 162 | // TB1 conveys information on the smart card's programming voltage requirements. |
||
| 163 | extern BYTE scTB1; |
||
| 164 | |||
| 165 | // TB2 conveys PI2, which determines the value of programming voltage required by |
||
| 166 | // the smart card. The value of PI1 in TB1 is superceded when TB2 is present |
||
| 167 | extern BYTE scTB2; |
||
| 168 | |||
| 169 | // TB3 indicates the value of the Character Waiting Time Integer (CWI) and |
||
| 170 | // Block Waiting Time Integer (BWI) used to compute the Character Waiting Time (CWT) |
||
| 171 | // and Block Waiting Time (BWT). |
||
| 172 | extern BYTE scTB3; |
||
| 173 | |||
| 174 | // TC1 determines the extra guard time to be added between consecutive characters |
||
| 175 | // sent to the smart card from the terminal. |
||
| 176 | extern BYTE scTC1; |
||
| 177 | |||
| 178 | // TC2 is specific to protocol type T=0. TC2 conveys work waiting-time integer (WI) |
||
| 179 | // that determines the maximum interval between the leading edge of the start bit |
||
| 180 | // of any character sent by the smart card and the leading edge of the start bit |
||
| 181 | // of the previous character sent either by the card or the reader |
||
| 182 | extern BYTE scTC2; |
||
| 183 | |||
| 184 | // When TC3 is present, it indicates the type of block-error detection to be used. |
||
| 185 | // When TC3 is not present, the default longitudinal redundancy check (LRC) is used. |
||
| 186 | extern BYTE scTC3; |
||
| 187 | |||
| 188 | // TD1 indicates if any further interface bytes are to be transmitted, and if so, |
||
| 189 | // which protocol will be used. |
||
| 190 | extern BYTE scTD1; |
||
| 191 | |||
| 192 | // The TD2 character has the same function as the TD1 character. |
||
| 193 | extern BYTE scTD2; |
||
| 194 | |||
| 195 | // TD3 indicates interface bytes similar to that of TD1 & TD2 |
||
| 196 | extern BYTE scTD3; |
||
| 197 | |||
| 198 | // Historical bytes sent by Smart Card |
||
| 199 | extern BYTE* scATR_HistoryBuffer; |
||
| 200 | |||
| 201 | // Number of Historical bytes present |
||
| 202 | extern BYTE scATR_HistoryLength; |
||
| 203 | |||
| 204 | // Status of required delay elapsed |
||
| 205 | extern BOOL delayLapsedFlag; |
||
| 206 | |||
| 207 | // Longitudnal Redundancy Check(LRC) type is used for EDC in Epilogue Field |
||
| 208 | #define SC_LRC_TYPE_EDC (BYTE)0 |
||
| 209 | |||
| 210 | // Cyclic Redundancy Check(CRC) type is used for EDC in Epilogue Field |
||
| 211 | #define SC_CRC_TYPE_EDC (BYTE)1 |
||
| 212 | |||
| 213 | // PCB byte for Resync Request of T1 Protocol |
||
| 214 | #define SC_RESYNC_REQ (BYTE)0xC0 |
||
| 215 | |||
| 216 | // PCB byte for Abort Response of T1 Protocol |
||
| 217 | #define SC_ABORT_RESPONSE (BYTE)0xE2 |
||
| 218 | |||
| 219 | // PCB byte for IFS Response of T1 Protocol |
||
| 220 | #define SC_IFS_RESPONSE (BYTE)0xE1 |
||
| 221 | |||
| 222 | // PCB byte for Wait Time Extension Response of T1 Protocol |
||
| 223 | #define SC_WAIT_TIME_EXT_RESPONSE (BYTE)0xE3 |
||
| 224 | |||
| 225 | // DEFAULT Value of WI Indicator used in calculation of WWT for T=0 protocol |
||
| 226 | #define SC_WI (BYTE)0x0A |
||
| 227 | |||
| 228 | // DEFAULT Value of CWI Indicator used in calculation of CWT for T=1 protocol |
||
| 229 | #define SC_CWI (BYTE)13 |
||
| 230 | |||
| 231 | // DEFAULT Value of BWI Indicator used in calculation of BWT for T=1 protocol |
||
| 232 | #define SC_BWI (BYTE)0x04 |
||
| 233 | |||
| 234 | ////////// SmartCard APDU Command 7816-4 |
||
| 235 | typedef struct |
||
| 236 | { |
||
| 237 | BYTE CLA; // Command class |
||
| 238 | BYTE INS; // Operation code |
||
| 239 | BYTE P1; // Selection Mode |
||
| 240 | BYTE P2; // Selection Option |
||
| 241 | BYTE LC; // Data length |
||
| 242 | BYTE LE; // Expected length of data to be returned |
||
| 243 | } SC_APDU_COMMAND; |
||
| 244 | |||
| 245 | /////////// SmartCard APDU Response structure 7816-4 |
||
| 246 | typedef struct |
||
| 247 | { |
||
| 248 | WORD RXDATALEN; // Recieved Data |
||
| 249 | BYTE SW1; // Trailer Response status 1 |
||
| 250 | BYTE SW2; // Trailer Response status 2 |
||
| 251 | } SC_APDU_RESPONSE; |
||
| 252 | |||
| 253 | //// Prologue Field for T=1 Protocol |
||
| 254 | typedef struct |
||
| 255 | { |
||
| 256 | BYTE NAD; // Node Address |
||
| 257 | BYTE PCB; // Protocol Control Byte |
||
| 258 | BYTE LENGTH; // LENGTH of I-Field |
||
| 259 | }SC_T1_PROLOGUE_FIELD; |
||
| 260 | |||
| 261 | // SmartCard States |
||
| 262 | #define SC_STATE_CARD_NOT_PRESENT 10 // No Card Detected |
||
| 263 | #define SC_STATE_CARD_ACTIVE 20 // Card is powered and ATR received |
||
| 264 | #define SC_STATE_CARD_INACTIVE 30 // Card present but not powered |
||
| 265 | |||
| 266 | // Longitudnal Redundancy Check(LRC) type is used for EDC in Epilogue Field |
||
| 267 | #define SC_LRC_TYPE_EDC (BYTE)0 |
||
| 268 | |||
| 269 | // Cyclic Redundancy Check(CRC) type is used for EDC in Epilogue Field |
||
| 270 | #define SC_CRC_TYPE_EDC (BYTE)1 |
||
| 271 | |||
| 272 | // PCB byte for Resync Request of T1 Protocol |
||
| 273 | #define SC_RESYNC_REQ (BYTE)0xC0 |
||
| 274 | |||
| 275 | // PCB byte for Abort Response of T1 Protocol |
||
| 276 | #define SC_ABORT_RESPONSE (BYTE)0xE2 |
||
| 277 | |||
| 278 | // PCB byte for IFS Response of T1 Protocol |
||
| 279 | #define SC_IFS_RESPONSE (BYTE)0xE1 |
||
| 280 | |||
| 281 | // PCB byte for Wait Time Extension Response of T1 Protocol |
||
| 282 | #define SC_WAIT_TIME_EXT_RESPONSE (BYTE)0xE3 |
||
| 283 | |||
| 284 | // Smart Card Lib API ////////////////// |
||
| 285 | |||
| 286 | /******************************************************************************* |
||
| 287 | Function: |
||
| 288 | void SC_Initialize(void) |
||
| 289 | |||
| 290 | Description: |
||
| 291 | This function initializes the smart card library |
||
| 292 | |||
| 293 | Precondition: |
||
| 294 | None |
||
| 295 | |||
| 296 | Parameters: |
||
| 297 | None |
||
| 298 | |||
| 299 | Return Values: |
||
| 300 | None |
||
| 301 | |||
| 302 | Remarks: |
||
| 303 | None |
||
| 304 | *****************************************************************************/ |
||
| 305 | void SC_Initialize(void); |
||
| 306 | |||
| 307 | |||
| 308 | /******************************************************************************* |
||
| 309 | Function: |
||
| 310 | BOOL SC_CardPresent(void) |
||
| 311 | |||
| 312 | Description: |
||
| 313 | This macro checks if card is inserted in the socket |
||
| 314 | |||
| 315 | Precondition: |
||
| 316 | SC_Initialize() is called |
||
| 317 | |||
| 318 | Parameters: |
||
| 319 | None |
||
| 320 | |||
| 321 | Return Values: |
||
| 322 | TRUE if Card is inserted, otherwise return FALSE |
||
| 323 | |||
| 324 | Remarks: |
||
| 325 | None |
||
| 326 | *****************************************************************************/ |
||
| 327 | BOOL SC_CardPresent(void); |
||
| 328 | |||
| 329 | |||
| 330 | /******************************************************************************* |
||
| 331 | Function: |
||
| 332 | int SC_GetCardState(void) |
||
| 333 | |||
| 334 | Description: |
||
| 335 | This function returns the current state of SmartCard |
||
| 336 | |||
| 337 | Precondition: |
||
| 338 | SC_Initialize is called. |
||
| 339 | |||
| 340 | Parameters: |
||
| 341 | None |
||
| 342 | |||
| 343 | Return Values: |
||
| 344 | SC_STATE_CARD_NOT_PRESENT: No Card Detected |
||
| 345 | SC_STATE_CARD_ACTIVE: Card is powered and ATR received |
||
| 346 | SC_STATE_CARD_INACTIVE: Card present but not powered |
||
| 347 | |||
| 348 | Remarks: |
||
| 349 | None |
||
| 350 | *****************************************************************************/ |
||
| 351 | int SC_GetCardState(void); |
||
| 352 | |||
| 353 | /******************************************************************************* |
||
| 354 | Function: |
||
| 355 | BOOL SC_PowerOnATR(void) |
||
| 356 | |||
| 357 | Description: |
||
| 358 | This function performs the power on sequence of the SmartCard and |
||
| 359 | interprets the Answer-to-Reset data received from the card. |
||
| 360 | |||
| 361 | Precondition: |
||
| 362 | SC_Initialize() is called, and card is present |
||
| 363 | |||
| 364 | Parameters: |
||
| 365 | None |
||
| 366 | |||
| 367 | Return Values: |
||
| 368 | TRUE if Answer to Reset (ATR) was successfuly received and processed |
||
| 369 | |||
| 370 | Remarks: |
||
| 371 | None |
||
| 372 | *****************************************************************************/ |
||
| 373 | BOOL SC_PowerOnATR(void); |
||
| 374 | |||
| 375 | /******************************************************************************* |
||
| 376 | Function: |
||
| 377 | BOOL SC_DoPPS(void) |
||
| 378 | |||
| 379 | Description: |
||
| 380 | This function configures the baud rate of the PIC UART to match with |
||
| 381 | Answer-to-Reset data after doing PPS to the card. |
||
| 382 | |||
| 383 | Precondition: |
||
| 384 | SC_PowerOnATR was success |
||
| 385 | |||
| 386 | Parameters: |
||
| 387 | None |
||
| 388 | |||
| 389 | Return Values: |
||
| 390 | TRUE if Baud rate is supported by the PIC |
||
| 391 | |||
| 392 | Remarks: |
||
| 393 | This function is called when SC_PowerOnATR() returns TRUE. If the Baud |
||
| 394 | rate configration file inside the card is changed, these function should |
||
| 395 | be called again for the new baud to take effect. |
||
| 396 | *****************************************************************************/ |
||
| 397 | BOOL SC_DoPPS(void); |
||
| 398 | |||
| 399 | |||
| 400 | /******************************************************************************* |
||
| 401 | Function: |
||
| 402 | BOOL SC_TransactT0(SC_APDU_COMMAND* apduCommand, SC_APDU_RESPONSE* apduResponse, BYTE* apduDataBuffer) |
||
| 403 | |||
| 404 | Description: |
||
| 405 | This function Sends the ISO 7816-3/4 compaliant T=0 commands to the card. |
||
| 406 | It also receive the expected response from the card as defined by the |
||
| 407 | command data. |
||
| 408 | |||
| 409 | Precondition: |
||
| 410 | SC_DoPPS was success |
||
| 411 | |||
| 412 | Parameters: |
||
| 413 | SC_APDU_COMMAND* apduCommand - Pointer to APDU Command Structure |
||
| 414 | SC_APDU_RESPONSE* pResp - Pointer to APDU Response structure |
||
| 415 | BYTE* apduDataBuffer - Pointer to the Command/Response Data buffer |
||
| 416 | |||
| 417 | Return Values: |
||
| 418 | TRUE if transaction was success, and followed the ISO 7816-4 protocol. |
||
| 419 | |||
| 420 | Remarks: |
||
| 421 | In the APDU command structure, the LC field defines the number of bytes to |
||
| 422 | transmit from the APDUdat array. This array can hold max 256 bytes, which |
||
| 423 | can be redefined by the user. The LE field in APDU command defines the number |
||
| 424 | of bytes to receive from the card. This array can hold max 256 bytes, which |
||
| 425 | can be redefined by the user. |
||
| 426 | |||
| 427 | *****************************************************************************/ |
||
| 428 | BOOL SC_TransactT0(SC_APDU_COMMAND* apduCommand, SC_APDU_RESPONSE* apduResponse, BYTE* apduDataBuffer); |
||
| 429 | |||
| 430 | |||
| 431 | /******************************************************************************* |
||
| 432 | Function: |
||
| 433 | void SC_Shutdown(void) |
||
| 434 | |||
| 435 | Description: |
||
| 436 | This function Performs the Power Down sequence of the SmartCard |
||
| 437 | |||
| 438 | Precondition: |
||
| 439 | SC_Initialize is called. |
||
| 440 | |||
| 441 | Parameters: |
||
| 442 | None |
||
| 443 | |||
| 444 | Return Values: |
||
| 445 | None |
||
| 446 | |||
| 447 | Remarks: |
||
| 448 | None |
||
| 449 | *****************************************************************************/ |
||
| 450 | void SC_Shutdown(void); |
||
| 451 | |||
| 452 | #ifdef SC_PROTO_T1 |
||
| 453 | |||
| 454 | /******************************************************************************* |
||
| 455 | Function: |
||
| 456 | BOOL SC_TransactT1(SC_T1_PROLOGUE_FIELD* pfield,BYTE* iField,SC_APDU_RESPONSE* apduResponse) |
||
| 457 | |||
| 458 | Description: |
||
| 459 | This function Sends the ISO 7816-3/4 compaliant T=1 commands to the card. |
||
| 460 | It also receive the expected response from the card as defined by the |
||
| 461 | command data. |
||
| 462 | |||
| 463 | Precondition: |
||
| 464 | SC_DoPPS was success |
||
| 465 | |||
| 466 | Parameters: |
||
| 467 | SC_T1_PROLOGUE_FIELD* pfield - Pointer to Prologue Field |
||
| 468 | BYTE* iField - Pointer to the Information Field of Tx/Rx Data |
||
| 469 | SC_APDU_RESPONSE* apduResponse - Pointer to APDU Response structure |
||
| 470 | |||
| 471 | Return Values: |
||
| 472 | TRUE if transaction was success, and followed the ISO 7816-4 protocol. |
||
| 473 | |||
| 474 | Remarks: |
||
| 475 | *****************************************************************************/ |
||
| 476 | |||
| 477 | extern BOOL SC_TransactT1(SC_T1_PROLOGUE_FIELD* pfield,BYTE* iField,SC_APDU_RESPONSE* apduResponse); |
||
| 478 | |||
| 479 | #endif |
||
| 480 | |||
| 481 | #endif |
||
| 482 | |||
| 483 | |||
| 484 |
Powered by WebSVN v2.8.3