file_crypto.cpp 8.7 KB

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