sim_simulator.c   [plain text]


/*
 * Copyright (c) 2015 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

/*
 * sim_simulator.c
 * - Software SIM implementation for testing purpose
 */
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <CommonCrypto/CommonCryptor.h>
#include "EAPLog.h"
#include "EAPSIMAKAUtil.h"
#include "sim_simulator.h"
#include "symbol_scope.h"

#define SIM_CK_SIZE 16
#define SIM_IK_SIZE 16

STATIC Boolean
sim_simulator_aes128_encrypt(const uint8_t *key, const uint8_t *ptext, uint8_t *ctext)
{
	Boolean				ret			= TRUE;
	CCCryptorRef		cryptor		= NULL;
	CCCryptorStatus		status;
	size_t				buf_used;

	status = CCCryptorCreate(kCCEncrypt,
			     kCCAlgorithmAES128,
			     kCCOptionECBMode,
			     key,
			     kCCKeySizeAES128,
			     NULL,
			     &cryptor);
	if (status != kCCSuccess) {
		EAPLOG(LOG_NOTICE, "CCCryptoCreate failed with %d", status);
		ret = FALSE;
		goto done;
	}
	status = CCCryptorUpdate(cryptor, ptext, kCCBlockSizeAES128, ctext, kCCBlockSizeAES128, &buf_used);
	if (status != kCCSuccess) {
		EAPLOG(LOG_NOTICE, "CCCryptorUpdate failed with %d", status);
		ret = FALSE;
		goto done;
	}

done:
	if (cryptor != NULL) {
		status = CCCryptorRelease(cryptor);
	}
	return ret;
}

/*
 * opc   : 128bit operator constant
 * Ki    : 128bit subscriber key
 * rand  : 128bit server random challenge
 * res   : 64bit result output
*/
STATIC void
sim_simulator_umts_f2(const uint8_t *opc, const uint8_t *Ki, const uint8_t *rand, uint8_t *res)
{
	int i;
	uint8_t temp[16];
	uint8_t ptext[16];

	for(i = 0; i < 16; i++) {
		ptext[i] = opc[i] ^ rand[i];
	}
	sim_simulator_aes128_encrypt(Ki, ptext, temp);

	/* rotate by r2 (= 0). nothing to do */
	for(i = 0; i < 16; i++) {
                ptext[i] = temp[i] ^ opc[i];
        }

	/* c2[i] = 0 for 0 ≤ i ≤ 127, except that c2[127] = 1 */
	ptext[15] ^= 1; /* XOR c2 */
	sim_simulator_aes128_encrypt(Ki, ptext, temp);
	for(i = 0; i < 16; i++) {
                temp[i] ^= opc[i];
        }
	if (res) {
		memcpy(res, temp + 8, 8);
	}
}

/*
 * opc   : 128bit operator constant
 * Ki    : 128bit subscriber key
 * rand  : 128bit server random challenge
 * ck    : 128bit confidentiality key
*/
STATIC void
sim_simulator_umts_f3(const uint8_t *opc, const uint8_t *Ki, const uint8_t *rand, uint8_t *ck)
{
	int i;
        uint8_t temp[SIM_CK_SIZE];
        uint8_t ptext[SIM_CK_SIZE];

        for(i = 0; i < SIM_CK_SIZE; i++) {
                ptext[i] = opc[i] ^ rand[i];
        }
        sim_simulator_aes128_encrypt(Ki, ptext, temp);

	/* rotate by r3 (= 4bytes) */
	for(i = 0; i < SIM_CK_SIZE; i++) {
		ptext[(i + 12) % 16] = temp[i] ^ opc[i];
	}

	/* c3[i] = 0 for 0 ≤ i ≤ 127, except that c3[126] = 1 */
	ptext[15] ^= 2; /* XOR c3 */
	sim_simulator_aes128_encrypt(Ki, ptext, temp);
	for(i = 0; i < SIM_CK_SIZE; i++) {
                temp[i] ^= opc[i];
        }
        if (ck) {
                memcpy(ck, temp, SIM_CK_SIZE);
        }
}

