optiga_util.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. * \file
  25. *
  26. * \brief This file implements
  27. *
  28. * \addtogroup grOptigaUtil
  29. * @{
  30. */
  31. #include "optiga/optiga_util.h"
  32. #include "optiga/comms/optiga_comms.h"
  33. #include "optiga/cmd/CommandLib.h"
  34. #include "optiga/pal/pal_os_timer.h"
  35. ///Length of metadata
  36. #define LENGTH_METADATA 0x1C
  37. volatile static host_lib_status_t optiga_comms_status;
  38. #ifdef MODULE_ENABLE_READ_WRITE
  39. static void __optiga_util_comms_event_handler(void* upper_layer_ctx, host_lib_status_t event)
  40. {
  41. optiga_comms_status = event;
  42. }
  43. optiga_lib_status_t optiga_util_open_application(optiga_comms_t* p_comms)
  44. {
  45. optiga_lib_status_t status = OPTIGA_LIB_ERROR;
  46. sOpenApp_d sOpenApp;
  47. do {
  48. // OPTIGA(TM) Initialization phase
  49. //Invoke optiga_comms_open to initialize the IFX I2C Protocol and security chip
  50. optiga_comms_status = OPTIGA_COMMS_BUSY;
  51. p_comms->upper_layer_handler = __optiga_util_comms_event_handler;
  52. status = optiga_comms_open(p_comms);
  53. if(E_COMMS_SUCCESS != status)
  54. {
  55. status = OPTIGA_LIB_ERROR;
  56. break;
  57. }
  58. //Wait until IFX I2C initialization is complete
  59. while(optiga_comms_status == OPTIGA_COMMS_BUSY)
  60. {
  61. pal_os_timer_delay_in_milliseconds(1);
  62. }
  63. if((OPTIGA_COMMS_SUCCESS != status) || (optiga_comms_status == OPTIGA_COMMS_ERROR))
  64. {
  65. status = OPTIGA_LIB_ERROR;
  66. break;
  67. }
  68. //Set OPTIGA comms context in Command library before invoking the use case APIs or command library APIs
  69. //This context will be used by command library to communicate with OPTIGA using IFX I2C Protocol.
  70. CmdLib_SetOptigaCommsContext(p_comms);
  71. //Open the application in Security Chip
  72. sOpenApp.eOpenType = eInit;
  73. status = CmdLib_OpenApplication(&sOpenApp);
  74. if(CMD_LIB_OK == status)
  75. {
  76. status = OPTIGA_LIB_SUCCESS;
  77. }
  78. } while(FALSE);
  79. return status;
  80. }
  81. optiga_lib_status_t optiga_util_read_data(uint16_t optiga_oid, uint16_t offset,
  82. uint8_t * p_buffer, uint16_t* buffer_size)
  83. {
  84. int32_t status = (int32_t)OPTIGA_LIB_ERROR;
  85. sGetData_d cmd_params;
  86. sCmdResponse_d cmd_resp;
  87. do
  88. {
  89. if((NULL == p_buffer) || (NULL == buffer_size) || (0 == *buffer_size))
  90. {
  91. status = (int32_t)OPTIGA_LIB_ERROR;
  92. break;
  93. }
  94. cmd_params.wOID = optiga_oid;
  95. cmd_params.wLength = *buffer_size;
  96. cmd_params.wOffset = offset;
  97. cmd_params.eDataOrMdata = eDATA;
  98. cmd_resp.prgbBuffer = p_buffer;
  99. cmd_resp.wBufferLength = *buffer_size;
  100. cmd_resp.wRespLength = 0;
  101. status = CmdLib_GetDataObject(&cmd_params,&cmd_resp);
  102. if(CMD_LIB_OK != status)
  103. {
  104. break;
  105. }
  106. *buffer_size = cmd_resp.wRespLength;
  107. status = OPTIGA_LIB_SUCCESS;
  108. }while(FALSE);
  109. return status;
  110. }
  111. optiga_lib_status_t optiga_util_read_metadata(uint16_t optiga_oid, uint8_t * p_buffer, uint16_t* buffer_size)
  112. {
  113. int32_t status = (int32_t)OPTIGA_LIB_ERROR;
  114. sGetData_d cmd_params;
  115. sCmdResponse_d cmd_resp;
  116. uint16_t buffer_limit = *buffer_size;
  117. do
  118. {
  119. if((NULL == p_buffer) || (NULL == buffer_size) || (0 == *buffer_size))
  120. {
  121. status = (int32_t)OPTIGA_LIB_ERROR;
  122. break;
  123. }
  124. //Get metadata of OID
  125. cmd_params.wOID = optiga_oid;
  126. cmd_params.wLength = LENGTH_METADATA;
  127. cmd_params.wOffset = 0;
  128. cmd_params.eDataOrMdata = eMETA_DATA;
  129. cmd_resp.prgbBuffer = p_buffer;
  130. cmd_resp.wBufferLength = buffer_limit;
  131. cmd_resp.wRespLength = 0;
  132. status = CmdLib_GetDataObject(&cmd_params,&cmd_resp);
  133. if(CMD_LIB_OK != status)
  134. {
  135. break;
  136. }
  137. *buffer_size = cmd_resp.wRespLength;
  138. status = OPTIGA_LIB_SUCCESS;
  139. }while(FALSE);
  140. return status;
  141. }
  142. optiga_lib_status_t optiga_util_write_data(uint16_t optiga_oid, uint8_t write_type, uint16_t offset, uint8_t * p_buffer, uint16_t buffer_size)
  143. {
  144. int32_t status = (int32_t)OPTIGA_LIB_ERROR;
  145. sSetData_d sd_params;
  146. do
  147. {
  148. if((NULL == p_buffer) || (0x00 == buffer_size))
  149. {
  150. break;
  151. }
  152. if ((OPTIGA_UTIL_WRITE_ONLY != write_type) && (OPTIGA_UTIL_ERASE_AND_WRITE != write_type))
  153. {
  154. status = OPTIGA_UTIL_ERROR_INVALID_INPUT;
  155. break;
  156. }
  157. sd_params.wOID = optiga_oid;
  158. sd_params.wOffset = offset;
  159. sd_params.eDataOrMdata = eDATA;
  160. if (OPTIGA_UTIL_ERASE_AND_WRITE == write_type)
  161. {
  162. sd_params.eWriteOption = eERASE_AND_WRITE;
  163. }
  164. else
  165. {
  166. sd_params.eWriteOption = eWRITE;
  167. }
  168. sd_params.prgbData = p_buffer;
  169. sd_params.wLength = buffer_size;
  170. status = CmdLib_SetDataObject(&sd_params);
  171. if(CMD_LIB_OK != status)
  172. {
  173. break;
  174. }
  175. status = OPTIGA_LIB_SUCCESS;
  176. }while(FALSE);
  177. return status;
  178. }
  179. optiga_lib_status_t optiga_util_write_metadata(uint16_t optiga_oid, uint8_t * p_buffer, uint8_t buffer_size)
  180. {
  181. int32_t status = (int32_t)OPTIGA_LIB_ERROR;
  182. sSetData_d sd_params;
  183. sd_params.wOID = optiga_oid;
  184. sd_params.wOffset = 0;
  185. sd_params.eDataOrMdata = eMETA_DATA;
  186. sd_params.eWriteOption = eWRITE;
  187. sd_params.prgbData = p_buffer;
  188. sd_params.wLength = buffer_size;
  189. status = CmdLib_SetDataObject(&sd_params);
  190. if(CMD_LIB_OK != status)
  191. {
  192. return status;
  193. }
  194. return OPTIGA_LIB_SUCCESS;
  195. }
  196. #endif // MODULE_ENABLE_READ_WRITE