eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

simutil.c (12104B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <ctype.h>
     29 #include <stdio.h>
     30 #include <errno.h>
     31 #include <string.h>
     32 #include <ell/ell.h>
     33 
     34 #include "src/missing.h"
     35 #include "src/eap-private.h"
     36 #include "src/crypto.h"
     37 #include "src/simutil.h"
     38 
     39 /*
     40  * RFC 3174 functions
     41  */
     42 /*
     43  * Section 3a - Circular left shift function S
     44  */
     45 #define S(n, x) (((x) << (n)) | ((x) >> (32 - (n))))
     46 
     47 /*
     48  * Section 5 - Functions and Constants Used
     49  *
     50  * K(t) - sequence of constant words K(0) - K(79)
     51  * (represented as a function, index t is constant for every 20 indexes)
     52  */
     53 static uint32_t K(int t)
     54 {
     55 	if (t >= 0 && t <= 19)
     56 		return 0x5a827999;
     57 	else if (t >= 20 && t <= 39)
     58 		return 0x6ed9eba1;
     59 	else if (t >= 40 && t <= 59)
     60 		return 0x8f1bbcdc;
     61 	else if (t >= 60 && t <= 79)
     62 		return 0xca62c1d6;
     63 
     64 	return 0;
     65 }
     66 
     67 /*
     68  * f(t, B, C, D) - sequence of logical functions f(0) - f(79)
     69  * Every 20 indexes the value of t computes a different bit manipulation of
     70  * B, C and D
     71  */
     72 static uint32_t f(int t, uint32_t B, uint32_t C, uint32_t D)
     73 {
     74 	if (t >= 0 && t <= 19)
     75 		return (B & C) | ((~B) & D);
     76 	else if (t >= 20 && t <= 39)
     77 		return B ^ C ^ D;
     78 	else if (t >= 40 && t <= 59)
     79 		return (B & C) | (B & D) | (C & D);
     80 	else if (t >= 60 && t <= 79)
     81 		return B ^ C ^ D;
     82 
     83 	return 0;
     84 }
     85 
     86 /*
     87  * RFC 3174 Section 6.1 Method 1
     88  *
     89  * Core SHA1 block digest function. Computes the SHA1 digest of a single block.
     90  * Named G as it appears in FIPS 182 PRNG.
     91  *
     92  * The Linux kernel does not expose this specific block digest function to the
     93  * user. The SHA1 function exposed in the kernel automatically does the length
     94  * encoded padding to the block which is different than what EAP-SIM requires.
     95  * EAP-SIM requires and extra bits in the block to be zero. This function was
     96  * implemented for this reason.
     97  */
     98 static void G(uint32_t *out, uint8_t *block)
     99 {
    100 	int t;
    101 	uint32_t H[5];
    102 	uint32_t W[80];
    103 	uint32_t A, B, C, D, E;
    104 	uint32_t TEMP;
    105 
    106 	H[0] = out[0];
    107 	H[1] = out[1];
    108 	H[2] = out[2];
    109 	H[3] = out[3];
    110 	H[4] = out[4];
    111 
    112 	/*
    113 	 * a. Divide M (block) into 16 words, W(0) ... W(15) where W(0) is the
    114 	 * left-most word
    115 	 */
    116 	for (t = 0; t < 16; t++) {
    117 		/* copy each word */
    118 		W[t] = L_BE32_TO_CPU(((uint32_t *)block)[t]);
    119 	}
    120 	/*
    121 	 * b. for t = 16 to 79 do
    122 	 */
    123 	for (t = 16; t <= 79; t++) {
    124 		/* W(t) = S^1(W(t-3) XOR W(t-8) XOR W(t-14) XOR W(t-16)) */
    125 		W[t] = S(1, (W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]));
    126 	}
    127 	/* c. Let A = H0, B = H1, C = H2, D = H3, E = H4 */
    128 	A = H[0];
    129 	B = H[1];
    130 	C = H[2];
    131 	D = H[3];
    132 	E = H[4];
    133 
    134 	/* d. For t = 0 to 79 do */
    135 	for (t = 0; t <= 79; t++) {
    136 		/* TEMP = S^5(A) + f(t;B,C,D) + E + W(t) + K(t); */
    137 		TEMP = (S(5, A)) + (f(t, B, C, D) + E + W[t] + K(t));
    138 		/* E = D;  D = C;  C = S^30(B);  B = A; A = TEMP; */
    139 		E = D; D = C; C = S(30, B); B = A; A = TEMP;
    140 	}
    141 
    142 	/*
    143 	 * e. Let H[0-4] == A, B, C, D, E
    144 	 */
    145 	H[0] += A;
    146 	H[1] += B;
    147 	H[2] += C;
    148 	H[3] += D;
    149 	H[4] += E;
    150 
    151 	memcpy(out, H, sizeof(H));
    152 }
    153 
    154 bool eap_aka_derive_primes(const uint8_t *ck, const uint8_t *ik,
    155 		const uint8_t *autn, const uint8_t *network, uint16_t net_len,
    156 		uint8_t *ck_p, uint8_t *ik_p)
    157 {
    158 	struct iovec iov[5];
    159 	struct l_checksum *hmac;
    160 	uint8_t key[32];
    161 	uint8_t fc = 0x20;
    162 	uint16_t l1 = L_CPU_TO_BE16(6);
    163 	uint16_t name_len = L_CPU_TO_BE16(net_len);
    164 	uint8_t digest[32];
    165 
    166 	memcpy(key, ck, EAP_AKA_CK_LEN);
    167 	memcpy(key + EAP_AKA_CK_LEN, ik, EAP_AKA_IK_LEN);
    168 
    169 	hmac = l_checksum_new_hmac(L_CHECKSUM_SHA256, key, 32);
    170 	explicit_bzero(key, sizeof(key));
    171 
    172 	if (!hmac)
    173 		return false;
    174 
    175 	iov[0].iov_base = &fc;
    176 	iov[0].iov_len = 1;
    177 	iov[1].iov_base = (void *)network;
    178 	iov[1].iov_len = net_len;
    179 	iov[2].iov_base = &name_len;
    180 	iov[2].iov_len = 2;
    181 	iov[3].iov_base = (void *)autn;
    182 	iov[3].iov_len = 6;
    183 	iov[4].iov_base = &l1;
    184 	iov[4].iov_len = 2;
    185 
    186 	l_checksum_updatev(hmac, iov, 5);
    187 	l_checksum_get_digest(hmac, digest, 32);
    188 	l_checksum_free(hmac);
    189 
    190 	memcpy(ck_p, digest, EAP_AKA_CK_LEN);
    191 	memcpy(ik_p, digest + EAP_AKA_CK_LEN, EAP_AKA_IK_LEN);
    192 	explicit_bzero(digest, sizeof(digest));
    193 
    194 	return true;
    195 }
    196 
    197 bool eap_aka_prf_prime(const uint8_t *ik_p, const uint8_t *ck_p,
    198 		const char *identity, uint8_t *k_encr, uint8_t *k_aut,
    199 		uint8_t *k_re, uint8_t *msk, uint8_t *emsk)
    200 {
    201 	struct l_checksum *hmac;
    202 	uint8_t key[32];
    203 	struct iovec iov[4];
    204 	/* digest continues to be reused each iteration */
    205 	uint8_t digest[32];
    206 	uint8_t i = 0x01;
    207 	/* 7 iterations will be 224 bytes, 208 of which will get used */
    208 	uint8_t out[224];
    209 	uint8_t *pos = out;
    210 
    211 	/* K = (IK'|CK') */
    212 	memcpy(key, ik_p, EAP_AKA_IK_LEN);
    213 	memcpy(key + EAP_AKA_IK_LEN, ck_p, EAP_AKA_CK_LEN);
    214 
    215 	hmac = l_checksum_new_hmac(L_CHECKSUM_SHA256, key, 32);
    216 	explicit_bzero(key, sizeof(key));
    217 
    218 	if (!hmac)
    219 		return false;
    220 
    221 	iov[0].iov_base = digest;
    222 	/* initial iteration digest is not used */
    223 	iov[0].iov_len = 0;
    224 	iov[1].iov_base = (void *)"EAP-AKA'";
    225 	iov[1].iov_len = strlen("EAP-AKA'");
    226 	iov[2].iov_base = (void *)identity;
    227 	iov[2].iov_len = strlen(identity);
    228 	iov[3].iov_base = &i;
    229 	iov[3].iov_len = 1;
    230 
    231 	/* need 208 bytes for all keys */
    232 	while (pos < out + 224) {
    233 		l_checksum_reset(hmac);
    234 		l_checksum_updatev(hmac, iov, 4);
    235 		l_checksum_get_digest(hmac, digest, 32);
    236 		memcpy(pos, digest, 32);
    237 		pos += 32;
    238 		i++;
    239 		/* set the digest length so it can be prepended as Tn */
    240 		iov[0].iov_len = 32;
    241 	}
    242 
    243 	explicit_bzero(digest, sizeof(digest));
    244 	l_checksum_free(hmac);
    245 
    246 	pos = out;
    247 	memcpy(k_encr, pos, EAP_SIM_K_ENCR_LEN);
    248 	pos += EAP_SIM_K_ENCR_LEN;
    249 	memcpy(k_aut, pos, EAP_AKA_PRIME_K_AUT_LEN);
    250 	pos += EAP_AKA_PRIME_K_AUT_LEN;
    251 	memcpy(k_re, pos, EAP_AKA_K_RE_LEN);
    252 	pos += EAP_AKA_K_RE_LEN;
    253 	memcpy(msk, pos, EAP_SIM_MSK_LEN);
    254 	pos += EAP_SIM_MSK_LEN;
    255 	memcpy(emsk, pos, EAP_SIM_EMSK_LEN);
    256 
    257 	explicit_bzero(out, sizeof(out));
    258 	return true;
    259 }
    260 
    261 void eap_sim_fips_prf(const void *seed, size_t slen, uint8_t *out, size_t olen)
    262 {
    263 	uint8_t xkey[64];
    264 	uint32_t w_i[5];
    265 	uint32_t t[] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476,
    266 			0xC3D2E1F0 };
    267 	uint8_t *pos = out;
    268 	uint32_t c;
    269 	int j, i;
    270 
    271 	/* Copy seed and zero pad remainder */
    272 	memcpy(xkey, seed, slen);
    273 	memset(xkey + slen, 0, sizeof(xkey) - slen);
    274 
    275 	for (j = 0; j < (int)olen / 40; j++) {
    276 		for (i = 0; i < 2; i++) {
    277 			int k;
    278 
    279 			memcpy(w_i, t, sizeof(t));
    280 			/* w_i = G(t, XVAL) */
    281 			G(w_i, xkey);
    282 			for (k = 0; k < 5; k++)
    283 				w_i[k] = L_CPU_TO_BE32(w_i[k]);
    284 
    285 			memcpy(pos, w_i, 20);
    286 			/* XKEY = (1 + XKEY + w_i) mod 2^b*/
    287 			c = 1;
    288 			for (k = 19; k >= 0; k--) {
    289 				uint32_t sum = xkey[k] + pos[k] + c;
    290 
    291 				xkey[k] = sum & 0xff;
    292 				c = sum >> 8;
    293 			}
    294 			pos += 20;
    295 		}
    296 	}
    297 }
    298 
    299 bool eap_sim_get_encryption_keys(const uint8_t *buf, uint8_t *k_encr,
    300 		uint8_t *k_aut, uint8_t *msk, uint8_t *emsk)
    301 {
    302 	const uint8_t *pos = buf;
    303 
    304 	if (!buf || !msk || !emsk) {
    305 		l_error("key pointers are invalid");
    306 		return false;
    307 	}
    308 
    309 	if (k_encr)
    310 		memcpy(k_encr, pos, EAP_SIM_K_ENCR_LEN);
    311 
    312 	pos += EAP_SIM_K_ENCR_LEN;
    313 	if (k_aut)
    314 		memcpy(k_aut, pos, EAP_SIM_K_AUT_LEN);
    315 
    316 	pos += EAP_SIM_K_AUT_LEN;
    317 	memcpy(msk, pos, EAP_SIM_MSK_LEN);
    318 	pos += EAP_SIM_MSK_LEN;
    319 	memcpy(emsk, pos, EAP_SIM_EMSK_LEN);
    320 
    321 	return true;
    322 }
    323 
    324 bool eap_sim_derive_mac(enum eap_type type, const uint8_t *buf, size_t len,
    325 		const uint8_t *key, uint8_t *mac)
    326 {
    327 	if (type == EAP_TYPE_AKA_PRIME)
    328 		return hmac_sha256(key, EAP_AKA_PRIME_K_AUT_LEN, buf, len,
    329 				mac, EAP_SIM_MAC_LEN);
    330 	else
    331 		return hmac_sha1(key, EAP_SIM_K_AUT_LEN, buf, len, mac,
    332 			EAP_SIM_MAC_LEN);
    333 }
    334 
    335 size_t eap_sim_build_header(struct eap_state *eap, enum eap_type method,
    336 		uint8_t type, uint8_t *buf, uint16_t len)
    337 {
    338 	buf[0] = 0x02;
    339 	eap_save_last_id(eap, &buf[1]);
    340 	l_put_be16(len, buf + 2);
    341 	buf[4] = method;
    342 	buf[5] = type;
    343 	buf[6] = 0x00;
    344 	buf[7] = 0x00;
    345 	return 8;
    346 }
    347 
    348 void eap_sim_client_error(struct eap_state *eap, enum eap_type type,
    349 		uint16_t code)
    350 {
    351 	uint8_t buf[12];
    352 
    353 	eap_sim_build_header(eap, type, 0x0e, buf, 12);
    354 	buf[8] = EAP_SIM_AT_CLIENT_ERROR_CODE;
    355 	buf[9] = 1;
    356 	l_put_be16(code, buf + 10);
    357 
    358 	eap_method_respond(eap, buf, 12);
    359 }
    360 
    361 size_t eap_sim_add_attribute(uint8_t *buf, enum eap_sim_at attr,
    362 		uint8_t ptype, const uint8_t *data, uint16_t dlen)
    363 {
    364 	int i;
    365 	uint8_t pos = 0;
    366 	uint8_t pad = 0;
    367 
    368 	buf[pos++] = attr;
    369 
    370 	if (ptype == EAP_SIM_PAD_NONE)
    371 		/* no padding indicates data directly follows ID/size */
    372 		buf[pos++] = EAP_SIM_ROUND(dlen + 2) / 4;
    373 	else
    374 		/* any padding indicates 2 extra bytes before data */
    375 		buf[pos++] = EAP_SIM_ROUND(dlen + 4) / 4;
    376 
    377 	if (ptype == EAP_SIM_PAD_LENGTH) {
    378 		/* Encode length in next two bytes */
    379 		l_put_be16(dlen, buf + pos);
    380 		pos += 2;
    381 	} else if (ptype == EAP_SIM_PAD_ZERO) {
    382 		buf[pos++] = 0x00;
    383 		buf[pos++] = 0x00;
    384 	} else if (ptype == EAP_SIM_PAD_LENGTH_BITS) {
    385 		l_put_be16(dlen * 8, buf + pos);
    386 		pos += 2;
    387 	} /* else no padding */
    388 
    389 	if (data)
    390 		memcpy(buf + pos, data, dlen);
    391 	else
    392 		memset(buf + pos, 0, dlen);
    393 
    394 	pad = (buf[1] * 4) - (dlen + pos);
    395 	pos += dlen;
    396 	/* If header + data is not in multiple of 4 bytes then pad */
    397 	for (i = 0; i < pad; i++)
    398 		buf[pos + i] = 0x00;
    399 
    400 	pos += pad;
    401 	return pos;
    402 }
    403 
    404 bool eap_sim_verify_mac(struct eap_state *eap, enum eap_type type,
    405 		const uint8_t *buf, uint16_t len, uint8_t *k_aut,
    406 		uint8_t *extra, size_t elen)
    407 {
    408 	struct l_checksum *hmac;
    409 	struct eap_sim_tlv_iter iter;
    410 	const uint8_t *mac_p = NULL;
    411 	uint8_t zero_mac[EAP_SIM_MAC_LEN] = { 0 };
    412 	uint8_t hdr[5];
    413 	struct iovec iov[4];
    414 
    415 	eap_sim_tlv_iter_init(&iter, buf + 3, len - 3);
    416 
    417 	while (eap_sim_tlv_iter_next(&iter)) {
    418 		if (eap_sim_tlv_iter_get_type(&iter) == EAP_SIM_AT_MAC) {
    419 			mac_p = eap_sim_tlv_iter_get_data(&iter) + 2;
    420 			break;
    421 		}
    422 	}
    423 
    424 	if (!mac_p) {
    425 		l_error("packet did not contain AT_MAC attribute");
    426 		return false;
    427 	}
    428 
    429 	/* re-build EAP packet header */
    430 	hdr[0] = 0x01;
    431 	eap_save_last_id(eap, &hdr[1]);
    432 	l_put_be16(len + 5, hdr + 2);
    433 	hdr[4] = type;
    434 
    435 	iov[0].iov_base = (void *)hdr;
    436 	iov[0].iov_len = 5;
    437 	iov[1].iov_base = (void *)buf;
    438 	iov[1].iov_len = len - EAP_SIM_MAC_LEN;
    439 	iov[2].iov_base = zero_mac;
    440 	iov[2].iov_len = EAP_SIM_MAC_LEN;
    441 	iov[3].iov_base = extra;
    442 	iov[3].iov_len = elen;
    443 
    444 	if (type == EAP_TYPE_AKA_PRIME)
    445 		hmac = l_checksum_new_hmac(L_CHECKSUM_SHA256, k_aut,
    446 				EAP_AKA_PRIME_K_AUT_LEN);
    447 	else
    448 		hmac = l_checksum_new_hmac(L_CHECKSUM_SHA1, k_aut,
    449 				EAP_SIM_K_AUT_LEN);
    450 
    451 	l_checksum_updatev(hmac, iov, 4);
    452 	/* reuse zero mac array for new mac */
    453 	l_checksum_get_digest(hmac, zero_mac, EAP_SIM_MAC_LEN);
    454 	l_checksum_free(hmac);
    455 
    456 	if (memcmp(zero_mac, mac_p, EAP_SIM_MAC_LEN)) {
    457 		l_error("MAC does not match");
    458 		return false;
    459 	}
    460 
    461 	return true;
    462 }
    463 
    464 bool eap_sim_tlv_iter_init(struct eap_sim_tlv_iter *iter, const uint8_t *data,
    465 		uint32_t len)
    466 {
    467 	iter->data = NULL;
    468 	iter->pos = data;
    469 	iter->len = 0;
    470 	iter->end = data + len;
    471 	return true;
    472 }
    473 
    474 bool eap_sim_tlv_iter_next(struct eap_sim_tlv_iter *iter)
    475 {
    476 	/* check room for tag/len */
    477 	if (iter->end - iter->pos < 2)
    478 		return false;
    479 
    480 	iter->tag = iter->pos[0];
    481 	iter->len = (iter->pos[1] * 4) - 2;
    482 	iter->pos += 2;
    483 
    484 	/* check room for value */
    485 	if (iter->end - iter->pos < iter->len)
    486 		return false;
    487 
    488 	iter->data = iter->pos;
    489 	iter->pos += iter->len;
    490 
    491 	return true;
    492 }
    493 
    494 uint8_t eap_sim_tlv_iter_get_type(struct eap_sim_tlv_iter *iter)
    495 {
    496 	return iter->tag;
    497 }
    498 
    499 uint16_t eap_sim_tlv_iter_get_length(struct eap_sim_tlv_iter *iter)
    500 {
    501 	return iter->len;
    502 }
    503 
    504 const void *eap_sim_tlv_iter_get_data(struct eap_sim_tlv_iter *iter)
    505 {
    506 	return iter->data;
    507 }