arch.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 _SE_ARCH_H_
  32. #define _SE_ARCH_H_
  33. #include "inst.h"
  34. #include "se_types.h"
  35. #include "sgx_attributes.h"
  36. #include "sgx_key.h"
  37. #include "sgx_report.h"
  38. #include "sgx_tcrypto.h"
  39. #define SE_PAGE_SIZE 0x1000
  40. #define TCS_SIZE SE_PAGE_SIZE
  41. #pragma pack(push, 1)
  42. #if !defined(__cplusplus) || defined(__INTEL_COMPILER) || (defined(SE_GNU) && !defined(__GXX_EXPERIMENTAL_CXX0X__))
  43. #define _ASSERT_CONCAT(a, b) a##b
  44. #define ASSERT_CONCAT(a, b) _ASSERT_CONCAT(a, b)
  45. #define se_static_assert(e) typedef char ASSERT_CONCAT(assert_line, __LINE__)[(e)?1:-1]
  46. #else
  47. #define se_static_assert(e) static_assert(e,#e)
  48. #endif
  49. se_static_assert(sizeof(sgx_key_request_t) == 512);
  50. se_static_assert(sizeof(sgx_target_info_t) == 512);
  51. /*SECS data structure*/
  52. typedef struct _secs_t
  53. {
  54. uint64_t size; /* ( 0) Size of the enclave in bytes */
  55. PADDED_POINTER(void, base); /* ( 8) Base address of enclave */
  56. uint32_t ssa_frame_size; /* ( 16) size of 1 SSA frame in pages */
  57. sgx_misc_select_t misc_select; /* ( 20) Which fields defined in SSA.MISC */
  58. #define SECS_RESERVED1_LENGTH 24
  59. uint8_t reserved1[SECS_RESERVED1_LENGTH]; /* ( 24) reserved */
  60. sgx_attributes_t attributes; /* ( 48) ATTRIBUTES Flags Field */
  61. sgx_measurement_t mr_enclave; /* ( 64) Integrity Reg 0 - Enclave measurement */
  62. #define SECS_RESERVED2_LENGTH 32
  63. uint8_t reserved2[SECS_RESERVED2_LENGTH]; /* ( 96) reserved */
  64. sgx_measurement_t mr_signer; /* (128) Integrity Reg 1 - Enclave signing key */
  65. #define SECS_RESERVED3_LENGTH 96
  66. uint8_t reserved3[SECS_RESERVED3_LENGTH]; /* (160) reserved */
  67. sgx_prod_id_t isv_prod_id; /* (256) product ID of enclave */
  68. sgx_isv_svn_t isv_svn; /* (258) Security Version of the Enclave */
  69. #define SECS_RESERVED4_LENGTH 3836
  70. uint8_t reserved4[SECS_RESERVED4_LENGTH];/* (260) reserved */
  71. } secs_t;
  72. /*
  73. TCS
  74. flags definitions
  75. */
  76. #define DBGOPTIN 1 /* used by debugger */
  77. typedef struct _tcs_t
  78. {
  79. uint64_t reserved0; /* (0) */
  80. uint64_t flags; /* (8)bit 0: DBGOPTION */
  81. uint64_t ossa; /* (16)State Save Area */
  82. uint32_t cssa; /* (24)Current SSA slot */
  83. uint32_t nssa; /* (28)Number of SSA slots */
  84. uint64_t oentry; /* (32)Offset in enclave to which control is transferred on EENTER if enclave INACTIVE state */
  85. uint64_t reserved1; /* (40) */
  86. uint64_t ofs_base; /* (48)When added to the base address of the enclave, produces the base address FS segment inside the enclave */
  87. uint64_t ogs_base; /* (56)When added to the base address of the enclave, produces the base address GS segment inside the enclave */
  88. uint32_t ofs_limit; /* (64)Size to become the new FS limit in 32-bit mode */
  89. uint32_t ogs_limit; /* (68)Size to become the new GS limit in 32-bit mode */
  90. #define TCS_RESERVED_LENGTH 4024
  91. uint8_t reserved[TCS_RESERVED_LENGTH]; /* (72) */
  92. }tcs_t;
  93. se_static_assert(sizeof(tcs_t) == SE_PAGE_SIZE);
  94. /****************************************************************************
  95. * Definitions for SSA
  96. ****************************************************************************/
  97. typedef struct _exit_info_t
  98. {
  99. uint32_t vector:8; /* Exception number of exceptions reported inside enclave */
  100. uint32_t exit_type:3; /* 3: Hardware exceptions, 6: Software exceptions */
  101. uint32_t reserved:20;
  102. uint32_t valid:1; /* 0: unsupported exceptions, 1: Supported exceptions */
  103. } exit_info_t;
  104. #define SE_VECTOR_DE 0
  105. #define SE_VECTOR_DB 1
  106. #define SE_VECTOR_BP 3
  107. #define SE_VECTOR_BR 5
  108. #define SE_VECTOR_UD 6
  109. #define SE_VECTOR_MF 16
  110. #define SE_VECTOR_AC 17
  111. #define SE_VECTOR_XM 19
  112. typedef struct _ssa_gpr_t
  113. {
  114. REGISTER( ax); /* (0) */
  115. REGISTER( cx); /* (8) */
  116. REGISTER( dx); /* (16) */
  117. REGISTER( bx); /* (24) */
  118. REGISTER( sp); /* (32) */
  119. REGISTER( bp); /* (40) */
  120. REGISTER( si); /* (48) */
  121. REGISTER( di); /* (56) */
  122. uint64_t r8; /* (64) */
  123. uint64_t r9; /* (72) */
  124. uint64_t r10; /* (80) */
  125. uint64_t r11; /* (88) */
  126. uint64_t r12; /* (96) */
  127. uint64_t r13; /* (104) */
  128. uint64_t r14; /* (112) */
  129. uint64_t r15; /* (120) */
  130. REGISTER(flags); /* (128) */
  131. REGISTER( ip); /* (136) */
  132. REGISTER( sp_u); /* (144) untrusted stack pointer. saved by EENTER */
  133. REGISTER( bp_u); /* (152) untrusted frame pointer. saved by EENTER */
  134. exit_info_t exit_info; /* (160) contain information for exits */
  135. uint32_t reserved; /* (164) padding to multiple of 8 bytes */
  136. uint64_t fs; /* (168) FS register */
  137. uint64_t gs; /* (176) GS register */
  138. } ssa_gpr_t;
  139. typedef uint64_t si_flags_t;
  140. #define SI_FLAG_NONE 0x0
  141. #define SI_FLAG_R 0x1 /* Read Access */
  142. #define SI_FLAG_W 0x2 /* Write Access */
  143. #define SI_FLAG_X 0x4 /* Execute Access */
  144. #define SI_FLAG_PT_LOW_BIT 0x8 /* PT low bit */
  145. #define SI_FLAG_PT_MASK (0xFF<<SI_FLAG_PT_LOW_BIT) /* Page Type Mask [15:8] */
  146. #define SI_FLAG_SECS (0x00<<SI_FLAG_PT_LOW_BIT) /* SECS */
  147. #define SI_FLAG_TCS (0x01<<SI_FLAG_PT_LOW_BIT) /* TCS */
  148. #define SI_FLAG_REG (0x02<<SI_FLAG_PT_LOW_BIT) /* Regular Page */
  149. #define SI_FLAGS_EXTERNAL (SI_FLAG_PT_MASK | SI_FLAG_R | SI_FLAG_W | SI_FLAG_X) /* Flags visible/usable by instructions */
  150. #define SI_FLAGS_R (SI_FLAG_R|SI_FLAG_REG)
  151. #define SI_FLAGS_RW (SI_FLAG_R|SI_FLAG_W|SI_FLAG_REG)
  152. #define SI_FLAGS_RX (SI_FLAG_R|SI_FLAG_X|SI_FLAG_REG)
  153. #define SI_FLAGS_TCS (SI_FLAG_TCS)
  154. #define SI_FLAGS_SECS (SI_FLAG_SECS)
  155. #define SI_MASK_TCS (SI_FLAG_PT_MASK)
  156. #define SI_MASK_MEM_ATTRIBUTE (0x7)
  157. typedef struct _sec_info_t
  158. {
  159. si_flags_t flags;
  160. uint64_t reserved[7];
  161. } sec_info_t;
  162. typedef struct _page_info_t
  163. {
  164. PADDED_POINTER(void, lin_addr); /* Enclave linear address */
  165. PADDED_POINTER(void, src_page); /* Linear address of the page where contents are located */
  166. PADDED_POINTER(sec_info_t, sec_info); /* Linear address of the SEC_INFO structure for the page */
  167. PADDED_POINTER(void, secs); /* Linear address of EPC slot that contains SECS for this enclave */
  168. } page_info_t;
  169. /****************************************************************************
  170. * Definitions for enclave signature
  171. ****************************************************************************/
  172. #define SE_KEY_SIZE 384 /* in bytes */
  173. #define SE_EXPONENT_SIZE 4 /* RSA public key exponent size in bytes */
  174. typedef struct _css_header_t { /* 128 bytes */
  175. uint8_t header[12]; /* (0) must be (06000000E100000000000100H) */
  176. uint32_t type; /* (12) bit 31: 0 = prod, 1 = debug; Bit 30-0: Must be zero */
  177. uint32_t module_vendor; /* (16) Intel=0x8086, ISV=0x0000 */
  178. uint32_t date; /* (20) build date as yyyymmdd */
  179. uint8_t header2[16]; /* (24) must be (01010000600000006000000001000000H) */
  180. uint32_t hw_version; /* (40) For Launch Enclaves: HWVERSION != 0. Others, HWVERSION = 0 */
  181. uint8_t reserved[84]; /* (44) Must be 0 */
  182. } css_header_t;
  183. se_static_assert(sizeof(css_header_t) == 128);
  184. typedef struct _css_key_t { /* 772 bytes */
  185. uint8_t modulus[SE_KEY_SIZE]; /* (128) Module Public Key (keylength=3072 bits) */
  186. uint8_t exponent[SE_EXPONENT_SIZE]; /* (512) RSA Exponent = 3 */
  187. uint8_t signature[SE_KEY_SIZE]; /* (516) Signature over Header and Body */
  188. } css_key_t;
  189. se_static_assert(sizeof(css_key_t) == 772);
  190. typedef struct _css_body_t { /* 128 bytes */
  191. sgx_misc_select_t misc_select; /* (900) The MISCSELECT that must be set */
  192. sgx_misc_select_t misc_mask; /* (904) Mask of MISCSELECT to enforce */
  193. uint8_t reserved[20]; /* (908) Reserved. Must be 0. */
  194. sgx_attributes_t attributes; /* (928) Enclave Attributes that must be set */
  195. sgx_attributes_t attribute_mask; /* (944) Mask of Attributes to Enforce */
  196. sgx_measurement_t enclave_hash; /* (960) MRENCLAVE - (32 bytes) */
  197. uint8_t reserved2[32]; /* (992) Must be 0 */
  198. uint16_t isv_prod_id; /* (1024) ISV assigned Product ID */
  199. uint16_t isv_svn; /* (1026) ISV assigned SVN */
  200. } css_body_t;
  201. se_static_assert(sizeof(css_body_t) == 128);
  202. typedef struct _css_buffer_t { /* 780 bytes */
  203. uint8_t reserved[12]; /* (1028) Must be 0 */
  204. uint8_t q1[SE_KEY_SIZE]; /* (1040) Q1 value for RSA Signature Verification */
  205. uint8_t q2[SE_KEY_SIZE]; /* (1424) Q2 value for RSA Signature Verification */
  206. } css_buffer_t;
  207. se_static_assert(sizeof(css_buffer_t) == 780);
  208. typedef struct _enclave_css_t { /* 1808 bytes */
  209. css_header_t header; /* (0) */
  210. css_key_t key; /* (128) */
  211. css_body_t body; /* (900) */
  212. css_buffer_t buffer; /* (1028) */
  213. } enclave_css_t;
  214. se_static_assert(sizeof(enclave_css_t) == 1808);
  215. /****************************************************************************
  216. * Definitions for launch token
  217. ****************************************************************************/
  218. typedef struct _launch_body_t
  219. {
  220. uint32_t valid; /* ( 0) 0 = Invalid, 1 = Valid */
  221. uint32_t reserved1[11]; /* ( 4) must be zero */
  222. sgx_attributes_t attributes; /* ( 48) ATTRIBUTES of Enclave */
  223. sgx_measurement_t mr_enclave; /* ( 64) MRENCLAVE of Enclave */
  224. uint8_t reserved2[32]; /* ( 96) */
  225. sgx_measurement_t mr_signer; /* (128) MRSIGNER of Enclave */
  226. uint8_t reserved3[32]; /* (160) */
  227. } launch_body_t;
  228. se_static_assert(sizeof(launch_body_t) == 192);
  229. typedef struct _launch_t {
  230. launch_body_t body;
  231. sgx_cpu_svn_t cpu_svn_le; /* (192) Launch Enclave's CPUSVN */
  232. uint16_t isv_prod_id_le; /* (208) Launch Enclave's ISVPRODID */
  233. uint16_t isv_svn_le; /* (210) Launch Enclave's ISVSVN */
  234. uint8_t reserved2[24]; /* (212) Must be 0 */
  235. sgx_misc_select_t masked_misc_select_le; /* (236) */
  236. sgx_attributes_t attributes_le; /* (240) ATTRIBUTES of Launch Enclave */
  237. sgx_key_id_t key_id; /* (256) Value for key wear-out protection */
  238. sgx_mac_t mac; /* (288) CMAC using Launch Token Key */
  239. } token_t;
  240. se_static_assert(sizeof(token_t) == 304);
  241. typedef struct _wl_cert_t /* All fields except the mr_signer_list fields, are big-endian integer format */
  242. {
  243. uint16_t version; /* ( 0) White List Cert format version. For 2015, only valid version is 1 */
  244. uint16_t cert_type; /* ( 2) White List Cert Type. For Enclave Signing Key White List Cert, must be 1 */
  245. uint16_t provider_id; /* ( 4) Enclave Signing Key White List Provider ID to identify the key used to sign this Enclave signing Key White List Certificate. For 2015, only one White List Provider is approved: WLProviderID-ISecG = 0 */
  246. uint16_t le_prod_id; /* ( 6) Launch Enclave ProdID the White List Cert applies to. Linux LE-ProdID = 0x20 */
  247. uint32_t wl_version; /* ( 8) Version of the Enclave Signing Key White List. For a specific LE-ProdID, should increase on every WL Cert signing request */
  248. uint32_t entry_number; /* (12) Number of MRSIGNER entries in the Cert. If the White List Certificate allows enclave signed by any key to launch, the White List Cert must only contain one all-0 MRSIGNER entry. */
  249. sgx_measurement_t mr_signer_list[]; /* (16) White Listed Enclave Signing Key entry 0 - SHA256 Hash of the little-endian format RSA-3072 Enclave Signing Key modulus. If the White List Cert allows enclave signed by any key to launch, this field must be all 0s */
  250. }wl_cert_t;
  251. typedef struct _wl_provider_cert_t /* All fields are big endian */
  252. {
  253. uint16_t version; /* ( 0) White List Cert format version. For 2015, only valid version is 1 */
  254. uint16_t cert_type; /* ( 2) White List Cert Type, For Enclave Signing Key White List Signer Cert, must be 0 */
  255. uint16_t provider_id; /* ( 4) Enclave Signing Key White List Signer ID assigned by the White List Root CA. For 2015, only one White List Provider is approved: WLProviderID-ISecG = 0 */
  256. uint16_t root_id; /* ( 6) Identify the White List Root CA key used to sign the Cert. For 2015, only one WLRootID is valid: WLRootID-iKGF-Key-0 = 0 */
  257. sgx_ec256_public_t pub_key; /* ( 8) ECDSA public key of the Enclave Signing Key White List Provider identified by WLProviderID */
  258. sgx_ec256_signature_t signature; /* (72) ECDSA Signature by WL Root CA identified by WLRootID */
  259. }wl_provider_cert_t;
  260. se_static_assert(sizeof(wl_provider_cert_t) == 136);
  261. typedef struct _wl_cert_chain_t
  262. {
  263. wl_provider_cert_t wl_provider_cert;
  264. wl_cert_t wl_cert;
  265. }wl_cert_chain_t;
  266. #pragma pack(pop)
  267. #endif/*_SE_ARCH_H_*/