int main(int argc, char** argv) { Build_DBMS(tab_serv); int List_request[10]; for(int i = 0; i < 10; i++) { List_request[i] = -1; } int col, position_request = 0; do { printf("\nPlease input the position you want to read:\n" " > (Request max 10)\n" " > (To Stop typing, input -1 for y)\n" ">>> y (ordinate) :\n"); scanf("%d", &col); if(col == -1) { break; } else if(col >= MAX_GRID_SIZE || col < -1) { printf("Value exceed the limit, try again.\n\n"); } else { List_request[position_request] = col; position_request++; } }while(position_request < 10 || col >= MAX_GRID_SIZE); printf("\nComing soon...\n\n"); mpz_t KPri[3]; position_request = 0; do { ClientKey(KPri); Client_Send_Encrypt(tab_cli, List_request[position_request]); mpz_init(valueReturn); Serveur(valueReturn, tab_serv, tab_cli); Client_Receive_Decrypt(valueReturn, KPri); gmp_printf ("\nvalueReturn : %Zd\n", valueReturn); position_request++; } while(position_request < 10 && List_request[position_request] != -1); for(int i = 0; i < 3; i++) { mpz_clear(KPri[i]); mpz_clear(KPub[i]); } for(int i = 0; i < MAX_GRID_SIZE; i++) { mpz_clear(tab_cli[i]); mpz_clear(tab_serv[i]); } mpz_clear(valueReturn); }
int main() { mpz_t KPri[3]; ClientKey(KPri); int option = 0; Choice: do { printf("\n---------------------------------\n" "Enter the dimension of your DBMS:\n" "[ 1] for one dimension\n" "[ 2] for two dimensions\n" "[ 3] for three dimensions\n" "[ 4] for x dimensions (x >= 3)\n" "[-1] for exit (you could input -1 for exit at anyplace)\n" "\nYour option: "); scanf("%d", &option); switch (option) { case 1: { mpz_t tab_serv [MAX_GRID_SIZE]; mpz_t tab_cli [MAX_GRID_SIZE]; mpz_t valueReturn; int col = 0; do { if (col > (MAX_GRID_SIZE - 1)) { printf("\nValue of row or col too big, " "please retry with another.\n"); } printf("\nPlease input the position you want to read " "(Request max %d):\n" ">>> x (abscissa) : ", MAX_GRID_SIZE - 1); scanf("%d", &col); } while(col > (MAX_GRID_SIZE - 1)); Build_DBMS(tab_serv); Client_Send_Encrypt(tab_cli, col); mpz_init(valueReturn); Serveur(valueReturn, tab_serv, tab_cli); Client_Receive_Decrypt(valueReturn, KPri); gmp_printf ("\nvalueReturn : %Zd\n", valueReturn); for(int i = 0; i < MAX_GRID_SIZE; i++) { mpz_clear(tab_cli[i]); mpz_clear(tab_serv[i]); } mpz_clear(valueReturn); break; } case 2: { mpz_t tab_serv_2D [MAX_GRID_SIZE][MAX_GRID_SIZE]; mpz_t tab_cli_row [MAX_GRID_SIZE]; mpz_t tab_cli_col [MAX_GRID_SIZE]; int row = 0, col = 0; do { if (row > (MAX_GRID_SIZE - 1) || col > (MAX_GRID_SIZE - 1)) { printf("\nValue of row or col too big, " "please retry with another.\n"); } printf("\nPlease input the position you want to read " "(Request max %d):\n" ">>> x (abscissa) : ", MAX_GRID_SIZE - 1); scanf("%d", &row); printf(">>> y (ordinate) : "); scanf("%d", &col); } while(row > (MAX_GRID_SIZE - 1) || col > (MAX_GRID_SIZE - 1)); Build_DBMS_2D(tab_serv_2D); mpz_t u, v; Client_Send_Encrypt_2D(tab_cli_row, tab_cli_col, row, col); mpz_init(u); mpz_init(v); Serveur_2D(u, v, tab_serv_2D, tab_cli_row, tab_cli_col); Client_Receive_Decrypt_2D(v, u, KPri); gmp_printf ("\nvalueReturn : %Zd\n", u); for(int i = 0; i < MAX_GRID_SIZE; i++) { mpz_clear(tab_cli_col[i]); mpz_clear(tab_cli_row[i]); for(int j = 0; j < MAX_GRID_SIZE; j++) { mpz_clear(tab_serv_2D[i][j]); } } break; } case 3: { mpz_t tab_serv_3D [MAX_GRID_SIZE][MAX_GRID_SIZE][MAX_GRID_SIZE]; mpz_t tab_cli_row [MAX_GRID_SIZE]; mpz_t tab_cli_col [MAX_GRID_SIZE]; mpz_t tab_cli_layer [MAX_GRID_SIZE]; int row = 0, col = 0, layer = 0; do { if (row > (MAX_GRID_SIZE - 1) || col > (MAX_GRID_SIZE - 1) || layer > (MAX_GRID_SIZE - 1)) { printf("\nValue of row or col too big, " "please retry with another.\n"); } printf("\nPlease input the position you want to read " "(Request max %d):\n" ">>> x (abscissa) : ", MAX_GRID_SIZE - 1); scanf("%d", &row); printf(">>> y (ordinate) : "); scanf("%d", &col); printf(">>> z (applicate) : "); scanf("%d", &layer); } while(row > (MAX_GRID_SIZE - 1) || col > (MAX_GRID_SIZE - 1) || layer > (MAX_GRID_SIZE - 1)); Build_DBMS_3D(tab_serv_3D); mpz_t uu, uv, vu, vv; Client_Send_Encrypt_3D(tab_cli_row, tab_cli_col, tab_cli_layer, row, col, layer); mpz_init(uu); mpz_init(uv); mpz_init(vu); mpz_init(vv); Serveur_3D(uu, uv, vu, vv, tab_serv_3D, tab_cli_row, tab_cli_col, tab_cli_layer); Client_Receive_Decrypt_3D(uu, uv, vu, vv, KPri); gmp_printf ("\nvalueReturn : %Zd\n", uu); for(int i = 0; i < MAX_GRID_SIZE; i++) { mpz_clear(tab_cli_col[i]); mpz_clear(tab_cli_row[i]); mpz_clear(tab_cli_layer[i]); for(int j = 0; j < MAX_GRID_SIZE; j++) { for(int k = 0; k < MAX_GRID_SIZE; k++) { mpz_clear(tab_serv_3D[i][j][k]); } } } mpz_clear(uu); mpz_clear(uv); mpz_clear(vu); mpz_clear(vv); break; } case 4: { int dim = 0; printf("\nPlease input the dimension of your data in server: \n"); scanf("%d", &dim); if(dim == -1) { goto bye; } if(dim < 3) { goto Choice; } int lenth = pow(MAX_GRID_SIZE, dim); mpz_t * tab_serv = malloc(sizeof(mpz_t) * lenth); Build_DBMS_xD(lenth, tab_serv); mpz_t ** tab_cli = malloc(sizeof(mpz_t *) * dim); int positions[dim]; printf("\nPlease input the position you want to read " "(Request max %d):\n", MAX_GRID_SIZE - 1); for(int i = 0; i < dim; i++) { do { printf("The %d element of your coordinates: ", i + 1); scanf("%d", &positions[i]); }while(positions[i] > (MAX_GRID_SIZE - 1) ); if(positions[i] == -1) { goto bye; } } Client_Send_Encrypt_xD(dim, tab_cli, positions); mpz_t Answer[(int)pow(2, (dim - 1))]; for (int j = 0; j < (int)pow (2, (dim - 1)); j++) { mpz_init (Answer[j]); } Serveur_xD(dim, tab_cli, Answer, tab_serv); Client_Receive_Decrypt_xD(dim, Answer, KPri); gmp_printf ("\nvalueReturn : %Zd\n", Answer[0]); /* Free part */ for(int i = 0; i < (int)lenth; i++) { mpz_clear(tab_serv[i]); } free(tab_serv); for(int i = 0; i < dim; i++) { free(tab_cli[i]); } free(tab_cli); for (int j = 0; j < (int)pow (2, (dim - 1)); j++) { mpz_clear(Answer[j]); } break; } case -1: { bye: { printf("\nBye ! :)\n\n"); exit(EXIT_SUCCESS); } } default: { printf("\nError: Wrong option, retry with a valid option !\n"); break; } } }while(option != -1); for(int i = 0; i < 3; i++) { mpz_clear(KPri[i]); mpz_clear(KPub[i]); } }
// Helper Funcitons void Socket::ClientHandshake() { std::cout << "Client Handshake" << std::endl; unsigned char previous = 0; bool bEncrypted = false; std::vector<unsigned char> handshakeData; bool bDone = false; while( !bDone ) { Record *record; if( !bEncrypted ) { record = new Record(); }else{ record = new Record( mClient ); } record->Read( *mSocket ); // Alert if( record->GetType() == Record::Alert ){ std::vector<unsigned char> data = record->GetData(); std::cerr << "Alert: " << std::hex << std::setw( 2 ) << std::setfill('0') << (int)data[0] << ", " << std::hex << std::setw( 2 ) << std::setfill('0') << (int)data[1] << std::endl; exit( EXIT_FAILURE ); } // Cipher if( record->GetType() == Record::Cipher ){ if( previous == Handshake::Type::ClientKey ){ bEncrypted = true; } previous = Record::Cipher; } // Handshake if( record->GetType() == Record::Handshake ){ std::vector<unsigned char> data = record->GetData(); if( previous == 0 ){ if( data[0] == Handshake::Type::ClientHello ){ Handshake::ClientHello hClientHello; hClientHello.Deserialize( std::vector<unsigned char>( data.begin() + 4, data.end() ) ); ClientRandom = hClientHello.Random(); handshakeData.insert( handshakeData.end(), data.begin(), data.end() ); Handshake::ServerHello hServerHello( 0x00, 0x35, 0x00 ); Record rServerHello( Record::Handshake, hServerHello.Serialize() ); rServerHello.Write( *mSocket ); ServerRandom = hServerHello.Random(); std::vector<unsigned char> dServerHello = hServerHello.Serialize(); handshakeData.insert( handshakeData.end(), dServerHello.begin(), dServerHello.end() ); Handshake::Certificate hCertificate( mCertificatePath ); Record rCertificate( Record::Handshake, hCertificate.Serialize() ); rCertificate.Write( *mSocket ); std::vector<unsigned char> dCertificate = hCertificate.Serialize(); handshakeData.insert( handshakeData.end(), dCertificate.begin(), dCertificate.end() ); std::vector<unsigned char> dServerDone; // Insert Size unsigned int size = htonl( dServerDone.size() ); unsigned char* cSize = (unsigned char*)&size; dServerDone.insert( dServerDone.begin(), cSize[3] ); dServerDone.insert( dServerDone.begin(), cSize[2] ); dServerDone.insert( dServerDone.begin(), cSize[1] ); // Insert Type dServerDone.insert( dServerDone.begin(), Handshake::Type::ServerDone ); Record rServerDone( Record::Handshake, dServerDone ); rServerDone.Write( *mSocket ); handshakeData.insert( handshakeData.end(), dServerDone.begin(), dServerDone.end() ); } previous = data[0]; } if( previous == Handshake::Type::ClientHello ){ if( data[0] == Handshake::Type::ClientKey ){ Handshake::ClientKeyExchange hClientKeyExchange( mKeyPath ); hClientKeyExchange.Deserialize( std::vector<unsigned char>( data.begin() + 4, data.end() ) ); handshakeData.insert( handshakeData.end(), data.begin(), data.end() ); DataArray secret = hClientKeyExchange.Secret(); MasterKey = PRF( 48, secret, "master secret", Concatonate( ClientRandom, ServerRandom ) ); KeyBlock = PRF( 136, MasterKey, "key expansion", Concatonate( ServerRandom, ClientRandom ) ); DataArray ClientMAC( KeyBlock.begin(), KeyBlock.begin() + 20 ); DataArray ServerMAC( KeyBlock.begin() + 20, KeyBlock.begin() + 40 ); DataArray ClientKey( KeyBlock.begin() + 40, KeyBlock.begin() + 72 ); DataArray ClientIV( KeyBlock.begin() + 104, KeyBlock.begin() + 120 ); mClient->Initialise( ClientMAC, ClientKey , ClientIV ); DataArray ServerKey( KeyBlock.begin() + 72, KeyBlock.begin() + 104 ); DataArray ServerIV( KeyBlock.begin() + 120, KeyBlock.begin() + 136 ); mServer->Initialise( ServerMAC, ServerKey , ServerIV ); } previous = data[0]; } if( previous == Record::Cipher ){ if( data[0] == Handshake::Type::Finished ){ Handshake::Finished hFinished; hFinished.Deserialize( std::vector<unsigned char>( data.begin() + 4, data.end() ) ); unsigned char md5hash[MD5_DIGEST_LENGTH]; unsigned char shahash[SHA_DIGEST_LENGTH]; MD5( &handshakeData[0], handshakeData.size(), md5hash ); SHA1( &handshakeData[0], handshakeData.size(), shahash ); std::vector<unsigned char> signiture; signiture.insert( signiture.end(), md5hash, md5hash + 16 ); signiture.insert( signiture.end(), shahash, shahash + 20 ); DataArray clientprf = PRF( 12, MasterKey, "client finished", signiture ); DataArray clientfin = std::vector<unsigned char>( data.begin() + 4, data.end() ); bool bDiff = false; for( unsigned int i = 0; i < clientfin.size(); i++ ){ if( clientfin[i] != clientprf[i] ) { bDiff = true; break; } } if( bDiff ) { std::cout << "Finished Different" << std::endl; std::cout << "Finish Should Be: " << std::dec << (int)clientprf.size() << std::endl; for( unsigned int i = 0 ; i < clientprf.size(); i++ ){ std::cout << std::hex << std::setw( 2 ) << std::setfill('0') << (int) clientprf[i] << " ";; if( (i + 1) % 16 == 0 ) std::cout << std::endl; } std::cout << std::endl; std::cout << "Finish Is: " << std::dec << (int)clientfin.size() << std::endl; for( unsigned int i = 0 ; i < clientfin.size(); i++ ){ std::cout << std::hex << std::setw( 2 ) << std::setfill('0') << (int) clientfin[i] << " ";; if( (i + 1) % 16 == 0 ) std::cout << std::endl; } std::cout << std::endl; } handshakeData.insert( handshakeData.end(), data.begin(), data.end() ); std::vector<unsigned char> dChangeCipher; dChangeCipher.push_back( 1 ); Record rChangeCipher( Record::Cipher, dChangeCipher ); rChangeCipher.Write( *mSocket ); MD5( &handshakeData[0], handshakeData.size(), md5hash ); SHA1( &handshakeData[0], handshakeData.size(), shahash ); std::vector<unsigned char> finSig; finSig.insert( finSig.end(), md5hash, md5hash + 16 ); finSig.insert( finSig.end(), shahash, shahash + 20 ); DataArray serverprf = PRF( 12, MasterKey, "server finished", finSig ); Record rServerFinished( Record::Handshake, Handshake::Finished( serverprf ).Serialize(), mServer ); rServerFinished.Write( *mSocket ); bDone = true; } previous = data[0]; } } } std::cout << "Finished " << this << " " << mSocket << std::endl; }