void testFactory(vector<char> data, seconds duration, seconds maxTimeToTest, int id) { Puzzle p; HardwareSpeedTester hst(1000, maxTimeToTest); TimeCapsuleFactory<char> tcf(hst); //char rawdata[] = "Hey, I hope it will finish in time.. tho probably not :D ..."; auto capsule = tcf.createTimeCapsule(p, data, duration); capsule.save("capsule_" + to_string(id) + ".dat"); Capsule<char> capsule2; capsule2.load("capsule_" + to_string(id) + ".dat"); auto crdata = capsule.getCryptedData(); auto crdata2 = capsule2.getCryptedData(); assert(capsule.getBase() == capsule2.getBase()); assert(capsule.getN() == capsule2.getN()); assert(capsule.getIV() == capsule2.getIV()); assert(capsule.getCryptedKey() == capsule2.getCryptedKey()); assert(capsule.getNumberOfOperations() == capsule2.getNumberOfOperations()); Logger::log("Original crypted data size: " + to_string(crdata.size())); Logger::log("Copypasted crypted data size: " + to_string(crdata2.size())); string datastr(crdata.begin(), crdata.end()); string datastr2(crdata2.begin(), crdata2.end()); Logger::log("Original crypted data: " + datastr); Logger::log("Copypasted crypted data: " + datastr2); //cout << capsule.getCryptedData().size() << ", " << capsule2.getCryptedData().size() << endl; //assert(capsule.getCryptedData().size() == capsule2.getCryptedData().size()); //for (size_t i = 0; i < capsule.getCryptedData().size(); ++i) // assert(capsule.getCryptedData()[i] == capsule2.getCryptedData()[i]); Puzzle p2(capsule2.getBase()); auto start = chrono::high_resolution_clock::now(); auto key = p2.solve(capsule2.getCryptedKey(), capsule2.getNumberOfOperations(), capsule2.getN()); auto end = chrono::high_resolution_clock::now(); Logger::log("Time specified to decode: " + to_string(duration.count()) + " seconds"); Logger::log("Time taken to decode: " + to_string(chrono::duration_cast<seconds>(end - start).count()) + " seconds"); Encryptor<char> cr; auto databack = cr.decrypt(capsule2.getCryptedData(), key, capsule2.getIV()); string tmpdata(data.begin(), data.end()); string tmpdataback(databack.begin(), databack.end()); Logger::log("Original data: " + tmpdata); Logger::log("Data decrypted: " + tmpdataback); /*char dummychar; if (crdata.size() != crdata2.size()) { Logger::log("PARA VAN!"); cin >> dummychar; }*/ }
PlaintextMessage EncryptedMessage::decrypt(const G2& P, const G2& Ppub, G1 D, PFC *pfc) { G2 uCalc; G2 U = (*autData).getU(); Big ud_hash = (*pfc).hash_to_aes_key((*pfc).pairing(U,D)); Big ses_key; Big r; int nbOfRecipients = (*autData).getNbOfRecipients(); Big W; Big V = (*autData).getV(); Big rho, rho_hash; vector <Big> ws = (*autData).getEncryptedRecipientKeys(); char P_text[Clen]; bool integrity = false; time_t begin_time = clock(); int i = 0; while(U != uCalc && i < nbOfRecipients){ // rho = V XOR Hash(e(D,U)) W=ws.at(i); rho = lxor(W, ud_hash); // M = W XOR Hash(rho) (*pfc).start_hash(); (*pfc).add_to_hash(rho); rho_hash = (*pfc).finish_hash_to_group(); ses_key = lxor(V, rho_hash); // r = Hash(rho,M) (*pfc).start_hash(); (*pfc).add_to_hash(rho); (*pfc).add_to_hash(ses_key); r = (*pfc).finish_hash_to_group(); uCalc = (*pfc).mult(P,r); i++; } cout << "ses_key is " << endl << ses_key << endl; /************************************************* * AES GCM part of the decryption step * **************************************************/ to_binary(ses_key, HASH_LEN, sessionKey, TRUE); char k1[HASH_LEN/2]; char iv[HASH_LEN/2]; char Tdec[TAG_LEN]; memset(P_text, 0, Clen+1); getIV(iv); getK1(k1); int Alen = (*autData).getLength(); char A[Alen]; (*autData).encodeTo(A); gcm g; gcm_init(&g, HASH_LEN/2, k1, HASH_LEN/2, iv); gcm_add_header(&g, A, Alen); gcm_add_cipher(&g, GCM_DECRYPTING, P_text, Clen, C); gcm_finish(&g, Tdec); integrity = true; for (int j = 0; j < TAG_LEN; j++) { if(Tdec[j] != T[j]) { integrity = false; } } if(integrity == false) { cout << "Received tag T does not correspond to decrypted T. There are some integrity issues here." << endl; } else { cout << "Successful integrity check!" << endl; } message = (string)P_text; return PlaintextMessage(message); }
bool handleKeys(arguments* args, ThreefishKey_t* cipher_context, MacCtx_t* mac_context) { pdebug("handleKeys()\n"); //sanity check if (args == NULL || cipher_context == NULL || mac_context == NULL) { return false; } const uint64_t block_byte_size = (uint64_t)args->state_size/8; uint64_t* cipher_key = NULL; uint64_t* mac_key = NULL; //no hash password (bad idea) if (args->hash == false && args->hash_from_file == false) { cipher_key = noHashKey(args->password, args->pw_length, args->state_size); } //no hash from file (bad idea) else if (args->hash == false && args->hash_from_file == true) { cipher_key = noHashBlockFromFile(args->key_file, args->state_size); } //hash user entered password to be hashed else if (args->hash == true && args->hash_from_file == false) { cipher_key = (uint64_t*)keyHash(args->password, args->pw_length, args->state_size); } //hash user entered password from file else if (args->hash == true && args->hash_from_file == true) { printf("Hashing key from file... "); cipher_key = hashKeyFromFile(args->key_file, args->state_size); printf("Done\n"); } if (cipher_key == NULL) { return false; } //failure //Generate IV if we are encrypting if (args->encrypt == true) { if (genIV(args) == false) { return false; } //can't continue without an IV } else //and get it from the first part of the file if we are decrypting { if (getIV(args) == false) { return false; } } //stretch the key with scrypt if (args->hash == true && args->legacy_hash == false) { printf("Stretching key this may take a bit... "); if (kdf_scrypt((uint8_t*)cipher_key, (size_t)(args->state_size/8), (uint8_t*)args->iv, (size_t)(args->state_size/8), SCRYPT_N, SCRYPT_R, SCRYPT_P, (uint8_t*)cipher_key, (size_t)(args->state_size/8) ) != 0) { return false; } //scrypt failed printf("Done\n"); } //generate the mac key from the cipher_key mac_key = (uint64_t*)keyHash((uint8_t*)cipher_key, block_byte_size, args->state_size); //initialize the key structure for the cipher key threefishSetKey(cipher_context, (ThreefishSize_t)args->state_size, cipher_key, threefizer_tweak); //initialize the mac context and undelying skein structures InitMacCtx(args, mac_context, mac_key); //free allocated resources if (cipher_key != NULL) { free(cipher_key); } if (mac_key != NULL) { free(mac_key); } return true; }