rfal_t4t.c 13 KB

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