Chameleon-Mini
DESFireISO7816Support.h
1 /*
2 The DESFire stack portion of this firmware source
3 is free software written by Maxie Dion Schmidt (@maxieds):
4 You can redistribute it and/or modify
5 it under the terms of this license.
6 
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 
11 The complete source distribution of
12 this firmware is available at the following link:
13 https://github.com/maxieds/ChameleonMiniFirmwareDESFireStack.
14 
15 Based in part on the original DESFire code created by
16 @dev-zzo (GitHub handle) [Dmitry Janushkevich] available at
17 https://github.com/dev-zzo/ChameleonMini/tree/desfire.
18 
19 This notice must be retained at the top of all source files where indicated.
20 */
21 
22 /* DESFireISO7816Support.h */
23 
24 #ifndef __DESFIRE_ISO7816_SUPPORT_H__
25 #define __DESFIRE_ISO7816_SUPPORT_H__
26 
27 #include <inttypes.h>
28 #include <stdbool.h>
29 
30 #define Iso7816CLA(cmdCode) \
31  (cmdCode == DESFIRE_ISO7816_CLA)
32 
33 #define ISO7816_PROLOGUE_SIZE (2)
34 #define ISO7816_STATUS_RESPONSE_SIZE (0x02)
35 #define ISO7816_EF_NOT_SPECIFIED (0xff)
36 #define ISO7816_EFID_NUMBER_MAX (0x0f)
37 #define ISO7816_MAX_FILE_SIZE (0xfd)
38 #define ISO7816_READ_ALL_BYTES_SIZE (0x00)
39 #define ISO7816_CMD_NO_ERROR (0x00)
40 #define ISO7816_ERROR_SW1 (0x6a)
41 #define ISO7816_ERROR_SW1_INS_UNSUPPORTED (0x6d)
42 #define ISO7816_ERROR_SW1_ACCESS (0x69)
43 #define ISO7816_ERROR_SW1_FSE (0x62)
44 #define ISO7816_ERROR_SW1_WRONG_FSPARAMS (0x6b)
45 #define ISO7816_ERROR_SW2_INCORRECT_P1P2 (0x86)
46 #define ISO7816_ERROR_SW2_UNSUPPORTED (0x81)
47 #define ISO7816_ERROR_SW2_FUNC_UNSUPPORTED (0x81)
48 #define ISO7816_ERROR_SW2_INS_UNSUPPORTED (0x00)
49 #define ISO7816_SELECT_ERROR_SW2_FUNC_UNSUPPORTED (0x81)
50 #define ISO7816_SELECT_ERROR_SW2_NOFILE (0x82)
51 #define ISO7816_ERROR_SW2_FILE_NOMEM (0x84)
52 #define ISO7816_GET_CHALLENGE_ERROR_SW2_UNSUPPORTED (0x81)
53 #define ISO7816_ERROR_SW2_INCOMPATFS (0x81)
54 #define ISO7816_ERROR_SW2_SECURITY (0x82)
55 #define ISO7816_ERROR_SW2_NOEF (0x86)
56 #define ISO7816_ERROR_SW2_WRONG_FSPARAMS (0x00)
57 #define ISO7816_ERROR_SW2_EOF (0x82)
58 
59 #define AppendSW12Bytes(sw1, sw2) \
60  ((uint16_t) ((sw1 << 8) | (sw2 & 0xff)))
61 
62 /* Some of the wrapped ISO7816 commands have extra meaning
63  * packed into the P1-P2 bytes of the APDU byte array.
64  * When we support these extra modes, this is a way to keep
65  * track of the local meanings without needing extra handling
66  * functions to distinguish between the wrapped command types
67  * for the ISO7816 versus native DESFire instructions.
68  */
69 typedef enum {
70  ISO7816_NO_DATA = 0,
71  ISO7816_UNSUPPORTED_MODE,
72  ISO7816_SELECT_EF,
73  ISO7816_SELECT_DF,
74  ISO7816_FILE_FIRST_RECORD,
75  ISO7816_FILE_LAST_RECORD,
76  ISO7816_FILE_NEXT_RECORD,
77  ISO7816_FILE_PREV_RECORD,
78 } Iso7816WrappedParams_t;
79 
80 extern Iso7816WrappedParams_t Iso7816P1Data;
81 extern Iso7816WrappedParams_t Iso7816P2Data;
82 extern bool Iso7816FileSelected;
83 extern uint8_t Iso7816FileOffset;
84 extern uint8_t Iso7816EfIdNumber;
85 
86 bool IsWrappedISO7816CommandType(uint8_t *Buffer, uint16_t ByteCount);
87 uint16_t SetIso7816WrappedParametersType(uint8_t *Buffer, uint16_t ByteCount);
88 
89 #endif