exception_t* serpent_serial_encrypt(key256_t* user_key, block128_t* blocks, int block_count) {
	char* function_name = "serpent_serial_encrypt()";
	exception_t* exception;
	uint32_t* subkey;

	// Validate parameters.
	#ifdef DEBUG_SERPENT
	if ( user_key == NULL ) {
		return exception_throw("user_key was NULL.", function_name);
	}
	if ( blocks == NULL ) {
		return exception_throw("blocks was NULL.", function_name);
	}
	#endif

	// Initialize the subkey.
	exception = serpent_init_subkey(user_key, &subkey);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Encrypt each block.
	for ( int i = 0; i < block_count; i++ ) {
		serpent_encrypt_block(&(blocks[i]), subkey);
	}

	// Free the subkey.
	exception = serpent_free_subkey(subkey);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Return success.
	return NULL;
}
Пример #2
0
exception_t* block128_read(int fd, block128_t* block) {
	char* function_name = "block128_read";
	uint32_t buffer;

	// Validate parameters.
	if ( block == NULL ) {
		return exception_throw("block was NULL.", function_name);
	}

	// Read data in from the file.
	if ( read(fd, &buffer, sizeof(buffer)) == -1 ) {
		perror(NULL);
		return exception_throw("Read 1 FAILED.", function_name);
	}
	block->x0 = buffer;
	if ( read(fd, &buffer, sizeof(buffer)) == -1 ) {
		perror(NULL);
		return exception_throw("Read 2 FAILED.", function_name);
	}
	block->x1 = buffer;
	if ( read(fd, &buffer, sizeof(buffer)) == -1 ) {
		perror(NULL);
		return exception_throw("Read 3 FAILED.", function_name);
	}
	block->x2 = buffer;
	if ( read(fd, &buffer, sizeof(buffer)) == -1 ) {
		perror(NULL);
		return exception_throw("Read 4 FAILED.", function_name);
	}
	block->x3 = buffer;

	// Return success.
	return NULL;
}
Пример #3
0
exception_t* file_write_padding(file_t* file) {
	char* function_name = "file_write_padding()";
	char buffer = '\0';

	// Validate parameters.
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}
	if ( file->flag != UNENCRYPTED ) {
		return exception_throw("file must be unencrypted.", function_name);
	}

	// Seek to the end of the file.
	if ( lseek64(file->fd, 0, SEEK_END) == -1 ) {
		return exception_throw("Unable to seek to end of file.", function_name);
	}

	// Write padding to the end of the file.
	// This is a terrible way to do this, but it will work!
	file->size += file->padding;
	for ( int i = 0; i < file->padding; i++ ) {
		if ( write(file->fd, &buffer, sizeof(buffer)) == -1 ) {
			return exception_throw("Unable to write padding.", function_name);
		}
	}

	// Return success.
	return NULL;
}
Пример #4
0
exception_t* block128_write(int fd, block128_t* block) {
	char* function_name = "block128_write";

	// Validate parameters.
	if ( block == NULL ) {
		return exception_throw("block was NULL.", function_name);
	}

	// Write data to the file.
	if ( write(fd, &block->x0, sizeof(block->x0)) == -1 ) {
		perror(function_name);
		return exception_throw("Write 1 FAILED.", function_name);
	}
	if ( write(fd, &block->x1, sizeof(block->x1)) == -1 ) {
		perror(function_name);
		return exception_throw("Write 2 FAILED.", function_name);
	}
	if ( write(fd, &block->x2, sizeof(block->x2)) == -1 ) {
		perror(function_name);
		return exception_throw("Write 3 FAILED.", function_name);
	}
	if ( write(fd, &block->x3, sizeof(block->x3)) == -1 ) {
		perror(function_name);
		return exception_throw("Write 4 FAILED.", function_name);
	}

	// Return success.
	return NULL;
}
exception_t* serpent_parallel_decrypt(key256_t* user_key, block128_t* blocks, int block_count) {
	char* function_name = "serpent_parallel_decrypt()";
	exception_t* exception;
	uint32_t* subkey;
	int blocks_per_thread;
	int thread_count;
	int thread_index;

	// Validate parameters.
	#ifdef DEBUG_SERPENT
	if ( user_key == NULL ) {
		return exception_throw("user_key was NULL.", function_name);
	}
	if ( blocks == NULL ) {
		return exception_throw("blocks was NULL.", function_name);
	}
	#endif

	// Initialize the subkey.
	exception = serpent_init_subkey(user_key, &subkey);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Decrypt each block.
	#pragma omp parallel shared(blocks, subkey, block_count, thread_count, thread_index, blocks_per_thread)
	{
		#if defined (_OPENMP)
			thread_count = omp_get_num_threads();
			thread_index = omp_get_thread_num();
		#endif

		// Calculate the current index.
		int index = thread_index;
		
		// Encrypted the minimal number of blocks.
		blocks_per_thread = block_count / thread_count;
		for ( int i = 0; i < blocks_per_thread; i++ ) {
			serpent_decrypt_block(&(blocks[index]), subkey);
			index += thread_count;
		}

		// Encrypt the extra blocks that fall outside the minimal number of block.s
		if ( index < block_count ) {
			serpent_decrypt_block(&(blocks[index]), subkey);
		}
	} 

	// Free the subkey.
	exception = serpent_free_subkey(subkey);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Return. 
	return NULL;
}
Пример #6
0
exception_t* file_free(file_t* file) {
	char* function_name = "file_free()";
	exception_t* exception;

	// Validate parameters.
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}

	// Write metadata or truncate file.
	// Note that file->flag reflects the file when it was opened,
	// and the file is now the _opposite_ of what the flag states.
	if ( file->flag == UNENCRYPTED ) {
		// Write padding to the end of the file.
		exception = file_write_metadata(file);
		if ( exception != NULL ) {
			return exception_append(exception, function_name);
		}
	}
	else if ( file->flag == ENCRYPTED ) {
		// Truncate the file padding.
		if ( ftruncate64(file->fd, (off64_t)(file->size - (off64_t)file->padding)) == -1 ) {
			return exception_throw("Unable to truncate file.", function_name);
		}
	}
	else {
		return exception_throw("Unexpected flag.", function_name);
	}

	// Free file name,
	free(file->name);

	// Close the file.
	if ( close(file->fd) == -1 ) {
		perror("Unable to close file.");
		return exception_throw("Unable to close file.", function_name);
	}

	// Set structure defaults.
	file->blocks = NULL;
	file->name = NULL;
	file->block_count = 0;
	file->size = 0;
	file->padding = 0;
	file->fd = 0;
	file->flag = 0;

	// Return success.
	return NULL;
}
/* Check expressions */
void exception_check(register bool *flag, register const char *file, register int line, register const char *function) {
  /* If errno was set, then we have an exception here */
  if(errno) {
    exception_throw(errno, file, line, function);
  };
  /* Just set the flag used in the check syntax sugar */
  *flag = false;
};
Пример #8
0
exception_t* file_get_block_count(file_t* file, long long* block_count) {
	char* function_name = "file_get_block_count()";

	// Validate parameters.
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}
	if ( block_count == NULL ) {
		return exception_throw("block_count was NULL.", function_name);
	}

	// Calculate number of blocks.
	(*block_count) = file->size / sizeof(block128_t);

	// Return success.
	return NULL;
}
Пример #9
0
void* eqp_realloc(Basic* basic, void* ptr, size_t len)
{
    ptr = realloc(ptr, len);
    
    if (ptr == NULL)
        exception_throw(basic, ErrorOutOfMemory);
    
    return ptr;
}
Пример #10
0
exception_t* file_read_metadata(file_t* file) {
	char* function_name = "file_read_metadata()";
	long long temp;

	// Validate parameters.
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}

	// Seek to end of the file.
	temp = lseek64(file->fd, 0, SEEK_END);
	if ( temp == -1 ) {
		perror(NULL);
		return exception_throw("Unable to lseek to file end.", function_name);
	}

	// Move back by the size of the metadata.
	temp = lseek64(file->fd, (int)(-sizeof(file->padding)), SEEK_CUR);
	if ( temp == -1 ) {
		perror(NULL);
		return exception_throw("Unable to lseek to metadata.", function_name);
	}

	// Read in metadata.
	temp = read(file->fd, &file->padding, sizeof(file->padding));
	if ( temp == -1 ) {
		perror(NULL);
		return exception_throw("Unable to read in metadata.", function_name);
	}

	// Delete the metadata.
	file->size -= sizeof(file->padding);
	if ( ftruncate64(file->fd, (off64_t)(file->size)) == -1 ) {
		perror(NULL);
		return exception_throw("Unable to truncate metadata.", function_name);
	}

	// Return success.
	return NULL;
}
Пример #11
0
exception_t* serpent_cuda_decrypt(key256_t* user_key, block128_t* blocks, int block_count, size_t* buffer_size) {
	char* function_name = "serpent_cuda_decrypt()";
	exception_t* exception;
	uint32_t* subkey;

	// Validate parameters.
	#ifdef DEBUG_SERPENT
	if ( user_key == NULL ) {
		return exception_throw("user_key was NULL.", function_name);
	}
	if ( blocks == NULL ) {
		return exception_throw("blocks was NULL.", function_name);
	}
	if ( buffer_size == NULL ) {
		return exception_throw("buffer_size was NULL.", function_name);
	}
	#endif

	// Initialize the subkey.
	exception = serpent_init_subkey(user_key, &subkey);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Run the encryption algorithm from a different file.
	if ( serpent_cuda_decrypt_cu(subkey, blocks, block_count, buffer_size) == -1 ) {
		return exception_throw("CUDA encryption FAILED.", function_name);
	}

	// Free the subkey.
	exception = serpent_free_subkey(subkey);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Return success.
	return NULL;
}
Пример #12
0
exception_t* file_write_metadata(file_t* file) {
	char* function_name = "file_write_metadata()";

	// Validate parameters.
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}

	// Seek to the end of the file.
	if ( lseek64(file->fd, 0, SEEK_END) == -1 ) {
		perror(NULL);
		return exception_throw("Unable to seek to end of file.", function_name);
	}

	// Write padding to end of file.
	if ( write(file->fd, &(file->padding), sizeof(file->padding)) == -1 ) {
		perror(NULL);
		return exception_throw("Unable to write metadata to file.", function_name);
	}

	// Return success.
	return NULL;
}
Пример #13
0
exception_t* serpent_free_subkey(uint32_t* subkey) {
	#ifdef DEBUG_SERPENT
	char* function_name = "serpent_free_subkey()";
	#endif

	// Validate parameters.
	#ifdef DEBUG_SERPENT
	if ( subkey == NULL ) {
		return exception_throw("subkey was NULL.", function_name);
	}
	#endif

	// Free the subkey.
	// Note that the original key be freed, too.
	free(subkey-8);

	// Return success.
	return NULL;
}
Пример #14
0
int main(void) {
	EXCEPTION *e = exception_new();

	printf ( "NEW\n" );
	DATABASE *db = database_new();
	database_setException(db, e);
	exception_throw(e);

	printf ( "PARAM\n" );
	database_setHost(db, "localhost");
	database_setName(db, "template1");
	database_setUser(db, "postgres");
	database_setPassword(db, "postgres");

	printf ( "OPEN\n" );
	database_open(db);
	exception_throw(e);

	printf ( "VERSION" );
	char *ver = database_getVersion(db);
	exception_throw(e);
	printf ( " (%s)\n", ver );
	free(ver);

	printf ( "RANDOM" );
	int rnd = database_getRandom(db);
	exception_throw(e);
	printf ( " (%d)\n", rnd );

	printf ( "CLOSE\n" );
	database_close(db);
	exception_throw(e);

	printf ( "FREE\n" );
	database_free(db);
	exception_throw(e);

	exception_free(e);

	return 0;
}
Пример #15
0
int main(void) {
	EXCEPTION *e = exception_new();

	printf ( "NEW\n" );
	DATABASE *db = database_new();
	database_setException(db, e);
	exception_throw(e);

	printf ( "PARAM\n" );
	database_setName(db, "sqlite.db");

	printf ( "OPEN\n" );
	database_open(db);
	exception_throw(e);

	printf ( "VERSION" );
	char *ver = database_getVersion(db);
	exception_throw(e);
	printf ( " (%s)\n", ver );
	free(ver);

	printf ( "RANDOM" );
	int rnd = database_getRandom(db);
	exception_throw(e);
	printf ( " (%d)\n", rnd );

	printf ( "CLOSE\n" );
	database_close(db);
	exception_throw(e);

	printf ( "FREE\n" );
	database_free(db);
	exception_throw(e);

	exception_free(e);

	return 0;
}
exception_t* benchmark(key256_t* key, char* input_filepath, enum algorithm algorithm, enum mode mode, enum encryption encryption, benchmark_run_t* benchmark_run ) {
	char* function_name = "benchmark()";
	exception_t* exception;
	block128_t* blocks;
	file_t file;
	struct timespec clock_begin;
	struct timespec clock_elapsed;
	struct timespec clock_end;
	long long block_count;
	size_t buffer_size;

	// Validate parameters.
	if ( key == NULL ) {
		return exception_throw("key was NULL.", function_name);
	}
	if ( input_filepath == NULL ) {
		return exception_throw("input_filepath was NULL.", function_name);
	}
	if ( benchmark_run == NULL ) {
		return exception_throw("benchmark_run was NULL.", function_name);
	}

	// Open input file.
	if ( encryption == ENCRYPT ) {
		exception = file_init(input_filepath, UNENCRYPTED, &file);
	}
	else if ( encryption == DECRYPT ) {
		exception = file_init(input_filepath, ENCRYPTED, &file);
	}
	else { 
		return exception_throw("Unhandled encryption parameter.", function_name);
	}
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Get number of blocks in file.
	exception = file_get_block_count(&file, &block_count);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Read data from file.
	fprintf(stdout, "Reading. ");
	fflush(stdout);
	exception = file_read(&file, 0, block_count, &blocks);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Print message for the user.
	if ( encryption == ENCRYPT ) {
		fprintf(stdout, "Encrypting. ");
	}
	else if ( encryption == DECRYPT ) {
		fprintf(stdout, "Decrypting. ");
	}
	else {
		return exception_throw("Invalid encryption parameter.", function_name);
	}
	fflush(stdout);

	// Get begin time.
	if ( clock_gettime(CLOCK_REALTIME, &clock_begin) == -1 ) {
		return exception_throw("Unable to get begin time.", function_name);
	}

	// Call the algorithm.
	switch(algorithm) {
	case AES:
		return exception_throw("Not implemented. Sorry!", function_name);
		break;
	case SERPENT:
		exception = serpent(key, blocks, block_count, mode, encryption, &buffer_size);
		break;
	case TWOFISH:
		exception = twofish(key, blocks, block_count, mode, encryption, &buffer_size);
		break;
	default:
		return exception_throw("Unrecognized algorithm.", function_name);
		break;
	}
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Get end time.
	if ( clock_gettime(CLOCK_REALTIME, &clock_end) == -1 ) {
		return exception_throw("Unable to get end time.", function_name);
	}

	// Write data to file.
	fprintf(stdout, "Writing. ");
	fflush(stdout);
	exception = file_write(&file, 0, block_count, blocks);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Close input file.
	exception = file_free(&file);
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Calculate execution time.
	clock_elapsed.tv_sec = clock_end.tv_sec - clock_begin.tv_sec;
	clock_elapsed.tv_nsec = clock_end.tv_nsec - clock_begin.tv_nsec;
	if ( clock_elapsed.tv_nsec < 0 ) {
		clock_elapsed.tv_nsec += 1000000000;
		clock_elapsed.tv_sec -= 1;
	}

	// Assign output parameters.
	benchmark_run->time_elapsed.tv_sec = clock_elapsed.tv_sec;
	benchmark_run->time_elapsed.tv_nsec = clock_elapsed.tv_nsec;
	benchmark_run->buffer_size = buffer_size;

	// Return success.
	return NULL;
}
Пример #17
0
void obj_do(OBJ *obj) {
	printf ( "Do!\n" );
	exception_addCallback(obj->exception, objException);
	if (obj->exception != NULL) exception_throw(obj->exception, 2, "Zwei!", NULL);
	exception_delCallback(obj->exception);
}
Пример #18
0
static void signal_sigfpe(int signum,siginfo_t* info,void*ptr) {
	exception_throw(NULL,sigfpe,info);
}
Пример #19
0
exception_t* file_read(file_t* file, int block_index, int block_count, block128_t** blocks) {
	const int BUFFER_SIZE_MAX = sizeof(block128_t) * 512;
	char* function_name = "file_read()";
	// Temporary storage for blocks to prevent multiple dereferences.
	block128_t* blocks_local; 
	uint32_t* buffer;
	int buffer_size;
	int bytes_read;
	int blocks_read_total;
	int blocks_read_local;

	// Validate parameters.
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}
	if ( block_index < 0 ) {
		return exception_throw("block_index must be greater than zero.", function_name);
	}
	if ( block_count < 0 ) {
		return exception_throw("block_count must be greater than zero.", function_name);
	}
	if ( blocks == NULL ) {
		return exception_throw("blocks was NULL.", function_name);
	}

	// Allocate space for block_count block128_ts.
	blocks_local = (block128_t*)malloc(sizeof(block128_t) * block_count);
	if ( blocks_local == NULL ) {
		return exception_throw("Unable to allocate space for blocks.", function_name);
	}

	// Allocate space for the read buffer.
	if ( (sizeof(block128_t) * block_count) < BUFFER_SIZE_MAX ) {
		buffer_size = sizeof(block128_t) * block_count;
	}
	else {
		buffer_size = BUFFER_SIZE_MAX;
	}
	buffer = (uint32_t*)malloc(buffer_size);
	if ( buffer == NULL ) {
		return exception_throw("Unable to allocate space for read buffer.", function_name);
	}

	// Seek to position in the file at block_index.
	if ( lseek64(file->fd, (int)(sizeof(block128_t)*block_index), SEEK_SET) == -1 ) {
		return exception_throw("Unable to seek to block index.", function_name);
	}

	// Read in the blocks.
	blocks_read_total = 0;
	while ( blocks_read_total < block_count ) {
		// Read into the buffer.
		bytes_read = read(file->fd, buffer, buffer_size);
		if ( bytes_read == -1 ) {
			perror(NULL);
			return exception_throw("Reading into buffer FAILED.", function_name);
		}
		if ( bytes_read == 0 ) {
			return exception_throw("End-of-file encountered before all blocks were read.", function_name);
		}
		if ( bytes_read % sizeof(block128_t) != 0 ) {
			return exception_throw("Unexpected number of bytes read.", function_name);
		}

		// Calculate number of blocks read.
		blocks_read_local = bytes_read / sizeof(block128_t);

		// Assign those blocks.
		for ( int i = 0; i < blocks_read_local; i++ ) {
			int blocks_index = blocks_read_total + i;
			int buffer_index = i*4;

			blocks_local[blocks_index].x0 = buffer[buffer_index];
			blocks_local[blocks_index].x1 = buffer[buffer_index + 1];
			blocks_local[blocks_index].x2 = buffer[buffer_index + 2];
			blocks_local[blocks_index].x3 = buffer[buffer_index + 3];
		}

		// Calculate total number of blocks read.
		blocks_read_total += blocks_read_local;
	}

	// Free the buffer.
	free(buffer);

	// Set output parameters.
	(*blocks) = blocks_local;

	// Return success.
	return NULL;
}
Пример #20
0
static void signal_segv(int32_t signum,siginfo_t* info,void*ptr) {
	exception_throw(NULL,segfault,info);
}
Пример #21
0
static void signal_sigbus(int32_t signum,siginfo_t* info,void*ptr) {
	exception_throw(NULL,sigbug,info);
}
Пример #22
0
void chk_exp_throw(const char *exp) {
	exception_throw(NULL,exp,NULL);
}
Пример #23
0
void chk_exp_rethrow(chk_expn_frame *frame) {
	exception_throw(frame,frame->exception,NULL);
}
Пример #24
0
exception_t* file_write(file_t* file, int block_index, int block_count, block128_t* blocks) {
	const int BUFFER_SIZE_MAX = sizeof(block128_t) * 512;
	char* function_name = "file_write()";
	uint32_t* buffer;
	long long blocks_written_total;
	int buffer_size;
	int blocks_to_write;
	int bytes_written;

	// Validate parameters.
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}
	if ( block_index < 0 ) {
		return exception_throw("block_index must be greater than zero.", function_name);
	}
	if ( block_count < 0 ) {
		return exception_throw("block_count must be greater than zero.", function_name);
	}
	if ( blocks == NULL ) {
		return exception_throw("blocks was NULL.", function_name);
	}

	// Seek to block_index.
	if ( lseek64(file->fd, (int)(sizeof(block128_t)  * block_index), SEEK_SET) == -1 ) {
		perror(NULL);
		return exception_throw("Unable to seek to block_index.", function_name);
	}

	// Calculate buffer size.
	if ( sizeof(block128_t) * block_count < BUFFER_SIZE_MAX ) {
		buffer_size = sizeof(block128_t) * block_count;
	}
	else {
		buffer_size = BUFFER_SIZE_MAX;
	}

	// Allocate space for the buffer.
	buffer = (uint32_t*)malloc(buffer_size);
	if ( buffer == NULL ) {
		return exception_throw("Unable to allocate space for buffer.", function_name);
	}

	// Write blocks.
	blocks_written_total = 0;
	while ( blocks_written_total < block_count ) {
		// Calculate number of blocks to write and re-adjust buffer_size if necessary.
		blocks_to_write = buffer_size / sizeof(block128_t);
		if ( blocks_to_write > block_count - blocks_written_total ) {
			blocks_to_write = block_count - blocks_written_total;
			buffer_size = blocks_to_write * sizeof(block128_t);
		}

		// Copy block data to the buffer.
		for ( int i = 0; i < blocks_to_write; i++ ) {
			int blocks_index = blocks_written_total + i;
			int buffer_index = i * 4;

			buffer[buffer_index] = blocks[blocks_index].x0;
			buffer[buffer_index + 1] = blocks[blocks_index].x1;
			buffer[buffer_index + 2] = blocks[blocks_index].x2;
			buffer[buffer_index + 3] = blocks[blocks_index].x3;
		}

		// Write the blocks.
		bytes_written = write(file->fd, buffer, buffer_size);
		if ( bytes_written == -1 ) {
			perror(NULL);
			return exception_throw("Unable to write buffer.", function_name);
		}
		if ( bytes_written < buffer_size ) {
			perror(NULL);
			return exception_throw("Not all data was written to the file.", function_name);
		}

		// Calculate total blocks written.
		blocks_written_total += blocks_to_write;
	}

	// Free the buffer.
	free(buffer);

	// Free the written blocks.
	free(blocks);

	// Return success.
	return NULL;
}
Пример #25
0
exception_t* file_init(const char* file_name, enum file_encryption flag, file_t* file) { 
	char* function_name = "file_init()";
	exception_t* exception;
	struct stat64 buffer;
	int temp;

	// Validate parameters.
	if ( file_name == NULL ) {
		return exception_throw("file_name was NULL.", function_name);
	}
	if ( file == NULL ) {
		return exception_throw("file was NULL.", function_name);
	}

	// Initialize file structure.
	file->blocks = NULL;
	file->block_count = 0;
	file->padding = 0;
	file->flag = flag;

	// Get file name,
	file->name = (char*)malloc(sizeof(char)*strlen(file_name));
	if ( file->name == NULL ) {
		return exception_throw("Malloc failed.", function_name);
	}
	strcpy(file->name, file_name);

	// Open the file.
	file->fd = open(file_name, O_RDWR | O_LARGEFILE);
	if ( file->fd == -1 ) {
		perror("Unable to open file.");
		return exception_throw("Unable to open file.", function_name);
	}

	// Get file size.
	temp = fstat64(file->fd, &buffer);
	if ( temp == -1 ) {
		perror("Unable to get stats.");
		return exception_throw("Unable to get stats.", function_name);
	}
	file->size = buffer.st_size;

	// Get file metadata or pad the file.
	if ( flag == ENCRYPTED ) {
		// Read in file metadata.
		exception = file_read_metadata(file);
		if ( exception != NULL ) {
			return exception_append(exception, function_name);
		}
	}
	else if ( flag == UNENCRYPTED ) {
		int temp;

		// Calculate file padding.
		temp = file->size % sizeof(block128_t);
		if ( temp == 0 ) {
			file->padding = temp;
		}
		else {
			file->padding = sizeof(block128_t) - temp;

			// Pad the file.
			exception = file_write_padding(file);
			if ( exception != NULL ) {
				return exception_append(exception, function_name);
			}
		}
	}
	else {
		return exception_throw("Unexpected flag.", function_name);
	}

	// Return success.
	return NULL;
}
Пример #26
0
exception_t* serpent(key256_t* user_key, block128_t* blocks, int block_count, enum mode mode, enum encryption encryption, size_t* buffer_size) {
	char* function_name = "serpent()";
	exception_t* exception;

	// Validate parameters.
	#ifdef DEBUG_SERPENT
	if ( user_key == NULL ) {
		return exception_throw("user_key was NULL.", function_name);
	}
	if ( blocks == NULL ) {
		return exception_throw("blocks was NULL.", function_name);
	}
	if ( buffer_size == NULL ) {
		return exception_throw("buffer_size was NULL.", function_name);
	}
	#endif

	// Run the appropirate algorithm.
	switch(mode) {
	case CUDA:
		switch(encryption) {
		case DECRYPT:
			exception = serpent_cuda_decrypt(user_key, blocks, block_count, buffer_size);
			break;
		case ENCRYPT:
			exception = serpent_cuda_encrypt(user_key, blocks, block_count, buffer_size);
			break;
		default:
			return exception_throw("Unrecognized encryption parameter for CUDA.", function_name);
		}
		break;
	case PARALLEL:
		switch(encryption) {
		case DECRYPT:
			exception = serpent_parallel_decrypt(user_key, blocks, block_count);
			break;
		case ENCRYPT:
			exception = serpent_parallel_encrypt(user_key, blocks, block_count);
			break;
		default:
			return exception_throw("Unrecognized encryption parameter for parallel.", function_name);
		}
		break;
	case SERIAL:
		switch(encryption) {
		case DECRYPT:
			exception = serpent_serial_decrypt(user_key, blocks, block_count);
			break;
		case ENCRYPT:
			exception = serpent_serial_encrypt(user_key, blocks, block_count);
			break;
		default:
			return exception_throw("Unrecognized encryption parameter for serial.", function_name);
		}
		break;
	default:
		return exception_throw("Unknown mode.", function_name);
	}
	if ( exception != NULL ) {
		return exception_append(exception, function_name);
	}

	// Return success.
	return NULL;
}
Пример #27
0
exception_t* serpent_init_subkey(key256_t* user_key, uint32_t** subkey) {
	char* function_name = "serpent_init_subkey()";
	const int PREKEY_SIZE = 140;
	exception_t* exception;
	uint32_t* genkey;
	uint32_t a, b, c, d, e;

	// Validate parameters.
	#ifdef DEBUG_SERPENT
	if ( user_key == NULL ) {
		return exception_throw("user_key was NULL.", function_name);
	}
	if ( subkey == NULL ) {
		return exception_throw("subkey was NULL.", function_name);
	}
	#endif

	// Allocate space for genkey.
	genkey = (uint32_t*)malloc(sizeof(uint32_t) * PREKEY_SIZE);
	if ( genkey == NULL ) {
		return exception_throw("Unable to allocate genkey.", function_name);
	}

	// Assign user_key to the genkey; making sure to properly little-endianized the user key.
	for ( int i = 0; i < 8; i++ ) {
		uint32_t word;
		// Get the key value.
		exception = key256_get_word(user_key, i, &word);
		if ( exception != NULL ) {
			return exception_append(exception, function_name);
		}

		// Endianize the key value.
		word = mirror_bytes32(word);

		// Set the key value.
		genkey[i] = word;
	}

	// Generate the prekey by the following affine recurrence.
	genkey += 8;
	uint32_t t = genkey[-1];
	for ( int i = 0; i < 132; ++i ) {
		genkey[i] = t = rotl_fixed(genkey[i-8] ^ genkey[i-5] ^ genkey[i-3] ^ t ^ 0x9E3779B9 ^ i, 11);
	}
	genkey -= 20;

#define LK(r, a, b, c, d, e)    {\
	a = genkey[(8-r)*4 + 0];	     \
	b = genkey[(8-r)*4 + 1];	     \
	c = genkey[(8-r)*4 + 2];	     \
	d = genkey[(8-r)*4 + 3];}

