sgx_tprotected_fs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /**
  32. * File: sgx_tprotected_fs.h
  33. * Description:
  34. * Interface for file API
  35. */
  36. #pragma once
  37. #ifndef _SGX_TPROTECTED_FS_H_
  38. #define _SGX_TPROTECTED_FS_H_
  39. #include <stddef.h>
  40. #include "sgx_defs.h"
  41. #include "sgx_key.h"
  42. #define SGX_FILE void
  43. #define EOF (-1)
  44. #define SEEK_SET 0
  45. #define SEEK_CUR 1
  46. #define SEEK_END 2
  47. #define FILENAME_MAX 260
  48. #define FOPEN_MAX 20
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /* sgx_fopen
  53. * Purpose: open existing protected file (created with previous call to sgc_fopen) or create a new one (see c++ fopen documentation for more details).
  54. *
  55. * Parameters:
  56. * filename - [IN] the name of the file to open/create.
  57. * mode - [IN] open mode. only supports 'r' or 'w' or 'a' (one and only one of them must be present), and optionally 'b' and/or '+'.
  58. * key - [IN] encryption key that will be used for the file encryption
  59. * NOTE - the key is actually used as a KDK (key derivation key) and only for the meta-data node, and not used directly for the encryption of any part of the file
  60. * this is important in order to prevent hitting the key wear-out problem, and some other issues with GCM encryptions using the same key
  61. *
  62. * Return value:
  63. * SGX_FILE* - pointer to the newly created file handle, NULL if an error occurred - check errno for the error code.
  64. */
  65. SGX_FILE* SGXAPI sgx_fopen(const char* filename, const char* mode, const sgx_key_128bit_t *key);
  66. /* sgx_fopen_auto_key
  67. * Purpose: open existing protected file (created with previous call to sgc_fopen_auto_key) or create a new one (see c++ fopen documentation for more details).
  68. * this API doesn't require a key from the user, and instead uses the enclave's SEAL key as a KDK for deriving the mete-data encryption keys
  69. * using the SEAL key as a KDK, may result losing file access in cases of disaster-recovery or in cases of VM migration in servers
  70. * users that any of these scenarious apply to them, are advised to use sgx_fopen, where they can provide their own key and manage the keys provisioning for those scenarious
  71. * for further information, please read the SGX SDK manual
  72. *
  73. * Parameters:
  74. * filename - [IN] the name of the file to open/create.
  75. * mode - [IN] open mode. only supports 'r' or 'w' or 'a' (one and only one of them must be present), and optionally 'b' and/or '+'.
  76. *
  77. * Return value:
  78. * SGX_FILE* - pointer to the newly created file handle, NULL if an error occurred - check errno for the error code.
  79. */
  80. SGX_FILE* SGXAPI sgx_fopen_auto_key(const char* filename, const char* mode);
  81. /* sgx_fwrite
  82. * Purpose: write data to a file (see c++ fwrite documentation for more details).
  83. *
  84. * Parameters:
  85. * ptr - [IN] pointer to the input data buffer
  86. * size - [IN] size of data block
  87. * count - [IN] count of data blocks to write
  88. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  89. *
  90. * Return value:
  91. * size_t - number of 'size' blocks written to the file, 0 in case of an error - check sgx_ferror for error code
  92. */
  93. size_t SGXAPI sgx_fwrite(const void* ptr, size_t size, size_t count, SGX_FILE* stream);
  94. /* sgx_fread
  95. * Purpose: read data from a file (see c++ fread documentation for more details).
  96. *
  97. * Parameters:
  98. * ptr - [OUT] pointer to the output data buffer
  99. * size - [IN] size of data block
  100. * count - [IN] count of data blocks to write
  101. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  102. *
  103. * Return value:
  104. * size_t - number of 'size' blocks read from the file, 0 in case of an error - check sgx_ferror for error code
  105. */
  106. size_t SGXAPI sgx_fread(void* ptr, size_t size, size_t count, SGX_FILE* stream);
  107. /* sgx_ftell
  108. * Purpose: get the current value of the position indicator of the file (see c++ ftell documentation for more details).
  109. *
  110. * Parameters:
  111. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  112. *
  113. * Return value:
  114. * int64_t - the current value of the position indicator, -1 on error - check errno for the error code
  115. */
  116. int64_t SGXAPI sgx_ftell(SGX_FILE* stream);
  117. /* sgx_fseek
  118. * Purpose: set the current value of the position indicator of the file (see c++ fseek documentation for more details).
  119. *
  120. * Parameters:
  121. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  122. * offset - [IN] the new required value, relative to the origin parameter
  123. * origin - [IN] the origin from which to calculate the offset (SEEK_SET, SEEK_CUR or SEEK_END)
  124. *
  125. * Return value:
  126. * int32_t - result, 0 on success, -1 in case of an error - check sgx_ferror for error code
  127. */
  128. int32_t SGXAPI sgx_fseek(SGX_FILE* stream, int64_t offset, int origin);
  129. /* sgx_fflush
  130. * Purpose: force actual write of all the cached data to the disk (see c++ fflush documentation for more details).
  131. *
  132. * Parameters:
  133. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  134. *
  135. * Return value:
  136. * int32_t - result, 0 on success, 1 in case of an error - check sgx_ferror for error code
  137. */
  138. int32_t SGXAPI sgx_fflush(SGX_FILE* stream);
  139. /* sgx_ferror
  140. * Purpose: get the latest operation error code (see c++ ferror documentation for more details).
  141. *
  142. * Parameters:
  143. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  144. *
  145. * Return value:
  146. * int32_t - the error code, 0 means no error, anything else is the latest operation error code
  147. */
  148. int32_t SGXAPI sgx_ferror(SGX_FILE* stream);
  149. /* sgx_feof
  150. * Purpose: did the file's position indicator hit the end of the file in a previous read operation (see c++ feof documentation for more details).
  151. *
  152. * Parameters:
  153. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  154. *
  155. * Return value:
  156. * int32_t - 1 - end of file was reached, 0 - end of file wasn't reached
  157. */
  158. int32_t SGXAPI sgx_feof(SGX_FILE* stream);
  159. /* sgx_clearerr
  160. * Purpose: try to clear an error in the file status, also clears the end-of-file flag (see c++ clearerr documentation for more details).
  161. * call sgx_ferror or sgx_feof after a call to this function to learn if it was successful or not
  162. *
  163. * Parameters:
  164. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  165. *
  166. * Return value:
  167. * none
  168. */
  169. void SGXAPI sgx_clearerr(SGX_FILE* stream);
  170. /* sgx_fclose
  171. * Purpose: close an open file handle (see c++ fclose documentation for more details).
  172. * after a call to this function, the handle is invalid even if an error is returned
  173. *
  174. * Parameters:
  175. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  176. *
  177. * Return value:
  178. * int32_t - result, 0 - file was closed successfully, 1 - there were errors during the operation
  179. */
  180. int32_t SGXAPI sgx_fclose(SGX_FILE* stream);
  181. /* sgx_remove
  182. * Purpose: delete a file from the file system (see c++ remove documentation for more details).
  183. *
  184. * Parameters:
  185. * filename - [IN] the name of the file to remvoe.
  186. *
  187. * Return value:
  188. * int32_t - result, 0 - success, 1 - there was an error, check errno for the error code
  189. */
  190. int32_t SGXAPI sgx_remove(const char* filename);
  191. /* sgx_fexport_auto_key
  192. * Purpose: export the last key that was used in the encryption of the file.
  193. * with this key we can import the file in a different enclave/system
  194. * NOTE - 1. in order for this function to work, the file should not be opened in any other process
  195. * 2. this function only works with files created with sgx_fopen_auto_key
  196. *
  197. * Parameters:
  198. * filename - [IN] the name of the file to export the encryption key from.
  199. * key - [OUT] the exported encryption key
  200. *
  201. * Return value:
  202. * int32_t - result, 0 - success, 1 - there was an error, check errno for the error code
  203. */
  204. int32_t SGXAPI sgx_fexport_auto_key(const char* filename, sgx_key_128bit_t *key);
  205. /* sgx_fimport_auto_key
  206. * Purpose: import a file that was created in a different enclave/system and make it a local file
  207. * after this call return successfully, the file can be opened with sgx_fopen_auto_key
  208. * NOTE - this function only works with files created with sgx_fopen_auto_key
  209. *
  210. * Parameters:
  211. * filename - [IN] the name of the file to be imported.
  212. * key - [IN] the encryption key, exported with a call to sgx_fexport_auto_key in the source enclave/system
  213. *
  214. * Return value:
  215. * int32_t - result, 0 - success, 1 - there was an error, check errno for the error code
  216. */
  217. int32_t SGXAPI sgx_fimport_auto_key(const char* filename, const sgx_key_128bit_t *key);
  218. /* sgx_fclear_cache
  219. * Purpose: scrubs and delete the internal cache used by the file, any changed data is flushed to disk before deletion
  220. * Note - only the secrets that were in the cache are deleted, the file structure itself still holds keys and plain file data
  221. * if a user wishes to remove all secrets from memory, he should close the file handle with sgx_fclose
  222. *
  223. * Parameters:
  224. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key
  225. *
  226. * Return value:
  227. * int32_t - result, 0 - success, 1 - there was an error, check errno for the error code
  228. */
  229. int32_t SGXAPI sgx_fclear_cache(SGX_FILE* stream);
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif // _SGX_TPROTECTED_FS_H_