summaryrefslogtreecommitdiff
path: root/src/libsimaka/simaka_crypto.c
blob: 92db19317a677a4e60ef2e4799d7cabc305a0f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2009-2011 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "simaka_crypto.h"

#include "simaka_manager.h"

#include <debug.h>

/** length of the k_encr key */
#define KENCR_LEN 16
/** length of the k_auth key */
#define KAUTH_LEN 16
/** length of the MSK */
#define MSK_LEN 64
/** length of the EMSK */
#define EMSK_LEN 64

typedef struct private_simaka_crypto_t private_simaka_crypto_t;

/**
 * Private data of an simaka_crypto_t object.
 */
struct private_simaka_crypto_t {

	/**
	 * Public simaka_crypto_t interface.
	 */
	simaka_crypto_t public;

	/**
	 * EAP type this crypto is used, SIM or AKA
	 */
	eap_type_t type;

	/**
	 * signer to create/verify AT_MAC
	 */
	signer_t *signer;

	/**
	 * crypter to encrypt/decrypt AT_ENCR_DATA
	 */
	crypter_t *crypter;

	/**
	 * hasher used in key derivation
	 */
	hasher_t *hasher;

	/**
	 * PRF function used in key derivation
	 */
	prf_t *prf;

	/**
	 * Random number generator to generate nonces
	 */
	rng_t *rng;

	/**
	 * Have k_encr/k_auth been derived?
	 */
	bool derived;
};

METHOD(simaka_crypto_t, get_signer, signer_t*,
	private_simaka_crypto_t *this)
{
	return this->derived ? this->signer : NULL;
}

METHOD(simaka_crypto_t, get_crypter, crypter_t*,
	private_simaka_crypto_t *this)
{
	return this->derived ? this->crypter : NULL;
}

METHOD(simaka_crypto_t, get_rng, rng_t*,
	private_simaka_crypto_t *this)
{
	return this->rng;
}

/**
 * Call SIM/AKA key hook
 */
static void call_hook(private_simaka_crypto_t *this, chunk_t encr, chunk_t auth)
{
	simaka_manager_t *mgr;

	switch (this->type)
	{
		case EAP_SIM:
			mgr = lib->get(lib, "sim-manager");
			break;
		case EAP_AKA:
			mgr = lib->get(lib, "aka-manager");
			break;
		default:
			return;
	}
	mgr->key_hook(mgr, encr, auth);
}

METHOD(simaka_crypto_t, derive_keys_full, bool,
	private_simaka_crypto_t *this, identification_t *id,
	chunk_t data, chunk_t *mk, chunk_t *msk)
{
	chunk_t str, k_encr, k_auth;
	int i;

	/* For SIM: MK = SHA1(Identity|n*Kc|NONCE_MT|Version List|Selected Version)
	 * For AKA: MK = SHA1(Identity|IK|CK) */
	if (!this->hasher->get_hash(this->hasher, id->get_encoding(id), NULL) ||
		!this->hasher->allocate_hash(this->hasher, data, mk))
	{
		return FALSE;
	}
	DBG3(DBG_LIB, "MK %B", mk);

	/* K_encr | K_auth | MSK | EMSK = prf() | prf() | prf() | prf() */
	if (!this->prf->set_key(this->prf, *mk))
	{
		chunk_clear(mk);
		return FALSE;
	}
	str = chunk_alloca(this->prf->get_block_size(this->prf) * 3);
	for (i = 0; i < 3; i++)
	{
		if (!this->prf->get_bytes(this->prf, chunk_empty,
								  str.ptr + str.len / 3 * i))
		{
			chunk_clear(mk);
			return FALSE;
		}
	}

	k_encr = chunk_create(str.ptr, KENCR_LEN);
	k_auth = chunk_create(str.ptr + KENCR_LEN, KAUTH_LEN);
	DBG3(DBG_LIB, "K_encr %B\nK_auth %B\nMSK %B", &k_encr, &k_auth, &msk);

	if (!this->signer->set_key(this->signer, k_auth) ||
		!this->crypter->set_key(this->crypter, k_encr))
	{
		chunk_clear(mk);
		return FALSE;
	}

	*msk = chunk_clone(chunk_create(str.ptr + KENCR_LEN + KAUTH_LEN, MSK_LEN));

	call_hook(this, k_encr, k_auth);

	this->derived = TRUE;
	return TRUE;
}

