simlock_storage.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 SIMLOCK_STORAGE_H
  13. #define SIMLOCK_STORAGE_H
  14. #include "simlock_type.h"
  15. #ifdef SIMLOCK_PROTECT_DATA
  16. #define SIM_LOCK_PART_SIZE (30 * 1024)
  17. #else
  18. #define SIM_LOCK_PART_SIZE (6 * 1024)
  19. #endif
  20. #define SIMLOCK_STORAGE_DUMMY_LEN (32)
  21. // RSA public key information
  22. //#define SL_RSA1024_C
  23. #define SL_RSA2048_C
  24. #if defined(SL_RSA1024_C)
  25. #define SL_KEY_LEN (128)
  26. #elif defined(SL_RSA2048_C)
  27. #define SL_KEY_LEN (256)
  28. #endif
  29. #define SL_RSA_N_LEN (SL_KEY_LEN << 1)
  30. #define SL_RSA_E_LEN (6)
  31. #define SL_RSA_N_H_LEN (SL_RSA_N_LEN >> 1)
  32. #define SL_RSA_PK_LEN (SL_RSA_N_LEN + SL_RSA_E_LEN)
  33. #define SL_RSA_SIGN_LEN (SL_KEY_LEN)
  34. #define SL_RSA_KEY_LEN (SL_KEY_LEN)
  35. #define SIMLOCK_DATA_MAX_LEN (5 * 1024)
  36. #ifdef SIMLOCK_PROTECT_DATA
  37. #define SL_RESTORE_RF_LEN (24 * 1024) // equal to NVITEM_MAX_SIZE
  38. #endif
  39. typedef struct
  40. {
  41. uint8_t n[SL_RSA_N_LEN];
  42. uint8_t e[SL_RSA_E_LEN];
  43. uint8_t reserved[2];
  44. } PUBLIC_KEY;
  45. typedef struct
  46. {
  47. uint8_t storage_magic[4];
  48. uint16_t secure_version;
  49. uint16_t config_version;
  50. } SIMLOCK_HEADER;
  51. // Simlock key information
  52. typedef struct
  53. {
  54. uint8_t nck[SIM_LOCK_KEY_MAX_LEN];
  55. uint8_t nsck[SIM_LOCK_KEY_MAX_LEN];
  56. uint8_t spck[SIM_LOCK_KEY_MAX_LEN];
  57. uint8_t cck[SIM_LOCK_KEY_MAX_LEN];
  58. uint8_t pck[SIM_LOCK_KEY_MAX_LEN];
  59. } simlock_keyinfo_t;
  60. typedef struct
  61. {
  62. uint8_t nck[SIM_LOCK_ENCRYPTED_KEY_MAX_LEN];
  63. uint8_t nsck[SIM_LOCK_ENCRYPTED_KEY_MAX_LEN];
  64. uint8_t spck[SIM_LOCK_ENCRYPTED_KEY_MAX_LEN];
  65. uint8_t cck[SIM_LOCK_ENCRYPTED_KEY_MAX_LEN];
  66. uint8_t pck[SIM_LOCK_ENCRYPTED_KEY_MAX_LEN];
  67. } simlock_encrypt_keyinfo_t;
  68. typedef struct
  69. {
  70. simlock_keyinfo_t pin_keys;
  71. simlock_keyinfo_t puk_keys;
  72. } simlock_keys_t;
  73. typedef struct
  74. {
  75. simlock_encrypt_keyinfo_t pin_keys;
  76. simlock_encrypt_keyinfo_t puk_keys;
  77. } simlock_encrypt_keys_t;
  78. #ifdef SIMLOCK_PROTECT_DATA
  79. typedef struct
  80. {
  81. BT_NV_PARAM bt_addr; // 8
  82. uint16_t rf_crc; // 2
  83. uint8_t IMEI1[8]; // BCD 8
  84. uint8_t IMEI2[8]; // BCD 8
  85. #ifdef SIMLOCK_PROTECT_DATA_DEBUG_SCN
  86. uint8_t rf_debug[8]; // 2
  87. uint8_t debug[126];
  88. #else
  89. uint8_t reseve[134];
  90. #endif
  91. } simlock_protect_data_t; // 160 byte
  92. #endif
  93. typedef struct
  94. {
  95. SIMLOCK_HEADER HEADER;
  96. PUBLIC_KEY SIMLOCK_PUBLICKEY;
  97. uint8_t SIGNATURE[SL_RSA_SIGN_LEN]; /*aes encrypted RSA SIMLOCK signature*/
  98. simlock_encrypt_keys_t ENCRYPTED_KEYS;
  99. uint8_t ENCRYPTED_DUMMY[SIMLOCK_STORAGE_DUMMY_LEN]; /*AES encrypted of customise "password" with the structure: size+encrypted password*/
  100. uint8_t ENCRYPTED_USER_DATA[32]; //other encrypted customise config should follow the encrypted dummy when need to be added.
  101. uint32_t SIMLOCK_DATA_SIZE;
  102. uint8_t SIMLOCK_DATA[SIMLOCK_DATA_MAX_LEN];
  103. #ifdef SIMLOCK_PROTECT_DATA
  104. simlock_protect_data_t PROTECT_DATA;
  105. uint8_t RF_REVERT_DATA[SL_RESTORE_RF_LEN];
  106. #endif
  107. } SIMLOCK_STORAGE_STR;
  108. #define STOR_OFFS(stor_id) \
  109. ((uint32_t)((char *)&((SIMLOCK_STORAGE_STR *)0)->stor_id - (char *)0))
  110. #define STOR_SIZE(stor_id) \
  111. sizeof(((SIMLOCK_STORAGE_STR *)0)->stor_id)
  112. #define simlock_read(stor_id, buffer, offset, length) \
  113. simlockStorageRead(buffer, STOR_OFFS(stor_id), \
  114. STOR_SIZE(stor_id), offset, length)
  115. #define simlock_write(stor_id, buffer, offset, length) \
  116. simlockStorageWrite(buffer, STOR_OFFS(stor_id), \
  117. STOR_SIZE(stor_id), offset, length)
  118. /**
  119. * @brief Read the simlock data
  120. * @param buffer Store the output data read from simlock partition.
  121. * @param stor_offset The field offset storage.
  122. * @param stor_length The filed lenght.
  123. * @param offset The offset in the field.
  124. * @param length The length read from the field.
  125. * @return:
  126. * - true Read successfully.
  127. * - false Fail to read.
  128. */
  129. bool simlockStorageRead(void *buffer,
  130. uint32_t stor_offset, uint32_t stor_length,
  131. uint32_t offset, uint32_t length);
  132. /**
  133. * @brief Write the simlock data
  134. * @param buffer Store the input data write to simlock partition.
  135. * @param stor_offset The field offset storage.
  136. * @param stor_length The filed lenght.
  137. * @param offset The offset in the field.
  138. * @param length The length write to the field.
  139. * @return:
  140. * - true Read successfully.
  141. * - false Fail to read.
  142. */
  143. bool simlockStorageWrite(void *buffer,
  144. uint32_t stor_offset, uint32_t stor_length,
  145. uint32_t offset, uint32_t length);
  146. /**
  147. * @brief Dump the buffer data
  148. * @param buffer Store the dump data
  149. * @param len The length of dump data
  150. * @return:
  151. */
  152. void simlockDump(uint8_t *pbuf, uint32_t len);
  153. /**
  154. * @brief Init the storage of simlock
  155. * @return:
  156. */
  157. bool simlockStorageInit(void);
  158. #endif