/*
 * opc  : 128bit operator constant
 * Ki   : 128bit subscriber key
 * rand : 128bit server random challenge
 * ik   : 128bitbit integrity key
*/
STATIC void
sim_simulator_umts_f4(const uint8_t *opc, const uint8_t *Ki, const uint8_t *rand, uint8_t *ik)
{
	int i;
        uint8_t temp[SIM_IK_SIZE];
        uint8_t ptext[SIM_IK_SIZE];

        for(i = 0; i < SIM_IK_SIZE; i++) {
                ptext[i] = opc[i] ^ rand[i];
        }
        sim_simulator_aes128_encrypt(Ki, ptext, temp);

	/* rotate by r4 (= 8bytes) */
        for(i = 0; i < SIM_IK_SIZE; i++) {
                ptext[(i + 8) % 16] = temp[i] ^ opc[i];
        }

	/* c4[i] = 0 for 0 ≤ i ≤ 127, except that c4[125] = 1 */
	ptext[15] ^= 4; /* XOR c4 */
	sim_simulator_aes128_encrypt(Ki, ptext, temp);
	for(i = 0; i < SIM_IK_SIZE; i++) {
                temp[i] ^= opc[i];
        }
        if (ik) {
                memcpy(ik, temp, SIM_IK_SIZE);
	}
}

/*
 * res  : 128bit res from f2()
 * sres : 64bit output sres
*/
STATIC void
sim_simulator_gsm_derive_sres_f1(const uint8_t res[8], uint8_t *sres)
{
	int i;
	for (i = 0; i < SIM_SRES_SIZE; i++)
		sres[i] = res[i]  ^ res[i+4];
}

#ifdef GSM_MILENAGE_DEBUG

STATIC void
sim_simulator_print_key_material(const uint8_t *opc, const uint8_t *Ki, const uint8_t *rand,
								 const uint8_t *sres, const uint8_t *Kc)
{
	int offset = 0;
	char arr[40];
	uint8_t sres2[4];

	memset(arr, 0, sizeof(arr));
	for(i = 0; i < 16; i++) {
		offset += snprintf(arr+offset, sizeof(arr), "%02X ", opc[i]);
	}
	EAPLOG(LOG_NOTICE, "sim_simulator_gsm_milenage_algo: opc : [%s]", arr);

	memset(arr, 0, sizeof(arr));
	offset = 0;
	for(i = 0; i < 16; i++) {
		offset += snprintf(arr+offset, sizeof(arr), "%02X ", Ki[i]);
	}
	EAPLOG(LOG_NOTICE, "sim_simulator_gsm_milenage_algo: Ki : [%s]", arr);

	memset(arr, 0, sizeof(arr));
	offset = 0;
	for(i = 0; i < 16; i++) {
		offset += snprintf(arr+offset, sizeof(arr), "%02X ", rand[i]);
	}
	EAPLOG(LOG_NOTICE, "sim_simulator_gsm_milenage_algo: rand : [%s]", arr);

	memset(arr, 0, sizeof(arr));
	for(i = 0; i < 4; i++) {
		offset += snprintf(arr+offset, sizeof(arr), "%02X ", sres[i]);
	}
	EAPLOG(LOG_NOTICE, "sim_simulator_gsm_milenage_algo: sres1 : [%s]", arr);

	sim_simulator_gsm_derive_sres_f2(res, sres2);
	memset(arr, 0, sizeof(arr));
	offset = 0;
	for(i = 0; i < 4; i++) {
		offset += snprintf(arr+offset, sizeof(arr), "%02X ", sres2[i]);
	}
	EAPLOG(LOG_NOTICE, "sim_simulator_gsm_milenage_algo: sres2 : [%s]", arr);

	memset(arr, 0, sizeof(arr));
	offset = 0;
	for(i = 0; i < 8; i++) {
		offset += snprintf(arr+offset, sizeof(arr), "%02X ", Kc[i]);
	}
	EAPLOG(LOG_NOTICE, "sim_simulator_gsm_milenage_algo: Kc : [%s]", arr);
}
#endif /* GSM_MILENAGE_DEBUG */

