simlock.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _SIM_LOCK_H_
  13. #define _SIM_LOCK_H_
  14. #include "simlock_type.h"
  15. #include "simlock_storage.h"
  16. #include "rsa.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. //#define SIMLOCK_PROTECT_DATA
  21. #ifdef SIMLOCK_PROTECT_DATA
  22. #define SIMLOCK_ERR_INIT_MALLOC_RF (0x00000001)
  23. #define SIMLOCK_ERR_INIT_RF (0x00000002)
  24. #define SIMLOCK_ERR_INIT_BT (0x00000004)
  25. #define SIMLOCK_ERR_INIT_IMEI (0x00000008)
  26. #define SIMLOCK_ERR_INIT_IMEI2 (0x00000010)
  27. #define SIMLOCK_ERR_READ_PRO (0x00000020)
  28. #define SIMLOCK_ERR_WRITE_PRO (0x00000040)
  29. #define SIMLOCK_ERR_SHA_GET (0x00000080)
  30. #define SIMLOCK_ERR_SIGN_DES (0x00000100)
  31. #define SIMLOCK_ERR_SIGN_GET (0x00000200)
  32. #define SIMLOCK_ERR_SIGN_VERI (0x00000400)
  33. #define SIMLOCK_ERR_DUMM_GET (0x00000800)
  34. #define SIMLOCK_ERR_IMEI_GET (0x00001000)
  35. #define SIMLOCK_ERR_SIMC_GET (0x00002000)
  36. #define SIMLOCK_ERR_KEY_GET (0x00004000)
  37. #define SIMLOCK_ERR_USER_GET (0x00008000)
  38. #define SIMLOCK_ERR_SIZE_GET (0x00010000)
  39. #define SIMLOCK_ERR_READ_REV (0x00020000)
  40. #define SIMLOCK_ERR_WRITE_REV (0x00040000)
  41. #define SIMLOCK_ERR_RESTORE_RF_ALOC (0x00080000) // restore fail
  42. #define SIMLOCK_ERR_RESTORE_BT (0x00100000)
  43. #define SIMLOCK_ERR_RESTORE_IMEI1 (0x00200000)
  44. #define SIMLOCK_ERR_RESTORE_IMEI2 (0x00400000)
  45. #define SIMLOCK_ERR_RESTORE_RF (0x00800000)
  46. #define SIMLOCK_ERR_INIT_ERR (SIMLOCK_ERR_INIT_MALLOC_RF | SIMLOCK_ERR_INIT_RF | \
  47. SIMLOCK_ERR_INIT_BT | SIMLOCK_ERR_INIT_IMEI | SIMLOCK_ERR_INIT_IMEI2)
  48. #define SIMLOCK_ERR_RESTORE_SIM (SIMLOCK_ERR_RESTORE_IMEI1 | SIMLOCK_ERR_RESTORE_IMEI2 | SIMLOCK_ERR_RESTORE_RF | SIMLOCK_ERR_RESTORE_RF_ALOC)
  49. #endif
  50. typedef enum _SIMLOCK_ERROR_TYPE
  51. {
  52. SIMLOCK_SUCCESS = 0,
  53. SIMLOCK_ERROR = 1,
  54. SIMLOCK_READ_DATA_ERROR = 2,
  55. SIMLOCK_WRITE_DATA_ERROR = 3,
  56. SIMLOCK_READ_DATASIZE_ERROR = 4,
  57. SIMLOCK_WRITE_DUMMY_ERROR = 5,
  58. SIMLOCK_READ_DUMMY_ERROR = 6,
  59. SIMLOCK_WRITE_USERDATA_ERROR,
  60. SIMLOCK_READ_USERDATA_ERROR,
  61. SIMLOCK_READ_HEADER_ERROR,
  62. SIMLOCK_READ_PUBLICKEY_ERROR,
  63. SIMLOCK_WRITE_PUBLICKEY_ERROR,
  64. SIMLOCK_READ_SIGNATURE_ERROR,
  65. SIMLOCK_WRITE_SIGNATURE_ERROR,
  66. SIMLOCK_READ_KEYS_ERROR,
  67. SIMLOCK_WRITE_KEYS_ERROR,
  68. SIMLOCK_READ_UID_ERROR,
  69. SIMLOCK_SHA1_ERROR,
  70. SIMLOCK_DATA_VERIFY_ERROR,
  71. #ifdef SIMLOCK_PROTECT_DATA
  72. SIMLOCK_READ_PROTECT_ERROR,
  73. SIMLOCK_WRITE_PROTECT_ERROR,
  74. SIMLOCK_READ_RF_REVERT_ERROR,
  75. SIMLOCK_PRE_RF_REVERT_ERROR, // prepare rf data for write
  76. SIMLOCK_WRITE_RF_REVERT_ERROR,
  77. SIMLOCK_READ_PROTECT_NV_ERROR,
  78. SIMLOCK_PROTECT_NV_ERROR,
  79. #endif
  80. SIMLOCK_DECRYPT_ERROR
  81. } SIMLOCK_ERROR_TYPE;
  82. /**
  83. * @brief Read simlock header from simlock partition.
  84. * @param simlock_header return simlock header.
  85. * @return
  86. * - 0 Read successfully
  87. * - OTHERS (>0) Fail to read
  88. */
  89. SIMLOCK_ERROR_TYPE Simlock_Readheader(SIMLOCK_HEADER *simlock_header);
  90. /**
  91. * @brief Get RSA publick key
  92. * @param public_key Return RSA public key.
  93. * @return:
  94. * - 0 Read successfully
  95. * - OTHERS (>0) Fail to read
  96. */
  97. SIMLOCK_ERROR_TYPE simlockReadPublicKey(PUBLIC_KEY *public_key);
  98. /**
  99. * @brief Write RSA publick key.
  100. * @param public_key The data of RSA public key.
  101. * @param size The size of public key.
  102. * @return:
  103. * - 0 Read successfully
  104. * - OTHERS (>0) Fail to read
  105. */
  106. SIMLOCK_ERROR_TYPE simlockWritePublicKey(PUBLIC_KEY *public_key, uint32_t size);
  107. /**
  108. * @brief Write the designed size of simlock dada to simlock partition
  109. * @param buf The data write to simlock partition.
  110. * @param size The size of data being written.
  111. * @return:
  112. * - 0 Read successfully
  113. * - OTHERS (>0) Fail to read
  114. */
  115. SIMLOCK_ERROR_TYPE simlockWriteDataAndSize(uint8_t *buf, uint32_t size);
  116. /**
  117. * @brief write encrypted signature simlock data
  118. * @param signature The singature of simlock data
  119. * @return:
  120. * - 0 Read successfully
  121. * - OTHERS (>0) Fail to read
  122. */
  123. SIMLOCK_ERROR_TYPE simlockWriteDataSignature(uint8_t *signature);
  124. /**
  125. * @brief write encrypted keys (pin/puk simlock password)
  126. * @param keys The keys of simlock.
  127. * @return:
  128. * - 0 Read successfully
  129. * - OTHERS (>0) Fail to read
  130. */
  131. SIMLOCK_ERROR_TYPE simlockWriteKeys(uint8_t *keys);
  132. /**
  133. * @brief Read Customize simlock data from simlock partition
  134. * @param simlock_customize_data Store the simlock data inlcude of network_locks and return.
  135. * note: dummy1, dummy2, dummy3, dummy4,
  136. * the first byte is password length store in dummy1.
  137. * the password of simlock_customize data store in the following word dummy1,dummy2 ...
  138. * @return:
  139. * - 0 Read successfully
  140. * - OTHERS (>0) Fail to read
  141. */
  142. SIMLOCK_ERROR_TYPE simlockReadCustomiseData(MN_SIM_LOCK_CUSTOMIZE_DATA_T *simlock_customize_data);
  143. /**
  144. * @brief Read Customize simlock data from simlock partition
  145. * @param simlock_user_data Store the simlock user locks and return.
  146. * @return:
  147. * - 0 Read successfully
  148. * - OTHERS (>0) Fail to read
  149. */
  150. SIMLOCK_ERROR_TYPE simlockReadUserData(MN_SIM_LOCK_USER_DATA_T *simlock_user_data);
  151. /**
  152. * @brief Read control key data from simlock partition
  153. * @param simlock_control_key Store the simlock control key and return.
  154. * @return:
  155. * - 0 Read successfully
  156. * - OTHERS (>0) Fail to read
  157. */
  158. SIMLOCK_ERROR_TYPE simlockReadControlkeyData(MN_SIM_LOCK_CONTROL_KEY_T *simlock_control_key);
  159. /**
  160. * @brief Write simlock encrypted dynamic data to simlock partition
  161. * @param enc_data The encypted dynamic data write to simlock partition.
  162. * @return:
  163. * - 0 Read successfully
  164. * - OTHERS (>0) Fail to write
  165. */
  166. SIMLOCK_ERROR_TYPE simlockWriteEncryptUserData(uint8_t *enc_data);
  167. /**
  168. * @brief Get simlock data hash
  169. * @param hash_id The hash algorithm selected,such as RSA_SHA1,RSA_SHA256,RSA_MD5.
  170. * @param hash_len The length of hash data.
  171. * @param shasum The hash data of simlock data.
  172. * @return:
  173. * - 0 Read successfully
  174. * - OTHERS (>0) Fail to read.
  175. */
  176. SIMLOCK_ERROR_TYPE simlockSHA(uint8_t hash_id, uint8_t hashlen, uint8_t *shasum);
  177. /**
  178. * @brief Write simlock encrypted dummy to simlock partition
  179. * @param dummy The dummy data write to simlock partition.
  180. * @param length The length of dummy data.
  181. * @return:
  182. * - 0 Write successfully
  183. * - OTHERS (>0) Fail to write.
  184. */
  185. SIMLOCK_ERROR_TYPE simlockWriteDummy(uint8_t *dummy, uint32_t length);
  186. /**
  187. * @brief Verify simlock data.
  188. * @return:
  189. * - true Verify successfully
  190. * - false Fail to verify.
  191. */
  192. bool simlockDataVerify(void);
  193. /**
  194. * @brief This function is used to update the user data.
  195. * @return:
  196. * - true Update successfully
  197. * - fail Fail to update.
  198. */
  199. bool simlockEncryptDataUpdate(void /* user_data*/); // the last step to protect the simlock status
  200. /**
  201. * @brief This function is used to check if the last updated keys from UI or NV can
  202. * match the encrypt keys, if match, the protocol stack can boot up
  203. * or else, the protocol stack will boot up restricted.
  204. * @param type The type of simlock key,such as nck, nsck, spck ,cck, pck.
  205. * @param key The data of key will be check.
  206. * @return:
  207. * - true Check successfully
  208. * - fail Fail to check.
  209. */
  210. bool simlockCheck(uint32_t type, SIM_LOCK_KEY_T *key);
  211. /**
  212. * @brief This function is used to get the uid of this chip.
  213. * @param uid Store the data of uid and return.
  214. * @return:
  215. * - 0 Read the uid successfully
  216. * - OTHERS (>0) Fail to get the uid.
  217. */
  218. uint32_t simlockGetUid(uint8_t *uid);
  219. /**
  220. * @brief Read simlock data size from simlock partition
  221. * @param length Store the length of user data and return.
  222. * @return:
  223. * - 0 Read the uid successfully
  224. * - OTHERS (>0) Fail to get the uid.
  225. */
  226. SIMLOCK_ERROR_TYPE simlockReadDataSize(uint32_t *length);
  227. /**
  228. * @brief Read simlock data size from simlock partition
  229. * @param buf Store the data of user data.
  230. * @param length Store the length of user data.
  231. * @return:
  232. * - 0 Read the uid successfully
  233. * - OTHERS (>0) Fail to get the uid.
  234. */
  235. SIMLOCK_ERROR_TYPE simlockReadData(uint8_t *buf, uint32_t length);
  236. /**
  237. * @brief Get the public key of simlock
  238. * @param rsa_ctx_ptr Store the data of simlock public key.
  239. * @return:
  240. * - 0 Fail to read.
  241. * - 1 Get successfully.
  242. */
  243. uint32_t simlockGetRsaPubkey(rsa_context *rsa_ctx_ptr);
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif /* _SIM_LOCK_H_*/