rfal_t4t.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R391x firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file rfal_t4t.h
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief Provides convenience methods and definitions for T4T (ISO7816-4)
  31. *
  32. * This module provides an interface to exchange T4T APDUs according to
  33. * NFC Forum T4T and ISO7816-4
  34. *
  35. * This implementation was based on the following specs:
  36. * - ISO/IEC 7816-4 3rd Edition 2013-04-15
  37. * - NFC Forum T4T Technical Specification 1.0 2017-08-28
  38. *
  39. * \addtogroup RFAL
  40. * @{
  41. *
  42. * \addtogroup RFAL-AL
  43. * \brief RFAL Abstraction Layer
  44. * @{
  45. *
  46. * \addtogroup T4T
  47. * \brief RFAL T4T Module
  48. * @{
  49. *
  50. */
  51. #ifndef RFAL_T4T_H
  52. #define RFAL_T4T_H
  53. /*
  54. ******************************************************************************
  55. * INCLUDES
  56. ******************************************************************************
  57. */
  58. #include "platform.h"
  59. #include "st_errno.h"
  60. #include "rfal_rf.h"
  61. #include "rfal_isoDep.h"
  62. /*
  63. ******************************************************************************
  64. * GLOBAL DEFINES
  65. ******************************************************************************
  66. */
  67. #define RFAL_T4T_MAX_CAPDU_PROLOGUE_LEN 4U /*!< Command-APDU prologue length (CLA INS P1 P2) */
  68. #define RFAL_T4T_LE_LEN 1U /*!< Le Expected Response Length (short field coding) */
  69. #define RFAL_T4T_LC_LEN 1U /*!< Lc Data field length (short field coding) */
  70. #define RFAL_T4T_MAX_RAPDU_SW1SW2_LEN 2U /*!< SW1 SW2 length */
  71. #define RFAL_T4T_CLA 0x00U /*!< Class byte (contains 00h because secure message are not used) */
  72. #define RFAL_T4T_ISO7816_P1_SELECT_BY_DF_NAME 0x04U /*!< P1 value for Select by name */
  73. #define RFAL_T4T_ISO7816_P1_SELECT_BY_FILEID 0x00U /*!< P1 value for Select by file identifier */
  74. #define RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE 0x00U /*!< b2b1 P2 value for First or only occurence */
  75. #define RFAL_T4T_ISO7816_P2_SELECT_RETURN_FCI_TEMPLATE 0x00U /*!< b4b3 P2 value for Return FCI template */
  76. #define RFAL_T4T_ISO7816_P2_SELECT_NO_RESPONSE_DATA 0x0CU /*!< b4b3 P2 value for No responce data */
  77. #define RFAL_T4T_ISO7816_STATUS_COMPLETE 0x9000U /*!< Command completed \ Normal processing - No further qualification*/
  78. /*
  79. ******************************************************************************
  80. * GLOBAL VARIABLES
  81. ******************************************************************************
  82. */
  83. /*
  84. ******************************************************************************
  85. * GLOBAL TYPES
  86. ******************************************************************************
  87. */
  88. /*! NFC-A T4T Command-APDU structure */
  89. typedef struct
  90. {
  91. uint8_t CLA; /*!< Class byte */
  92. uint8_t INS; /*!< Instruction byte */
  93. uint8_t P1; /*!< Parameter byte 1 */
  94. uint8_t P2; /*!< Parameter byte 2 */
  95. uint8_t Lc; /*!< Data field length */
  96. bool LcFlag; /*!< Lc flag (append Lc when true) */
  97. uint8_t Le; /*!< Expected Response Length */
  98. bool LeFlag; /*!< Le flag (append Le when true) */
  99. rfalIsoDepApduBufFormat *cApduBuf; /*!< Command-APDU buffer (Tx) */
  100. uint16_t *cApduLen; /*!< Command-APDU Length */
  101. }rfalT4tCApduParam;
  102. /*! NFC-A T4T Response-APDU structure */
  103. typedef struct
  104. {
  105. rfalIsoDepApduBufFormat *rApduBuf; /*!< Response-APDU buffer (Rx) */
  106. uint16_t rcvdLen; /*!< Full response length */
  107. uint16_t rApduBodyLen; /*!< Response body length */
  108. uint16_t statusWord; /*!< R-APDU Status Word SW1|SW2 */
  109. }rfalT4tRApduParam;
  110. /*! NFC-A T4T command set T4T 1.0 & ISO7816-4 2013 Table 4 */
  111. typedef enum
  112. {
  113. RFAL_T4T_INS_SELECT = 0xA4U, /*!< T4T Select */
  114. RFAL_T4T_INS_READBINARY = 0xB0U, /*!< T4T ReadBinary */
  115. RFAL_T4T_INS_UPDATEBINARY = 0xD6U, /*!< T4T UpdateBinay */
  116. RFAL_T4T_INS_READBINARY_ODO = 0xB1U, /*!< T4T ReadBinary using ODO */
  117. RFAL_T4T_INS_UPDATEBINARY_ODO = 0xD7U /*!< T4T UpdateBinay using ODO */
  118. } rfalT4tCmds;
  119. /*
  120. ******************************************************************************
  121. * GLOBAL FUNCTION PROTOTYPES
  122. ******************************************************************************
  123. */
  124. /*!
  125. *****************************************************************************
  126. * \brief T4T Compose APDU
  127. *
  128. * This method computes a C-APDU according to NFC Forum T4T and ISO7816-4.
  129. *
  130. * If C-APDU contains data to be sent, it must be placed inside the buffer
  131. * rfalT4tTxRxApduParam.txRx.cApduBuf.apdu and signaled by Lc
  132. *
  133. * To transceive the formed APDU the ISO-DEP layer shall be used
  134. *
  135. * \see rfalIsoDepStartApduTransceive()
  136. * \see rfalIsoDepGetApduTransceiveStatus()
  137. * \see rfalT4TPollerParseRAPDU()
  138. *
  139. * \warning The ISO-DEP module is used to perform the tranceive. Usually
  140. * activation has been done via ISO-DEP activatiavtion. If not
  141. * please call rfalIsoDepInitialize() before.
  142. *
  143. * \param[in,out] apduParam : APDU parameters
  144. * apduParam.cApduLen will contain the APDU length
  145. *
  146. * \return ERR_PARAM : Invalid parameter
  147. * \return ERR_PROTO : Protocol error
  148. * \return ERR_NONE : No error
  149. *****************************************************************************
  150. */
  151. ReturnCode rfalT4TPollerComposeCAPDU( const rfalT4tCApduParam *apduParam );
  152. /*!
  153. *****************************************************************************
  154. * \brief T4T Parse R-APDU
  155. *
  156. * This method parses a R-APDU according to NFC Forum T4T and ISO7816-4.
  157. * It will extract the data length and check if the Satus word is expected.
  158. *
  159. * \param[in,out] apduParam : APDU parameters
  160. * apduParam.rApduBodyLen will contain the data length
  161. * apduParam.statusWord will contain the SW1 and SW2
  162. *
  163. * \return ERR_REQUEST : Status word (SW1 SW2) different from 9000
  164. * \return ERR_PARAM : Invalid parameter
  165. * \return ERR_PROTO : Protocol error
  166. * \return ERR_NONE : No error
  167. *****************************************************************************
  168. */
  169. ReturnCode rfalT4TPollerParseRAPDU( rfalT4tRApduParam *apduParam );
  170. /*!
  171. *****************************************************************************
  172. * \brief T4T Compose Select Application APDU
  173. *
  174. * This method computes a Select Application APDU according to NFC Forum T4T
  175. *
  176. * To transceive the formed APDU the ISO-DEP layer shall be used
  177. *
  178. * \see rfalIsoDepStartApduTransceive()
  179. * \see rfalIsoDepGetApduTransceiveStatus()
  180. *
  181. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  182. * \param[in] aid : Application ID to be used
  183. * \param[in] aidLen : Application ID length
  184. * \param[out] cApduLen : Composed C-APDU length
  185. *
  186. * \return ERR_PARAM : Invalid parameter
  187. * \return ERR_PROTO : Protocol error
  188. * \return ERR_NONE : No error
  189. *****************************************************************************
  190. */
  191. ReturnCode rfalT4TPollerComposeSelectAppl( rfalIsoDepApduBufFormat *cApduBuf, const uint8_t* aid, uint8_t aidLen, uint16_t *cApduLen );
  192. /*!
  193. *****************************************************************************
  194. * \brief T4T Compose Select File APDU
  195. *
  196. * This method computes a Select File APDU according to NFC Forum T4T
  197. *
  198. * To transceive the formed APDU the ISO-DEP layer shall be used
  199. *
  200. * \see rfalIsoDepStartApduTransceive()
  201. * \see rfalIsoDepGetApduTransceiveStatus()
  202. *
  203. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  204. * \param[in] fid : File ID to be used
  205. * \param[in] fidLen : File ID length
  206. * \param[out] cApduLen : Composed C-APDU length
  207. *
  208. * \return ERR_PARAM : Invalid parameter
  209. * \return ERR_PROTO : Protocol error
  210. * \return ERR_NONE : No error
  211. *****************************************************************************
  212. */
  213. ReturnCode rfalT4TPollerComposeSelectFile( rfalIsoDepApduBufFormat *cApduBuf, const uint8_t* fid, uint8_t fidLen, uint16_t *cApduLen );
  214. /*!
  215. *****************************************************************************
  216. * \brief T4T Compose Select File APDU for Mapping Version 1
  217. *
  218. * This method computes a Select File APDU according to NFC Forum T4TOP_v1.0
  219. *
  220. * To transceive the formed APDU the ISO-DEP layer shall be used
  221. *
  222. * \see rfalIsoDepStartApduTransceive()
  223. * \see rfalIsoDepGetApduTransceiveStatus()
  224. *
  225. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  226. * \param[in] fid : File ID to be used
  227. * \param[in] fidLen : File ID length
  228. * \param[out] cApduLen : Composed C-APDU length
  229. *
  230. * \return ERR_PARAM : Invalid parameter
  231. * \return ERR_PROTO : Protocol error
  232. * \return ERR_NONE : No error
  233. *****************************************************************************
  234. */
  235. ReturnCode rfalT4TPollerComposeSelectFileV1Mapping( rfalIsoDepApduBufFormat *cApduBuf, const uint8_t* fid, uint8_t fidLen, uint16_t *cApduLen );
  236. /*!
  237. *****************************************************************************
  238. * \brief T4T Compose Read Data APDU
  239. *
  240. * This method computes a Read Data APDU according to NFC Forum T4T
  241. *
  242. * To transceive the formed APDU the ISO-DEP layer shall be used
  243. *
  244. * \see rfalIsoDepStartApduTransceive()
  245. * \see rfalIsoDepGetApduTransceiveStatus()
  246. *
  247. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  248. * \param[in] offset : File offset
  249. * \param[in] expLen : Expected length (Le)
  250. * \param[out] cApduLen : Composed C-APDU length
  251. *
  252. * \return ERR_PARAM : Invalid parameter
  253. * \return ERR_PROTO : Protocol error
  254. * \return ERR_NONE : No error
  255. *****************************************************************************
  256. */
  257. ReturnCode rfalT4TPollerComposeReadData( rfalIsoDepApduBufFormat *cApduBuf, uint16_t offset, uint8_t expLen, uint16_t *cApduLen );
  258. /*!
  259. *****************************************************************************
  260. * \brief T4T Compose Read Data ODO APDU
  261. *
  262. * This method computes a Read Data ODO APDU according to NFC Forum T4T
  263. *
  264. * To transceive the formed APDU the ISO-DEP layer shall be used
  265. *
  266. * \see rfalIsoDepStartApduTransceive()
  267. * \see rfalIsoDepGetApduTransceiveStatus()
  268. *
  269. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  270. * \param[in] offset : File offset
  271. * \param[in] expLen : Expected length (Le)
  272. * \param[out] cApduLen : Composed C-APDU length
  273. *
  274. * \return ERR_PARAM : Invalid parameter
  275. * \return ERR_PROTO : Protocol error
  276. * \return ERR_NONE : No error
  277. *****************************************************************************
  278. */
  279. ReturnCode rfalT4TPollerComposeReadDataODO( rfalIsoDepApduBufFormat *cApduBuf, uint32_t offset, uint8_t expLen, uint16_t *cApduLen );
  280. /*!
  281. *****************************************************************************
  282. * \brief T4T Compose Write Data APDU
  283. *
  284. * This method computes a Write Data APDU according to NFC Forum T4T
  285. *
  286. * To transceive the formed APDU the ISO-DEP layer shall be used
  287. *
  288. * \see rfalIsoDepStartApduTransceive()
  289. * \see rfalIsoDepGetApduTransceiveStatus()
  290. *
  291. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  292. * \param[in] offset : File offset
  293. * \param[in] data : Data to be written
  294. * \param[in] dataLen : Data length to be written (Lc)
  295. * \param[out] cApduLen : Composed C-APDU length
  296. *
  297. * \return ERR_PARAM : Invalid parameter
  298. * \return ERR_PROTO : Protocol error
  299. * \return ERR_NONE : No error
  300. *****************************************************************************
  301. */
  302. ReturnCode rfalT4TPollerComposeWriteData( rfalIsoDepApduBufFormat *cApduBuf, uint16_t offset, const uint8_t* data, uint8_t dataLen, uint16_t *cApduLen );
  303. /*!
  304. *****************************************************************************
  305. * \brief T4T Compose Write Data ODO APDU
  306. *
  307. * This method computes a Write Data ODO sAPDU according to NFC Forum T4T
  308. *
  309. * To transceive the formed APDU the ISO-DEP layer shall be used
  310. *
  311. * \see rfalIsoDepStartApduTransceive()
  312. * \see rfalIsoDepGetApduTransceiveStatus()
  313. *
  314. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  315. * \param[in] offset : File offset
  316. * \param[in] data : Data to be written
  317. * \param[in] dataLen : Data length to be written (Lc)
  318. * \param[out] cApduLen : Composed C-APDU length
  319. *
  320. * \return ERR_PARAM : Invalid parameter
  321. * \return ERR_PROTO : Protocol error
  322. * \return ERR_NONE : No error
  323. *****************************************************************************
  324. */
  325. ReturnCode rfalT4TPollerComposeWriteDataODO( rfalIsoDepApduBufFormat *cApduBuf, uint32_t offset, const uint8_t* data, uint8_t dataLen, uint16_t *cApduLen );
  326. #endif /* RFAL_T4T_H */
  327. /**
  328. * @}
  329. *
  330. * @}
  331. *
  332. * @}
  333. */