/*
 * Ki = subscriber Key
 * rand = RAND challenge from server
 * opc = operator specific constant
*/
PRIVATE_EXTERN void
sim_simulator_gsm_milenage_algo(const uint8_t *opc, const uint8_t *Ki, const uint8_t *rand, uint8_t *sres, uint8_t *Kc)
{
	uint8_t temp_ck[8];
	uint8_t temp_ik[8];
	uint8_t res[8];
	uint8_t ik[SIM_IK_SIZE];
	uint8_t ck[SIM_CK_SIZE];

	int i;
	sim_simulator_umts_f2(opc, Ki, rand, res);
	sim_simulator_umts_f3(opc, Ki, rand, ck);
	sim_simulator_umts_f4(opc, Ki, rand, ik);
	for(i = 0; i < 8; i++)
	{
		temp_ck[i] = ck[i] ^ ck[i+8];
		temp_ik[i] = ik[i] ^ ik[i+8];
		Kc[i] = temp_ck[i] ^ temp_ik[i];
	}
	sim_simulator_gsm_derive_sres_f1(res, sres);
#ifdef GSM_MILENAGE_DEBUG
	sim_simulator_print_key_material(opc, Ki, rand,	sres, Kc);
#endif /* GSM_MILENAGE_DEBUG */
}


#ifdef TEST_GSM_MILENAGE_TEST_VECTOR

/*
 * res : 128bit res from f2()
 * sres: 64bit output sres
 */
STATIC void
sim_simulator_gsm_derive_sres_f2(const uint8_t *res, uint8_t *sres)
{
	memcpy(sres, res, 4);
}

STATIC void
sim_simulator_gsm_milenage_algo_sres2(const uint8_t *opc, const uint8_t *Ki, const uint8_t *rand, uint8_t *sres, uint8_t *Kc)
{
	uint8_t temp_ck[8];
	uint8_t temp_ik[8];
	uint8_t res[8];
	uint8_t ik[SIM_IK_SIZE];
	uint8_t ck[SIM_CK_SIZE];

	int i;

	sim_simulator_umts_f2(opc, Ki, rand, res);
	sim_simulator_umts_f3(opc, Ki, rand, ck);
	sim_simulator_umts_f4(opc, Ki, rand, ik);
	for(i = 0; i < 8; i++)
	{
		temp_ck[i] = ck[i] ^ ck[i+8];
		temp_ik[i] = ik[i] ^ ik[i+8];
		Kc[i] = temp_ck[i] ^ temp_ik[i];
	}
	sim_simulator_gsm_derive_sres_f2(res, sres);
	return;
}

typedef struct input_param_set {
	uint8_t m_opc[SIM_OPC_SIZE];
	uint8_t m_Ki[SIM_KI_SIZE];
	uint8_t m_rand[SIM_RAND_SIZE];
}input_param_set_t;

typedef struct output_value_set {
	uint8_t sres1[SIM_SRES_SIZE];
	uint8_t sres2[SIM_SRES_SIZE];
	uint8_t Kc[SIM_KC_SIZE];
}output_value_set_t;