#define SK(r, a, b, c, d, e)    {\
	genkey[(8-r)*4 + 4] = a;	     \
	genkey[(8-r)*4 + 5] = b;	     \
	genkey[(8-r)*4 + 6] = c;	     \
	genkey[(8-r)*4 + 7] = d;}    \

	// Generare the subkey using the prekey.
	for ( int i = 0; i < 4 ; i++ )
	{
		afterS2(LK); afterS2(S3); afterS3(SK);
		afterS1(LK); afterS1(S2); afterS2(SK);
		afterS0(LK); afterS0(S1); afterS1(SK);
		beforeS0(LK); beforeS0(S0); afterS0(SK);
		genkey += 8*4;
		afterS6(LK); afterS6(S7); afterS7(SK);
		afterS5(LK); afterS5(S6); afterS6(SK);
		afterS4(LK); afterS4(S5); afterS5(SK);
		afterS3(LK); afterS3(S4); afterS4(SK);
	}
	afterS2(LK); afterS2(S3); afterS3(SK);
	genkey -= 108;

	// Assign output parameter.
	(*subkey) = genkey;

	// Return success.
	return NULL;
}
Пример #28
0
void chk_exp_rethrow(chk_expn_frame *frame) {
	exception_throw(frame,chk_exp_get_thread_expn()->exception,NULL);
}