nfc_t4t_apdu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_config.h"
  41. #if NFC_T4T_APDU_ENABLED
  42. #include "nfc_t4t_apdu.h"
  43. #include "sdk_macros.h"
  44. #include "nordic_common.h"
  45. #include "app_util.h"
  46. #define NRF_LOG_MODULE_NAME nfc_t4t_apdu
  47. #if NFC_T4T_APDU_LOG_ENABLED
  48. #define NRF_LOG_LEVEL NFC_T4T_APDU_LOG_LEVEL
  49. #define NRF_LOG_INFO_COLOR NFC_T4T_APDU_LOG_COLOR
  50. #include "nrf_log.h"
  51. NRF_LOG_MODULE_REGISTER();
  52. #else // NFC_T4T_APDU_LOG_ENABLED
  53. #define NRF_LOG_LEVEL 0
  54. #include "nrf_log.h"
  55. #endif // NFC_T4T_APDU_LOG_ENABLED
  56. /**
  57. * @brief Field sizes that can be present in CAPDU.
  58. */
  59. #define CLASS_TYPE_SIZE 1U
  60. #define INSTRUCTION_TYPE_SIZE 1U
  61. #define PARAMETER_SIZE 2U
  62. #define LC_SHORT_FORMAT_SIZE 1U
  63. #define LC_LONG_FORMAT_SIZE 3U
  64. #define LE_SHORT_FORMAT_SIZE 1U
  65. #define LE_LONG_FORMAT_SIZE 2U
  66. /**
  67. * @brief Values used to encode Lc field in CAPDU.
  68. */
  69. #define LC_LONG_FORMAT_TOKEN 0x00
  70. #define LC_LONG_FORMAT_THR 0xFF
  71. /**
  72. * @brief Values used to encode Le field in CAPDU.
  73. */
  74. #define LE_FIELD_ABSENT 0U
  75. #define LE_LONG_FORMAT_THR 0x0100
  76. #define LE_ENCODED_VAL_256 0x00
  77. #define STATUS_SIZE 2U ///< Size of Status field contained in RAPDU.
  78. /**
  79. * @brief Function for calculating size of CAPDU.
  80. */
  81. __STATIC_INLINE uint16_t nfc_t4t_comm_apdu_size_calc(nfc_t4t_comm_apdu_t const * const p_cmd_apdu)
  82. {
  83. uint16_t res = CLASS_TYPE_SIZE + INSTRUCTION_TYPE_SIZE + PARAMETER_SIZE;
  84. if (p_cmd_apdu->data.p_buff != NULL)
  85. {
  86. if (p_cmd_apdu->data.len > LC_LONG_FORMAT_THR)
  87. {
  88. res += LC_LONG_FORMAT_SIZE;
  89. }
  90. else
  91. {
  92. res += LC_SHORT_FORMAT_SIZE;
  93. }
  94. }
  95. res += p_cmd_apdu->data.len;
  96. if (p_cmd_apdu->resp_len != LE_FIELD_ABSENT)
  97. {
  98. if (p_cmd_apdu->resp_len > LE_LONG_FORMAT_THR)
  99. {
  100. res += LE_LONG_FORMAT_SIZE;
  101. }
  102. else
  103. {
  104. res += LE_SHORT_FORMAT_SIZE;
  105. }
  106. }
  107. return res;
  108. }
  109. /**
  110. * @brief Function for validating arguments used by CAPDU encoding procedure.
  111. */
  112. __STATIC_INLINE ret_code_t nfc_t4t_comm_apdu_args_validate(nfc_t4t_comm_apdu_t const * const p_cmd_apdu,
  113. uint8_t * p_raw_data,
  114. uint16_t * const p_len)
  115. {
  116. if ((p_cmd_apdu == NULL) || (p_raw_data == NULL) || (p_len == NULL))
  117. {
  118. return NRF_ERROR_NULL;
  119. }
  120. if ((p_cmd_apdu->data.p_buff != NULL) && (p_cmd_apdu->data.len == 0))
  121. {
  122. return NRF_ERROR_INVALID_PARAM;
  123. }
  124. return NRF_SUCCESS;
  125. }
  126. ret_code_t nfc_t4t_comm_apdu_encode(nfc_t4t_comm_apdu_t const * const p_cmd_apdu,
  127. uint8_t * p_raw_data,
  128. uint16_t * const p_len)
  129. {
  130. // Validate passed arguments.
  131. ret_code_t err_code = nfc_t4t_comm_apdu_args_validate(p_cmd_apdu, p_raw_data, p_len);
  132. VERIFY_SUCCESS(err_code);
  133. // Check if there is enough memory in the provided buffer to store described CAPDU.
  134. uint16_t comm_apdu_len = nfc_t4t_comm_apdu_size_calc(p_cmd_apdu);
  135. if (comm_apdu_len > *p_len)
  136. {
  137. return NRF_ERROR_NO_MEM;
  138. }
  139. *p_len = comm_apdu_len;
  140. // Start to encode described CAPDU in the buffer.
  141. *p_raw_data++ = p_cmd_apdu->class_byte;
  142. *p_raw_data++ = p_cmd_apdu->instruction;
  143. *p_raw_data++ = MSB_16(p_cmd_apdu->parameter);
  144. *p_raw_data++ = LSB_16(p_cmd_apdu->parameter);
  145. // Check if optional data field should be included.
  146. if (p_cmd_apdu->data.p_buff != NULL)
  147. {
  148. if (p_cmd_apdu->data.len > LC_LONG_FORMAT_THR) // Use long data length encoding.
  149. {
  150. *p_raw_data++ = LC_LONG_FORMAT_TOKEN;
  151. *p_raw_data++ = MSB_16(p_cmd_apdu->data.len);
  152. *p_raw_data++ = LSB_16(p_cmd_apdu->data.len);
  153. }
  154. else // Use short data length encoding.
  155. {
  156. *p_raw_data++ = LSB_16(p_cmd_apdu->data.len);
  157. }
  158. memcpy(p_raw_data, p_cmd_apdu->data.p_buff, p_cmd_apdu->data.len);
  159. p_raw_data += p_cmd_apdu->data.len;
  160. }
  161. // Check if optional response length field present (Le) should be included.
  162. if (p_cmd_apdu->resp_len != LE_FIELD_ABSENT)
  163. {
  164. if (p_cmd_apdu->resp_len > LE_LONG_FORMAT_THR) // Use long response length encoding.
  165. {
  166. *p_raw_data++ = MSB_16(p_cmd_apdu->resp_len);
  167. *p_raw_data++ = LSB_16(p_cmd_apdu->resp_len);
  168. }
  169. else // Use short response length encoding.
  170. {
  171. if (p_cmd_apdu->resp_len == LE_LONG_FORMAT_THR)
  172. {
  173. *p_raw_data++ = LE_ENCODED_VAL_256;
  174. }
  175. else
  176. {
  177. *p_raw_data++ = LSB_16(p_cmd_apdu->resp_len);
  178. }
  179. }
  180. }
  181. return NRF_SUCCESS;
  182. }
  183. /**
  184. * @brief Function for validating arguments used by RAPDU decoding procedure.
  185. */
  186. __STATIC_INLINE ret_code_t nfc_t4t_resp_apdu_args_validate(nfc_t4t_resp_apdu_t const * const p_resp_apdu,
  187. uint8_t const * const p_raw_data,
  188. uint16_t len)
  189. {
  190. if ((p_resp_apdu == NULL) || (p_raw_data == NULL))
  191. {
  192. return NRF_ERROR_NULL;
  193. }
  194. if (len < STATUS_SIZE)
  195. {
  196. return NRF_ERROR_INVALID_LENGTH;
  197. }
  198. return NRF_SUCCESS;
  199. }
  200. ret_code_t nfc_t4t_resp_apdu_decode(nfc_t4t_resp_apdu_t * const p_resp_apdu,
  201. uint8_t const * const p_raw_data,
  202. uint16_t len)
  203. {
  204. // Validate passed arguments.
  205. ret_code_t err_code = nfc_t4t_resp_apdu_args_validate(p_resp_apdu, p_raw_data, len);
  206. VERIFY_SUCCESS(err_code);
  207. nfc_t4t_resp_apdu_clear(p_resp_apdu);
  208. if (len != STATUS_SIZE) // Optional data field is present in RAPDU.
  209. {
  210. p_resp_apdu->data.len = len - STATUS_SIZE;
  211. p_resp_apdu->data.p_buff = (uint8_t *) p_raw_data;
  212. }
  213. p_resp_apdu->status = uint16_big_decode(p_raw_data + p_resp_apdu->data.len);
  214. return NRF_SUCCESS;
  215. }
  216. void nfc_t4t_resp_apdu_printout(nfc_t4t_resp_apdu_t * p_resp_apdu)
  217. {
  218. NRF_LOG_INFO("R-APDU status: %4X ", p_resp_apdu->status);
  219. if (p_resp_apdu->data.p_buff != NULL)
  220. {
  221. NRF_LOG_INFO("R-APDU data: ");
  222. NRF_LOG_HEXDUMP_INFO(p_resp_apdu->data.p_buff, p_resp_apdu->data.len);
  223. }
  224. else
  225. {
  226. NRF_LOG_INFO("R-APDU no data field present.");
  227. }
  228. }
  229. #endif // NFC_T4T_APDU_ENABLED