input_param_set_t inputs[19] =
{
	/* Test Set 1 */
	{
		{0xcd, 0x63, 0xcb, 0x71, 0x95, 0x4a, 0x9f, 0x4e, 0x48, 0xa5, 0x99, 0x4e,
			0x37, 0xa0, 0x2b, 0xaf},
		{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}
	},

	/* Test Set 2 */
	{
		{0x10, 0x06, 0x02, 0x0f, 0x0a, 0x47, 0x8b, 0xf6, 0xb6, 0x99, 0xf1, 0x5c,
			0x06, 0x2e, 0x42, 0xb3},
		{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}
	},

	/* Test Set 3 */
	{
		{0xa6, 0x4a, 0x50, 0x7a, 0xe1, 0xa2, 0xa9, 0x8b, 0xb8, 0x8e, 0xb4, 0x21,
			0x01, 0x35, 0xdc, 0x87},
		{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}
	},

	/* Test Set 4 */
	{
		{0xdc, 0xf0, 0x7c, 0xbd, 0x51, 0x85, 0x52, 0x90, 0xb9, 0x2a, 0x07, 0xa9,
			0x89, 0x1e, 0x52, 0x3e},
		{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}
	},

	/* Test Set 5 */
	{
		{0x38, 0x03, 0xef, 0x53, 0x63, 0xb9, 0x47, 0xc6, 0xaa, 0xa2, 0x25, 0xe5,
			0x8f, 0xae, 0x39, 0x34},
		{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}
	},

	/* Test Set 6 */
	{
		{0xc3, 0x5a, 0x0a, 0xb0, 0xbc, 0xbf, 0xc9, 0x25, 0x2c, 0xaf, 0xf1, 0x5f,
			0x24, 0xef, 0xbd, 0xe0},
		{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}
	},

	/* Test Set 7 */
	{
		{0x27, 0x95, 0x3e, 0x49, 0xbc, 0x8a, 0xf6, 0xdc, 0xc6, 0xe7, 0x30, 0xeb,
			0x80, 0x28, 0x6b, 0xe3},
		{0xa5, 0x30, 0xa7, 0xfe, 0x42, 0x8f, 0xad, 0x10, 0x82, 0xc4, 0x5e, 0xdd,
			0xfc, 0xe1, 0x38, 0x84},
		{0x3a, 0x4c, 0x2b, 0x32, 0x45, 0xc5, 0x0e, 0xb5, 0xc7, 0x1d, 0x08, 0x63,
			0x93, 0x95, 0x76, 0x4d}
	},

	/* Test Set 8 */
	{
		{0xc4, 0xc9, 0x3e, 0xff, 0xe8, 0xa0, 0x81, 0x38, 0xc2, 0x03, 0xd4, 0xc2,
			0x7c, 0xe4, 0xe3, 0xd9},
		{0xd9, 0x15, 0x1c, 0xf0, 0x48, 0x96, 0xe2, 0x58, 0x30, 0xbf, 0x2e, 0x08,
			0x26, 0x7b, 0x83, 0x60},
		{0xf7, 0x61, 0xe5, 0xe9, 0x3d, 0x60, 0x3f, 0xeb, 0x73, 0x0e, 0x27, 0x55,
			0x6c, 0xb8, 0xa2, 0xca}
	},

	/* Test Set 9 */
	{
		{0x82, 0xa2, 0x6f, 0x22, 0xbb, 0xa9, 0xe9, 0x48, 0x8f, 0x94, 0x9a, 0x10,
			0xd9, 0x8e, 0x9c, 0xc4},
		{0xa0, 0xe2, 0x97, 0x1b, 0x68, 0x22, 0xe8, 0xd3, 0x54, 0xa1, 0x8c, 0xc2,
			0x35, 0x62, 0x4e, 0xcb},
		{0x08, 0xef, 0xf8, 0x28, 0xb1, 0x3f, 0xdb, 0x56, 0x27, 0x22, 0xc6, 0x5c,
			0x7f, 0x30, 0xa9, 0xb2}
	},

	/* Test Set 10 */
	{
		{0x0d, 0xb1, 0x07, 0x1f, 0x87, 0x67, 0x56, 0x2c, 0xa4, 0x3a, 0x0a, 0x64,
			0xc4, 0x1e, 0x8d, 0x08},
		{0x0d, 0xa6, 0xf7, 0xba, 0x86, 0xd5, 0xea, 0xc8, 0xa1, 0x9c, 0xf5, 0x63,
			0xac, 0x58, 0x64, 0x2d},
		{0x67, 0x9a, 0xc4, 0xdb, 0xac, 0xd7, 0xd2, 0x33, 0xff, 0x9d, 0x68, 0x06,
			0xf4, 0x14, 0x9c, 0xe3}
	},

	/* Test Set 11 */
	{
		{0xd4, 0x83, 0xaf, 0xae, 0x56, 0x24, 0x09, 0xa3, 0x26, 0xb5, 0xbb, 0x0b,
			0x20, 0xc4, 0xd7, 0x62},
		{0x77, 0xb4, 0x58, 0x43, 0xc8, 0x8e, 0x58, 0xc1, 0x0d, 0x20, 0x26, 0x84,
			0x51, 0x5e, 0xd4, 0x30},
		{0x4c, 0x47, 0xeb, 0x30, 0x76, 0xdc, 0x55, 0xfe, 0x51, 0x06, 0xcb, 0x20,
			0x34, 0xb8, 0xcd, 0x78}
	},

	/* Test Set 12 */
	{
		{0x22, 0x8c, 0x2f, 0x2f, 0x06, 0xac, 0x32, 0x68, 0xa9, 0xe6, 0x16, 0xee,
			0x16, 0xdb, 0x4b, 0xa1},
		{0x72, 0x9b, 0x17, 0x72, 0x92, 0x70, 0xdd, 0x87, 0xcc, 0xdf, 0x1b, 0xfe,
			0x29, 0xb4, 0xe9, 0xbb},
		{0x31, 0x1c, 0x4c, 0x92, 0x97, 0x44, 0xd6, 0x75, 0xb7, 0x20, 0xf3, 0xb7,
			0xe9, 0xb1, 0xcb, 0xd0}
	},

	/* Test Set 13 */
	{
		{0xd2, 0x2a, 0x4b, 0x41, 0x80, 0xa5, 0x32, 0x57, 0x08, 0xa5, 0xff, 0x70,
			0xd9, 0xf6, 0x7e, 0xc7},
		{0xd3, 0x2d, 0xd2, 0x3e, 0x89, 0xdc, 0x66, 0x23, 0x54, 0xca, 0x12, 0xeb,
			0x79, 0xdd, 0x32, 0xfa},
		{0xcf, 0x7d, 0x0a, 0xb1, 0xd9, 0x43, 0x06, 0x95, 0x0b, 0xf1, 0x20, 0x18,
			0xfb, 0xd4, 0x68, 0x87}
	},

	/* Test Set 14 */
	{
		{0xa4, 0xcf, 0x5c, 0x81, 0x55, 0xc0, 0x8a, 0x7e, 0xff, 0x41, 0x8e, 0x54,
			0x43, 0xb9, 0x8e, 0x55},
		{0xaf, 0x7c, 0x65, 0xe1, 0x92, 0x72, 0x21, 0xde, 0x59, 0x11, 0x87, 0xa2,
			0xc5, 0x98, 0x7a, 0x53},
		{ 0x1f, 0x0f, 0x85, 0x78, 0x46, 0x4f, 0xd5, 0x9b, 0x64, 0xbe, 0xd2, 0xd0,
			0x94, 0x36, 0xb5, 0x7a}
	},

	/* Test Set 15 */
	{
		{0x76, 0x08, 0x9d, 0x3c, 0x0f, 0xf3, 0xef, 0xdc, 0x6e, 0x36, 0x72, 0x1d,
			0x4f, 0xce, 0xb7, 0x47},
		{0x5b, 0xd7, 0xec, 0xd3, 0xd3, 0x12, 0x7a, 0x41, 0xd1, 0x25, 0x39, 0xbe,
			0xd4, 0xe7, 0xcf, 0x71},
		{0x59, 0xb7, 0x5f, 0x14, 0x25, 0x1c, 0x75, 0x03, 0x1d, 0x0b, 0xcb, 0xac,
			0x1c, 0x2c, 0x04, 0xc7}
	},

	/* Test Set 16 */
	{
		{0xa2, 0x19, 0xdc, 0x37, 0xf1, 0xdc, 0x7d, 0x66, 0x73, 0x8b, 0x58, 0x43,
			0xc7, 0x99, 0xf2, 0x06},
		{0x6c, 0xd1, 0xc6, 0xce, 0xb1, 0xe0, 0x1e, 0x14, 0xf1, 0xb8, 0x23, 0x16,
			0xa9, 0x0b, 0x7f, 0x3d},
		{0xf6, 0x9b, 0x78, 0xf3, 0x00, 0xa0, 0x56, 0x8b, 0xce, 0x9f, 0x0c, 0xb9,
			0x3c, 0x4b, 0xe4, 0xc9}
	},

	/* Test Set 17 */
	{
		{0xdf, 0x0c, 0x67, 0x86, 0x8f, 0xa2, 0x5f, 0x74, 0x8b, 0x70, 0x44, 0xc6,
			0xe7, 0xc2, 0x45, 0xb8},
		{0xb7, 0x3a, 0x90, 0xcb, 0xcf, 0x3a, 0xfb, 0x62, 0x2d, 0xba, 0x83, 0xc5,
			0x8a, 0x84, 0x15, 0xdf},
		{0xb1, 0x20, 0xf1, 0xc1, 0xa0, 0x10, 0x2a, 0x2f, 0x50, 0x7d, 0xd5, 0x43,
			0xde, 0x68, 0x28, 0x1f}
	},

	/* Test Set 18 */
	{
		{0x98, 0x1d, 0x46, 0x4c, 0x7c, 0x52, 0xeb, 0x6e, 0x50, 0x36, 0x23, 0x49,
			0x84, 0xad, 0x0b, 0xcf},
		{0x51, 0x22, 0x25, 0x02, 0x14, 0xc3, 0x3e, 0x72, 0x3a, 0x5d, 0xd5, 0x23,
			0xfc, 0x14, 0x5f, 0xc0},
		{0x81, 0xe9, 0x2b, 0x6c, 0x0e, 0xe0, 0xe1, 0x2e, 0xbc, 0xeb, 0xa8, 0xd9,
			0x2a, 0x99, 0xdf, 0xa5}
	},

	/* Test Set 19 */
	{
		{0xcb, 0x9c, 0xcc, 0xc4, 0xb9, 0x25, 0x8e, 0x6d, 0xca, 0x47, 0x60, 0x37,
			0x9f, 0xb8, 0x25, 0x81},
		{0x90, 0xdc, 0xa4, 0xed, 0xa4, 0x5b, 0x53, 0xcf, 0x0f, 0x12, 0xd7, 0xc9,
			0xc3, 0xbc, 0x6a, 0x89},
		{0x9f, 0xdd, 0xc7, 0x20, 0x92, 0xc6, 0xad, 0x03, 0x6b, 0x6e, 0x46, 0x47,
			0x89, 0x31, 0x5b, 0x78}
	},
};

