file_crypto.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include "protected_fs_file.h"
  32. #include <sgx_utils.h>
  33. #include <sgx_trts.h>
  34. #define MASTER_KEY_NAME "SGX-PROTECTED-FS-MASTER-KEY"
  35. #define RANDOM_KEY_NAME "SGX-PROTECTED-FS-RANDOM-KEY"
  36. #define METADATA_KEY_NAME "SGX-PROTECTED-FS-METADATA-KEY"
  37. #define MAX_LABEL_LEN 64
  38. typedef struct {
  39. uint32_t index;
  40. char label[MAX_LABEL_LEN];
  41. uint64_t node_number; // context 1
  42. union { // context 2
  43. sgx_cmac_128bit_tag_t nonce16;
  44. sgx_key_id_t nonce32;
  45. };
  46. uint32_t output_len; // in bits
  47. } kdf_input_t;
  48. #define MAX_MASTER_KEY_USAGES 65536
  49. bool protected_fs_file::generate_secure_blob(sgx_aes_gcm_128bit_key_t* key, const char* label, uint64_t physical_node_number, sgx_aes_gcm_128bit_tag_t* output)
  50. {
  51. kdf_input_t buf = {0, "", 0, "", 0};
  52. uint32_t len = (uint32_t)strnlen(label, MAX_LABEL_LEN + 1);
  53. if (len > MAX_LABEL_LEN)
  54. {
  55. last_error = EINVAL;
  56. return false;
  57. }
  58. // index
  59. // SP800-108:
  60. // i – A counter, a binary string of length r that is an input to each iteration of a PRF in counter mode [...].
  61. buf.index = 0x01;
  62. // label
  63. // SP800-108:
  64. // Label – A string that identifies the purpose for the derived keying material, which is encoded as a binary string.
  65. // The encoding method for the Label is defined in a larger context, for example, in the protocol that uses a KDF.
  66. strncpy(buf.label, label, len);
  67. // context and nonce
  68. // SP800-108:
  69. // Context – A binary string containing the information related to the derived keying material.
  70. // It may include identities of parties who are deriving and / or using the derived keying material and,
  71. // optionally, a nonce known by the parties who derive the keys.
  72. buf.node_number = physical_node_number;
  73. sgx_status_t status = sgx_read_rand((unsigned char*)&(buf.nonce16), sizeof(sgx_cmac_128bit_tag_t));
  74. if (status != SGX_SUCCESS)
  75. {
  76. last_error = status;
  77. return false;
  78. }
  79. // length of output (128 bits)
  80. buf.output_len = 0x80;
  81. status = sgx_rijndael128_cmac_msg(key, (const uint8_t*)&buf, sizeof(kdf_input_t), output);
  82. if (status != SGX_SUCCESS)
  83. {
  84. last_error = status;
  85. return false;
  86. }
  87. memset_s(&buf, sizeof(kdf_input_t), 0, sizeof(kdf_input_t));
  88. return true;
  89. }
  90. bool protected_fs_file::generate_secure_blob_from_user_kdk(bool restore)
  91. {
  92. kdf_input_t buf = {0, "", 0, "", 0};
  93. sgx_status_t status = SGX_SUCCESS;
  94. // index
  95. // SP800-108:
  96. // i – A counter, a binary string of length r that is an input to each iteration of a PRF in counter mode [...].
  97. buf.index = 0x01;
  98. // label
  99. // SP800-108:
  100. // Label – A string that identifies the purpose for the derived keying material, which is encoded as a binary string.
  101. // The encoding method for the Label is defined in a larger context, for example, in the protocol that uses a KDF.
  102. strncpy(buf.label, METADATA_KEY_NAME, strlen(METADATA_KEY_NAME));
  103. // context and nonce
  104. // SP800-108:
  105. // Context – A binary string containing the information related to the derived keying material.
  106. // It may include identities of parties who are deriving and / or using the derived keying material and,
  107. // optionally, a nonce known by the parties who derive the keys.
  108. buf.node_number = 0;
  109. // use 32 bytes here just for compatibility with the seal key API
  110. if (restore == false)
  111. {
  112. status = sgx_read_rand((unsigned char*)&(buf.nonce32), sizeof(sgx_key_id_t));
  113. if (status != SGX_SUCCESS)
  114. {
  115. last_error = status;
  116. return false;
  117. }
  118. }
  119. else
  120. {
  121. memcpy(&buf.nonce32, &file_meta_data.plain_part.meta_data_key_id, sizeof(sgx_key_id_t));
  122. }
  123. // length of output (128 bits)
  124. buf.output_len = 0x80;
  125. status = sgx_rijndael128_cmac_msg(&user_kdk_key, (const uint8_t*)&buf, sizeof(kdf_input_t), &cur_key);
  126. if (status != SGX_SUCCESS)
  127. {
  128. last_error = status;
  129. return false;
  130. }
  131. if (restore == false)
  132. {
  133. memcpy(&file_meta_data.plain_part.meta_data_key_id, &buf.nonce32, sizeof(sgx_key_id_t));
  134. }
  135. memset_s(&buf, sizeof(kdf_input_t), 0, sizeof(kdf_input_t));
  136. return true;
  137. }
  138. bool protected_fs_file::init_session_master_key()
  139. {
  140. sgx_aes_gcm_128bit_key_t empty_key = {0};
  141. if (generate_secure_blob(&empty_key, MASTER_KEY_NAME, 0, (sgx_aes_gcm_128bit_tag_t*)&session_master_key) == false)
  142. return false;
  143. master_key_count = 0;
  144. return true;
  145. }
  146. bool protected_fs_file::derive_random_node_key(uint64_t physical_node_number)
  147. {
  148. if (master_key_count++ > MAX_MASTER_KEY_USAGES)
  149. {
  150. if (init_session_master_key() == false)
  151. return false;
  152. }
  153. if (generate_secure_blob(&session_master_key, RANDOM_KEY_NAME, physical_node_number, (sgx_aes_gcm_128bit_tag_t*)&cur_key) == false)
  154. return false;
  155. return true;
  156. }
  157. bool protected_fs_file::generate_random_meta_data_key()
  158. {
  159. if (use_user_kdk_key == 1)
  160. {
  161. return generate_secure_blob_from_user_kdk(false);
  162. }
  163. // derive a random key from the enclave sealing key
  164. sgx_key_request_t key_request;
  165. memset(&key_request, 0, sizeof(sgx_key_request_t));
  166. key_request.key_name = SGX_KEYSELECT_SEAL;
  167. key_request.key_policy = SGX_KEYPOLICY_MRSIGNER;
  168. memcpy(&key_request.cpu_svn, &report.body.cpu_svn, sizeof(sgx_cpu_svn_t));
  169. memcpy(&key_request.isv_svn, &report.body.isv_svn, sizeof(sgx_isv_svn_t));
  170. key_request.attribute_mask.flags = SGX_FLAGS_RESERVED | SGX_FLAGS_INITTED | SGX_FLAGS_DEBUG;
  171. key_request.attribute_mask.xfrm = 0x0;
  172. key_request.misc_mask = TSEAL_DEFAULT_MISCMASK;
  173. sgx_status_t status = sgx_read_rand((unsigned char*)&key_request.key_id, sizeof(sgx_key_id_t));
  174. if (status != SGX_SUCCESS)
  175. {
  176. last_error = status;
  177. return false;
  178. }
  179. status = sgx_get_key(&key_request, &cur_key);
  180. if (status != SGX_SUCCESS)
  181. {
  182. last_error = status;
  183. return false;
  184. }
  185. // save the key_id and svn's so the key can be restored even if svn's are updated
  186. memcpy(&file_meta_data.plain_part.meta_data_key_id, &key_request.key_id, sizeof(sgx_key_id_t)); // save this value in the meta data
  187. memcpy(&file_meta_data.plain_part.cpu_svn, &key_request.cpu_svn, sizeof(sgx_cpu_svn_t));
  188. memcpy(&file_meta_data.plain_part.isv_svn, &key_request.isv_svn, sizeof(sgx_isv_svn_t));
  189. return true;
  190. }
  191. bool protected_fs_file::restore_current_meta_data_key(const sgx_aes_gcm_128bit_key_t* import_key)
  192. {
  193. if (import_key != NULL)
  194. {
  195. memcpy(&cur_key, import_key, sizeof(sgx_aes_gcm_128bit_key_t));
  196. return true;
  197. }
  198. if (use_user_kdk_key == 1)
  199. {
  200. return generate_secure_blob_from_user_kdk(true);
  201. }
  202. sgx_key_id_t empty_key_id = {0};
  203. if (consttime_memequal(&file_meta_data.plain_part.meta_data_key_id, &empty_key_id, sizeof(sgx_key_id_t)) == 1)
  204. {
  205. last_error = SGX_ERROR_FILE_NO_KEY_ID;
  206. return false;
  207. }
  208. sgx_key_request_t key_request;
  209. memset(&key_request, 0, sizeof(sgx_key_request_t));
  210. key_request.key_name = SGX_KEYSELECT_SEAL;
  211. key_request.key_policy = SGX_KEYPOLICY_MRSIGNER;
  212. key_request.attribute_mask.flags = SGX_FLAGS_RESERVED | SGX_FLAGS_INITTED | SGX_FLAGS_DEBUG;
  213. key_request.attribute_mask.xfrm = 0x0;
  214. key_request.misc_mask = TSEAL_DEFAULT_MISCMASK;
  215. memcpy(&key_request.cpu_svn, &file_meta_data.plain_part.cpu_svn, sizeof(sgx_cpu_svn_t));
  216. memcpy(&key_request.isv_svn, &file_meta_data.plain_part.isv_svn, sizeof(sgx_isv_svn_t));
  217. memcpy(&key_request.key_id, &file_meta_data.plain_part.meta_data_key_id, sizeof(sgx_key_id_t));
  218. sgx_status_t status = sgx_get_key(&key_request, &cur_key);
  219. if (status != SGX_SUCCESS)
  220. {
  221. last_error = status;
  222. return false;
  223. }
  224. return true;
  225. }