monotonic_counter_database_sqlite_access_hw_mc.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "monotonic_counter_database_types.h"
  32. #include "monotonic_counter_database_sqlite_access_hw_mc.h"
  33. #include "psda_service.h"
  34. #include "pse_op_t.h"
  35. #include <assert.h>
  36. #include <limits.h>
  37. static cse_rpdata_t global_cached_rpdata;
  38. static bool is_rpdata_cache_initialized = false;
  39. pse_op_error_t read_rpdata()
  40. {
  41. uint8_t rpdata[ROOT_HASH_SIZE];
  42. uint32_t rp_epoch;
  43. pse_op_error_t rc;
  44. rc = psda_read_rpdata(rpdata, &rp_epoch);
  45. if(OP_SUCCESS == rc)
  46. {
  47. // Check epoch first
  48. if(is_rpdata_cache_initialized && global_cached_rpdata.rpdata_epoch != rp_epoch)
  49. {
  50. // RPEPOCH has changed
  51. return OP_ERROR_INTERNAL;
  52. }
  53. // update rpdata cache
  54. global_cached_rpdata.rpdata_epoch = rp_epoch;
  55. memcpy(global_cached_rpdata.rpdata_roothash, rpdata, sizeof(global_cached_rpdata.rpdata_roothash));
  56. is_rpdata_cache_initialized = true;
  57. }
  58. return rc;
  59. }
  60. pse_op_error_t update_rpdata(uint8_t* rpdata_new)
  61. {
  62. assert(rpdata_new!=NULL);
  63. assert(false != is_rpdata_cache_initialized);
  64. uint32_t rp_epoch;
  65. pse_op_error_t rc;
  66. rc = psda_update_rpdata(global_cached_rpdata.rpdata_roothash, rpdata_new, &rp_epoch);
  67. if(OP_SUCCESS == rc)
  68. {
  69. // Check epoch
  70. if(is_rpdata_cache_initialized && global_cached_rpdata.rpdata_epoch != rp_epoch)
  71. {
  72. // RPEPOCH has changed
  73. return OP_ERROR_INTERNAL;
  74. }
  75. // update rpdata cache
  76. global_cached_rpdata.rpdata_epoch = rp_epoch;
  77. memcpy(global_cached_rpdata.rpdata_roothash, rpdata_new, sizeof(global_cached_rpdata.rpdata_roothash));
  78. is_rpdata_cache_initialized = true;
  79. }
  80. return rc;
  81. }
  82. pse_op_error_t reset_rpdata()
  83. {
  84. assert(false != is_rpdata_cache_initialized);
  85. uint8_t rpdata_new[SGX_RPDATA_SIZE];
  86. uint32_t rp_epoch;
  87. pse_op_error_t rc;
  88. rc = psda_reset_rpdata(global_cached_rpdata.rpdata_roothash, rpdata_new, &rp_epoch);
  89. if(OP_SUCCESS == rc)
  90. {
  91. // update rpdata cache
  92. global_cached_rpdata.rpdata_epoch = rp_epoch;
  93. memcpy(global_cached_rpdata.rpdata_roothash, rpdata_new, sizeof(global_cached_rpdata.rpdata_roothash));
  94. is_rpdata_cache_initialized = true;
  95. }
  96. return rc;
  97. }
  98. pse_op_error_t get_cached_roothash(uint8_t *roothash)
  99. {
  100. assert(roothash);
  101. assert(is_rpdata_cache_initialized);
  102. if(!is_rpdata_cache_initialized) // RPDATA hasn't been cached in PSE-OP
  103. {
  104. return OP_ERROR_INTERNAL;
  105. }
  106. memcpy(roothash, global_cached_rpdata.rpdata_roothash, ROOT_HASH_SIZE);
  107. return OP_SUCCESS;
  108. }
  109. pse_op_error_t get_cached_rpepoch(uint32_t *rpepoch)
  110. {
  111. assert(rpepoch);
  112. assert(is_rpdata_cache_initialized);
  113. if(!is_rpdata_cache_initialized)
  114. {
  115. return OP_ERROR_INTERNAL;
  116. }
  117. *rpepoch = global_cached_rpdata.rpdata_epoch;
  118. return OP_SUCCESS;
  119. }
  120. // clear cached RPDATA
  121. void clear_cached_rpdata()
  122. {
  123. is_rpdata_cache_initialized = false;
  124. memset(&global_cached_rpdata, 0, sizeof(global_cached_rpdata));
  125. }