rfal_t4t.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 \
  68. 4U /*!< Command-APDU prologue length (CLA INS P1 P2) */
  69. #define RFAL_T4T_LE_LEN 1U /*!< Le Expected Response Length (short field coding) */
  70. #define RFAL_T4T_LC_LEN 1U /*!< Lc Data field length (short field coding) */
  71. #define RFAL_T4T_MAX_RAPDU_SW1SW2_LEN \
  72. 2U /*!< SW1 SW2 length */
  73. #define RFAL_T4T_CLA 0x00U /*!< Class byte (contains 00h because secure message are not used) */
  74. #define RFAL_T4T_ISO7816_P1_SELECT_BY_DF_NAME \
  75. 0x04U /*!< P1 value for Select by name */
  76. #define RFAL_T4T_ISO7816_P1_SELECT_BY_FILEID \
  77. 0x00U /*!< P1 value for Select by file identifier */
  78. #define RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE \
  79. 0x00U /*!< b2b1 P2 value for First or only occurrence */
  80. #define RFAL_T4T_ISO7816_P2_SELECT_RETURN_FCI_TEMPLATE \
  81. 0x00U /*!< b4b3 P2 value for Return FCI template */
  82. #define RFAL_T4T_ISO7816_P2_SELECT_NO_RESPONSE_DATA \
  83. 0x0CU /*!< b4b3 P2 value for No response data */
  84. #define RFAL_T4T_ISO7816_STATUS_COMPLETE \
  85. 0x9000U /*!< Command completed \ Normal processing - No further qualification*/
  86. /*
  87. ******************************************************************************
  88. * GLOBAL VARIABLES
  89. ******************************************************************************
  90. */
  91. /*
  92. ******************************************************************************
  93. * GLOBAL TYPES
  94. ******************************************************************************
  95. */
  96. /*! NFC-A T4T Command-APDU structure */
  97. typedef struct {
  98. uint8_t CLA; /*!< Class byte */
  99. uint8_t INS; /*!< Instruction byte */
  100. uint8_t P1; /*!< Parameter byte 1 */
  101. uint8_t P2; /*!< Parameter byte 2 */
  102. uint8_t Lc; /*!< Data field length */
  103. bool LcFlag; /*!< Lc flag (append Lc when true) */
  104. uint8_t Le; /*!< Expected Response Length */
  105. bool LeFlag; /*!< Le flag (append Le when true) */
  106. rfalIsoDepApduBufFormat* cApduBuf; /*!< Command-APDU buffer (Tx) */
  107. uint16_t* cApduLen; /*!< Command-APDU Length */
  108. } rfalT4tCApduParam;
  109. /*! NFC-A T4T Response-APDU structure */
  110. typedef struct {
  111. rfalIsoDepApduBufFormat* rApduBuf; /*!< Response-APDU buffer (Rx) */
  112. uint16_t rcvdLen; /*!< Full response length */
  113. uint16_t rApduBodyLen; /*!< Response body length */
  114. uint16_t statusWord; /*!< R-APDU Status Word SW1|SW2 */
  115. } rfalT4tRApduParam;
  116. /*! NFC-A T4T command set T4T 1.0 & ISO7816-4 2013 Table 4 */
  117. typedef enum {
  118. RFAL_T4T_INS_SELECT = 0xA4U, /*!< T4T Select */
  119. RFAL_T4T_INS_READBINARY = 0xB0U, /*!< T4T ReadBinary */
  120. RFAL_T4T_INS_UPDATEBINARY = 0xD6U, /*!< T4T UpdateBinay */
  121. RFAL_T4T_INS_READBINARY_ODO = 0xB1U, /*!< T4T ReadBinary using ODO */
  122. RFAL_T4T_INS_UPDATEBINARY_ODO =
  123. 0xD7U /*!< T4T UpdateBinay using ODO */
  124. } rfalT4tCmds;
  125. /*
  126. ******************************************************************************
  127. * GLOBAL FUNCTION PROTOTYPES
  128. ******************************************************************************
  129. */
  130. /*!
  131. *****************************************************************************
  132. * \brief T4T Compose APDU
  133. *
  134. * This method computes a C-APDU according to NFC Forum T4T and ISO7816-4.
  135. *
  136. * If C-APDU contains data to be sent, it must be placed inside the buffer
  137. * rfalT4tTxRxApduParam.txRx.cApduBuf.apdu and signaled by Lc
  138. *
  139. * To transceive the formed APDU the ISO-DEP layer shall be used
  140. *
  141. * \see rfalIsoDepStartApduTransceive()
  142. * \see rfalIsoDepGetApduTransceiveStatus()
  143. * \see rfalT4TPollerParseRAPDU()
  144. *
  145. * \warning The ISO-DEP module is used to perform the tranceive. Usually
  146. * activation has been done via ISO-DEP activatiavtion. If not
  147. * please call rfalIsoDepInitialize() before.
  148. *
  149. * \param[in,out] apduParam : APDU parameters
  150. * apduParam.cApduLen will contain the APDU length
  151. *
  152. * \return ERR_PARAM : Invalid parameter
  153. * \return ERR_PROTO : Protocol error
  154. * \return ERR_NONE : No error
  155. *****************************************************************************
  156. */
  157. ReturnCode rfalT4TPollerComposeCAPDU(const rfalT4tCApduParam* apduParam);
  158. /*!
  159. *****************************************************************************
  160. * \brief T4T Parse R-APDU
  161. *
  162. * This method parses a R-APDU according to NFC Forum T4T and ISO7816-4.
  163. * It will extract the data length and check if the Status word is expected.
  164. *
  165. * \param[in,out] apduParam : APDU parameters
  166. * apduParam.rApduBodyLen will contain the data length
  167. * apduParam.statusWord will contain the SW1 and SW2
  168. *
  169. * \return ERR_REQUEST : Status word (SW1 SW2) different from 9000
  170. * \return ERR_PARAM : Invalid parameter
  171. * \return ERR_PROTO : Protocol error
  172. * \return ERR_NONE : No error
  173. *****************************************************************************
  174. */
  175. ReturnCode rfalT4TPollerParseRAPDU(rfalT4tRApduParam* apduParam);
  176. /*!
  177. *****************************************************************************
  178. * \brief T4T Compose Select Application APDU
  179. *
  180. * This method computes a Select Application APDU according to NFC Forum T4T
  181. *
  182. * To transceive the formed APDU the ISO-DEP layer shall be used
  183. *
  184. * \see rfalIsoDepStartApduTransceive()
  185. * \see rfalIsoDepGetApduTransceiveStatus()
  186. *
  187. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  188. * \param[in] aid : Application ID to be used
  189. * \param[in] aidLen : Application ID length
  190. * \param[out] cApduLen : Composed C-APDU length
  191. *
  192. * \return ERR_PARAM : Invalid parameter
  193. * \return ERR_PROTO : Protocol error
  194. * \return ERR_NONE : No error
  195. *****************************************************************************
  196. */
  197. ReturnCode rfalT4TPollerComposeSelectAppl(
  198. rfalIsoDepApduBufFormat* cApduBuf,
  199. const uint8_t* aid,
  200. uint8_t aidLen,
  201. uint16_t* cApduLen);
  202. /*!
  203. *****************************************************************************
  204. * \brief T4T Compose Select File APDU
  205. *
  206. * This method computes a Select File APDU according to NFC Forum T4T
  207. *
  208. * To transceive the formed APDU the ISO-DEP layer shall be used
  209. *
  210. * \see rfalIsoDepStartApduTransceive()
  211. * \see rfalIsoDepGetApduTransceiveStatus()
  212. *
  213. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  214. * \param[in] fid : File ID to be used
  215. * \param[in] fidLen : File ID length
  216. * \param[out] cApduLen : Composed C-APDU length
  217. *
  218. * \return ERR_PARAM : Invalid parameter
  219. * \return ERR_PROTO : Protocol error
  220. * \return ERR_NONE : No error
  221. *****************************************************************************
  222. */
  223. ReturnCode rfalT4TPollerComposeSelectFile(
  224. rfalIsoDepApduBufFormat* cApduBuf,
  225. const uint8_t* fid,
  226. uint8_t fidLen,
  227. uint16_t* cApduLen);
  228. /*!
  229. *****************************************************************************
  230. * \brief T4T Compose Select File APDU for Mapping Version 1
  231. *
  232. * This method computes a Select File APDU according to NFC Forum T4TOP_v1.0
  233. *
  234. * To transceive the formed APDU the ISO-DEP layer shall be used
  235. *
  236. * \see rfalIsoDepStartApduTransceive()
  237. * \see rfalIsoDepGetApduTransceiveStatus()
  238. *
  239. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  240. * \param[in] fid : File ID to be used
  241. * \param[in] fidLen : File ID length
  242. * \param[out] cApduLen : Composed C-APDU length
  243. *
  244. * \return ERR_PARAM : Invalid parameter
  245. * \return ERR_PROTO : Protocol error
  246. * \return ERR_NONE : No error
  247. *****************************************************************************
  248. */
  249. ReturnCode rfalT4TPollerComposeSelectFileV1Mapping(
  250. rfalIsoDepApduBufFormat* cApduBuf,
  251. const uint8_t* fid,
  252. uint8_t fidLen,
  253. uint16_t* cApduLen);
  254. /*!
  255. *****************************************************************************
  256. * \brief T4T Compose Read Data APDU
  257. *
  258. * This method computes a Read Data APDU according to NFC Forum T4T
  259. *
  260. * To transceive the formed APDU the ISO-DEP layer shall be used
  261. *
  262. * \see rfalIsoDepStartApduTransceive()
  263. * \see rfalIsoDepGetApduTransceiveStatus()
  264. *
  265. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  266. * \param[in] offset : File offset
  267. * \param[in] expLen : Expected length (Le)
  268. * \param[out] cApduLen : Composed C-APDU length
  269. *
  270. * \return ERR_PARAM : Invalid parameter
  271. * \return ERR_PROTO : Protocol error
  272. * \return ERR_NONE : No error
  273. *****************************************************************************
  274. */
  275. ReturnCode rfalT4TPollerComposeReadData(
  276. rfalIsoDepApduBufFormat* cApduBuf,
  277. uint16_t offset,
  278. uint8_t expLen,
  279. uint16_t* cApduLen);
  280. /*!
  281. *****************************************************************************
  282. * \brief T4T Compose Read Data ODO APDU
  283. *
  284. * This method computes a Read Data ODO 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] expLen : Expected length (Le)
  294. * \param[out] cApduLen : Composed C-APDU length
  295. *
  296. * \return ERR_PARAM : Invalid parameter
  297. * \return ERR_PROTO : Protocol error
  298. * \return ERR_NONE : No error
  299. *****************************************************************************
  300. */
  301. ReturnCode rfalT4TPollerComposeReadDataODO(
  302. rfalIsoDepApduBufFormat* cApduBuf,
  303. uint32_t offset,
  304. uint8_t expLen,
  305. uint16_t* cApduLen);
  306. /*!
  307. *****************************************************************************
  308. * \brief T4T Compose Write Data APDU
  309. *
  310. * This method computes a Write Data APDU according to NFC Forum T4T
  311. *
  312. * To transceive the formed APDU the ISO-DEP layer shall be used
  313. *
  314. * \see rfalIsoDepStartApduTransceive()
  315. * \see rfalIsoDepGetApduTransceiveStatus()
  316. *
  317. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  318. * \param[in] offset : File offset
  319. * \param[in] data : Data to be written
  320. * \param[in] dataLen : Data length to be written (Lc)
  321. * \param[out] cApduLen : Composed C-APDU length
  322. *
  323. * \return ERR_PARAM : Invalid parameter
  324. * \return ERR_PROTO : Protocol error
  325. * \return ERR_NONE : No error
  326. *****************************************************************************
  327. */
  328. ReturnCode rfalT4TPollerComposeWriteData(
  329. rfalIsoDepApduBufFormat* cApduBuf,
  330. uint16_t offset,
  331. const uint8_t* data,
  332. uint8_t dataLen,
  333. uint16_t* cApduLen);
  334. /*!
  335. *****************************************************************************
  336. * \brief T4T Compose Write Data ODO APDU
  337. *
  338. * This method computes a Write Data ODO sAPDU according to NFC Forum T4T
  339. *
  340. * To transceive the formed APDU the ISO-DEP layer shall be used
  341. *
  342. * \see rfalIsoDepStartApduTransceive()
  343. * \see rfalIsoDepGetApduTransceiveStatus()
  344. *
  345. * \param[out] cApduBuf : buffer where the C-APDU will be placed
  346. * \param[in] offset : File offset
  347. * \param[in] data : Data to be written
  348. * \param[in] dataLen : Data length to be written (Lc)
  349. * \param[out] cApduLen : Composed C-APDU length
  350. *
  351. * \return ERR_PARAM : Invalid parameter
  352. * \return ERR_PROTO : Protocol error
  353. * \return ERR_NONE : No error
  354. *****************************************************************************
  355. */
  356. ReturnCode rfalT4TPollerComposeWriteDataODO(
  357. rfalIsoDepApduBufFormat* cApduBuf,
  358. uint32_t offset,
  359. const uint8_t* data,
  360. uint8_t dataLen,
  361. uint16_t* cApduLen);
  362. #endif /* RFAL_T4T_H */
  363. /**
  364. * @}
  365. *
  366. * @}
  367. *
  368. * @}
  369. */