output_value_set_t outputs[19] =
{
	/* OUTPUT-1 */
	{
		/*sres1*/{0x46, 0xf8, 0x41, 0x6a},
		/*sres2*/{0xa5, 0x42, 0x11, 0xd5},
		/*Kc*/{0xea, 0xe4, 0xbe, 0x82, 0x3a, 0xf9, 0xa0, 0x8b}
	},
	/* OUTPUT-2 */
	{
		/*sres1*/{0x8c, 0x30, 0x8a, 0x5e},
		/*sres2*/{0x80, 0x11, 0xc4, 0x8c},
		/*Kc*/{0xaa, 0x01, 0x73, 0x9b, 0x8c, 0xaa, 0x97, 0x6d}
	},
	/* OUTPUT-3 */
	{
		/*sres1*/{0xcf, 0xbc, 0xe3, 0xfe},
		/*sres2*/{0xf3, 0x65, 0xcd, 0x68},
		/*Kc*/{0x9a, 0x8e, 0xc9, 0x5f, 0x40, 0x8c, 0xc5, 0x07}
	},
	/* OUTPUT-4 */
	{
		/*sres1*/{0x96, 0x55, 0xe2, 0x65},
		/*sres2*/{0x58, 0x60, 0xfc, 0x1b},
		/*Kc*/{0xcd, 0xc1, 0xdc, 0x08, 0x41, 0xb8, 0x1a, 0x22}
	},
	/* OUTPUT-5 */
	{
		/*sres1*/{0x13, 0x68, 0x8f, 0x17},
		/*sres2*/{0x16, 0xc8, 0x23, 0x3f},
		/*Kc*/{0xdf, 0x75, 0xbc, 0x5e, 0xa8, 0x99, 0x87, 0x9f}
	},
	/* OUTPUT-6 */
	{
		/*sres1*/{ 0x55, 0x3d, 0x00, 0xb3},
		/*sres2*/{0x8c, 0x25, 0xa1, 0x6c},
		/*Kc*/{0x84, 0xb4, 0x17, 0xae, 0x3a, 0xea, 0xb4, 0xf3}
	},
	/* OUTPUT-7 */
	{
		/*sres1*/{0x59, 0xf1, 0xa4, 0x4a},
		/*sres2*/{0xa6, 0x32, 0x41, 0xe1},
		/*Kc*/{0x3b, 0x4e, 0x24, 0x4c, 0xdc, 0x60, 0xce, 0x03}
	},
	/* OUTPUT-8 */
	{
		/*sres1*/{0x50, 0x58, 0x88, 0x61},
		/*sres2*/{0x4a, 0x90, 0xb2, 0x17},
		/*Kc*/{0x8d, 0x4e, 0xc0, 0x1d, 0xe5, 0x97, 0xac, 0xfe}
	},
	/* OUTPUT-9 */
	{
		/*sres1*/{0xcd, 0xe6, 0xb0, 0x27},
		/*sres2*/{0x4b, 0xc2, 0x21, 0x2d},
		/*Kc*/{0xd8, 0xde, 0xbc, 0x4f, 0xfb, 0xcd, 0x60, 0xaa}
	},
	/* OUTPUT-10 */
	{
		/*sres1*/{0x02, 0xd1, 0x3a, 0xcd},
		/*sres2*/{0x6f, 0xc3, 0x0f, 0xee},
		/*Kc*/{0xf0, 0xea, 0xa5, 0x0a, 0x1e, 0xdc, 0xeb, 0xb7}
	},
	/* OUTPUT-11 */
	{
		/*sres1*/{0x44, 0x38, 0x9d, 0x01},
		/*sres2*/{0xae, 0xfa, 0x35, 0x7b},
		/*Kc*/{0x82, 0xdb, 0xab, 0x7f, 0x83, 0xf0, 0x63, 0xda}
	},
	/* OUTPUT-12 */
	{
		/*sres1*/{0x03, 0xe0, 0xfd, 0x84},
		/*sres2*/{0x98, 0xdb, 0xbd, 0x09},
		/*Kc*/{0x3c, 0x66, 0xcb, 0x98, 0xca, 0xb2, 0xd3, 0x3d}
	},
	/* OUTPUT-13 */
	{
		/*sres1*/{0xbe, 0x73, 0xb3, 0xdc},
		/*sres2*/{0xaf, 0x4a, 0x41, 0x1e},
		/*Kc*/{0x96, 0x12, 0xb5, 0xd8, 0x8a, 0x41, 0x30, 0xbb}
	},
	/* OUTPUT-14 */
	{
		/*sres1*/{0x8f, 0xe0, 0x19, 0xc7},
		/*sres2*/{0x7b, 0xff, 0xa5, 0xc2},
		/*Kc*/{0x75, 0xa1, 0x50, 0xdf, 0x3c, 0x6a, 0xed, 0x08}
	},
	/* OUTPUT-15 */
	{
		/*sres1*/{0x27, 0x20, 0x2b, 0x82},
		/*sres2*/{0x7e, 0x3f, 0x44, 0xc7},
		/*Kc*/{0xb7, 0xf9, 0x2e, 0x42, 0x6a, 0x36, 0xfe, 0xc5}
	},
	/* OUTPUT-16 */
	{
		/*sres1*/{0xdd, 0xd7, 0xef, 0xe6},
		/*sres2*/{0x70, 0xf6, 0xbd, 0xb9},
		/*Kc*/{0x88, 0xd9, 0xde, 0x10, 0xa2, 0x20, 0x04, 0xc5}
	},
	/* OUTPUT-17 */
	{
		/*sres1*/{0x67, 0xe4, 0xff, 0x3f},
		/*sres2*/{0x47, 0x9d, 0xd2, 0x5c},
		/*Kc*/{0xa8, 0x19, 0xe5, 0x77, 0xa8, 0xd6, 0x17, 0x5b}
	},
	/* OUTPUT-18 */
	{
		/*sres1*/{0x8a, 0x3b, 0x8d, 0x17},
		/*sres2*/{0x28, 0xd7, 0xb0, 0xf2},
		/*Kc*/{0x9a, 0x8d, 0x0e, 0x88, 0x3f, 0xf0, 0x88, 0x7a}
	},
	/* OUTPUT-19 */
	{
		/*sres1*/{0xdf, 0x58, 0x52, 0x2f},
		/*sres2*/{0xa9, 0x51, 0x00, 0xe2},
		/*Kc*/{0xed, 0x29, 0xb2, 0xf1, 0xc2, 0x7f, 0x9f, 0x34}
	},
};


int main()
{
	int test_num;
	uint8_t sres1[4];
	uint8_t sres2[4];
	uint8_t Kc[8];

	for (test_num = 0; test_num < 19; test_num++)
	{
		sim_simulator_gsm_milenage_algo(inputs[test_num].m_opc,
										inputs[test_num].m_Ki, inputs[test_num].m_rand, sres1, Kc);
		sim_simulator_gsm_milenage_algo_sres2(inputs[test_num].m_opc,
											  inputs[test_num].m_Ki, inputs[test_num].m_rand, sres2, Kc);
		if (0 == memcmp(outputs[test_num].sres1, sres1, 4) &&
			0 == memcmp(outputs[test_num].sres2, sres2, 4) &&
			0 == memcmp(outputs[test_num].Kc, Kc, 8))
		{
			printf("test case # %d passed.\n", test_num+1);
		} else {
			printf("test case # %d failed.\n", test_num+1);
		}
	}
	return 0;
}
#endif /* TEST_GSM_MILENAGE_TEST_VECTOR */