pse_types.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (C) 2011-2016 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_msssage_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. }pse_op_error_t;
  132. typedef enum _pse_service_resp_status_t
  133. {
  134. PSE_SUCCESS = 0,
  135. PSE_ERROR_UNKNOWN_REQ,
  136. PSE_ERROR_CAP_NOT_AVAILABLE,
  137. PSE_ERROR_INVALID_PARAM,
  138. PSE_ERROR_BUSY,
  139. PSE_ERROR_INTERNAL,
  140. PSE_ERROR_INVALID_POLICY,
  141. PSE_ERROR_QUOTA_EXCEEDED,
  142. PSE_ERROR_MC_NOT_FOUND,
  143. PSE_ERROR_MC_NO_ACCESS_RIGHT,
  144. PSE_ERROR_MC_USED_UP,
  145. PSE_ERROR_MC_OVER_QUOTA
  146. } pse_service_resp_status_t;
  147. typedef struct _pse_req_hdr_t
  148. {
  149. uint32_t seq_num;
  150. uint16_t service_id;
  151. uint16_t service_cmd;
  152. }pse_req_hdr_t;
  153. typedef struct _pse_resp_hdr_t
  154. {
  155. uint32_t seq_num;
  156. uint16_t service_id;
  157. uint16_t service_cmd;
  158. pse_service_resp_status_t status;
  159. }pse_resp_hdr_t;
  160. typedef struct _pse_mc_create_req_t
  161. {
  162. pse_req_hdr_t req_hdr;
  163. uint16_t policy;
  164. uint8_t attr_mask[16];
  165. }pse_mc_create_req_t;
  166. typedef struct _pse_mc_create_resp_t
  167. {
  168. pse_resp_hdr_t resp_hdr;
  169. uint8_t counter_id[3];
  170. uint8_t nonce[13];
  171. }pse_mc_create_resp_t;
  172. typedef struct _pse_mc_read_req_t
  173. {
  174. pse_req_hdr_t req_hdr;
  175. uint8_t counter_id[3];
  176. uint8_t nonce[13];
  177. }pse_mc_read_req_t;
  178. typedef struct _pse_mc_inc_req_t
  179. {
  180. pse_req_hdr_t req_hdr;
  181. uint8_t counter_id[3];
  182. uint8_t nonce[13];
  183. }pse_mc_inc_req_t;
  184. typedef struct _pse_mc_del_req_t
  185. {
  186. pse_req_hdr_t req_hdr;
  187. uint8_t counter_id[3];
  188. uint8_t nonce[13];
  189. }pse_mc_del_req_t;
  190. typedef struct _pse_mc_read_resp_t
  191. {
  192. pse_resp_hdr_t resp_hdr;
  193. uint32_t counter_value;
  194. uint16_t pse_svn;
  195. }pse_mc_read_resp_t;
  196. typedef struct _pse_mc_inc_resp_t
  197. {
  198. pse_resp_hdr_t resp_hdr;
  199. uint32_t counter_value;
  200. uint16_t pse_svn;
  201. }pse_mc_inc_resp_t;
  202. typedef struct _pse_mc_del_resp_t
  203. {
  204. pse_resp_hdr_t resp_hdr;
  205. }pse_mc_del_resp_t;
  206. typedef struct _pse_timer_read_req_t
  207. {
  208. pse_req_hdr_t req_hdr;
  209. }pse_timer_read_req_t;
  210. typedef struct _pse_timer_read_resp_t
  211. {
  212. pse_resp_hdr_t resp_hdr;
  213. uint64_t timestamp;
  214. uint8_t time_source_nonce[32];
  215. }pse_timer_read_resp_t;
  216. /*message length*/
  217. #define PSE_CREATE_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_create_req_t))
  218. #define PSE_CREATE_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_create_resp_t))
  219. #define PSE_READ_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_read_req_t))
  220. #define PSE_READ_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_read_resp_t))
  221. #define PSE_INC_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_inc_req_t))
  222. #define PSE_INC_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_inc_resp_t))
  223. #define PSE_DEL_MC_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_del_req_t))
  224. #define PSE_DEL_MC_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_mc_del_resp_t))
  225. #define PSE_TIMER_READ_REQ_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_timer_read_req_t))
  226. #define PSE_TIMER_READ_RESP_SIZE static_cast<uint32_t>(sizeof(pse_message_t) + sizeof(pse_timer_read_resp_t))
  227. /*********************************************\
  228. ** Define macros for CSE session and messages**
  229. \*********************************************/
  230. #define PSDA_API_VERSION 1
  231. #define BE_PSDA_API_VERSION 0x01000000
  232. #define PSDA_COMMAND_INFO 0
  233. #define PSDA_COMMAND_EP 2
  234. #define PSDA_COMMAND_SERVICE 3
  235. #define PSDA_MSG_TYPE_CAP_QUERY 0
  236. #define PSDA_MSG_TYPE_CAP_RESULT 1
  237. #define PSDA_MSG_TYPE_CERT_INFO_QUERY 2
  238. #define PSDA_MSG_TYPE_CERT_INFO_RESULT 3
  239. #define PSDA_MSG_TYPE_LT_M1 0
  240. #define PSDA_MSG_TYPE_LT_M2 1
  241. #define PSDA_MSG_TYPE_LT_M7 2
  242. #define PSDA_MSG_TYPE_LT_M8 3
  243. #define PSDA_MSG_TYPE_EP_M1 0
  244. #define PSDA_MSG_TYPE_EP_M2 1
  245. #define PSDA_MSG_TYPE_EP_M3 2
  246. #define PSDA_MSG_TYPE_EP_M4 3
  247. #define PSDA_MSG_TYPE_SERV_REQ 0
  248. #define PSDA_MSG_TYPE_SERV_RESP 1
  249. #define BE_PSDA_MSG_TYPE_SERV_REQ 0x00000000
  250. #define BE_PSDA_MSG_TYPE_SERV_RESP 0x01000000
  251. typedef struct _psda_msg_hdr_t
  252. {
  253. uint8_t pse_instance_id[SW_INSTANCE_ID_SIZE];
  254. uint32_t msg_type;
  255. uint32_t msg_len;
  256. }psda_msg_hdr_t;
  257. typedef struct _psda_info_query_msg_t
  258. {
  259. psda_msg_hdr_t msg_hdr;
  260. }psda_info_query_msg_t;
  261. typedef struct _psda_cap_result_msg_t
  262. {
  263. psda_msg_hdr_t msg_hdr;
  264. uint32_t cap_descriptor_version;
  265. uint32_t cap_descriptor0;
  266. uint32_t cap_descriptor1;
  267. }psda_cap_result_msg_t;
  268. typedef struct _psda_cert_result_msg_t
  269. {
  270. psda_msg_hdr_t msg_hdr;
  271. uint8_t cert_info[24];
  272. }psda_cert_result_msg_t;
  273. /* messages used for pse-cse ephemeral session establishment */
  274. typedef struct _pse_cse_msg2_t
  275. {
  276. uint8_t id_cse[CSE_ID_SIZE];
  277. uint8_t nonce_r_cse[EPH_SESSION_NONCE_SIZE];
  278. }pse_cse_msg2_t;
  279. typedef struct _pse_cse_msg3_t
  280. {
  281. uint8_t id_pse[CSE_ID_SIZE];
  282. uint8_t id_cse[CSE_ID_SIZE];
  283. uint8_t nonce_r_cse[EPH_SESSION_NONCE_SIZE];
  284. uint8_t nonce_r_pse[EPH_SESSION_NONCE_SIZE];
  285. uint8_t mac[SGX_SHA256_HASH_SIZE];
  286. }pse_cse_msg3_t;
  287. typedef struct _pse_cse_msg4_t
  288. {
  289. uint8_t id_cse[CSE_ID_SIZE];
  290. uint8_t nonce_r_pse[EPH_SESSION_NONCE_SIZE];
  291. uint8_t mac[SGX_SHA256_HASH_SIZE];
  292. }pse_cse_msg4_t;
  293. typedef struct _eph_session_m1_t
  294. {
  295. psda_msg_hdr_t msg_hdr;
  296. }eph_session_m1_t;
  297. typedef struct _eph_session_m2_t
  298. {
  299. psda_msg_hdr_t msg_hdr;
  300. pse_cse_msg2_t msg2;
  301. }eph_session_m2_t;
  302. typedef struct _eph_session_m3_t
  303. {
  304. psda_msg_hdr_t msg_hdr;
  305. pse_cse_msg3_t msg3;
  306. }eph_session_m3_t;
  307. typedef struct _eph_session_m4_t
  308. {
  309. psda_msg_hdr_t msg_hdr;
  310. pse_cse_msg4_t msg4;
  311. }eph_session_m4_t;
  312. /*********************************\
  313. **PSDA service message definition**
  314. \*********************************/
  315. #define PSDA_MC_READ 1 /* Read MC command*/
  316. #define PSDA_MC_INC 2 /* Incroment MC command*/
  317. #define PSDA_MESSAGE_IV_SIZE 16 /* IV size*/
  318. #define PSDA_MESSAGE_MAC_SIZE 32 /* MAC size*/
  319. #define SGX_RPDATA_SIZE 16 /* RPDATA size*/
  320. typedef struct _service_message_t
  321. {
  322. uint32_t version;
  323. uint32_t session_id;
  324. uint32_t msg_type_exp_resp_size;
  325. uint32_t payload_size;
  326. uint8_t payload_iv[PSDA_MESSAGE_IV_SIZE];
  327. uint8_t payload_mac[PSDA_MESSAGE_MAC_SIZE];
  328. uint8_t payload[0]; /*encrypted payload*/
  329. }service_message_t;
  330. typedef struct _psda_service_message_t
  331. {
  332. psda_msg_hdr_t msg_hdr;
  333. service_message_t service_message;
  334. }psda_service_message_t;
  335. typedef struct _psda_req_hdr_t
  336. {
  337. uint32_t seqnum;
  338. uint16_t service_id;
  339. uint16_t service_cmd;
  340. }psda_req_hdr_t;
  341. typedef struct _psda_resp_hdr_t
  342. {
  343. uint32_t seqnum;
  344. uint16_t service_id;
  345. uint16_t service_cmd;
  346. uint32_t status;
  347. }psda_resp_hdr_t;
  348. typedef struct _cse_mc_read_req_t
  349. {
  350. psda_req_hdr_t req_hdr;
  351. uint8_t counter_id;
  352. }cse_mc_read_req_t;
  353. typedef struct _cse_mc_inc_req_t
  354. {
  355. psda_req_hdr_t req_hdr;
  356. uint8_t counter_id;
  357. uint8_t increase_amount;
  358. }cse_mc_inc_req_t;
  359. typedef struct _cse_mc_resp_t
  360. {
  361. psda_resp_hdr_t resp_hdr;
  362. uint32_t counter_value;
  363. uint32_t mc_epoch;
  364. }cse_mc_resp_t;
  365. typedef struct _cse_rpdata_read_req_t
  366. {
  367. psda_req_hdr_t req_hdr;
  368. }cse_rpdata_read_req_t;
  369. typedef struct _cse_rpdata_update_req_t
  370. {
  371. psda_req_hdr_t req_hdr;
  372. uint8_t rpdata_cur[SGX_RPDATA_SIZE];
  373. uint8_t rpdata_new[SGX_RPDATA_SIZE];
  374. }cse_rpdata_update_req_t;
  375. typedef struct _cse_rpdata_reset_req_t
  376. {
  377. psda_req_hdr_t req_hdr;
  378. uint8_t rpdata_cur[SGX_RPDATA_SIZE];
  379. }cse_rpdata_reset_req_t;
  380. typedef struct _cse_rpdata_resp_t
  381. {
  382. psda_resp_hdr_t resp_hdr;
  383. uint8_t rpdata[SGX_RPDATA_SIZE];
  384. uint32_t rp_epoch;
  385. }cse_rpdata_resp_t;
  386. typedef struct _cse_timer_read_req_t
  387. {
  388. psda_req_hdr_t req_hdr;
  389. }cse_timer_read_req_t;
  390. typedef struct _cse_timer_read_resp_t
  391. {
  392. psda_resp_hdr_t resp_hdr;
  393. uint64_t timestamp;
  394. uint32_t epoch;
  395. }cse_timer_read_resp_t;
  396. /* Because PSDA requires buffer size to be a multiple of AES_BLOCK_SIZE, add an extra AES_BLOCK_SIZE here
  397. * to make sure response message can be stored in the buffer*/
  398. #define AES_BLOCK_SIZE 16
  399. #pragma pack(pop)
  400. #endif