sgx_api.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef SGX_API_H
  14. #define SGX_API_H
  15. #include "sgx_arch.h"
  16. #include "pal_error.h"
  17. int sgx_ocall (unsigned long code, void * ms);
  18. bool sgx_is_completely_within_enclave (const void * addr, uint64_t size);
  19. bool sgx_is_completely_outside_enclave(const void * addr, uint64_t size);
  20. void* sgx_alloc_on_ustack(uint64_t size);
  21. void* sgx_copy_to_ustack(const void* ptr, uint64_t size);
  22. void sgx_reset_ustack(void);
  23. bool sgx_copy_ptr_to_enclave(void** ptr, void* uptr, uint64_t size);
  24. uint64_t sgx_copy_to_enclave(const void* ptr, uint64_t maxsize, const void* uptr, uint64_t usize);
  25. int sgx_get_report (sgx_arch_hash_t * mrenclave,
  26. sgx_arch_attributes_t * attributes,
  27. void * enclave_data,
  28. sgx_arch_report_t * report);
  29. int sgx_verify_report (sgx_arch_report_t * report);
  30. /*
  31. * sgx_report:
  32. * Generate SGX hardware signed report.
  33. */
  34. static inline int sgx_report (sgx_arch_targetinfo_t * targetinfo,
  35. void * reportdata, sgx_arch_report_t * report)
  36. {
  37. __asm__ volatile(
  38. ENCLU "\n"
  39. :: "a"(EREPORT), "b"(targetinfo), "c"(reportdata), "d"(report)
  40. : "memory");
  41. return 0;
  42. }
  43. /*
  44. * sgx_getkey:
  45. * Retrieve SGX hardware enclave cryptography key.
  46. */
  47. static inline int64_t sgx_getkey (sgx_arch_keyrequest_t * keyrequest,
  48. sgx_arch_key128_t * key)
  49. {
  50. int64_t rax = EGETKEY;
  51. __asm__ volatile(
  52. ENCLU "\n"
  53. : "+a"(rax)
  54. : "b"(keyrequest), "c"(key)
  55. : "memory");
  56. return rax;
  57. }
  58. /*
  59. * rdrand:
  60. * Get hardware generated random value.
  61. */
  62. static inline uint32_t rdrand (void)
  63. {
  64. uint32_t ret;
  65. __asm__ volatile(
  66. "1: .byte 0x0f, 0xc7, 0xf0\n" /* RDRAND %EAX */
  67. "jnc 1b\n"
  68. :"=a"(ret)
  69. :: "cc");
  70. return ret;
  71. }
  72. /*
  73. * rdfsbase:
  74. * read FS register (allowed in enclaves).
  75. */
  76. static inline uint64_t rdfsbase (void)
  77. {
  78. uint64_t fsbase;
  79. __asm__ volatile(
  80. ".byte 0xf3, 0x48, 0x0f, 0xae, 0xc0\n" /* RDFSBASE %RAX */
  81. : "=a"(fsbase));
  82. return fsbase;
  83. }
  84. /*
  85. * wrfsbase:
  86. * modify FS register (allowed in enclaves).
  87. */
  88. static inline void wrfsbase (uint64_t addr)
  89. {
  90. __asm__ volatile(
  91. ".byte 0xf3, 0x48, 0x0f, 0xae, 0xd7\n" /* WRFSBASE %RDI */
  92. :: "D"(addr));
  93. }
  94. void restore_sgx_context(sgx_context_t *ctx);
  95. void _restore_sgx_context(sgx_context_t *ctx);
  96. #endif /* SGX_API_H */