⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 milenage.c

📁 hostapd源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * 3GPP AKA - Milenage algorithm (3GPP TS 35.205, .206, .207, .208) * Copyright (c) 2006 <jkmaline@cc.hut.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. * * This file implements an example authentication algorithm defined for 3GPP * AKA. This can be used to implement a simple HLR/AuC into hlr_auc_gw to allow * EAP-AKA to be tested properly with real USIM cards. */#include "includes.h"#include "common.h"#include "milenage.h"#include "aes_wrap.h"/** * milenage_opc - Determine OPc from OP and K * @op: OP = 128-bit operator variant algorithm configuration field * @k: K = 128-bit subscriber key * @opc: Buffer for OPc = 128-bit value derived from OP and K */static void milenage_opc(const u8 *op, const u8 *k, u8 *opc){	int i;	aes_128_encrypt_block(k, op, opc);	for (i = 0; i < 16; i++)		opc[i] ^= op[i];}/** * milenage_f1 - Milenage f1 and f1* algorithms * @opc: OPc = 128-bit value derived from OP and K with milenage_opc() * @k: K = 128-bit subscriber key * @rand: RAND = 128-bit random challenge * @sqn: SQN = 48-bit sequence number * @amf: AMF = 16-bit authentication management field * @mac_a: Buffer for MAC-A = 64-bit network authentication code, or %NULL * @mac_s: Buffer for MAC-S = 64-bit resync authentication code, or %NULL */static void milenage_f1(const u8 *opc, const u8 *k, const u8 *rand,			const u8 *sqn, const u8 *amf, u8 *mac_a, u8 *mac_s){	u8 tmp1[16], tmp2[16], tmp3[16];	int i;	for (i = 0; i < 16; i++)		tmp1[i] = rand[i] ^ opc[i];	aes_128_encrypt_block(k, tmp1, tmp1);	/* SQN || AMF || SQN || AMF */	memcpy(tmp2, sqn, 6);	memcpy(tmp2 + 6, amf, 2);	memcpy(tmp2 + 8, tmp2, 8);	for (i = 0; i < 16; i++)		tmp3[(i + 8) % 16] = tmp2[i] ^ opc[i];	for (i = 0; i < 16; i++)		tmp3[i] ^= tmp1[i];	aes_128_encrypt_block(k, tmp3, tmp1);	for (i = 0; i < 16; i++)		tmp1[i] ^= opc[i];	if (mac_a)		memcpy(mac_a, tmp1, 8);	if (mac_s)		memcpy(mac_s, tmp1 + 8, 8);}/** * milenage_f2345 - Milenage f2, f3, f4, f5, f5* algorithms * @opc: OPc = 128-bit value derived from OP and K with milenage_opc() * @k: K = 128-bit subscriber key * @rand: RAND = 128-bit random challenge * @res: Buffer for RES = 64-bit signed response (f2), or %NULL * @ck: Buffer for CK = 128-bit confidentiality key (f3), or %NULL * @ik: Buffer for IK = 128-bit integrity key (f4), or %NULL * @ak: Buffer for AK = 48-bit anonymity key (f5), or %NULL * @akstar: Buffer for AK = 48-bit anonymity key (f5*), or %NULL */static void milenage_f2345(const u8 *opc, const u8 *k, const u8 *rand, u8 *res,			   u8 *ck, u8 *ik, u8 *ak, u8 *akstar){	u8 tmp1[16], tmp2[16], tmp3[16];	int i;	for (i = 0; i < 16; i++)		tmp1[i] = rand[i] ^ opc[i];	aes_128_encrypt_block(k, tmp1, tmp2);	/* f2 and f5 */	for (i = 0; i < 16; i++)		tmp1[i] = tmp2[i] ^ opc[i];	tmp1[15] ^= 1;	aes_128_encrypt_block(k, tmp1, tmp3);	for (i = 0; i < 16; i++)		tmp3[i] ^= opc[i];	if (res)		memcpy(res, tmp3 + 8, 8);	if (ak)		memcpy(ak, tmp3, 6);	/* f3 */	if (ck) {		for (i = 0; i < 16; i++)			tmp1[(i + 12) % 16] = tmp2[i] ^ opc[i];		tmp1[15] ^= 2;		aes_128_encrypt_block(k, tmp1, ck);		for (i = 0; i < 16; i++)			ck[i] ^= opc[i];	}	/* f4 */	if (ik) {		for (i = 0; i < 16; i++)			tmp1[(i + 8) % 16] = tmp2[i] ^ opc[i];		tmp1[15] ^= 4;		aes_128_encrypt_block(k, tmp1, ik);		for (i = 0; i < 16; i++)			ik[i] ^= opc[i];	}	/* f5* */	if (akstar) {		for (i = 0; i < 16; i++)			tmp1[(i + 4) % 16] = tmp2[i] ^ opc[i];		tmp1[15] ^= 8;		aes_128_encrypt_block(k, tmp1, tmp1);		for (i = 0; i < 6; i++)			akstar[i] = tmp1[i] ^ opc[i];	}}/** * milenage_generate - Generate AKA AUTN,IK,CK,RES * @op: OP = 128-bit operator variant algorithm configuration field * @amf: AMF = 16-bit authentication management field * @k: K = 128-bit subscriber key * @sqn: SQN = 48-bit sequence number * @rand: RAND = 128-bit random challenge * @autn: Buffer for AUTN = 128-bit authentication token * @ik: Buffer for IK = 128-bit integrity key (f4), or %NULL * @ck: Buffer for CK = 128-bit confidentiality key (f3), or %NULL * @res: Buffer for RES = 64-bit signed response (f2), or %NULL * @res_len: Max length for res; set to used length or 0 on failure */void milenage_generate(const u8 *op, const u8 *amf, const u8 *k, const u8 *sqn,		       const u8 *rand, u8 *autn, u8 *ik, u8 *ck, u8 *res,		       size_t *res_len){	int i;	u8 opc[16], mac_a[16], ak[6];	if (*res_len < 8) {		*res_len = 0;		return;	}	*res_len = 8;	milenage_opc(op, k, opc);	milenage_f1(opc, k, rand, sqn, amf, mac_a, NULL);	milenage_f2345(opc, k, rand, res, ck, ik, ak, NULL);	/* AUTN = (SQN ^ AK) || AMF || MAC */	for (i = 0; i < 6; i++)		autn[i] = sqn[i] ^ ak[i];	memcpy(autn + 6, amf, 2);	memcpy(autn + 8, mac_a, 8);}/** * milenage_auts - Milenage AUTS validation * @op: OP = 128-bit operator variant algorithm configuration field * @k: K = 128-bit subscriber key * @rand: RAND = 128-bit random challenge * @auts: AUTS = 112-bit authentication token from client * @sqn: Buffer for SQN = 48-bit sequence number * Returns: 0 = success (sqn filled), -1 on failure */int milenage_auts(const u8 *op, const u8 *k, const u8 *rand, const u8 *auts,		  u8 *sqn){	u8 amf[2] = { 0x00, 0x00 }; /* TS 33.102 v7.0.0, 6.3.3 */	u8 ak[6], mac_s[8], opc[16];	int i;	milenage_opc(op, k, opc);	milenage_f2345(opc, k, rand, NULL, NULL, NULL, NULL, ak);	for (i = 0; i < 6; i++)		sqn[i] = auts[i] ^ ak[i];	milenage_f1(opc, k, rand, sqn, amf, NULL, mac_s);	if (memcmp(mac_s, auts + 6, 8) != 0)		return -1;	return 0;}#ifdef TEST_MAIN_MILENAGEextern int wpa_debug_level;struct milenage_test_set {	u8 k[16];	u8 rand[16];	u8 sqn[6];	u8 amf[2];	u8 op[16];	u8 opc[16];	u8 f1[8];	u8 f1star[8];	u8 f2[8];	u8 f3[16];	u8 f4[16];	u8 f5[6];	u8 f5star[6];};static const struct milenage_test_set test_sets[] ={	{		/* 3GPP TS 35.208 v6.0.0 - 4.3.1 Test Set 1 */		{ 0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f,		  0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc },		{ 0x23, 0x55, 0x3c, 0xbe, 0x96, 0x37, 0xa8, 0x9d,		  0x21, 0x8a, 0xe6, 0x4d, 0xae, 0x47, 0xbf, 0x35 },		{ 0xff, 0x9b, 0xb4, 0xd0, 0xb6, 0x07 },		{ 0xb9, 0xb9 },		{ 0xcd, 0xc2, 0x02, 0xd5, 0x12, 0x3e, 0x20, 0xf6,		  0x2b, 0x6d, 0x67, 0x6a, 0xc7, 0x2c, 0xb3, 0x18 },		{ 0xcd, 0x63, 0xcb, 0x71, 0x95, 0x4a, 0x9f, 0x4e,		  0x48, 0xa5, 0x99, 0x4e, 0x37, 0xa0, 0x2b, 0xaf },		{ 0x4a, 0x9f, 0xfa, 0xc3, 0x54, 0xdf, 0xaf, 0xb3 },		{ 0x01, 0xcf, 0xaf, 0x9e, 0xc4, 0xe8, 0x71, 0xe9 },		{ 0xa5, 0x42, 0x11, 0xd5, 0xe3, 0xba, 0x50, 0xbf },		{ 0xb4, 0x0b, 0xa9, 0xa3, 0xc5, 0x8b, 0x2a, 0x05,		  0xbb, 0xf0, 0xd9, 0x87, 0xb2, 0x1b, 0xf8, 0xcb },		{ 0xf7, 0x69, 0xbc, 0xd7, 0x51, 0x04, 0x46, 0x04,		  0x12, 0x76, 0x72, 0x71, 0x1c, 0x6d, 0x34, 0x41 },		{ 0xaa, 0x68, 0x9c, 0x64, 0x83, 0x70 },		{ 0x45, 0x1e, 0x8b, 0xec, 0xa4, 0x3b }	}, {		/* 3GPP TS 35.208 v6.0.0 - 4.3.2 Test Set 2 */		{ 0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f,		  0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc },		{ 0x23, 0x55, 0x3c, 0xbe, 0x96, 0x37, 0xa8, 0x9d,		  0x21, 0x8a, 0xe6, 0x4d, 0xae, 0x47, 0xbf, 0x35 },		{ 0xff, 0x9b, 0xb4, 0xd0, 0xb6, 0x07 },		{ 0xb9, 0xb9 },		{ 0xcd, 0xc2, 0x02, 0xd5, 0x12, 0x3e, 0x20, 0xf6,		  0x2b, 0x6d, 0x67, 0x6a, 0xc7, 0x2c, 0xb3, 0x18 },		{ 0xcd, 0x63, 0xcb, 0x71, 0x95, 0x4a, 0x9f, 0x4e,		  0x48, 0xa5, 0x99, 0x4e, 0x37, 0xa0, 0x2b, 0xaf },		{ 0x4a, 0x9f, 0xfa, 0xc3, 0x54, 0xdf, 0xaf, 0xb3 },		{ 0x01, 0xcf, 0xaf, 0x9e, 0xc4, 0xe8, 0x71, 0xe9 },		{ 0xa5, 0x42, 0x11, 0xd5, 0xe3, 0xba, 0x50, 0xbf },		{ 0xb4, 0x0b, 0xa9, 0xa3, 0xc5, 0x8b, 0x2a, 0x05,		  0xbb, 0xf0, 0xd9, 0x87, 0xb2, 0x1b, 0xf8, 0xcb },		{ 0xf7, 0x69, 0xbc, 0xd7, 0x51, 0x04, 0x46, 0x04,		  0x12, 0x76, 0x72, 0x71, 0x1c, 0x6d, 0x34, 0x41 },		{ 0xaa, 0x68, 0x9c, 0x64, 0x83, 0x70 },		{ 0x45, 0x1e, 0x8b, 0xec, 0xa4, 0x3b }	}, {		/* 3GPP TS 35.208 v6.0.0 - 4.3.3 Test Set 3 */		{ 0xfe, 0xc8, 0x6b, 0xa6, 0xeb, 0x70, 0x7e, 0xd0,		  0x89, 0x05, 0x75, 0x7b, 0x1b, 0xb4, 0x4b, 0x8f },		{ 0x9f, 0x7c, 0x8d, 0x02, 0x1a, 0xcc, 0xf4, 0xdb,		  0x21, 0x3c, 0xcf, 0xf0, 0xc7, 0xf7, 0x1a, 0x6a },		{ 0x9d, 0x02, 0x77, 0x59, 0x5f, 0xfc },		{ 0x72, 0x5c },		{ 0xdb, 0xc5, 0x9a, 0xdc, 0xb6, 0xf9, 0xa0, 0xef,		  0x73, 0x54, 0x77, 0xb7, 0xfa, 0xdf, 0x83, 0x74 },		{ 0x10, 0x06, 0x02, 0x0f, 0x0a, 0x47, 0x8b, 0xf6,		  0xb6, 0x99, 0xf1, 0x5c, 0x06, 0x2e, 0x42, 0xb3 },		{ 0x9c, 0xab, 0xc3, 0xe9, 0x9b, 0xaf, 0x72, 0x81 },		{ 0x95, 0x81, 0x4b, 0xa2, 0xb3, 0x04, 0x43, 0x24 },		{ 0x80, 0x11, 0xc4, 0x8c, 0x0c, 0x21, 0x4e, 0xd2 },		{ 0x5d, 0xbd, 0xbb, 0x29, 0x54, 0xe8, 0xf3, 0xcd,		  0xe6, 0x65, 0xb0, 0x46, 0x17, 0x9a, 0x50, 0x98 },		{ 0x59, 0xa9, 0x2d, 0x3b, 0x47, 0x6a, 0x04, 0x43,		  0x48, 0x70, 0x55, 0xcf, 0x88, 0xb2, 0x30, 0x7b },		{ 0x33, 0x48, 0x4d, 0xc2, 0x13, 0x6b },		{ 0xde, 0xac, 0xdd, 0x84, 0x8c, 0xc6 }	}, {		/* 3GPP TS 35.208 v6.0.0 - 4.3.4 Test Set 4 */		{ 0x9e, 0x59, 0x44, 0xae, 0xa9, 0x4b, 0x81, 0x16,		  0x5c, 0x82, 0xfb, 0xf9, 0xf3, 0x2d, 0xb7, 0x51 },		{ 0xce, 0x83, 0xdb, 0xc5, 0x4a, 0xc0, 0x27, 0x4a,		  0x15, 0x7c, 0x17, 0xf8, 0x0d, 0x01, 0x7b, 0xd6 },		{ 0x0b, 0x60, 0x4a, 0x81, 0xec, 0xa8 },		{ 0x9e, 0x09 },		{ 0x22, 0x30, 0x14, 0xc5, 0x80, 0x66, 0x94, 0xc0,		  0x07, 0xca, 0x1e, 0xee, 0xf5, 0x7f, 0x00, 0x4f },		{ 0xa6, 0x4a, 0x50, 0x7a, 0xe1, 0xa2, 0xa9, 0x8b,		  0xb8, 0x8e, 0xb4, 0x21, 0x01, 0x35, 0xdc, 0x87 },		{ 0x74, 0xa5, 0x82, 0x20, 0xcb, 0xa8, 0x4c, 0x49 },		{ 0xac, 0x2c, 0xc7, 0x4a, 0x96, 0x87, 0x18, 0x37 },		{ 0xf3, 0x65, 0xcd, 0x68, 0x3c, 0xd9, 0x2e, 0x96 },		{ 0xe2, 0x03, 0xed, 0xb3, 0x97, 0x15, 0x74, 0xf5,		  0xa9, 0x4b, 0x0d, 0x61, 0xb8, 0x16, 0x34, 0x5d },		{ 0x0c, 0x45, 0x24, 0xad, 0xea, 0xc0, 0x41, 0xc4,		  0xdd, 0x83, 0x0d, 0x20, 0x85, 0x4f, 0xc4, 0x6b },		{ 0xf0, 0xb9, 0xc0, 0x8a, 0xd0, 0x2e },		{ 0x60, 0x85, 0xa8, 0x6c, 0x6f, 0x63 }	}, {		/* 3GPP TS 35.208 v6.0.0 - 4.3.5 Test Set 5 */		{ 0x4a, 0xb1, 0xde, 0xb0, 0x5c, 0xa6, 0xce, 0xb0,		  0x51, 0xfc, 0x98, 0xe7, 0x7d, 0x02, 0x6a, 0x84 },		{ 0x74, 0xb0, 0xcd, 0x60, 0x31, 0xa1, 0xc8, 0x33,		  0x9b, 0x2b, 0x6c, 0xe2, 0xb8, 0xc4, 0xa1, 0x86 },		{ 0xe8, 0x80, 0xa1, 0xb5, 0x80, 0xb6 },		{ 0x9f, 0x07 },		{ 0x2d, 0x16, 0xc5, 0xcd, 0x1f, 0xdf, 0x6b, 0x22,		  0x38, 0x35, 0x84, 0xe3, 0xbe, 0xf2, 0xa8, 0xd8 },		{ 0xdc, 0xf0, 0x7c, 0xbd, 0x51, 0x85, 0x52, 0x90,		  0xb9, 0x2a, 0x07, 0xa9, 0x89, 0x1e, 0x52, 0x3e },		{ 0x49, 0xe7, 0x85, 0xdd, 0x12, 0x62, 0x6e, 0xf2 },		{ 0x9e, 0x85, 0x79, 0x03, 0x36, 0xbb, 0x3f, 0xa2 },		{ 0x58, 0x60, 0xfc, 0x1b, 0xce, 0x35, 0x1e, 0x7e },		{ 0x76, 0x57, 0x76, 0x6b, 0x37, 0x3d, 0x1c, 0x21,		  0x38, 0xf3, 0x07, 0xe3, 0xde, 0x92, 0x42, 0xf9 },		{ 0x1c, 0x42, 0xe9, 0x60, 0xd8, 0x9b, 0x8f, 0xa9,		  0x9f, 0x27, 0x44, 0xe0, 0x70, 0x8c, 0xcb, 0x53 },		{ 0x31, 0xe1, 0x1a, 0x60, 0x91, 0x18 },		{ 0xfe, 0x25, 0x55, 0xe5, 0x4a, 0xa9 }	}, {		/* 3GPP TS 35.208 v6.0.0 - 4.3.6 Test Set 6 */		{ 0x6c, 0x38, 0xa1, 0x16, 0xac, 0x28, 0x0c, 0x45,		  0x4f, 0x59, 0x33, 0x2e, 0xe3, 0x5c, 0x8c, 0x4f },		{ 0xee, 0x64, 0x66, 0xbc, 0x96, 0x20, 0x2c, 0x5a,		  0x55, 0x7a, 0xbb, 0xef, 0xf8, 0xba, 0xbf, 0x63 },		{ 0x41, 0x4b, 0x98, 0x22, 0x21, 0x81 },		{ 0x44, 0x64 },		{ 0x1b, 0xa0, 0x0a, 0x1a, 0x7c, 0x67, 0x00, 0xac,		  0x8c, 0x3f, 0xf3, 0xe9, 0x6a, 0xd0, 0x87, 0x25 },		{ 0x38, 0x03, 0xef, 0x53, 0x63, 0xb9, 0x47, 0xc6,		  0xaa, 0xa2, 0x25, 0xe5, 0x8f, 0xae, 0x39, 0x34 },		{ 0x07, 0x8a, 0xdf, 0xb4, 0x88, 0x24, 0x1a, 0x57 },		{ 0x80, 0x24, 0x6b, 0x8d, 0x01, 0x86, 0xbc, 0xf1 },		{ 0x16, 0xc8, 0x23, 0x3f, 0x05, 0xa0, 0xac, 0x28 },		{ 0x3f, 0x8c, 0x75, 0x87, 0xfe, 0x8e, 0x4b, 0x23,		  0x3a, 0xf6, 0x76, 0xae, 0xde, 0x30, 0xba, 0x3b },		{ 0xa7, 0x46, 0x6c, 0xc1, 0xe6, 0xb2, 0xa1, 0x33,		  0x7d, 0x49, 0xd3, 0xb6, 0x6e, 0x95, 0xd7, 0xb4 },		{ 0x45, 0xb0, 0xf6, 0x9a, 0xb0, 0x6c },		{ 0x1f, 0x53, 0xcd, 0x2b, 0x11, 0x13 }	}, {		/* 3GPP TS 35.208 v6.0.0 - 4.3.7 Test Set 7 */		{ 0x2d, 0x60, 0x9d, 0x4d, 0xb0, 0xac, 0x5b, 0xf0,		  0xd2, 0xc0, 0xde, 0x26, 0x70, 0x14, 0xde, 0x0d },		{ 0x19, 0x4a, 0xa7, 0x56, 0x01, 0x38, 0x96, 0xb7,		  0x4b, 0x4a, 0x2a, 0x3b, 0x0a, 0xf4, 0x53, 0x9e },		{ 0x6b, 0xf6, 0x94, 0x38, 0xc2, 0xe4 },		{ 0x5f, 0x67 },		{ 0x46, 0x0a, 0x48, 0x38, 0x54, 0x27, 0xaa, 0x39,		  0x26, 0x4a, 0xac, 0x8e, 0xfc, 0x9e, 0x73, 0xe8 },		{ 0xc3, 0x5a, 0x0a, 0xb0, 0xbc, 0xbf, 0xc9, 0x25,		  0x2c, 0xaf, 0xf1, 0x5f, 0x24, 0xef, 0xbd, 0xe0 },		{ 0xbd, 0x07, 0xd3, 0x00, 0x3b, 0x9e, 0x5c, 0xc3 },		{ 0xbc, 0xb6, 0xc2, 0xfc, 0xad, 0x15, 0x22, 0x50 },		{ 0x8c, 0x25, 0xa1, 0x6c, 0xd9, 0x18, 0xa1, 0xdf },		{ 0x4c, 0xd0, 0x84, 0x60, 0x20, 0xf8, 0xfa, 0x07,		  0x31, 0xdd, 0x47, 0xcb, 0xdc, 0x6b, 0xe4, 0x11 },		{ 0x88, 0xab, 0x80, 0xa4, 0x15, 0xf1, 0x5c, 0x73,		  0x71, 0x12, 0x54, 0xa1, 0xd3, 0x88, 0xf6, 0x96 },

⌨️ 快捷键说明

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