METHOD(simaka_crypto_t, derive_keys_reauth, bool,
	private_simaka_crypto_t *this, chunk_t mk)
{
	chunk_t str, k_encr, k_auth;
	int i;

	/* K_encr | K_auth = prf() | prf() */
	if (!this->prf->set_key(this->prf, mk))
	{
		return FALSE;
	}
	str = chunk_alloca(this->prf->get_block_size(this->prf) * 2);
	for (i = 0; i < 2; i++)
	{
		if (!this->prf->get_bytes(this->prf, chunk_empty,
								  str.ptr + str.len / 2 * i))
		{
			return FALSE;
		}
	}
	k_encr = chunk_create(str.ptr, KENCR_LEN);
	k_auth = chunk_create(str.ptr + KENCR_LEN, KAUTH_LEN);
	DBG3(DBG_LIB, "K_encr %B\nK_auth %B", &k_encr, &k_auth);

	if (!this->signer->set_key(this->signer, k_auth) ||
		!this->crypter->set_key(this->crypter, k_encr))
	{
		return FALSE;
	}

	call_hook(this, k_encr, k_auth);

	this->derived = TRUE;
	return TRUE;
}

METHOD(simaka_crypto_t, derive_keys_reauth_msk, bool,
	private_simaka_crypto_t *this, identification_t *id, chunk_t counter,
	chunk_t nonce_s, chunk_t mk, chunk_t *msk)
{
	char xkey[HASH_SIZE_SHA1];
	chunk_t str;
	int i;

	if (!this->hasher->get_hash(this->hasher, id->get_encoding(id), NULL) ||
		!this->hasher->get_hash(this->hasher, counter, NULL) ||
		!this->hasher->get_hash(this->hasher, nonce_s, NULL) ||
		!this->hasher->get_hash(this->hasher, mk, xkey))
	{
		return FALSE;
	}

	/* MSK | EMSK = prf() | prf() | prf() | prf() */
	if (!this->prf->set_key(this->prf, chunk_create(xkey, sizeof(xkey))))
	{
		return FALSE;
	}
	str = chunk_alloca(this->prf->get_block_size(this->prf) * 2);
	for (i = 0; i < 2; i++)
	{
		if (!this->prf->get_bytes(this->prf, chunk_empty,
								  str.ptr + str.len / 2 * i))
		{
			return FALSE;
		}
	}
	*msk = chunk_clone(chunk_create(str.ptr, MSK_LEN));
	DBG3(DBG_LIB, "MSK %B", msk);

	return TRUE;
}

METHOD(simaka_crypto_t, clear_keys, void,
	private_simaka_crypto_t *this)
{
	this->derived = FALSE;
}

METHOD(simaka_crypto_t, destroy, void,
	private_simaka_crypto_t *this)
{
	DESTROY_IF(this->rng);
	DESTROY_IF(this->hasher);
	DESTROY_IF(this->prf);
	DESTROY_IF(this->signer);
	DESTROY_IF(this->crypter);
	free(this);
}

/**
 * See header
 */
simaka_crypto_t *simaka_crypto_create(eap_type_t type)
{
	private_simaka_crypto_t *this;

	INIT(this,
		.public = {
			.get_signer = _get_signer,
			.get_crypter = _get_crypter,
			.get_rng = _get_rng,
			.derive_keys_full = _derive_keys_full,
			.derive_keys_reauth = _derive_keys_reauth,
			.derive_keys_reauth_msk = _derive_keys_reauth_msk,
			.clear_keys = _clear_keys,
			.destroy = _destroy,
		},
		.type = type,
		.rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
		.hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1),
		.prf = lib->crypto->create_prf(lib->crypto, PRF_FIPS_SHA1_160),
		.signer = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_SHA1_128),
		.crypter = lib->crypto->create_crypter(lib->crypto, ENCR_AES_CBC, 16),
	);
	if (!this->rng || !this->hasher || !this->prf ||
		!this->signer || !this->crypter)
	{
		DBG1(DBG_LIB, "unable to use %N, missing algorithms",
			 eap_type_names, type);
		destroy(this);
		return NULL;
	}
	return &this->public;
}