rfal_t4t.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. */
  40. /*
  41. ******************************************************************************
  42. * INCLUDES
  43. ******************************************************************************
  44. */
  45. #include "rfal_t4t.h"
  46. #include "utils.h"
  47. /*
  48. ******************************************************************************
  49. * ENABLE SWITCH
  50. ******************************************************************************
  51. */
  52. #ifndef RFAL_FEATURE_T4T
  53. #define RFAL_FEATURE_T4T false /* T4T module configuration missing. Disabled by default */
  54. #endif
  55. #if RFAL_FEATURE_T4T
  56. /*
  57. ******************************************************************************
  58. * GLOBAL DEFINES
  59. ******************************************************************************
  60. */
  61. #define RFAL_T4T_OFFSET_DO 0x54U /*!< Tag value for offset BER-TLV data object */
  62. #define RFAL_T4T_LENGTH_DO 0x03U /*!< Len value for offset BER-TLV data object */
  63. #define RFAL_T4T_DATA_DO 0x53U /*!< Tag value for data BER-TLV data object */
  64. #define RFAL_T4T_MAX_LC 255U /*!< Maximum Lc value for short Lc coding */
  65. /*
  66. ******************************************************************************
  67. * GLOBAL TYPES
  68. ******************************************************************************
  69. */
  70. /*
  71. ******************************************************************************
  72. * GLOBAL MACROS
  73. ******************************************************************************
  74. */
  75. /*
  76. ******************************************************************************
  77. * LOCAL VARIABLES
  78. ******************************************************************************
  79. */
  80. /*
  81. ******************************************************************************
  82. * GLOBAL FUNCTIONS
  83. ******************************************************************************
  84. */
  85. /*******************************************************************************/
  86. ReturnCode rfalT4TPollerComposeCAPDU(const rfalT4tCApduParam* apduParam) {
  87. uint8_t hdrLen;
  88. uint16_t msgIt;
  89. if((apduParam == NULL) || (apduParam->cApduBuf == NULL) || (apduParam->cApduLen == NULL)) {
  90. return ERR_PARAM;
  91. }
  92. msgIt = 0;
  93. *(apduParam->cApduLen) = 0;
  94. /*******************************************************************************/
  95. /* Compute Command-APDU according to the format T4T 1.0 5.1.2 & ISO7816-4 2013 Table 1 */
  96. /* Check if Data is present */
  97. if(apduParam->LcFlag) {
  98. if(apduParam->Lc == 0U) {
  99. /* Extented field coding not supported */
  100. return ERR_PARAM;
  101. }
  102. /* Check whether requested Lc fits */
  103. if((uint16_t)apduParam->Lc >
  104. (uint16_t)(RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN - RFAL_T4T_LE_LEN)) {
  105. return ERR_PARAM; /* PRQA S 2880 # MISRA 2.1 - Unreachable code due to configuration option being set/unset */
  106. }
  107. /* Calculate the header length a place the data/body where it should be */
  108. hdrLen = RFAL_T4T_MAX_CAPDU_PROLOGUE_LEN + RFAL_T4T_LC_LEN;
  109. /* make sure not to exceed buffer size */
  110. if(((uint16_t)hdrLen + (uint16_t)apduParam->Lc +
  111. (apduParam->LeFlag ? RFAL_T4T_LC_LEN : 0U)) > RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN) {
  112. return ERR_NOMEM; /* PRQA S 2880 # MISRA 2.1 - Unreachable code due to configuration option being set/unset */
  113. }
  114. ST_MEMMOVE(&apduParam->cApduBuf->apdu[hdrLen], apduParam->cApduBuf->apdu, apduParam->Lc);
  115. }
  116. /* Prepend the ADPDU's header */
  117. apduParam->cApduBuf->apdu[msgIt++] = apduParam->CLA;
  118. apduParam->cApduBuf->apdu[msgIt++] = apduParam->INS;
  119. apduParam->cApduBuf->apdu[msgIt++] = apduParam->P1;
  120. apduParam->cApduBuf->apdu[msgIt++] = apduParam->P2;
  121. /* Check if Data field length is to be added */
  122. if(apduParam->LcFlag) {
  123. apduParam->cApduBuf->apdu[msgIt++] = apduParam->Lc;
  124. msgIt += apduParam->Lc;
  125. }
  126. /* Check if Expected Response Length is to be added */
  127. if(apduParam->LeFlag) {
  128. apduParam->cApduBuf->apdu[msgIt++] = apduParam->Le;
  129. }
  130. *(apduParam->cApduLen) = msgIt;
  131. return ERR_NONE;
  132. }
  133. /*******************************************************************************/
  134. ReturnCode rfalT4TPollerParseRAPDU(rfalT4tRApduParam* apduParam) {
  135. if((apduParam == NULL) || (apduParam->rApduBuf == NULL)) {
  136. return ERR_PARAM;
  137. }
  138. if(apduParam->rcvdLen < RFAL_T4T_MAX_RAPDU_SW1SW2_LEN) {
  139. return ERR_PROTO;
  140. }
  141. apduParam->rApduBodyLen = (apduParam->rcvdLen - (uint16_t)RFAL_T4T_MAX_RAPDU_SW1SW2_LEN);
  142. apduParam->statusWord = GETU16((&apduParam->rApduBuf->apdu[apduParam->rApduBodyLen]));
  143. /* Check SW1 SW2 T4T 1.0 5.1.3 NOTE */
  144. if(apduParam->statusWord == RFAL_T4T_ISO7816_STATUS_COMPLETE) {
  145. return ERR_NONE;
  146. }
  147. return ERR_REQUEST;
  148. }
  149. /*******************************************************************************/
  150. ReturnCode rfalT4TPollerComposeSelectAppl(
  151. rfalIsoDepApduBufFormat* cApduBuf,
  152. const uint8_t* aid,
  153. uint8_t aidLen,
  154. uint16_t* cApduLen) {
  155. rfalT4tCApduParam cAPDU;
  156. /* CLA INS P1 P2 Lc Data Le */
  157. /* 00h A4h 00h 00h 07h AID 00h */
  158. cAPDU.CLA = RFAL_T4T_CLA;
  159. cAPDU.INS = (uint8_t)RFAL_T4T_INS_SELECT;
  160. cAPDU.P1 = RFAL_T4T_ISO7816_P1_SELECT_BY_DF_NAME;
  161. cAPDU.P2 = RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE |
  162. RFAL_T4T_ISO7816_P2_SELECT_RETURN_FCI_TEMPLATE;
  163. cAPDU.Lc = aidLen;
  164. cAPDU.Le = 0x00;
  165. cAPDU.LcFlag = true;
  166. cAPDU.LeFlag = true;
  167. cAPDU.cApduBuf = cApduBuf;
  168. cAPDU.cApduLen = cApduLen;
  169. if(aidLen > 0U) {
  170. ST_MEMCPY(cAPDU.cApduBuf->apdu, aid, aidLen);
  171. }
  172. return rfalT4TPollerComposeCAPDU(&cAPDU);
  173. }
  174. /*******************************************************************************/
  175. ReturnCode rfalT4TPollerComposeSelectFile(
  176. rfalIsoDepApduBufFormat* cApduBuf,
  177. const uint8_t* fid,
  178. uint8_t fidLen,
  179. uint16_t* cApduLen) {
  180. rfalT4tCApduParam cAPDU;
  181. /* CLA INS P1 P2 Lc Data Le */
  182. /* 00h A4h 00h 0Ch 02h FID - */
  183. cAPDU.CLA = RFAL_T4T_CLA;
  184. cAPDU.INS = (uint8_t)RFAL_T4T_INS_SELECT;
  185. cAPDU.P1 = RFAL_T4T_ISO7816_P1_SELECT_BY_FILEID;
  186. cAPDU.P2 = RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE |
  187. RFAL_T4T_ISO7816_P2_SELECT_NO_RESPONSE_DATA;
  188. cAPDU.Lc = fidLen;
  189. cAPDU.Le = 0x00;
  190. cAPDU.LcFlag = true;
  191. cAPDU.LeFlag = false;
  192. cAPDU.cApduBuf = cApduBuf;
  193. cAPDU.cApduLen = cApduLen;
  194. if(fidLen > 0U) {
  195. ST_MEMCPY(cAPDU.cApduBuf->apdu, fid, fidLen);
  196. }
  197. return rfalT4TPollerComposeCAPDU(&cAPDU);
  198. }
  199. /*******************************************************************************/
  200. ReturnCode rfalT4TPollerComposeSelectFileV1Mapping(
  201. rfalIsoDepApduBufFormat* cApduBuf,
  202. const uint8_t* fid,
  203. uint8_t fidLen,
  204. uint16_t* cApduLen) {
  205. rfalT4tCApduParam cAPDU;
  206. /* CLA INS P1 P2 Lc Data Le */
  207. /* 00h A4h 00h 00h 02h FID - */
  208. cAPDU.CLA = RFAL_T4T_CLA;
  209. cAPDU.INS = (uint8_t)RFAL_T4T_INS_SELECT;
  210. cAPDU.P1 = RFAL_T4T_ISO7816_P1_SELECT_BY_FILEID;
  211. cAPDU.P2 = RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE |
  212. RFAL_T4T_ISO7816_P2_SELECT_RETURN_FCI_TEMPLATE;
  213. cAPDU.Lc = fidLen;
  214. cAPDU.Le = 0x00;
  215. cAPDU.LcFlag = true;
  216. cAPDU.LeFlag = false;
  217. cAPDU.cApduBuf = cApduBuf;
  218. cAPDU.cApduLen = cApduLen;
  219. if(fidLen > 0U) {
  220. ST_MEMCPY(cAPDU.cApduBuf->apdu, fid, fidLen);
  221. }
  222. return rfalT4TPollerComposeCAPDU(&cAPDU);
  223. }
  224. /*******************************************************************************/
  225. ReturnCode rfalT4TPollerComposeReadData(
  226. rfalIsoDepApduBufFormat* cApduBuf,
  227. uint16_t offset,
  228. uint8_t expLen,
  229. uint16_t* cApduLen) {
  230. rfalT4tCApduParam cAPDU;
  231. /* CLA INS P1 P2 Lc Data Le */
  232. /* 00h B0h [Offset] - - len */
  233. cAPDU.CLA = RFAL_T4T_CLA;
  234. cAPDU.INS = (uint8_t)RFAL_T4T_INS_READBINARY;
  235. cAPDU.P1 = (uint8_t)((offset >> 8U) & 0xFFU);
  236. cAPDU.P2 = (uint8_t)((offset >> 0U) & 0xFFU);
  237. cAPDU.Le = expLen;
  238. cAPDU.LcFlag = false;
  239. cAPDU.LeFlag = true;
  240. cAPDU.cApduBuf = cApduBuf;
  241. cAPDU.cApduLen = cApduLen;
  242. return rfalT4TPollerComposeCAPDU(&cAPDU);
  243. }
  244. /*******************************************************************************/
  245. ReturnCode rfalT4TPollerComposeReadDataODO(
  246. rfalIsoDepApduBufFormat* cApduBuf,
  247. uint32_t offset,
  248. uint8_t expLen,
  249. uint16_t* cApduLen) {
  250. rfalT4tCApduParam cAPDU;
  251. uint8_t dataIt;
  252. /* CLA INS P1 P2 Lc Data Le */
  253. /* 00h B1h 00h 00h Lc 54 03 xxyyzz len */
  254. /* [Offset] */
  255. cAPDU.CLA = RFAL_T4T_CLA;
  256. cAPDU.INS = (uint8_t)RFAL_T4T_INS_READBINARY_ODO;
  257. cAPDU.P1 = 0x00U;
  258. cAPDU.P2 = 0x00U;
  259. cAPDU.Le = expLen;
  260. cAPDU.LcFlag = true;
  261. cAPDU.LeFlag = true;
  262. cAPDU.cApduBuf = cApduBuf;
  263. cAPDU.cApduLen = cApduLen;
  264. dataIt = 0U;
  265. cApduBuf->apdu[dataIt++] = RFAL_T4T_OFFSET_DO;
  266. cApduBuf->apdu[dataIt++] = RFAL_T4T_LENGTH_DO;
  267. cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 16U);
  268. cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 8U);
  269. cApduBuf->apdu[dataIt++] = (uint8_t)(offset);
  270. cAPDU.Lc = dataIt;
  271. return rfalT4TPollerComposeCAPDU(&cAPDU);
  272. }
  273. /*******************************************************************************/
  274. ReturnCode rfalT4TPollerComposeWriteData(
  275. rfalIsoDepApduBufFormat* cApduBuf,
  276. uint16_t offset,
  277. const uint8_t* data,
  278. uint8_t dataLen,
  279. uint16_t* cApduLen) {
  280. rfalT4tCApduParam cAPDU;
  281. /* CLA INS P1 P2 Lc Data Le */
  282. /* 00h D6h [Offset] len Data - */
  283. cAPDU.CLA = RFAL_T4T_CLA;
  284. cAPDU.INS = (uint8_t)RFAL_T4T_INS_UPDATEBINARY;
  285. cAPDU.P1 = (uint8_t)((offset >> 8U) & 0xFFU);
  286. cAPDU.P2 = (uint8_t)((offset >> 0U) & 0xFFU);
  287. cAPDU.Lc = dataLen;
  288. cAPDU.LcFlag = true;
  289. cAPDU.LeFlag = false;
  290. cAPDU.cApduBuf = cApduBuf;
  291. cAPDU.cApduLen = cApduLen;
  292. if(dataLen > 0U) {
  293. ST_MEMCPY(cAPDU.cApduBuf->apdu, data, dataLen);
  294. }
  295. return rfalT4TPollerComposeCAPDU(&cAPDU);
  296. }
  297. /*******************************************************************************/
  298. ReturnCode rfalT4TPollerComposeWriteDataODO(
  299. rfalIsoDepApduBufFormat* cApduBuf,
  300. uint32_t offset,
  301. const uint8_t* data,
  302. uint8_t dataLen,
  303. uint16_t* cApduLen) {
  304. rfalT4tCApduParam cAPDU;
  305. uint8_t dataIt;
  306. /* CLA INS P1 P2 Lc Data Le */
  307. /* 00h D7h 00h 00h len 54 03 xxyyzz 53 Ld data - */
  308. /* [offset] [data] */
  309. cAPDU.CLA = RFAL_T4T_CLA;
  310. cAPDU.INS = (uint8_t)RFAL_T4T_INS_UPDATEBINARY_ODO;
  311. cAPDU.P1 = 0x00U;
  312. cAPDU.P2 = 0x00U;
  313. cAPDU.LcFlag = true;
  314. cAPDU.LeFlag = false;
  315. cAPDU.cApduBuf = cApduBuf;
  316. cAPDU.cApduLen = cApduLen;
  317. dataIt = 0U;
  318. cApduBuf->apdu[dataIt++] = RFAL_T4T_OFFSET_DO;
  319. cApduBuf->apdu[dataIt++] = RFAL_T4T_LENGTH_DO;
  320. cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 16U);
  321. cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 8U);
  322. cApduBuf->apdu[dataIt++] = (uint8_t)(offset);
  323. cApduBuf->apdu[dataIt++] = RFAL_T4T_DATA_DO;
  324. cApduBuf->apdu[dataIt++] = dataLen;
  325. if((((uint32_t)dataLen + (uint32_t)dataIt) >= RFAL_T4T_MAX_LC) ||
  326. (((uint32_t)dataLen + (uint32_t)dataIt) >= RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN)) {
  327. return (ERR_NOMEM);
  328. }
  329. if(dataLen > 0U) {
  330. ST_MEMCPY(&cAPDU.cApduBuf->apdu[dataIt], data, dataLen);
  331. }
  332. dataIt += dataLen;
  333. cAPDU.Lc = dataIt;
  334. return rfalT4TPollerComposeCAPDU(&cAPDU);
  335. }
  336. #endif /* RFAL_FEATURE_T4T */