pcl_mem.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <stdint.h>
  32. #include <stdlib.h>
  33. #include <global_data.h>
  34. #include <sgx_tcrypto.h>
  35. #include <sgx_tseal.h>
  36. #include <pcl_common.h>
  37. #include <pcl_internal.h>
  38. /*
  39. * @func pcl_memcpy implements memcpy that can run before runtime initiazliation
  40. * @param OUT void* dst, output destination buffer
  41. * @param IN void* src, input source buffer
  42. * @param size_t size, buffer size in bytes
  43. */
  44. void pcl_memcpy(OUT void* dst, IN void* src, size_t size)
  45. {
  46. if(NULL == dst || NULL == src)abort();
  47. for(size_t i=0;i<size;i++)
  48. {
  49. CASTU8P(dst)[i]=CASTU8P(src)[i];
  50. }
  51. }
  52. /*
  53. * @func pcl_memset implements memset that can run before runtime initiazliation
  54. * @param OUT void* dst, output destination buffer
  55. * @param uint8_t val, value to set buffer
  56. * @param size_t size, buffer size in bytes
  57. */
  58. void pcl_memset(OUT void* dst, uint8_t val, size_t size)
  59. {
  60. if(NULL == dst)abort();
  61. for(size_t i=0;i<size;i++)
  62. {
  63. CASTU8P(dst)[i]=val;
  64. }
  65. }
  66. /*
  67. * @func pcl_volatile_memset implements memset that will not be optimized by compiler
  68. * and can run before runtime initiazliation
  69. * @param OUT volatile void* dst, output volatile destination buffer
  70. * @param uint8_t val, value to set buffer
  71. * @param size_t size, buffer size in bytes
  72. */
  73. void pcl_volatile_memset(OUT volatile void* dst, uint8_t val, size_t size)
  74. {
  75. if(NULL == dst)abort();
  76. for(size_t i=0;i<size;i++)
  77. {
  78. CAST_VOLATILE_U8P(dst)[i]=val;
  79. }
  80. }
  81. /*
  82. * @func pcl_consttime_memequal compares two buffers content in constant time
  83. * Code is taken from consttime_memequal. C runtime is unavailable during PCL execution
  84. * @par IN const void *b1 - pointer to buffer 1
  85. * @par IN const void *b2 - pointer to buffer 2
  86. * @par size_t len - number of bytes to compare
  87. * @return uint32_t
  88. * 0 - if no match
  89. * 1 - if match
  90. */
  91. uint32_t pcl_consttime_memequal(IN const void *b1, IN const void *b2, size_t len)
  92. {
  93. if(NULL == b1 || NULL == b2)abort();
  94. const unsigned char *c1 = (const unsigned char *)b1, *c2 = (const unsigned char *)b2;
  95. uint32_t res = 0;
  96. while (len--)
  97. res |= *c1++ ^ *c2++;
  98. /*
  99. * Map 0 to 1 and [1, 256) to 0 using only constant-time
  100. * arithmetic.
  101. *
  102. * This is not simply `!res' because although many CPUs support
  103. * branchless conditional moves and many compilers will take
  104. * advantage of them, certain compilers generate branches on
  105. * certain CPUs for `!res'.
  106. */
  107. return (1 & ((res - 1) >> 8));
  108. }
  109. /*
  110. * g_pcl_imagebase is set by PCL at runtime to ELF base address.
  111. * It is used by functions pcl_is_outside_enclave and pcl_is_within_enclave
  112. */
  113. extern uintptr_t g_pcl_imagebase;
  114. /*
  115. * @func pcl_is_outside_enclave checks if buffer is completely outside the enclave
  116. * @par IN const void* addr - the start address of the buffer
  117. * @par size_t size - buffer size in bytes
  118. * @return int
  119. * 1 - the buffer is strictly outside the enclave
  120. * 0 - the whole buffer or part of the buffer is not outside the enclave,
  121. * or the buffer is wrap around
  122. */
  123. int pcl_is_outside_enclave(IN const void *addr, size_t size)
  124. {
  125. size_t start = (size_t)addr;
  126. size_t end = 0;
  127. size_t enclave_start = (size_t)g_pcl_imagebase;
  128. size_t enclave_end = enclave_start + g_global_data.enclave_size - 1;
  129. // g_global_data.enclave_end = enclave_base + enclave_size - 1;
  130. // so the enclave range is [enclave_start, enclave_end] inclusively
  131. if(size > 0)
  132. {
  133. end = start + size - 1;
  134. }
  135. else
  136. {
  137. end = start;
  138. }
  139. if( (start <= end) && ((end < enclave_start) || (start > enclave_end)) )
  140. {
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * @func pcl_is_outside_enclave checks if buffer is completely inside the enclave
  147. * @par IN const void* addr - the start address of the buffer
  148. * @par size_t size - buffer size in bytes
  149. * @return int
  150. * 1 - the buffer is strictly within the enclave
  151. * 0 - the whole buffer or part of the buffer is not within the enclave,
  152. * or the buffer is wrap around
  153. */
  154. int pcl_is_within_enclave(const void *addr, size_t size)
  155. {
  156. size_t start = (size_t)addr;
  157. size_t end = 0;
  158. size_t enclave_start = (size_t)g_pcl_imagebase;
  159. size_t enclave_end = enclave_start + g_global_data.enclave_size - 1;
  160. // g_global_data.enclave_end = enclave_base + enclave_size - 1;
  161. // so the enclave range is [enclave_start, enclave_end] inclusively
  162. if(size > 0)
  163. {
  164. end = start + size - 1;
  165. }
  166. else
  167. {
  168. end = start;
  169. }
  170. if( (start <= end) && (start >= enclave_start) && (end <= enclave_end) )
  171. {
  172. return 1;
  173. }
  174. return 0;
  175. }