// Perform the key expansion for AES-128 using 16-byte key; each round stored in round_key
//	Pseudocode taken from Fig.11 in FIPS-197 (page 20)
void key_expansion(unsigned char init_key[16], unsigned char round_key[44][4], FILE* table) {
	unsigned char* temp = (unsigned char*)calloc(1,4);
	unsigned char* s_box = (unsigned char*)calloc(1,256);
	unsigned char* rcon = (unsigned char*)calloc(1,256);
	init_s_box(s_box, table);
	init_rcon(rcon);
	
	// Store initial round keys (form words from each piece of init key)
	for(int i=0; i<4; i++) {
		round_key[i][0] = init_key[4*i];
		round_key[i][1] = init_key[4*i+1];
		round_key[i][2] = init_key[4*i+2];
		round_key[i][3] = init_key[4*i+3];
		// Tried defining round key as a WORD (unsigned int), but I forgot there are
		//	little-endian/big-endian issues with different architectures. F**k that.
	}
	
	// Generate subsequent round keys (from Nk -> Nb*(Nr+1))
	for(int i=4; i<44; i++) {
		// Set temp var to previous round key
		temp[0] = round_key[i-1][0];
		temp[1] = round_key[i-1][1];
		temp[2] = round_key[i-1][2];
		temp[3] = round_key[i-1][3];
		
		// Perform Rijndael key schedule core every 4 iterations
		if (i%4 == 0) {
			// For AES-128 and AES-192; rotate temp vals, put through s_box, xor the MSB with rcon
			rot_word(temp);
			temp[0] = s_box[ temp[0] ] ^ rcon[i/4];
			temp[1] = s_box[ temp[1] ];
			temp[2] = s_box[ temp[2] ];
			temp[3] = s_box[ temp[3] ];
		}
		
		// Store current round key
		round_key[i][0] = round_key[i-4][0] ^ temp[0];
		round_key[i][1] = round_key[i-4][1] ^ temp[1];
		round_key[i][2] = round_key[i-4][2] ^ temp[2];
		round_key[i][3] = round_key[i-4][3] ^ temp[3];
	}
	
	free(temp);
	free(s_box);
	free(rcon);
}
Esempio n. 2
0
void init()
{
	sigset_t sigmask;
	sigemptyset(&sigmask);
	sigaddset(&sigmask, SIGALRM);
	sigaddset(&sigmask, SIGUSR1);
	
	sigprocmask(SIG_BLOCK, &sigmask, NULL);

	console_print("Emergence Client " VERSION "\n");
	
	SDL_Init(SDL_INIT_AUDIO);
	
	init_user();
	init_network();
	init_timer();
	init_openssl();
	init_key();
	init_download();
	init_servers();

	create_cvars();
	init_console_cvars();
	init_render_cvars();
	init_map_cvars();
	create_control_cvars();
//	create_input_cvars();
	init_tick_cvars();

	init_console();
	create_colour_cvars();
	
	struct string_t *string = new_string_string(emergence_home_dir);
	string_cat_text(string, "/client.config");
	
	if(!exec_config_file(string->text))
	{
		exec_config_file(find_resource("default-controls.config"));
	}
	else
	{
		char *ver = get_cvar_string("version");
		
		if(*ver == '\0')
		{
			struct string_t *command = new_string_text("rm ");
			string_cat_string(command, emergence_home_dir);
			string_cat_text(command, "/skins/default.skin*");
			
			console_print("%s\n", command->text);
			system(command->text);
			
			vid_mode = -1;	// find a nice mode

			exec_config_file(find_resource("default-controls.config"));
		}
		
		free(ver);
	}
	
	free_string(string);
	
	
	set_cvar_string("version", VERSION);
	
	init_skin();
	init_input();
	init_control();
	

	init_render();
	init_rcon();
	init_ping();

	create_cvar_command("quit", client_shutdown_char);
	

	init_sound();
	init_game();
	
	init_alarm();
	
	render_frame();
	
	string = new_string_text("%s%s", emergence_home_dir->text, "/client.autoexec");
	if(!exec_config_file(string->text))
		exec_config_file(find_resource("default-client.autoexec"));
	free_string(string);
	
	start_server_discovery();
}