okv.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "okv.h"
  2. #include <openssl/rand.h>
  3. #include <omp.h>
  4. #include <math.h>
  5. //opti! adjust size
  6. #define MAX_DB_SIZE 1000010
  7. vatRow db[MAX_DB_SIZE];
  8. int dbSize = 0;
  9. uint128_t seed;
  10. EVP_CIPHER_CTX *ctx[MAX_THREADS];
  11. void initializeServer(int numThreads){
  12. for(int i = 0; i < numThreads; i++){
  13. //set fixed key
  14. if(!(ctx[i] = EVP_CIPHER_CTX_new()))
  15. printf("errors occured in creating context\n");
  16. unsigned char *aeskey = (unsigned char*) "0123456789123456";
  17. if(1 != EVP_EncryptInit_ex(ctx[i], EVP_aes_128_ecb(), NULL, aeskey, NULL))
  18. printf("errors occured in init\n");
  19. EVP_CIPHER_CTX_set_padding(ctx[i], 0);
  20. }
  21. memset(&seed, 0, 16);
  22. }
  23. //creates a new DB
  24. void createDb(int isLeader, int dataSize){
  25. vatRow entry;
  26. if (isLeader == 1) {
  27. entry.newKeyA = getRandomBlock();
  28. if(!(entry.rowKeyA = EVP_CIPHER_CTX_new()))
  29. printf("errors occured in creating context\n");
  30. if(1 != EVP_EncryptInit_ex(entry.rowKeyA, EVP_aes_128_ecb(), NULL, (uint8_t*)&entry.newKeyA, NULL))
  31. printf("errors occured in init\n");
  32. EVP_CIPHER_CTX_set_padding(entry.rowKeyA, 0);
  33. } else {
  34. entry.newKeyB = getRandomBlock();
  35. if(!(entry.rowKeyB = EVP_CIPHER_CTX_new()))
  36. printf("errors occured in creating context\n");
  37. if(1 != EVP_EncryptInit_ex(entry.rowKeyB, EVP_aes_128_ecb(), NULL, (uint8_t*)&entry.newKeyB, NULL))
  38. printf("errors occured in init\n");
  39. EVP_CIPHER_CTX_set_padding(entry.rowKeyB, 0);
  40. }
  41. entry.dataSize = dataSize;
  42. entry.data = malloc(dataSize);
  43. entry.mask = malloc(dataSize);
  44. memset(entry.mask, 0 , dataSize);
  45. memset(entry.data, 0 , dataSize);
  46. db[dbSize] = entry;
  47. int i = dbSize;//to make code below work without changing stuff
  48. dbSize = dbSize + 1;
  49. //now do the encryption/rerandomization for this entry so it can be retrieved normally
  50. uint8_t* maskTemp = (uint8_t*) malloc(dataSize+16);
  51. uint8_t* seedTemp = (uint8_t*) malloc(dataSize+16);
  52. //get rerandomization mask
  53. for(int j = 0; j < (db[i].dataSize+16)/16; j++){
  54. memcpy(&seedTemp[16*j], &seed, 16);
  55. seedTemp[16*j] = seedTemp[16*j] ^ j;
  56. }
  57. int len;
  58. if (isLeader == 1) {
  59. if(1 != EVP_EncryptUpdate(db[i].rowKeyA, maskTemp, &len, seedTemp, ((dataSize-1)|15)+1))
  60. printf("errors occured in rerandomization of entry %d\n", i);
  61. } else {
  62. if(1 != EVP_EncryptUpdate(db[i].rowKeyB, maskTemp, &len, seedTemp, ((dataSize-1)|15)+1))
  63. printf("errors occured in rerandomization of entry %d\n", i);
  64. }
  65. //xor data into db and rerandomize db entry
  66. for(int j = 0; j < dataSize; j++){
  67. db[i].data[j] = db[i].data[j] ^ maskTemp[j];
  68. db[i].mask[j] = maskTemp[j];
  69. }
  70. free(maskTemp);
  71. free(seedTemp);
  72. }
  73. void xorIn(int i, uint8_t *data){
  74. for(int j = 0; j < db[i].dataSize; j++){
  75. db[i].data[j] = db[i].data[j] ^ data[j];
  76. }
  77. }
  78. void resetDb() {
  79. memset(db, 0, MAX_DB_SIZE);
  80. dbSize = 0;
  81. }
  82. //read an entry
  83. void readData(int index, uint8_t *data){
  84. memcpy(data, db[index].data, db[index].dataSize);
  85. }
  86. //gets the seed
  87. void readSeed(uint8_t *seedIn){
  88. memcpy(seedIn, &seed, 16);
  89. }
  90. //returns a uint128_t representation of the input
  91. uint128_t getUint128_t(int i){
  92. return (uint128_t) i;
  93. }
  94. //decrypt and recover a row
  95. void decryptRow(int localIndex, uint8_t *out, uint8_t *dataA, uint8_t *dataB, uint8_t *seedA, uint8_t *seedB){
  96. int len;
  97. uint8_t *maskA = (uint8_t*) malloc(db[localIndex].dataSize+16);
  98. uint8_t *maskB = (uint8_t*) malloc(db[localIndex].dataSize+16);
  99. uint8_t *seedTempA = (uint8_t*) malloc(db[localIndex].dataSize+16);
  100. uint8_t *seedTempB = (uint8_t*) malloc(db[localIndex].dataSize+16);
  101. //get the masks
  102. for(int j = 0; j < (db[localIndex].dataSize+16)/16; j++){
  103. memcpy(&seedTempA[16*j], seedA, 16);
  104. seedTempA[16*j] = seedTempA[16*j] ^ j;
  105. }
  106. if(1 != EVP_EncryptUpdate(db[localIndex].rowKeyA, maskA, &len, seedTempA, ((db[localIndex].dataSize-1)|15)+1))
  107. printf("errors occured in rerandomization of entry %d\n", localIndex);
  108. for(int j = 0; j < (db[localIndex].dataSize+16)/16; j++){
  109. memcpy(&seedTempB[16*j], seedB, 16);
  110. seedTempB[16*j] = seedTempB[16*j] ^ j;
  111. }
  112. if(1 != EVP_EncryptUpdate(db[localIndex].rowKeyB, maskB, &len, seedTempB, ((db[localIndex].dataSize-1)|15)+1))
  113. printf("errors occured in rerandomization of entry %d\n", localIndex);
  114. for(int i = 0; i < db[localIndex].dataSize; i++){
  115. out[i] = dataA[i] ^ dataB[i] ^ maskA[i] ^ maskB[i];
  116. }
  117. free(maskA);
  118. free(maskB);
  119. free(seedTempA);
  120. free(seedTempB);
  121. }
  122. //gets the block that was used to calculate the cipher
  123. void getCipher(int isLeader, int i, uint8_t *array) {
  124. if (isLeader == 1) {
  125. memcpy(array, &db[i].newKeyA, 16);
  126. } else {
  127. memcpy(array, &db[i].newKeyB, 16);
  128. }
  129. }
  130. //puts in the block that was used to calculate the cipher on the other server and calculates the missing one
  131. void putCipher(int isLeader, int i, uint8_t *array) {
  132. if (isLeader == 0) {
  133. memcpy(&db[i].newKeyA, array, 16);
  134. if(!(db[i].rowKeyA = EVP_CIPHER_CTX_new()))
  135. printf("errors occured in creating context\n");
  136. if(1 != EVP_EncryptInit_ex(db[i].rowKeyA, EVP_aes_128_ecb(), NULL, (uint8_t*)&db[i].newKeyA, NULL))
  137. printf("errors occured in init\n");
  138. EVP_CIPHER_CTX_set_padding(db[i].rowKeyA, 0);
  139. } else {
  140. memcpy(&db[i].newKeyB, array, 16);
  141. if(!(db[i].rowKeyB = EVP_CIPHER_CTX_new()))
  142. printf("errors occured in creating context\n");
  143. if(1 != EVP_EncryptInit_ex(db[i].rowKeyB, EVP_aes_128_ecb(), NULL, (uint8_t*)&db[i].newKeyB, NULL))
  144. printf("errors occured in init\n");
  145. EVP_CIPHER_CTX_set_padding(db[i].rowKeyB, 0);
  146. }
  147. }