utils.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #ifndef __UTILS_HPP__
  2. #define __UTILS_HPP__
  3. #ifndef BEFTS_MODE
  4. #include <string.h>
  5. #include <vector>
  6. #include <stdarg.h>
  7. #include <stdio.h> /* vsnprintf */
  8. #include "Enclave_t.h" /* print_string */
  9. #include <stdlib.h>
  10. #include <stdint.h>
  11. #include <math.h>
  12. #include "sgx_thread.h"
  13. #include <sgx_tcrypto.h>
  14. #include "sgx_trts.h"
  15. #include <assert.h>
  16. #if 0
  17. #include <openssl/ec.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/err.h>
  22. #include <openssl/rand.h>
  23. #endif
  24. #include "CONFIG.h"
  25. #include "oasm_lib.h"
  26. #else
  27. #include<pthread.h>
  28. #endif
  29. #define CTR_INC_BITS 128
  30. inline size_t min(size_t a, size_t b){
  31. return (a<b)? a: b;
  32. }
  33. int compare(const void *buf1, const void *buf2);
  34. // High-level oswap_buffer function that handles all buffer sizes internally
  35. void oswap_buffer(unsigned char *dest, unsigned char *source, uint32_t buffer_size, uint8_t flag);
  36. #if 0
  37. // Encrypt/Decrypt Buffers
  38. size_t decryptBuffer(unsigned char *buffer, uint64_t N, size_t block_size,
  39. unsigned char **decrypted_buffer);
  40. size_t encryptBuffer(unsigned char *buffer, uint64_t N, size_t block_size,
  41. unsigned char *encrypted_buffer);
  42. size_t decryptBuffer_attachRTags(unsigned char *encrypted_buffer, uint64_t N,
  43. size_t encrypted_block_size, unsigned char *random_bytes, unsigned char **decrypted_buffer);
  44. size_t encryptBuffer_removeRTags(unsigned char *decrypted_buffer, uint64_t N,
  45. size_t decrypted_block_size, unsigned char *encrypted_buffer);
  46. size_t decryptBuffer_attachRTags_addDummies(unsigned char *encrypted_buffer, uint64_t N,
  47. uint64_t N_prime, uint64_t B, uint64_t Z, size_t encrypted_block_size, unsigned char *random_bytes,
  48. unsigned char **decrypted_buffer);
  49. #endif
  50. // Display/Debug functions
  51. #ifndef BEFTS_MODE
  52. void printf(const char *fmt, ...);
  53. #endif
  54. template <typename t>
  55. void print_array(t array, size_t N) {
  56. for(size_t i = 0; i < N; i++)
  57. printf("%d, ", array[i]);
  58. printf("\n");
  59. }
  60. unsigned long printf_with_rtclock(const char *fmt, ...);
  61. unsigned long printf_with_rtclock_diff(unsigned long before, const char *fmt, ...);
  62. #if 0
  63. void displayPacket(unsigned char* packet_in);
  64. void displayZeroEncryptedPacket(unsigned char* packet_in);
  65. void displayEncryptedPacket(unsigned char* packet_in);
  66. void displayORPPacket(unsigned char* packet_in, size_t block_size);
  67. void displayKeysInBuffer(unsigned char *buffer, size_t N, size_t block_size);
  68. // Packet processing functions
  69. bool isDummy(unsigned char *ptr_to_serialized_packet);
  70. void setDummy(unsigned char *ptr_to_serialized_packet);
  71. // Test Packet Dummy
  72. bool isORPDummy(unsigned char *ptr_to_serialized_packet);
  73. void setORPDummy(unsigned char *ptr_to_serialized_packet);
  74. // BORP utility
  75. size_t packetsConsumedUptoMSN(signed long msn_no, size_t msns_with_extra_packets, size_t packets_per_entry_msn);
  76. #endif
  77. // Other utility functions
  78. int calculatelog2(uint64_t value);
  79. int calculatelog2_floor(uint64_t value);
  80. uint64_t pow2_lt(uint64_t N);
  81. uint64_t pow2_gt(uint64_t N);
  82. void merge(unsigned char *data, size_t l, size_t m, size_t r, unsigned char* (*comparator)(unsigned char*, unsigned char*));
  83. void mergeSort(unsigned char *data, size_t data_size, size_t start_index, size_t end_index, unsigned char* (*comparator)(unsigned char*, unsigned char*));
  84. unsigned char* compare_keys(unsigned char *packet_1, unsigned char *packet_2);
  85. #if 0
  86. // For TightCompaction & Expansion:
  87. uint8_t isBlockReal_16(unsigned char *block_ptr);
  88. uint8_t isBlockReal_32(unsigned char *block_ptr);
  89. uint8_t isBlockReal_64(unsigned char *block_ptr);
  90. // Correctness test for new inline oswap functions
  91. uint8_t isCorrect16x(uint32_t block_size);
  92. uint8_t isCorrect8_16x(uint32_t block_size);
  93. // For BOS_TC:
  94. void swapBuckets(unsigned char *bkt1, unsigned char *bkt2, unsigned char *temp_bucket, size_t bucket_size);
  95. #endif
  96. /*** Thread pool implementation ***/
  97. /* Implements a restricted-model thread pool. The restriction is that
  98. * every thread is the "parent" of a number of other threads (and no
  99. * thread has more than one parent). Each thread can be dispatched and
  100. * joined only by its parent, so there's no contention on the dispatch
  101. * and join inter-thread communication. A parent thread has to specify
  102. * the exact thread id of the child thread it dispatches work to. */
  103. extern thread_local threadid_t g_thread_id;
  104. /* Create the threadpool, with numthreads-1 additional threads (numbered
  105. * 1 through numthreads-1) in addition to the current "main" thread
  106. * (numbered 0). Returns 0 on success, -1 on failure. It is allowed, but
  107. * not very useful, to pass 1 here. */
  108. int threadpool_init(threadid_t numthreads);
  109. /* Ask all the threads to terminate, wait for that to happen, and clean
  110. * up. */
  111. void threadpool_shutdown();
  112. /* Dispatch some work to a particular thread in the thread pool. */
  113. void threadpool_dispatch(threadid_t threadid, void *(*func)(void*), void *data);
  114. /* Join a thread */
  115. void threadpool_join(threadid_t threadid, void **resp);
  116. // PRB = PseudoRandomBytes
  117. #ifdef USE_PRB
  118. class PRB_buffer{
  119. private:
  120. sgx_aes_ctr_128bit_key_t random_seed[SGX_AESCTR_KEY_SIZE];
  121. unsigned char counter[SGX_AESCTR_KEY_SIZE];
  122. unsigned char random_bytes[PRB_BUFFER_SIZE];
  123. unsigned char *random_bytes_ptr;
  124. int64_t random_bytes_left;
  125. uint64_t req_ctr;
  126. bool initialized = false;
  127. public:
  128. PRB_buffer();
  129. sgx_status_t init_PRB_buffer(uint32_t buffer_size);
  130. /* Intended for getting random bytes of size << PRB_BUFFER_SIZE at a time.
  131. Draws random bytes from the (typically) pre-filled random_bytes[PRB_BUFFER_SIZE]
  132. buffer, refilling random_bytes[PRB_BUFFER_SIZE] when the call uses up all the
  133. PRB stored in the buffer. */
  134. sgx_status_t getRandomBytes(unsigned char *random_bytes, size_t size);
  135. /* Intended for getting random bytes of sizes > PRB_BUFFER_SIZE at a time.
  136. Populates the random_bytes buffer directly with output of SGX_AES_CTR_ENCRYPT, without
  137. touching the pre-filled random_bytes[PRB_BUFFER_SIZE].
  138. */
  139. sgx_status_t getBulkRandomBytes(unsigned char *random_bytes, size_t size);
  140. };
  141. extern thread_local PRB_buffer PRB_buf;
  142. // Spawn a PRB pool for each thread
  143. void PRB_pool_init(int nthreads);
  144. // Cleanup PRBPool
  145. void PRB_pool_shutdown();
  146. inline sgx_status_t getRandomBytes(unsigned char *random_bytes, size_t size) {
  147. FOAV_SAFE_CNTXT(PRB, size)
  148. return(PRB_buf.getRandomBytes(random_bytes, size));
  149. }
  150. // Return a random bit
  151. extern thread_local uint64_t PRB_rand_bits;
  152. extern thread_local uint32_t PRB_rand_bits_remaining;
  153. inline bool getRandomBit() {
  154. FOAV_SAFE_CNTXT(getRandomBit, PRB_rand_bits_remaining)
  155. if (PRB_rand_bits_remaining == 0) {
  156. getRandomBytes((unsigned char *)&PRB_rand_bits,
  157. sizeof(PRB_rand_bits));
  158. PRB_rand_bits_remaining = 64;
  159. }
  160. bool ret = PRB_rand_bits & 1;
  161. PRB_rand_bits >>= 1;
  162. PRB_rand_bits_remaining -= 1;
  163. return ret;
  164. }
  165. sgx_status_t initialize_BRB();
  166. sgx_status_t getBulkRandomBytes(unsigned char *buffer, size_t size);
  167. #else
  168. sgx_status_t getRandomBytes(unsigned char *random_bytes, size_t size);
  169. #endif
  170. #if 0
  171. #include "SortingNetwork/SortingNetwork.hpp"
  172. void generateSortPermutation_OA(uint32_t N, unsigned char *buffer, size_t block_size, uint32_t *permutation);
  173. void generateSortPermutation_DJB(size_t N, unsigned char *buffer, size_t block_size, size_t *permutation);
  174. /*
  175. Generate a random permutation of range(N), and return it in
  176. random_permutation
  177. - random_permutation: a pointer to a uint64_t array. The function expects
  178. this array to have been initialized already and populates it with the
  179. random permutation.
  180. - N : the number of elements (and correspondingly the MAX+1 value) of the
  181. returned array.
  182. */
  183. template <typename T>
  184. void generateRandomPermutation(size_t N, T *random_permutation){
  185. //Initialize random permutation as 1,...,N
  186. FOAV_SAFE_CNTXT(GRP, N)
  187. for(T i=0; i<N; i++) {
  188. FOAV_SAFE_CNTXT(i, N)
  189. random_permutation[i]=i;
  190. }
  191. //Convert it to a random permutation of [1,N]
  192. RecursiveShuffle_M2((unsigned char*) random_permutation, N, sizeof(T));
  193. /*
  194. random_permutation[0] = 8;
  195. random_permutation[1] = 12;
  196. random_permutation[2] = 6;
  197. random_permutation[3] = 29;
  198. random_permutation[4] = 22;
  199. random_permutation[5] = 0;
  200. random_permutation[6] = 15;
  201. random_permutation[7] = 24;
  202. random_permutation[8] = 30;
  203. random_permutation[9] = 19;
  204. random_permutation[10] = 13;
  205. random_permutation[11] = 28;
  206. random_permutation[12] = 7;
  207. random_permutation[13] = 17;
  208. random_permutation[14] = 14;
  209. random_permutation[15] = 27;
  210. random_permutation[16] = 18;
  211. random_permutation[17] = 25;
  212. random_permutation[18] = 5;
  213. random_permutation[19] = 10;
  214. random_permutation[20] = 23;
  215. random_permutation[21] = 2;
  216. random_permutation[22] = 4;
  217. random_permutation[23] = 9;
  218. random_permutation[24] = 26;
  219. random_permutation[25] = 1;
  220. random_permutation[26] = 21;
  221. random_permutation[27] = 3;
  222. random_permutation[28] = 20;
  223. random_permutation[29] = 16;
  224. random_permutation[30] = 11;
  225. random_permutation[31] = 31;
  226. */
  227. /*
  228. printf("\nPermutation output\n");
  229. for(T i=0; i<N; i++)
  230. printf("%ld, ", random_permutation[i]);
  231. printf("\n");
  232. */
  233. }
  234. #endif
  235. #define __OSORT_UTILS__
  236. #endif