main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // Initialize a fixed size hash tree which has 8192 VMC entries.
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <malloc.h>
  36. #include <stdint.h>
  37. #include <math.h>
  38. #include "sqlite3.h"
  39. #include "sgx_error.h"
  40. #include "sgx_tcrypto.h"
  41. #include "se_memcpy.h"
  42. #include "monotonic_counter_database_types.h"
  43. #define SQLITE_DB_FILE_NAME "prebuild_pse_vmc.db"
  44. static sqlite3 *gDb = NULL;
  45. #define EXIT_IFNOT_SQLITE_OK(rc) if(SQLITE_OK!=(rc)){goto error;}
  46. #define HASH_TREE_NODE_TYPE_UNKNOWN 0
  47. #define HASH_TREE_NODE_TYPE_ROOT 1
  48. #define HASH_TREE_NODE_TYPE_INTERNAL 2
  49. #define HASH_TREE_NODE_TYPE_LEAF 3
  50. // internal_node_hash_value_table[0] is calculated by two empty leaf nodes
  51. // internal_node_hash_value_table[1] is calculated by two internal_node_hash_value_table[0]
  52. // ...
  53. int verify_precalculated_hash_table()
  54. {
  55. sgx_status_t stat = SGX_SUCCESS;
  56. vmc_data_blob_t leaf[2] = {0};
  57. hash_tree_internal_node_t inter[2] = {0};
  58. hash_tree_internal_node_t inter_parent = {0};
  59. // inter = sha256(leaf|leaf) which should match internal_node_hash_value_table[0]
  60. stat = sgx_sha256_msg((uint8_t*)&leaf[0],
  61. 2*LEAF_NODE_SIZE,
  62. (sgx_sha256_hash_t*)&inter[0]);
  63. if( stat != SGX_SUCCESS || 0 != memcmp(&inter[0], &internal_node_hash_value_table[0][0], HASH_VALUE_SIZE) )
  64. {
  65. goto error;
  66. }
  67. memcpy(inter+1, inter, sizeof(hash_tree_internal_node_t));
  68. // inter_parent = sha256(inter|inter) should match internal_node_hash_value_table[i]
  69. for(int i = 1; i<23 ; i++)
  70. {
  71. stat = sgx_sha256_msg((uint8_t*)&inter[0],
  72. 2*HASH_VALUE_SIZE,
  73. (sgx_sha256_hash_t*)&inter_parent);
  74. if( stat != SGX_SUCCESS || 0 != memcmp(&inter_parent, &internal_node_hash_value_table[i][0], HASH_VALUE_SIZE) )
  75. {
  76. goto error;
  77. }
  78. memcpy(inter, &inter_parent, sizeof(hash_tree_internal_node_t));
  79. memcpy(inter+1, &inter_parent, sizeof(hash_tree_internal_node_t));
  80. }
  81. return 0;
  82. error:
  83. return -1;
  84. }
  85. int sqlite_generate_prebuild_db()
  86. {
  87. sqlite3* db = NULL;
  88. int rc;
  89. char* errmsg = NULL;
  90. int node_type;
  91. int start_id, end_id;
  92. sqlite3_stmt* stat = NULL;
  93. char sql_sentence[512] = {0};
  94. uint8_t* ptr_buf = NULL;
  95. uint32_t bufflen;
  96. uint8_t* ptr_precalc_node_buff = NULL;
  97. int layer;
  98. bufflen = (INIT_MAX_HASH_TREE_LAYER-2)*sizeof(hash_tree_internal_node_t) +
  99. sizeof(hash_tree_leaf_node_t);
  100. ptr_precalc_node_buff = (uint8_t*)malloc(bufflen);
  101. if(NULL == ptr_precalc_node_buff)
  102. {
  103. return -1;
  104. }
  105. memset(ptr_precalc_node_buff, 0, bufflen);
  106. hash_tree_internal_node_t* internal_node = (hash_tree_internal_node_t*)ptr_precalc_node_buff;
  107. for(int index=INIT_MAX_HASH_TREE_LAYER-3; index>=0; index--)
  108. {
  109. if(memcpy_s(internal_node->hash,
  110. sizeof(internal_node->hash),
  111. &(internal_node_hash_value_table[index][0]),
  112. HASH_VALUE_SIZE))
  113. {
  114. free(ptr_precalc_node_buff);
  115. return -1;
  116. }
  117. internal_node++;
  118. }
  119. if(gDb)
  120. {
  121. db = gDb;
  122. }
  123. else
  124. {
  125. rc = sqlite3_open(SQLITE_DB_FILE_NAME, &gDb);
  126. if( SQLITE_OK != rc )
  127. {
  128. free(ptr_precalc_node_buff);
  129. return rc;
  130. }
  131. db = gDb;
  132. }
  133. rc = sqlite3_exec( db,
  134. "create table VMC_QUOTA_TABLE( ID integer primary key AUTOINCREMENT, MRSIGNER char(64), COUNTER integer)",
  135. NULL,
  136. NULL,
  137. &errmsg );
  138. EXIT_IFNOT_SQLITE_OK(rc)
  139. rc = sqlite3_exec( db,
  140. "create table HASH_TREE_NODE_TABLE( ID integer primary key, node_content blob, USED integer, REFID integer NULL REFERENCES VMC_QUOTA_TABLE(ID))",
  141. NULL,
  142. NULL,
  143. &errmsg );
  144. EXIT_IFNOT_SQLITE_OK(rc)
  145. rc = sqlite3_exec( db,
  146. "create table BACKUP_TABLE( ID integer primary key, node_content blob, USED integer, REFID integer)",
  147. NULL,
  148. NULL,
  149. &errmsg );
  150. EXIT_IFNOT_SQLITE_OK(rc)
  151. // all nodes in the same layer have the same precalculated value
  152. // the merkel hash tree has 12 layers including root layer
  153. rc = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
  154. EXIT_IFNOT_SQLITE_OK(rc)
  155. sprintf(sql_sentence, "insert into HASH_TREE_NODE_TABLE( ID, node_content, USED, REFID) values( ?, ?, 0, NULL)");
  156. rc = sqlite3_prepare_v2(db,
  157. sql_sentence,
  158. -1,
  159. &stat,
  160. 0);
  161. EXIT_IFNOT_SQLITE_OK(rc)
  162. layer = INIT_MAX_HASH_TREE_LAYER - 1;
  163. do{
  164. if(INIT_MAX_HASH_TREE_LAYER - 1 == layer)
  165. {
  166. node_type = HASH_TREE_NODE_TYPE_LEAF;
  167. }
  168. else
  169. {
  170. node_type = HASH_TREE_NODE_TYPE_INTERNAL;
  171. }
  172. start_id = (int)pow((double)2,layer);
  173. end_id = (int)pow((double)2,layer+1) - 1;
  174. for(int id = start_id; id <= end_id; id++)
  175. {
  176. rc =sqlite3_bind_int(stat, 1, id);
  177. EXIT_IFNOT_SQLITE_OK(rc)
  178. switch(node_type)
  179. {
  180. case HASH_TREE_NODE_TYPE_INTERNAL:
  181. ptr_buf = ptr_precalc_node_buff;
  182. rc = sqlite3_bind_blob(stat,
  183. 2,
  184. (hash_tree_internal_node_t*)ptr_buf + layer - 1,
  185. sizeof(hash_tree_internal_node_t),
  186. NULL
  187. );
  188. break;
  189. case HASH_TREE_NODE_TYPE_LEAF:
  190. rc = sqlite3_bind_blob(stat,
  191. 2,
  192. ptr_precalc_node_buff + (INIT_MAX_HASH_TREE_LAYER-2)*sizeof(hash_tree_internal_node_t),
  193. sizeof(hash_tree_leaf_node_t),
  194. NULL
  195. );
  196. break;
  197. default:
  198. goto error;
  199. }
  200. EXIT_IFNOT_SQLITE_OK(rc)
  201. rc = sqlite3_step(stat);
  202. if(rc != SQLITE_DONE)
  203. {
  204. goto error;
  205. }
  206. rc = sqlite3_clear_bindings(stat);
  207. EXIT_IFNOT_SQLITE_OK(rc)
  208. rc = sqlite3_reset(stat);
  209. EXIT_IFNOT_SQLITE_OK(rc)
  210. }
  211. layer--;
  212. if(layer&0x1)
  213. sqlite3_sleep(1);
  214. }while(layer>0);
  215. rc = sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);
  216. EXIT_IFNOT_SQLITE_OK(rc)
  217. rc = sqlite3_finalize(stat);
  218. EXIT_IFNOT_SQLITE_OK(rc)
  219. stat = NULL;
  220. sqlite3_close_v2(db);
  221. gDb = NULL;
  222. free(ptr_precalc_node_buff);
  223. return 0;
  224. error:
  225. free(ptr_precalc_node_buff);
  226. if(db)
  227. {
  228. sqlite3_finalize(stat);
  229. sqlite3_exec(db, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
  230. sqlite3_close_v2(db);
  231. gDb = NULL;
  232. }
  233. return -1;
  234. }
  235. int main()
  236. {
  237. if(verify_precalculated_hash_table())
  238. {
  239. printf("failed to verify precalculated hash table.\n");
  240. return -1;
  241. }
  242. int ret = sqlite_generate_prebuild_db();
  243. if(0 != ret)
  244. printf("failed to generate VMC DB.\n");
  245. return ret;
  246. }