ms_funcs.c

来自「WPA在Linux下实现的原代码 WPA在Linux下实现的原代码」· C语言 代码 · 共 504 行 · 第 1/2 页

C
504
字号
		0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,		0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x64, 0x65,		0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,		0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,		0x6b, 0x65, 0x79, 0x2e	};	static const u8 magic3[84] = {		0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,		0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,		0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,		0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,		0x6b, 0x65, 0x79, 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68,		0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73,		0x69, 0x64, 0x65, 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73,		0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20,		0x6b, 0x65, 0x79, 0x2e	};	static const u8 shs_pad1[40] = {		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	};	static const u8 shs_pad2[40] = {		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2	};	u8 digest[SHA1_MAC_LEN];	const unsigned char *addr[4];	const size_t len[4] = { 16, 40, 84, 40 };	addr[0] = master_key;	addr[1] = shs_pad1;	if (is_send) {		addr[2] = is_server ? magic3 : magic2;	} else {		addr[2] = is_server ? magic2 : magic3;	}	addr[3] = shs_pad2;	sha1_vector(4, addr, len, digest);	if (session_key_len > SHA1_MAC_LEN)		session_key_len = SHA1_MAC_LEN;	memcpy(session_key, digest, session_key_len);}#define PWBLOCK_LEN 516/** * encrypt_pw_block_with_password_hash - EncryptPwBlockWithPasswordHash() - RFC 2759, Sect. 8.10 * @password: 0-to-256-unicode-char Password (IN; ASCII) * @password_len: Length of password * @password_hash: 16-octet PasswordHash (IN) * @pw_block: 516-byte PwBlock (OUT) */static void encrypt_pw_block_with_password_hash(	const u8 *password, size_t password_len,	const u8 *password_hash, u8 *pw_block){	size_t i, offset;	u8 *pos;	if (password_len > 256)		return;	memset(pw_block, 0, PWBLOCK_LEN);	offset = (256 - password_len) * 2;	hostapd_get_rand(pw_block, offset);	for (i = 0; i < password_len; i++)		pw_block[offset + i * 2] = password[i];	/*	 * PasswordLength is 4 octets, but since the maximum password length is	 * 256, only first two (in little endian byte order) can be non-zero.	 */	pos = &pw_block[2 * 256];	WPA_PUT_LE16(pos, password_len * 2);	rc4(pw_block, PWBLOCK_LEN, password_hash, 16);}/** * new_password_encrypted_with_old_nt_password_hash - NewPasswordEncryptedWithOldNtPasswordHash() - RFC 2759, Sect. 8.9 * @new_password: 0-to-256-unicode-char NewPassword (IN; ASCII) * @new_password_len: Length of new_password * @old_password: 0-to-256-unicode-char OldPassword (IN; ASCII) * @old_password_len: Length of old_password * @encrypted_pw_block: 516-octet EncryptedPwBlock (OUT) */void new_password_encrypted_with_old_nt_password_hash(	const u8 *new_password, size_t new_password_len,	const u8 *old_password, size_t old_password_len,	u8 *encrypted_pw_block){	u8 password_hash[16];	nt_password_hash(old_password, old_password_len, password_hash);	encrypt_pw_block_with_password_hash(new_password, new_password_len,					    password_hash, encrypted_pw_block);}/** * nt_password_hash_encrypted_with_block - NtPasswordHashEncryptedWithBlock() - RFC 2759, Sect 8.13 * @password_hash: 16-octer PasswordHash (IN) * @block: 16-octet Block (IN) * @cypher: 16-octer Cypher (OUT) */static void nt_password_hash_encrypted_with_block(const u8 *password_hash,						  const u8 *block,						  u8 *cypher){	des_encrypt(password_hash, block, cypher);	des_encrypt(password_hash + 8, block + 7, cypher + 8);}/** * old_nt_password_hash_encrypted_with_new_nt_password_hash - OldNtPasswordHashEncryptedWithNewNtPasswordHash() - RFC 2759, Sect. 8.12 * @new_password: 0-to-256-unicode-char NewPassword (IN; ASCII) * @new_password_len: Length of new_password * @old_password: 0-to-256-unicode-char OldPassword (IN; ASCII) * @old_password_len: Length of old_password * @encrypted_password_ash: 16-octet EncryptedPasswordHash (OUT) */void old_nt_password_hash_encrypted_with_new_nt_password_hash(	const u8 *new_password, size_t new_password_len,	const u8 *old_password, size_t old_password_len,	u8 *encrypted_password_hash){	u8 old_password_hash[16], new_password_hash[16];	nt_password_hash(old_password, old_password_len, old_password_hash);	nt_password_hash(new_password, new_password_len, new_password_hash);	nt_password_hash_encrypted_with_block(old_password_hash,					      new_password_hash,					      encrypted_password_hash);}#ifdef TEST_MAIN_MS_FUNCS#include "rc4.c"int main(int argc, char *argv[]){	/* Test vector from RFC2759 example */	u8 *username = "User";	u8 *password = "clientPass";	u8 auth_challenge[] = {		0x5B, 0x5D, 0x7C, 0x7D, 0x7B, 0x3F, 0x2F, 0x3E,		0x3C, 0x2C, 0x60, 0x21, 0x32, 0x26, 0x26, 0x28	};	u8 peer_challenge[] = {		0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, 0x26, 0x2A,		0x28, 0x29, 0x5F, 0x2B, 0x3A, 0x33, 0x7C, 0x7E	};	u8 challenge[] = { 0xD0, 0x2E, 0x43, 0x86, 0xBC, 0xE9, 0x12, 0x26 };	u8 password_hash[] = {		0x44, 0xEB, 0xBA, 0x8D, 0x53, 0x12, 0xB8, 0xD6,		0x11, 0x47, 0x44, 0x11, 0xF5, 0x69, 0x89, 0xAE	};	u8 nt_response[] = {		0x82, 0x30, 0x9E, 0xCD, 0x8D, 0x70, 0x8B, 0x5E,		0xA0, 0x8F, 0xAA, 0x39, 0x81, 0xCD, 0x83, 0x54,		0x42, 0x33, 0x11, 0x4A, 0x3D, 0x85, 0xD6, 0xDF	};	u8 password_hash_hash[] = {		0x41, 0xC0, 0x0C, 0x58, 0x4B, 0xD2, 0xD9, 0x1C,		0x40, 0x17, 0xA2, 0xA1, 0x2F, 0xA5, 0x9F, 0x3F	};	u8 authenticator_response[] = {		0x40, 0x7A, 0x55, 0x89, 0x11, 0x5F, 0xD0, 0xD6,		0x20, 0x9F, 0x51, 0x0F, 0xE9, 0xC0, 0x45, 0x66,		0x93, 0x2C, 0xDA, 0x56	};	u8 master_key[] = {		0xFD, 0xEC, 0xE3, 0x71, 0x7A, 0x8C, 0x83, 0x8C,		0xB3, 0x88, 0xE5, 0x27, 0xAE, 0x3C, 0xDD, 0x31	};	u8 send_start_key[] = {		0x8B, 0x7C, 0xDC, 0x14, 0x9B, 0x99, 0x3A, 0x1B,		0xA1, 0x18, 0xCB, 0x15, 0x3F, 0x56, 0xDC, 0xCB	};	u8 buf[32];	int errors = 0;	printf("Testing ms_funcs.c\n");	challenge_hash(peer_challenge, auth_challenge,		       username, strlen(username),		       buf);	if (memcmp(challenge, buf, sizeof(challenge)) != 0) {		printf("challenge_hash failed\n");		errors++;	}	nt_password_hash(password, strlen(password), buf);	if (memcmp(password_hash, buf, sizeof(password_hash)) != 0) {		printf("nt_password_hash failed\n");		errors++;	}	generate_nt_response(auth_challenge, peer_challenge,			     username, strlen(username),			     password, strlen(password),			     buf);	if (memcmp(nt_response, buf, sizeof(nt_response)) != 0) {		printf("generate_nt_response failed\n");		errors++;	}	hash_nt_password_hash(password_hash, buf);	if (memcmp(password_hash_hash, buf, sizeof(password_hash_hash)) != 0) {		printf("hash_nt_password_hash failed\n");		errors++;	}	generate_authenticator_response(password, strlen(password),					peer_challenge, auth_challenge,					username, strlen(username),					nt_response, buf);	if (memcmp(authenticator_response, buf, sizeof(authenticator_response))	    != 0) {		printf("generate_authenticator_response failed\n");		errors++;	}	get_master_key(password_hash_hash, nt_response, buf);	if (memcmp(master_key, buf, sizeof(master_key)) != 0) {		printf("get_master_key failed\n");		errors++;	}	get_asymetric_start_key(master_key, buf, sizeof(send_start_key), 1, 1);	if (memcmp(send_start_key, buf, sizeof(send_start_key)) != 0) {		printf("get_asymetric_start_key failed\n");		errors++;	}	if (errors)		printf("FAILED! %d errors\n", errors);	return errors;}#endif /* TEST_MAIN_MS_FUNCS */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?