Exemple #1
0
static std::string
randomBucketName(std::string const& tmpDir)
{
    for (;;)
    {
        std::string name =
            tmpDir + "/tmp-bucket-" + binToHex(randomBytes(8)) + ".xdr";
        std::ifstream ifile(name);
        if (!ifile)
        {
            return name;
        }
    }
}
void writeRandom(tSectorId sid) {
	LogicalSector *ls;
	char *content;
	if (sectorInvalido(sid)) {
			if (isDebugEnabled()) errorInvalidSector(sid);
			return;
		}
	content = randomBytes(SECTOR_SIZE , getGlobalHeap());
	ls = sector_BuildLogical(sid, content);
	putSectorOnDisk(ls);
	content[SECTOR_SIZE-1] = '\0';
	printf ("Sector escrito: %d\n", sid);
	printf ("Contenido: %s\n", content);
	myFree(ls);
}
void writeRandom(tSectorId sid) {
	LogicalSector *ls;
	char *content;
	if (sectorInvalido(sid)) {
			if (isDebugEnabled()) errorInvalidSector(sid);
			return;
		}
	content = randomBytes(SECTOR_SIZE);
	ls = sector_BuildLogical(sid, content);
	putSectorOnDisk(ls);
	//vdadisk_write(getVdaName(), ls);
	printf ("Sector escrito: %d\n", sid);
	printf ("Contenido: %s\n", content);
	myFree(ls);
}
	char * generateRandomKey(int size , HeapHandler hh){		
		return randomBytes(size , hh);
	}
Exemple #5
0
/* Generate new salt/hash combinations for verification of the given
 * file.  I.e the client can download their file, then call this function
 * to give them another NUM_HASHES new verification calls */
int clientRefreshHashes(BIO *conn, char *filename) {

	//download the file from the server (but dont decrypt it)
	//this saves it as TEMP_ENCRYPTED_FILENAME
	int status = clientDownloadFile(conn, filename, 0);
	if(status != 0) return status;

	//we need to store NUM_HASHES salts and digests for later verification
	unsigned char *salts[NUM_HASHES];
	unsigned char *hashes[NUM_HASHES];

	//get the deatils of this file
	FILERECORD *record = getRecord(filename);
	if(record == NULL) return -1;

	//check the file matches the hashes we have stored
	//(to stop the server switching the file on us as we update the hashes)
	for(int i=0; i < NUM_HASHES; ++i) {
		unsigned char *recordHash = record->hashData[i];
		unsigned char *salt = record->hashData[i] + HASH_LENGTH;

		unsigned char *hash = calculateMD5(TEMP_ENCRYPTED_FILENAME, salt, SALT_LENGTH);
		if(memcmp(recordHash, hash, HASH_LENGTH) != 0) {
			fprintf(stderr, "Mismatching hash for downloaded file during clientRefreshHashes()\n");
			free(hash);
			return -1;
		}
		free(hash);
	}

	//generate new salts and hashes to give us NUM_HASHES more calls to clientVerifyFile
	for(int i=0; i<NUM_HASHES; ++i) {
		//generate a random salt
		salts[i] = randomBytes(SALT_LENGTH);
		//compute the digest for the file with that salt
		hashes[i] = calculateMD5(TEMP_ENCRYPTED_FILENAME, salts[i], SALT_LENGTH);
		if(hashes[i] == NULL) {
			fprintf(stderr, "Failed to calculate digest in clientRefreshHashes()\n");
			return -1;
		}
	}

	//we no longer need the copy we downloaded
	unlink(TEMP_ENCRYPTED_FILENAME);

	//update the record with these new hashes and reset the hashIndex
	status = addRecord(filename, 0, hashes, salts, record->key, record->iv);
	if(status == -1) {
		fprintf(stderr, "Failed to update record for %s in clientRefreshHashes()\n", filename);
		return -1;
	}

	//free the salts and hashes we generated
	for(int i=0; i < NUM_HASHES; ++i) {
		free(salts[i]);
		free(hashes[i]);
	}

	printf("Verified the file (all %d digests match),\n", NUM_HASHES);
	printf("and generated %d new digests.\n", NUM_HASHES);
	
	return 0;	
}
Exemple #6
0
int clientUploadFile(BIO *conn, char *filename) {

	/* CALCULATE AND STORE ANY RECORDS OF THIS FILE WE NEED
	 * BEFORE UPLOADING IT */

	FILE *ifp = fopen(filename, "rb");
	if ( ifp == NULL )  return NO_SUCH_FILE;
	
	//generate key and iv for encryption
	unsigned char *key = randomBytes(32);
	unsigned char *iv = randomBytes(32);
	
	//encrypt the file
	int status = encryptFile(filename, TEMP_ENCRYPTED_FILENAME, key, iv);
	if(status == -1) {
		fprintf(stderr, "Failed to encrypt %s in clientUploadFile()\n", filename);
		return -1;
	}

	//we need to store NUM_HASHES salts and digests for later verification
	unsigned char *salts[NUM_HASHES];
	unsigned char *hashes[NUM_HASHES];

	for(int i=0; i<NUM_HASHES; ++i) {
		//generate a random salt
		salts[i] = randomBytes(SALT_LENGTH);
		//compute the digest for the file with that salt
		hashes[i] = calculateMD5(TEMP_ENCRYPTED_FILENAME, salts[i], SALT_LENGTH);
		if(hashes[i] == NULL) {
			fprintf(stderr, "Failed to calculate digest in clientUploadFile()\n");
			return -1;
		}
	}
	
	//store all this data for later
	status = addRecord(filename, 0, hashes, salts, key, iv);
	if(status == -1) {
		fprintf(stderr, "addRecord() failed for in clientUploadFile()\n");
		return -1;
	}
	
	//free the memory we allocated above
	for(int i=0; i < NUM_HASHES; ++i) {
		free(salts[i]);
		free(hashes[i]);
	}
	free(key);
	free(iv);

	/* START THE ACTUAL COMMUNICATION WITH THE SERVER */
	
	//send the code which causes the server to call serverUploadFile()
	if(writeInt(conn, UPLOAD_FILE_CODE) == -1) return -1;

	//send the fileSize
	int fileSize = sizeOfFile(TEMP_ENCRYPTED_FILENAME);
	if(writeInt(conn, fileSize) == -1) return -1;

	printf("NOTE: Original size: %f MB. Encrypted size: %f MB.\n", (double)sizeOfFile(filename)/MEGABYTE, (double)fileSize/MEGABYTE);


	//wait for an int telling us the balance owing
	unsigned int fee = readInt(conn);
	if(fee > 0) {
		printf("Purchase %d more cloud dollar(s) to upload this file.\n", fee);
		removeRecord(filename);
		return -1;
	}
	else if(fee < 0) return -1;

	//send the file
	if(writeFile(conn, TEMP_ENCRYPTED_FILENAME, filename) < 1) return -1;
	unlink( TEMP_ENCRYPTED_FILENAME );
	printf("Succesfully uploaded the file.\n");
	return 0;
}