pse_types.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _PSE_TYPES_H_
  32. #define _PSE_TYPES_H_
  33. #include "sgx_ecp_types.h"
  34. #include "sgx_report.h"
  35. #define SAFE_FREE(ptr) {if (NULL != (ptr)) {free(ptr); (ptr)=NULL;}}
  36. #define EPH_SESSION_NONCE_SIZE 16
  37. #define EPH_MESSAGE_MAC_SIZE 16
  38. #define CSE_ID_SIZE 32
  39. #define EPH_SESSION_TSK_SIZE 16
  40. #define EPH_SESSION_TMK_SIZE 16
  41. #define SW_INSTANCE_ID_SIZE 16
  42. #define DERIVE_MAC_KEY 0x0
  43. #define DERIVE_SEAL_KEY 0x1
  44. #pragma pack(push, 1)
  45. typedef struct _pse_dh_msg1_t
  46. {
  47. sgx_ec256_public_t dh_ga; /* the Endian-ness of Ga is Little-Endian*/
  48. sgx_target_info_t pse_info;
  49. }pse_dh_msg1_t;
  50. typedef struct _cse_sec_prop_t
  51. {
  52. uint32_t sec_info_type; /* MBZ */
  53. uint32_t gid_cse; /* from PSE-CSE pairing blob */
  54. uint32_t prvrl_version; /* from PSE-CSE pairing blob */
  55. uint32_t sigrl_version; /* from PSE-CSE pairing blob */
  56. uint8_t ca_id_cse[20]; /* from PSE-CSE pairing blob */
  57. uint8_t sec_info[92]; /* from PSE-CSE pairing blob */
  58. }cse_sec_prop_t;
  59. typedef struct _secu_info{
  60. uint32_t jom_task_id; /* must be the hardcoded value - 8 */
  61. uint32_t reserved; /* MBZ */
  62. uint32_t psda_id; /* must be hardcoded value - 1 */
  63. uint32_t psda_svn; /* from PSE-CSE pairing blob */
  64. uint8_t reserved2[76]; /* MBZ */
  65. }secu_info_t;
  66. typedef struct _pse_dh_msg3_t
  67. {
  68. uint8_t cmac[EPH_MESSAGE_MAC_SIZE];
  69. sgx_report_t report;
  70. uint32_t additional_prop_length;
  71. cse_sec_prop_t cse_sec_prop;
  72. }pse_dh_msg3_t;
  73. /***********************\
  74. **message handling data**
  75. \***********************/
  76. #define PSE_TRUSTED_TIME_SERVICE 0
  77. #define PSE_MC_SERVICE 1
  78. /*monotonic counter*/
  79. #define PSE_MC_CREATE 0
  80. #define PSE_MC_READ 1
  81. #define PSE_MC_INC 2
  82. #define PSE_MC_DEL 3
  83. /*trusted time*/
  84. #define PSE_TIMER_READ 0
  85. /*VMC creation policy*/
  86. #define MC_POLICY_SIGNER 0x1
  87. #define MC_POLICY_ENCLAVE 0x2
  88. #define PAYLOAD_IV_SIZE 12
  89. typedef struct _pse_message_t
  90. {
  91. uint32_t session_id;
  92. uint32_t exp_resp_size; /* 0: response message*/
  93. uint8_t payload_iv[PAYLOAD_IV_SIZE];
  94. uint32_t payload_size;
  95. uint8_t payload_tag[SGX_AESGCM_MAC_SIZE]; /* 16: AES-GMAC of the Plain Text, Payload, and the sizes*/
  96. uint8_t payload[0]; /* encrypted payload*/
  97. }pse_message_t;
  98. typedef enum _pse_op_error_t
  99. {
  100. OP_SUCCESS = 0,
  101. OP_ERROR_INTERNAL, /* Internal errors */
  102. OP_ERROR_INVALID_PARAMETER, /* Invalid input parameter */
  103. OP_ERROR_MALLOC, /* malloc() fails */
  104. OP_ERROR_UNKNOWN_REQUEST, /* Unknown request sent to CSE */
  105. OP_ERROR_CAP_NOT_AVAILABLE, /* The required service is not available */
  106. OP_ERROR_MAX_NUM_SESSION_REACHED, /* All session slots are in use and
  107. the least active session does not exceed predefined time */
  108. OP_ERROR_INVALID_SESSION, /* Create APP Enclave - PSE session failed
  109. or the session indicated by SID is invalid */
  110. OP_ERROR_DATABASE_FULL, /* No empty vmc nodes left in VMC DB */
  111. OP_ERROR_DATABASE_OVER_QUOTA, /* The quota for the MRSIGNER is exceeded */
  112. OP_ERROR_INVALID_EPH_SESSION, /* Ephemeral session is not valid or sequence no overflows.
  113. Need do repairing. */
  114. OP_ERROR_PSDA_SESSION_LOST, /* CSME session is lost during OCALL */
  115. OP_ERROR_PSDA_BUSY, /* CSME is busy */
  116. OP_ERROR_EPH_SESSION_ESTABLISHMENT_INTEGRITY_ERROR, /* Integrity error of ephemeral session message */
  117. OP_ERROR_SQLITE_INTERNAL, /* SQLite internal errors */
  118. OP_ERROR_COPY_PREBUILD_DB, /* Copy of prebuilt DB failed */
  119. OP_ERROR_BACKUP_CURRENT_DB, /* Copy of current DB failed */
  120. OP_ERROR_INVALID_HW_MC, /* CC_MC > WR_MC or WR_MC > CC_MC + 2 */
  121. OP_ERROR_INVALID_COUNTER, /* The VMC counter ID passed in is not valid */
  122. OP_ERROR_INVALID_OWNER, /* owner ID or isv_svn doesn't match */
  123. OP_ERROR_UNSEAL_PAIRING_BLOB, /* Unsealing LT pairing blob failed */
  124. OP_ERROR_INVALID_POLICY, /* Invalid owner policy */
  125. OP_ERROR_INVALID_PAIRING_BLOB, /* LT pairing blob is invalid */
  126. /* Errors for internal use. Won't be returned to AESM*/
  127. OP_ERROR_INVALID_VMC_DB, /* Verification of VMC DB failed. Should re-initialize DB */
  128. OP_ERROR_DATABASE_FATAL, /* Fatal error returned when opening VMC DB, Should re-initialize DB */
  129. OP_ERROR_SQLITE_NOT_FOUND, /* Record not found. */
  130. OP_ERROR_CACHE_MISS, /* The related nodes of a leaf node are not cached */
  131. OP_ERROR_KDF_MISMATCH, /* Key derivation function doesn't match during exchange report */
  132. OP_ERROR_LTPB_SEALING_OUT_OF_DATE, /* The ISV SVN in the LTP blob doesn't match PSE ISV SVN */
  133. }pse_op_error_t;
  134. typedef enum _pse_service_resp_status_t
  135. {
  136. PSE_SUCCESS = 0,
  137. PSE_ERROR_UNKNOWN_REQ,
  138. PSE_ERROR_CAP_NOT_AVAILABLE,
  139. PSE_ERROR_INVALID_PARAM,
  140. PSE_ERROR_BUSY,
  141. PSE_ERROR_INTERNAL,
  142. PSE_ERROR_INVALID_POLICY,
  143. PSE_ERROR_QUOTA_EXCEEDED,
  144. PSE_ERROR_MC_NOT_FOUND,
  145. PSE_ERROR_MC_NO_ACCESS_RIGHT,
  146. PSE_ERROR_MC_USED_UP,
  147. PSE_ERROR_MC_OVER_QUOTA
  148. } pse_service_resp_status_t;
  149. typedef struct _pse_req_hdr_t
  150. {
  151. uint32_t seq_num;
  152. uint16_t service_id;
  153. uint16_t service_cmd;
  154. }pse_req_hdr_t;
  155. typedef struct _pse_resp_hdr_t
  156. {
  157. uint32_t seq_num;
  158. uint16_t service_id;
  159. uint16_t service_cmd;
  160. pse_service_resp_status_t status;
  161. }pse_resp_hdr_t;
  162. typedef struct _pse_mc_create_req_t
  163. {
  164. pse_req_hdr_t req_hdr;
  165. uint16_t policy;
  166. uint8_t attr_mask[16];
  167. }pse_mc_create_req_t;
  168. typedef struct _pse_mc_create_resp_t
  169. {
  170. pse_resp_hdr_t resp_hdr;
  171. uint8_t counter_id[3];
  172. uint8_t nonce[13];
  173. }pse_mc_create_resp_t;
  174. typedef struct _pse_mc_read_req_t
  175. {
  176. pse_req_hdr_t req_hdr;
  177. uint8_t counter_id[3];
  178. uint8_t nonce[13];
  179. }pse_mc_read_req_t;
  180. typedef struct _pse_mc_inc_req_t
  181. {
  182. pse_req_hdr_t req_hdr;
  183. uint8_t counter_id[3];
  184. uint8_t nonce[13];
  185. }pse_mc_inc_req_t;
  186. typedef struct _pse_mc_del_req_t
  187. {
  188. pse_req_hdr_t req_hdr;
  189. uint8_t counter_id[3];
  190. uint8_t nonce[13];
  191. }pse_mc_del_req_t;
  192. typedef struct _pse_mc_read_resp_t
  193. {
  194. pse_resp_hdr_t resp_hdr;
  195. uint32_t counter_value;
  196. uint16_t pse_svn;
  197. }pse_mc_read_resp_t;
  198. typedef struct _pse_mc_inc_resp_t
  199. {
  200. pse_resp_hdr_t resp_hdr;
  201. uint32_t counter_value;
  202. uint16_t pse_svn;
  203. }pse_mc_inc_resp_t;
  204. typedef struct _pse_mc_del_resp_t
  205. {
  206. pse_resp_hdr_t resp_hdr;
  207. }pse_mc_del_resp_t;
  208. typedef struct _pse_timer_read_req_t
  209. {
  210. pse_req_hdr_t req_hdr;
  211. }pse_timer_read_req_t;
  212. typedef struct _pse_timer_read_resp_t
  213. {
  214. pse_resp_hdr_t resp_hdr;
  215. uint64_t timestamp;
  216. uint8_t time_source_nonce[32];
  217. }pse_timer_read_resp_t;
  218. /*message length*/
  219. #define PSE_CREATE_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_create_req_t))
  220. #define PSE_CREATE_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_create_resp_t))
  221. #define PSE_READ_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_read_req_t))
  222. #define PSE_READ_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_read_resp_t))
  223. #define PSE_INC_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_inc_req_t))
  224. #define PSE_INC_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_inc_resp_t))
  225. #define PSE_DEL_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_del_req_t))
  226. #define PSE_DEL_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_del_resp_t))
  227. #define PSE_TIMER_READ_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_timer_read_req_t))
  228. #define PSE_TIMER_READ_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_timer_read_resp_t))
  229. /*********************************************\
  230. ** Define macros for CSE session and messages**
  231. \*********************************************/
  232. #define PSDA_API_VERSION 1
  233. #define BE_PSDA_API_VERSION 0x01000000
  234. #define PSDA_COMMAND_INFO 0
  235. #define PSDA_COMMAND_EP 2
  236. #define PSDA_COMMAND_SERVICE 3
  237. #define PSDA_MSG_TYPE_CAP_QUERY 0
  238. #define PSDA_MSG_TYPE_CAP_RESULT 1
  239. #define PSDA_MSG_TYPE_CERT_INFO_QUERY 2
  240. #define PSDA_MSG_TYPE_CERT_INFO_RESULT 3
  241. #define PSDA_MSG_TYPE_LT_M1 0
  242. #define PSDA_MSG_TYPE_LT_M2 1
  243. #define PSDA_MSG_TYPE_LT_M7 2
  244. #define PSDA_MSG_TYPE_LT_M8 3
  245. #define PSDA_MSG_TYPE_EP_M1 0
  246. #define PSDA_MSG_TYPE_EP_M2 1
  247. #define PSDA_MSG_TYPE_EP_M3 2
  248. #define PSDA_MSG_TYPE_EP_M4 3
  249. #define PSDA_MSG_TYPE_SERV_REQ 0
  250. #define PSDA_MSG_TYPE_SERV_RESP 1
  251. #define BE_PSDA_MSG_TYPE_SERV_REQ 0x00000000
  252. #define BE_PSDA_MSG_TYPE_SERV_RESP 0x01000000
  253. typedef struct _psda_msg_hdr_t
  254. {
  255. uint8_t pse_instance_id[SW_INSTANCE_ID_SIZE];
  256. uint32_t msg_type;
  257. uint32_t msg_len;
  258. }psda_msg_hdr_t;
  259. typedef struct _psda_info_query_msg_t
  260. {
  261. psda_msg_hdr_t msg_hdr;
  262. }psda_info_query_msg_t;
  263. typedef struct _psda_cap_result_msg_t
  264. {
  265. psda_msg_hdr_t msg_hdr;
  266. uint32_t cap_descriptor_version;
  267. uint32_t cap_descriptor0;
  268. uint32_t cap_descriptor1;
  269. }psda_cap_result_msg_t;
  270. typedef struct _psda_cert_result_msg_t
  271. {
  272. psda_msg_hdr_t msg_hdr;
  273. uint8_t cert_info[24];
  274. }psda_cert_result_msg_t;
  275. /* messages used for pse-cse ephemeral session establishment */
  276. typedef struct _pse_cse_msg2_t
  277. {
  278. uint8_t id_cse[CSE_ID_SIZE];
  279. uint8_t nonce_r_cse[EPH_SESSION_NONCE_SIZE];
  280. }pse_cse_msg2_t;
  281. typedef struct _pse_cse_msg3_t
  282. {
  283. uint8_t id_pse[CSE_ID_SIZE];
  284. uint8_t id_cse[CSE_ID_SIZE];
  285. uint8_t nonce_r_cse[EPH_SESSION_NONCE_SIZE];
  286. uint8_t nonce_r_pse[EPH_SESSION_NONCE_SIZE];
  287. uint8_t mac[SGX_SHA256_HASH_SIZE];
  288. }pse_cse_msg3_t;
  289. typedef struct _pse_cse_msg4_t
  290. {
  291. uint8_t id_cse[CSE_ID_SIZE];
  292. uint8_t nonce_r_pse[EPH_SESSION_NONCE_SIZE];
  293. uint8_t mac[SGX_SHA256_HASH_SIZE];
  294. }pse_cse_msg4_t;
  295. typedef struct _eph_session_m1_t
  296. {
  297. psda_msg_hdr_t msg_hdr;
  298. }eph_session_m1_t;
  299. typedef struct _eph_session_m2_t
  300. {
  301. psda_msg_hdr_t msg_hdr;
  302. pse_cse_msg2_t msg2;
  303. }eph_session_m2_t;
  304. typedef struct _eph_session_m3_t
  305. {
  306. psda_msg_hdr_t msg_hdr;
  307. pse_cse_msg3_t msg3;
  308. }eph_session_m3_t;
  309. typedef struct _eph_session_m4_t
  310. {
  311. psda_msg_hdr_t msg_hdr;
  312. pse_cse_msg4_t msg4;
  313. }eph_session_m4_t;
  314. /*********************************\
  315. **PSDA service message definition**
  316. \*********************************/
  317. #define PSDA_MC_READ 1 /* Read MC command*/
  318. #define PSDA_MC_INC 2 /* Incroment MC command*/
  319. #define PSDA_MESSAGE_IV_SIZE 16 /* IV size*/
  320. #define PSDA_MESSAGE_MAC_SIZE 32 /* MAC size*/
  321. #define SGX_RPDATA_SIZE 16 /* RPDATA size*/
  322. typedef struct _service_message_t
  323. {
  324. uint32_t version;
  325. uint32_t session_id;
  326. uint32_t msg_type_exp_resp_size;
  327. uint32_t payload_size;
  328. uint8_t payload_iv[PSDA_MESSAGE_IV_SIZE];
  329. uint8_t payload_mac[PSDA_MESSAGE_MAC_SIZE];
  330. uint8_t payload[0]; /*encrypted payload*/
  331. }service_message_t;
  332. typedef struct _psda_service_message_t
  333. {
  334. psda_msg_hdr_t msg_hdr;
  335. service_message_t service_message;
  336. }psda_service_message_t;
  337. typedef struct _psda_req_hdr_t
  338. {
  339. uint32_t seqnum;
  340. uint16_t service_id;
  341. uint16_t service_cmd;
  342. }psda_req_hdr_t;
  343. typedef struct _psda_resp_hdr_t
  344. {
  345. uint32_t seqnum;
  346. uint16_t service_id;
  347. uint16_t service_cmd;
  348. uint32_t status;
  349. }psda_resp_hdr_t;
  350. typedef struct _cse_mc_read_req_t
  351. {
  352. psda_req_hdr_t req_hdr;
  353. uint8_t counter_id;
  354. }cse_mc_read_req_t;
  355. typedef struct _cse_mc_inc_req_t
  356. {
  357. psda_req_hdr_t req_hdr;
  358. uint8_t counter_id;
  359. uint8_t increase_amount;
  360. }cse_mc_inc_req_t;
  361. typedef struct _cse_mc_resp_t
  362. {
  363. psda_resp_hdr_t resp_hdr;
  364. uint32_t counter_value;
  365. uint32_t mc_epoch;
  366. }cse_mc_resp_t;
  367. typedef struct _cse_rpdata_read_req_t
  368. {
  369. psda_req_hdr_t req_hdr;
  370. }cse_rpdata_read_req_t;
  371. typedef struct _cse_rpdata_update_req_t
  372. {
  373. psda_req_hdr_t req_hdr;
  374. uint8_t rpdata_cur[SGX_RPDATA_SIZE];
  375. uint8_t rpdata_new[SGX_RPDATA_SIZE];
  376. }cse_rpdata_update_req_t;
  377. typedef struct _cse_rpdata_reset_req_t
  378. {
  379. psda_req_hdr_t req_hdr;
  380. uint8_t rpdata_cur[SGX_RPDATA_SIZE];
  381. }cse_rpdata_reset_req_t;
  382. typedef struct _cse_rpdata_resp_t
  383. {
  384. psda_resp_hdr_t resp_hdr;
  385. uint8_t rpdata[SGX_RPDATA_SIZE];
  386. uint32_t rp_epoch;
  387. }cse_rpdata_resp_t;
  388. typedef struct _cse_timer_read_req_t
  389. {
  390. psda_req_hdr_t req_hdr;
  391. }cse_timer_read_req_t;
  392. typedef struct _cse_timer_read_resp_t
  393. {
  394. psda_resp_hdr_t resp_hdr;
  395. uint64_t timestamp;
  396. uint32_t epoch;
  397. }cse_timer_read_resp_t;
  398. /* Because PSDA requires buffer size to be a multiple of AES_BLOCK_SIZE, add an extra AES_BLOCK_SIZE here
  399. * to make sure response message can be stored in the buffer*/
  400. #define AES_BLOCK_SIZE 16
  401. #pragma pack(pop)
  402. #endif