arch.h 16 KB

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