#ifndef __UTILS_HPP__ #define __UTILS_HPP__ #ifndef BEFTS_MODE #include #include #include #include /* vsnprintf */ #include "Enclave_t.h" /* print_string */ #include #include #include #include "sgx_thread.h" #include #include "sgx_trts.h" #include #if 0 #include #include #include #include #include #include #endif #include "CONFIG.h" #include "oasm_lib.h" #else #include #endif #define CTR_INC_BITS 128 inline size_t min(size_t a, size_t b){ return (a void print_array(t array, size_t N) { for(size_t i = 0; i < N; i++) printf("%d, ", array[i]); printf("\n"); } unsigned long printf_with_rtclock(const char *fmt, ...); unsigned long printf_with_rtclock_diff(unsigned long before, const char *fmt, ...); #if 0 void displayPacket(unsigned char* packet_in); void displayZeroEncryptedPacket(unsigned char* packet_in); void displayEncryptedPacket(unsigned char* packet_in); void displayORPPacket(unsigned char* packet_in, size_t block_size); void displayKeysInBuffer(unsigned char *buffer, size_t N, size_t block_size); // Packet processing functions bool isDummy(unsigned char *ptr_to_serialized_packet); void setDummy(unsigned char *ptr_to_serialized_packet); // Test Packet Dummy bool isORPDummy(unsigned char *ptr_to_serialized_packet); void setORPDummy(unsigned char *ptr_to_serialized_packet); // BORP utility size_t packetsConsumedUptoMSN(signed long msn_no, size_t msns_with_extra_packets, size_t packets_per_entry_msn); #endif // Other utility functions int calculatelog2(uint64_t value); int calculatelog2_floor(uint64_t value); uint64_t pow2_lt(uint64_t N); uint64_t pow2_gt(uint64_t N); void merge(unsigned char *data, size_t l, size_t m, size_t r, unsigned char* (*comparator)(unsigned char*, unsigned char*)); void mergeSort(unsigned char *data, size_t data_size, size_t start_index, size_t end_index, unsigned char* (*comparator)(unsigned char*, unsigned char*)); unsigned char* compare_keys(unsigned char *packet_1, unsigned char *packet_2); #if 0 // For TightCompaction & Expansion: uint8_t isBlockReal_16(unsigned char *block_ptr); uint8_t isBlockReal_32(unsigned char *block_ptr); uint8_t isBlockReal_64(unsigned char *block_ptr); // Correctness test for new inline oswap functions uint8_t isCorrect16x(uint32_t block_size); uint8_t isCorrect8_16x(uint32_t block_size); // For BOS_TC: void swapBuckets(unsigned char *bkt1, unsigned char *bkt2, unsigned char *temp_bucket, size_t bucket_size); #endif /*** Thread pool implementation ***/ /* Implements a restricted-model thread pool. The restriction is that * every thread is the "parent" of a number of other threads (and no * thread has more than one parent). Each thread can be dispatched and * joined only by its parent, so there's no contention on the dispatch * and join inter-thread communication. A parent thread has to specify * the exact thread id of the child thread it dispatches work to. */ extern thread_local threadid_t g_thread_id; /* Create the threadpool, with numthreads-1 additional threads (numbered * 1 through numthreads-1) in addition to the current "main" thread * (numbered 0). Returns 0 on success, -1 on failure. It is allowed, but * not very useful, to pass 1 here. */ int threadpool_init(threadid_t numthreads); /* Ask all the threads to terminate, wait for that to happen, and clean * up. */ void threadpool_shutdown(); /* Dispatch some work to a particular thread in the thread pool. */ void threadpool_dispatch(threadid_t threadid, void *(*func)(void*), void *data); /* Join a thread */ void threadpool_join(threadid_t threadid, void **resp); // PRB = PseudoRandomBytes #ifdef USE_PRB class PRB_buffer{ private: sgx_aes_ctr_128bit_key_t random_seed[SGX_AESCTR_KEY_SIZE]; unsigned char counter[SGX_AESCTR_KEY_SIZE]; unsigned char random_bytes[PRB_BUFFER_SIZE]; unsigned char *random_bytes_ptr; int64_t random_bytes_left; uint64_t req_ctr; bool initialized = false; public: PRB_buffer(); sgx_status_t init_PRB_buffer(uint32_t buffer_size); /* Intended for getting random bytes of size << PRB_BUFFER_SIZE at a time. Draws random bytes from the (typically) pre-filled random_bytes[PRB_BUFFER_SIZE] buffer, refilling random_bytes[PRB_BUFFER_SIZE] when the call uses up all the PRB stored in the buffer. */ sgx_status_t getRandomBytes(unsigned char *random_bytes, size_t size); /* Intended for getting random bytes of sizes > PRB_BUFFER_SIZE at a time. Populates the random_bytes buffer directly with output of SGX_AES_CTR_ENCRYPT, without touching the pre-filled random_bytes[PRB_BUFFER_SIZE]. */ sgx_status_t getBulkRandomBytes(unsigned char *random_bytes, size_t size); }; extern thread_local PRB_buffer PRB_buf; // Spawn a PRB pool for each thread void PRB_pool_init(int nthreads); // Cleanup PRBPool void PRB_pool_shutdown(); inline sgx_status_t getRandomBytes(unsigned char *random_bytes, size_t size) { FOAV_SAFE_CNTXT(PRB, size) return(PRB_buf.getRandomBytes(random_bytes, size)); } // Return a random bit extern thread_local uint64_t PRB_rand_bits; extern thread_local uint32_t PRB_rand_bits_remaining; inline bool getRandomBit() { FOAV_SAFE_CNTXT(getRandomBit, PRB_rand_bits_remaining) if (PRB_rand_bits_remaining == 0) { getRandomBytes((unsigned char *)&PRB_rand_bits, sizeof(PRB_rand_bits)); PRB_rand_bits_remaining = 64; } bool ret = PRB_rand_bits & 1; PRB_rand_bits >>= 1; PRB_rand_bits_remaining -= 1; return ret; } sgx_status_t initialize_BRB(); sgx_status_t getBulkRandomBytes(unsigned char *buffer, size_t size); #else sgx_status_t getRandomBytes(unsigned char *random_bytes, size_t size); #endif #if 0 #include "SortingNetwork/SortingNetwork.hpp" void generateSortPermutation_OA(uint32_t N, unsigned char *buffer, size_t block_size, uint32_t *permutation); void generateSortPermutation_DJB(size_t N, unsigned char *buffer, size_t block_size, size_t *permutation); /* Generate a random permutation of range(N), and return it in random_permutation - random_permutation: a pointer to a uint64_t array. The function expects this array to have been initialized already and populates it with the random permutation. - N : the number of elements (and correspondingly the MAX+1 value) of the returned array. */ template void generateRandomPermutation(size_t N, T *random_permutation){ //Initialize random permutation as 1,...,N FOAV_SAFE_CNTXT(GRP, N) for(T i=0; i