eiwd

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

crypto.c (30318B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-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  *  (contains ARC4 implementation copyright (c) 2001 Niels Möller)
     22  *
     23  */
     24 
     25 #ifdef HAVE_CONFIG_H
     26 #include <config.h>
     27 #endif
     28 
     29 #define _GNU_SOURCE
     30 #include <stdbool.h>
     31 #include <string.h>
     32 #include <errno.h>
     33 #include <linux/if_ether.h>
     34 
     35 #include <ell/ell.h>
     36 
     37 #include "ell/useful.h"
     38 #include "src/missing.h"
     39 #include "src/defs.h"
     40 #include "src/crypto.h"
     41 
     42 #define ARC4_MIN_KEY_SIZE	1
     43 #define ARC4_MAX_KEY_SIZE	256
     44 #define ARC4_KEY_SIZE		16
     45 
     46 struct arc4_ctx {
     47 	uint8_t S[256];
     48 	uint8_t i;
     49 	uint8_t j;
     50 };
     51 
     52 /* RFC 3526, Section 2 */
     53 const unsigned char crypto_dh5_prime[] = {
     54 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2,
     55 	0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1,
     56 	0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6,
     57 	0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e, 0x34, 0x04, 0xdd,
     58 	0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d,
     59 	0xf2, 0x5f, 0x14, 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
     60 	0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4, 0x4c, 0x42, 0xe9,
     61 	0xa6, 0x37, 0xed, 0x6b, 0x0b, 0xff, 0x5c, 0xb6, 0xf4, 0x06, 0xb7, 0xed,
     62 	0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5, 0xae, 0x9f, 0x24, 0x11,
     63 	0x7c, 0x4b, 0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51, 0xec, 0xe4, 0x5b, 0x3d,
     64 	0xc2, 0x00, 0x7c, 0xb8, 0xa1, 0x63, 0xbf, 0x05, 0x98, 0xda, 0x48, 0x36,
     65 	0x1c, 0x55, 0xd3, 0x9a, 0x69, 0x16, 0x3f, 0xa8, 0xfd, 0x24, 0xcf, 0x5f,
     66 	0x83, 0x65, 0x5d, 0x23, 0xdc, 0xa3, 0xad, 0x96, 0x1c, 0x62, 0xf3, 0x56,
     67 	0x20, 0x85, 0x52, 0xbb, 0x9e, 0xd5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6d,
     68 	0x67, 0x0c, 0x35, 0x4e, 0x4a, 0xbc, 0x98, 0x04, 0xf1, 0x74, 0x6c, 0x08,
     69 	0xca, 0x23, 0x73, 0x27, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     70 };
     71 size_t crypto_dh5_prime_size = sizeof(crypto_dh5_prime);
     72 
     73 const unsigned char crypto_dh5_generator[] = { 0x2 };
     74 size_t crypto_dh5_generator_size = sizeof(crypto_dh5_generator);
     75 
     76 static bool hmac_common(enum l_checksum_type type,
     77 			const void *key, size_t key_len,
     78 			const void *data, size_t data_len,
     79 			void *output, size_t size)
     80 {
     81 	struct l_checksum *hmac;
     82 
     83 	hmac = l_checksum_new_hmac(type, key, key_len);
     84 	if (!hmac)
     85 		return false;
     86 
     87 	l_checksum_update(hmac, data, data_len);
     88 	l_checksum_get_digest(hmac, output, size);
     89 	l_checksum_free(hmac);
     90 
     91 	return true;
     92 }
     93 
     94 bool hmac_md5(const void *key, size_t key_len,
     95 		const void *data, size_t data_len, void *output, size_t size)
     96 {
     97 	return hmac_common(L_CHECKSUM_MD5, key, key_len, data, data_len,
     98 				output, size);
     99 }
    100 
    101 bool hmac_sha1(const void *key, size_t key_len,
    102 		const void *data, size_t data_len, void *output, size_t size)
    103 {
    104 	return hmac_common(L_CHECKSUM_SHA1, key, key_len, data, data_len,
    105 				output, size);
    106 }
    107 
    108 bool hmac_sha256(const void *key, size_t key_len,
    109 		const void *data, size_t data_len, void *output, size_t size)
    110 {
    111 	return hmac_common(L_CHECKSUM_SHA256, key, key_len, data, data_len,
    112 				output, size);
    113 }
    114 
    115 bool hmac_sha384(const void *key, size_t key_len,
    116 		const void *data, size_t data_len, void *output, size_t size)
    117 {
    118 	return hmac_common(L_CHECKSUM_SHA384, key, key_len, data, data_len,
    119 				output, size);
    120 }
    121 
    122 bool cmac_aes(const void *key, size_t key_len,
    123 		const void *data, size_t data_len, void *output, size_t size)
    124 {
    125 	struct l_checksum *cmac_aes;
    126 
    127 	cmac_aes = l_checksum_new_cmac_aes(key, key_len);
    128 	if (!cmac_aes)
    129 		return false;
    130 
    131 	l_checksum_update(cmac_aes, data, data_len);
    132 	l_checksum_get_digest(cmac_aes, output, size);
    133 	l_checksum_free(cmac_aes);
    134 
    135 	return true;
    136 }
    137 
    138 /*
    139  * Implements AES Key-Unwrap from RFC 3394
    140  *
    141  * The key is specified using @kek.  @in contains the encrypted data and @len
    142  * contains its length.  @out will contain the decrypted data.  The result
    143  * will be (len - 8) bytes.
    144  *
    145  * Returns: true on success, false if an IV mismatch has occurred.
    146  *
    147  * NOTE: Buffers @in and @out can overlap
    148  */
    149 bool aes_unwrap(const uint8_t *kek, size_t kek_len, const uint8_t *in, size_t len,
    150 			uint8_t *out)
    151 {
    152 	uint64_t b[2];
    153 	uint64_t *r;
    154 	size_t n = (len - 8) >> 3;
    155 	int i, j;
    156 	struct l_cipher *cipher;
    157 	uint64_t t = n * 6;
    158 
    159 	cipher = l_cipher_new(L_CIPHER_AES, kek, kek_len);
    160 	if (!cipher)
    161 		return false;
    162 
    163 	/* Set up */
    164 	memcpy(b, in, 8);
    165 	memmove(out, in + 8, n * 8);
    166 
    167 	/* Unwrap */
    168 	for (j = 5; j >= 0; j--) {
    169 		r = (uint64_t *) out + n - 1;
    170 
    171 		for (i = n; i >= 1; i--, t--) {
    172 			b[0] ^= L_CPU_TO_BE64(t);
    173 			b[1] = L_GET_UNALIGNED(r);
    174 
    175 			if (!l_cipher_decrypt(cipher, b, b, 16)) {
    176 				b[0] = 0;
    177 				goto done;
    178 			}
    179 
    180 			L_PUT_UNALIGNED(b[1], r);
    181 			r -= 1;
    182 		}
    183 	}
    184 
    185 done:
    186 	l_cipher_free(cipher);
    187 	explicit_bzero(&b[1], 8);
    188 
    189 	/* Check IV */
    190 	if (b[0] != 0xa6a6a6a6a6a6a6a6)
    191 		return false;
    192 
    193 	return true;
    194 }
    195 
    196 /*
    197  * AES Key-wrap from RFC 3394 for 128-bit key
    198  *
    199  * The key is specified using @kek.  @in contains the plaintext data and @len
    200  * contains its length.  @out will contain the encrypted data.  The result
    201  * will be (len + 8) bytes.
    202  *
    203  * Returns: true on success, false if an IV mismatch has occurred.
    204  *
    205  * NOTE: Buffers @in and @out can overlap
    206  */
    207 bool aes_wrap(const uint8_t *kek, const uint8_t *in, size_t len, uint8_t *out)
    208 {
    209 	uint64_t b[2] = { 0xa6a6a6a6a6a6a6a6, 0 };
    210 	uint64_t *r = (uint64_t *) out + 1;
    211 	size_t n = len >> 3;
    212 	unsigned int i, j;
    213 	uint32_t t = 1;
    214 	struct l_cipher *cipher;
    215 
    216 	cipher = l_cipher_new(L_CIPHER_AES, kek, 16);
    217 	if (!cipher)
    218 		return false;
    219 
    220 	memmove(r, in, len);
    221 
    222 	for (j = 0; j < 6; j++) {
    223 		for (i = 0; i < n; i++, t++) {
    224 			b[1] = L_GET_UNALIGNED(r + i);
    225 			l_cipher_encrypt(cipher, b, b, 16);
    226 			L_PUT_UNALIGNED(b[1], r + i);
    227 			b[0] ^= L_CPU_TO_BE64(t);
    228 		}
    229 	}
    230 
    231 	L_PUT_UNALIGNED(b[0], r - 1);
    232 
    233 	l_cipher_free(cipher);
    234 
    235 	return true;
    236 }
    237 
    238 /*
    239  * RFC 5297 Section 2.3 - Doubling
    240  */
    241 static void dbl(uint8_t *val)
    242 {
    243 	int i;
    244 	int c = val[0] & (1 << 7);
    245 
    246 	/* shift all but last byte (since i + 1 would overflow) */
    247 	for (i = 0; i < 15; i++)
    248 		val[i] = (val[i] << 1) | (val[i + 1] >> 7);
    249 
    250 	val[15] <<= 1;
    251 
    252 	/*
    253 	 * "The condition under which the xor operation is performed is when the
    254 	 * bit being shifted off is one."
    255 	 */
    256 	if (c)
    257 		val[15] ^= 0x87;
    258 }
    259 
    260 static void xor(uint8_t *a, uint8_t *b, size_t len)
    261 {
    262 	size_t i;
    263 
    264 	for (i = 0; i < len; i++)
    265 		a[i] ^= b[i];
    266 }
    267 
    268 /*
    269  * RFC 5297 Section 2.4 - S2V
    270  */
    271 static bool s2v(struct l_checksum *cmac, struct iovec *iov, size_t iov_len,
    272 		uint8_t *v)
    273 {
    274 	uint8_t zero[16] = { 0 };
    275 	uint8_t d[16];
    276 	uint8_t tmp[16];
    277 	size_t i;
    278 
    279 	/* AES-CMAC(K, <zero>) */
    280 	if (!l_checksum_update(cmac, zero, sizeof(zero)))
    281 		return false;
    282 
    283 	l_checksum_get_digest(cmac, d, sizeof(d));
    284 
    285 	/* Last element is treated special */
    286 	for (i = 0; i < iov_len - 1; i++) {
    287 		/* D = dbl(D) */
    288 		dbl(d);
    289 
    290 		/* AES-CMAC(K, Si) */
    291 		if (!l_checksum_update(cmac, iov[i].iov_base, iov[i].iov_len))
    292 			return false;
    293 
    294 		l_checksum_get_digest(cmac, tmp, sizeof(tmp));
    295 		/* D = D xor AES-CMAC(K, Si) */
    296 		xor(d, tmp, sizeof(tmp));
    297 	}
    298 
    299 	if (iov[i].iov_len >= 16) {
    300 		if (!l_checksum_update(cmac, iov[i].iov_base,
    301 					iov[i].iov_len - 16))
    302 			return false;
    303 		/* xorend(d) */
    304 		xor(d, iov[i].iov_base + iov[i].iov_len - 16, 16);
    305 	} else {
    306 		dbl(d);
    307 		xor(d, iov[i].iov_base, iov[i].iov_len);
    308 		/*
    309 		 * pad(X) indicates padding of string X, len(X) < 128, out to
    310 		 * 128 bits by the concatenation of a single bit of 1 followed
    311 		 * by as many 0 bits as are necessary.
    312 		 */
    313 		d[iov[i].iov_len] ^= 0x80;
    314 	}
    315 
    316 	if (!l_checksum_update(cmac, d, 16))
    317 		return false;
    318 
    319 	l_checksum_get_digest(cmac, v, 16);
    320 
    321 	return true;
    322 }
    323 
    324 /*
    325  * RFC 5297 Section 2.6 - SIV Encrypt
    326  */
    327 bool aes_siv_encrypt(const void *key, size_t key_len, const void *in,
    328 			size_t in_len, struct iovec *ad, size_t num_ad,
    329 			void *out)
    330 {
    331 	struct l_checksum *cmac;
    332 	struct l_cipher *ctr;
    333 	struct iovec iov[num_ad + 1];
    334 	uint8_t v[16];
    335 
    336 	if (ad && num_ad)
    337 		memcpy(iov, ad, sizeof(struct iovec) * num_ad);
    338 
    339 	iov[num_ad].iov_base = (void *)in;
    340 	iov[num_ad].iov_len = in_len;
    341 	num_ad++;
    342 
    343 	/*
    344 	 * key is split into two equal halves... K1 is used for S2V and K2 is
    345 	 * used for CTR
    346 	 */
    347 	cmac = l_checksum_new_cmac_aes(key, key_len / 2);
    348 	if (!cmac)
    349 		return false;
    350 
    351 	if (!s2v(cmac, iov, num_ad, v)) {
    352 		l_checksum_free(cmac);
    353 		return false;
    354 	}
    355 
    356 	l_checksum_free(cmac);
    357 
    358 	memcpy(out, v, 16);
    359 
    360 	v[8] &= 0x7f;
    361 	v[12] &= 0x7f;
    362 
    363 	ctr = l_cipher_new(L_CIPHER_AES_CTR, key + (key_len / 2), key_len / 2);
    364 	if (!ctr)
    365 		return false;
    366 
    367 	if (!l_cipher_set_iv(ctr, v, 16))
    368 		goto free_ctr;
    369 
    370 	if (!l_cipher_encrypt(ctr, in, out + 16, in_len))
    371 		goto free_ctr;
    372 
    373 	l_cipher_free(ctr);
    374 
    375 	return true;
    376 
    377 free_ctr:
    378 	l_cipher_free(ctr);
    379 	return false;
    380 }
    381 
    382 bool aes_siv_decrypt(const void *key, size_t key_len, const void *in,
    383 			size_t in_len, struct iovec *ad, size_t num_ad,
    384 			void *out)
    385 {
    386 	struct l_checksum *cmac;
    387 	struct l_cipher *ctr;
    388 	struct iovec iov[num_ad + 1];
    389 	uint8_t iv[16];
    390 	uint8_t v[16];
    391 
    392 	if (in_len < 16)
    393 		return false;
    394 
    395 	if (ad && num_ad)
    396 		memcpy(iov, ad, sizeof(struct iovec) * num_ad);
    397 
    398 	iov[num_ad].iov_base = (void *)out;
    399 	iov[num_ad].iov_len = in_len - 16;
    400 	num_ad++;
    401 
    402 	if (in_len == 16)
    403 		goto check_cmac;
    404 
    405 	memcpy(iv, in, 16);
    406 
    407 	iv[8] &= 0x7f;
    408 	iv[12] &= 0x7f;
    409 
    410 	ctr = l_cipher_new(L_CIPHER_AES_CTR, key + (key_len / 2), key_len / 2);
    411 	if (!ctr)
    412 		return false;
    413 
    414 	if (!l_cipher_set_iv(ctr, iv, 16))
    415 		goto free_ctr;
    416 
    417 	if (!l_cipher_decrypt(ctr, in + 16, out, in_len - 16))
    418 		goto free_ctr;
    419 
    420 	l_cipher_free(ctr);
    421 
    422 check_cmac:
    423 	cmac = l_checksum_new_cmac_aes(key, key_len / 2);
    424 	if (!cmac)
    425 		return false;
    426 
    427 	if (!s2v(cmac, iov, num_ad, v)) {
    428 		l_checksum_free(cmac);
    429 		return false;
    430 	}
    431 
    432 	l_checksum_free(cmac);
    433 
    434 	if (memcmp(v, in, 16))
    435 		return false;
    436 
    437 	return true;
    438 
    439 free_ctr:
    440 	l_cipher_free(ctr);
    441 	return false;
    442 }
    443 
    444 static void arc4_set_key(struct arc4_ctx *ctx, unsigned int length,
    445 			 const uint8_t *key)
    446 {
    447 	unsigned int i, j, k;
    448 
    449 	/* Initialize context */
    450 	for (i = 0; i < 256; i++)
    451 		ctx->S[i] = i;
    452 
    453 	for (i = j = k = 0; i < 256; i++) {
    454 		j += ctx->S[i] + key[k]; j &= 0xff;
    455 		SWAP(ctx->S[i], ctx->S[j]);
    456 		/* Repeat key as needed */
    457 		k = (k + 1) % length;
    458 	}
    459 	ctx->i = ctx->j = 0;
    460 }
    461 
    462 static void arc4_crypt(struct arc4_ctx *ctx, unsigned int length,
    463 			uint8_t *dst, const uint8_t *src)
    464 {
    465 	uint8_t i, j;
    466 
    467 	i = ctx->i; j = ctx->j;
    468 	while (length--) {
    469 		i++; i &= 0xff;
    470 		j += ctx->S[i]; j &= 0xff;
    471 		SWAP(ctx->S[i], ctx->S[j]);
    472 		if (!dst || !src)
    473 			continue;
    474 		*dst++ = *src++ ^ ctx->S[(ctx->S[i] + ctx->S[j]) & 0xff];
    475 	}
    476 	ctx->i = i; ctx->j = j;
    477 }
    478 
    479 bool arc4_skip(const uint8_t *key, size_t key_len, size_t skip,
    480 		const uint8_t *in, size_t len, uint8_t *out)
    481 {
    482 	struct arc4_ctx cipher;
    483 
    484 	arc4_set_key(&cipher, key_len, key);
    485 	arc4_crypt(&cipher, skip, NULL, NULL);
    486 	arc4_crypt(&cipher, len, out, in);
    487 	explicit_bzero(&cipher, sizeof(cipher));
    488 
    489 	return true;
    490 }
    491 
    492 /* 802.11, Section 11.6.2, Table 11-4 */
    493 int crypto_cipher_key_len(enum crypto_cipher cipher)
    494 {
    495 	switch (cipher) {
    496 	case CRYPTO_CIPHER_WEP40:
    497 		return 5;
    498 	case CRYPTO_CIPHER_WEP104:
    499 		return 13;
    500 	case CRYPTO_CIPHER_TKIP:
    501 		return 32;
    502 	case CRYPTO_CIPHER_CCMP:
    503 	case CRYPTO_CIPHER_GCMP:
    504 		return 16;
    505 	case CRYPTO_CIPHER_CCMP_256:
    506 	case CRYPTO_CIPHER_GCMP_256:
    507 		return 32;
    508 	case CRYPTO_CIPHER_BIP_CMAC:
    509 	case CRYPTO_CIPHER_BIP_GMAC:
    510 		return 16;
    511 	case CRYPTO_CIPHER_BIP_CMAC_256:
    512 	case CRYPTO_CIPHER_BIP_GMAC_256:
    513 		return 32;
    514 	}
    515 
    516 	return 0;
    517 }
    518 
    519 int crypto_cipher_tk_bits(enum crypto_cipher cipher)
    520 {
    521 	return crypto_cipher_key_len(cipher) * 8;
    522 }
    523 
    524 bool crypto_passphrase_is_valid(const char *passphrase)
    525 {
    526 	size_t passphrase_len;
    527 	size_t i;
    528 
    529 	/*
    530 	 * IEEE 802.11, Annex M, Section M.4.1:
    531 	 * "A pass-phrase is a sequence of between 8 and 63 ASCII-encoded
    532 	 * characters. The limit of 63 comes from the desire to distinguish
    533 	 * between a pass-phrase and a PSK displayed as 64 hexadecimal
    534 	 * characters."
    535 	 */
    536 	passphrase_len = strlen(passphrase);
    537 	if (passphrase_len < 8 || passphrase_len > 63)
    538 		return false;
    539 
    540 	/* IEEE 802.11, Annex M, Section M.4.1:
    541 	 * "Each character in the pass-phrase must have an encoding in the
    542 	 * range of 32 to 126 (decimal), inclusive."
    543 	 *
    544 	 * This corresponds to printable characters only
    545 	 */
    546 	for (i = 0; i < passphrase_len; i++) {
    547 		if (l_ascii_isprint(passphrase[i]))
    548 			continue;
    549 
    550 		return false;
    551 	}
    552 
    553 	return true;
    554 }
    555 
    556 int crypto_psk_from_passphrase(const char *passphrase,
    557 				const unsigned char *ssid, size_t ssid_len,
    558 				unsigned char *out_psk)
    559 {
    560 	bool result;
    561 	unsigned char psk[32];
    562 
    563 	if (!passphrase)
    564 		return -EINVAL;
    565 
    566 	if (!ssid)
    567 		return -EINVAL;
    568 
    569 	if (!crypto_passphrase_is_valid(passphrase))
    570 		return -ERANGE;
    571 
    572 	if (ssid_len == 0 || ssid_len > SSID_MAX_SIZE)
    573 		return -ERANGE;
    574 
    575 	result = l_cert_pkcs5_pbkdf2(L_CHECKSUM_SHA1, passphrase,
    576 					ssid, ssid_len, 4096,
    577 					psk, sizeof(psk));
    578 	if (!result)
    579 		return -ENOKEY;
    580 
    581 	if (out_psk)
    582 		memcpy(out_psk, psk, sizeof(psk));
    583 
    584 	explicit_bzero(psk, sizeof(psk));
    585 	return 0;
    586 }
    587 
    588 bool prf_sha1(const void *key, size_t key_len,
    589 		const void *prefix, size_t prefix_len,
    590 		const void *data, size_t data_len, void *output, size_t size)
    591 {
    592 	struct l_checksum *hmac;
    593 	unsigned int i, offset = 0;
    594 	unsigned char empty = '\0';
    595 	unsigned char counter;
    596 	struct iovec iov[4] = {
    597 		[0] = { .iov_base = (void *) prefix, .iov_len = prefix_len },
    598 		[1] = { .iov_base = &empty, .iov_len = 1 },
    599 		[2] = { .iov_base = (void *) data, .iov_len = data_len },
    600 		[3] = { .iov_base = &counter, .iov_len = 1 },
    601 	};
    602 
    603 	hmac = l_checksum_new_hmac(L_CHECKSUM_SHA1, key, key_len);
    604 	if (!hmac)
    605 		return false;
    606 
    607 	/* PRF processes in 160-bit chunks (20 bytes) */
    608 	for (i = 0, counter = 0; i < (size + 19) / 20; i++, counter++) {
    609 		size_t len;
    610 
    611 		if (size - offset > 20)
    612 			len = 20;
    613 		else
    614 			len = size - offset;
    615 
    616 		l_checksum_updatev(hmac, iov, 4);
    617 		l_checksum_get_digest(hmac, output + offset, len);
    618 
    619 		offset += len;
    620 	}
    621 
    622 	l_checksum_free(hmac);
    623 
    624 	return true;
    625 }
    626 
    627 /* PRF+ from RFC 5295 Section 3.1.2 (also RFC 4306 Section 2.13) */
    628 bool prf_plus(enum l_checksum_type type, const void *key, size_t key_len,
    629 		void *out, size_t out_len,
    630 		size_t n_extra, ...)
    631 {
    632 	struct iovec iov[n_extra + 2];
    633 	uint8_t *t = out;
    634 	size_t t_len = 0;
    635 	uint8_t count = 1;
    636 	uint8_t *out_ptr = out;
    637 	va_list va;
    638 	struct l_checksum *hmac;
    639 	ssize_t ret;
    640 	size_t i;
    641 
    642 	va_start(va, n_extra);
    643 
    644 	for (i = 0; i < n_extra; i++) {
    645 		iov[i + 1].iov_base = va_arg(va, void *);
    646 		iov[i + 1].iov_len = va_arg(va, size_t);
    647 	}
    648 
    649 	va_end(va);
    650 
    651 	iov[n_extra + 1].iov_base = &count;
    652 	iov[n_extra + 1].iov_len = 1;
    653 
    654 	hmac = l_checksum_new_hmac(type, key, key_len);
    655 	if (!hmac)
    656 		return false;
    657 
    658 	while (out_len > 0) {
    659 		iov[0].iov_base = t;
    660 		iov[0].iov_len = t_len;
    661 
    662 		if (!l_checksum_updatev(hmac, iov, n_extra + 2)) {
    663 			l_checksum_free(hmac);
    664 			return false;
    665 		}
    666 
    667 		ret = l_checksum_get_digest(hmac, out_ptr, out_len);
    668 		if (ret < 0) {
    669 			l_checksum_free(hmac);
    670 			return false;
    671 		}
    672 
    673 		/*
    674 		 * RFC specifies that T(0) = empty string, so after the first
    675 		 * iteration we update the length for T(1)...T(N)
    676 		 */
    677 		t_len = ret;
    678 		t = out_ptr;
    679 		count++;
    680 
    681 		out_len -= ret;
    682 		out_ptr += ret;
    683 
    684 		if (out_len)
    685 			l_checksum_reset(hmac);
    686 	}
    687 
    688 	l_checksum_free(hmac);
    689 
    690 	return true;
    691 }
    692 
    693 bool prf_plus_sha1(const void *key, size_t key_len,
    694 					const void *label, size_t label_len,
    695 					const void *seed, size_t seed_len,
    696 					void *output, size_t size)
    697 {
    698 	/*
    699 	 * PRF+ (K, S, LEN) = T1 | T2 | T3 | T4 | ... where:
    700 	 *
    701 	 * T1 = HMAC-SHA1 (K, S | LEN | 0x01 | 0x00 | 0x00)
    702 	 *
    703 	 * T2 = HMAC-SHA1 (K, T1 | S | LEN | 0x02 | 0x00 | 0x00)
    704 	 *
    705 	 * T3 = HMAC-SHA1 (K, T2 | S | LEN | 0x03 | 0x00 | 0x00)
    706 	 *
    707 	 * T4 = HMAC-SHA1 (K, T3 | S | LEN | 0x04 | 0x00 | 0x00)
    708 	 *
    709 	 * ...
    710 	 */
    711 
    712 	static const uint8_t SHA1_MAC_LEN = 20;
    713 	static const uint8_t nil_bytes[2] = { 0, 0 };
    714 	struct l_checksum *hmac;
    715 	uint8_t t[SHA1_MAC_LEN];
    716 	uint8_t counter;
    717 	struct iovec iov[5] = {
    718 		[0] = { .iov_base = (void *) t, .iov_len = 0 },
    719 		[1] = { .iov_base = (void *) label, .iov_len = label_len },
    720 		[2] = { .iov_base = (void *) seed, .iov_len = seed_len },
    721 		[3] = { .iov_base = &counter, .iov_len = 1 },
    722 		[4] = { .iov_base = (void *) nil_bytes, .iov_len = 2 },
    723 	};
    724 
    725 	hmac = l_checksum_new_hmac(L_CHECKSUM_SHA1, key, key_len);
    726 	if (!hmac)
    727 		return false;
    728 
    729 	/* PRF processes in 160-bit chunks (20 bytes) */
    730 	for (counter = 1;; counter++) {
    731 		size_t len;
    732 
    733 		if (size > SHA1_MAC_LEN)
    734 			len = SHA1_MAC_LEN;
    735 		else
    736 			len = size;
    737 
    738 		l_checksum_updatev(hmac, iov, 5);
    739 		l_checksum_get_digest(hmac, t, len);
    740 
    741 		memcpy(output, t, len);
    742 
    743 		size -= len;
    744 
    745 		if (!size)
    746 			break;
    747 
    748 		output += len;
    749 		iov[0].iov_len = len;
    750 	}
    751 
    752 	l_checksum_free(hmac);
    753 
    754 	return true;
    755 }
    756 
    757 /* Defined in 802.11-2012, Section 11.6.1.7.2 Key derivation function (KDF) */
    758 bool crypto_kdf(enum l_checksum_type type, const void *key, size_t key_len,
    759 		const void *prefix, size_t prefix_len,
    760 		const void *data, size_t data_len, void *output, size_t size)
    761 {
    762 	struct l_checksum *hmac;
    763 	unsigned int i, offset = 0;
    764 	unsigned int counter;
    765 	unsigned int chunk_size;
    766 	unsigned int n_iterations;
    767 	uint8_t counter_le[2];
    768 	uint8_t length_le[2];
    769 	struct iovec iov[4] = {
    770 		[0] = { .iov_base = counter_le, .iov_len = 2 },
    771 		[1] = { .iov_base = (void *) prefix, .iov_len = prefix_len },
    772 		[2] = { .iov_base = (void *) data, .iov_len = data_len },
    773 		[3] = { .iov_base = length_le, .iov_len = 2 },
    774 	};
    775 
    776 	hmac = l_checksum_new_hmac(type, key, key_len);
    777 	if (!hmac)
    778 		return false;
    779 
    780 	chunk_size = l_checksum_digest_length(type);
    781 	n_iterations = (size + chunk_size - 1) / chunk_size;
    782 
    783 	/* Length is denominated in bits, not bytes */
    784 	l_put_le16(size * 8, length_le);
    785 
    786 	for (i = 0, counter = 1; i < n_iterations; i++, counter++) {
    787 		size_t len;
    788 
    789 		if (size - offset > chunk_size)
    790 			len = chunk_size;
    791 		else
    792 			len = size - offset;
    793 
    794 		l_put_le16(counter, counter_le);
    795 
    796 		l_checksum_updatev(hmac, iov, 4);
    797 		l_checksum_get_digest(hmac, output + offset, len);
    798 
    799 		offset += len;
    800 	}
    801 
    802 	l_checksum_free(hmac);
    803 
    804 	return true;
    805 }
    806 
    807 bool kdf_sha256(const void *key, size_t key_len,
    808 		const void *prefix, size_t prefix_len,
    809 		const void *data, size_t data_len, void *output, size_t size)
    810 {
    811 	return crypto_kdf(L_CHECKSUM_SHA256, key, key_len, prefix, prefix_len,
    812 				data, data_len, output, size);
    813 }
    814 
    815 bool kdf_sha384(const void *key, size_t key_len,
    816 		const void *prefix, size_t prefix_len,
    817 		const void *data, size_t data_len, void *output, size_t size)
    818 {
    819 	return crypto_kdf(L_CHECKSUM_SHA384, key, key_len, prefix, prefix_len,
    820 				data, data_len, output, size);
    821 }
    822 
    823 /*
    824  * Defined in RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function
    825  *
    826  * Null key equates to a zero key (makes calls in EAP-PWD more convenient)
    827  */
    828 bool hkdf_extract(enum l_checksum_type type, const void *key,
    829 				size_t key_len, uint8_t num_args,
    830 				void *out, ...)
    831 {
    832 	struct l_checksum *hmac;
    833 	struct iovec iov[num_args];
    834 	const uint8_t zero_key[64] = { 0 };
    835 	size_t dlen = l_checksum_digest_length(type);
    836 	const uint8_t *k = key ? key : zero_key;
    837 	size_t k_len = key ? key_len : dlen;
    838 	va_list va;
    839 	int i;
    840 	int ret;
    841 
    842 	if (dlen <= 0)
    843 		return false;
    844 
    845 	hmac = l_checksum_new_hmac(type, k, k_len);
    846 	if (!hmac)
    847 		return false;
    848 
    849 	va_start(va, out);
    850 
    851 	for (i = 0; i < num_args; i++) {
    852 		iov[i].iov_base = va_arg(va, void *);
    853 		iov[i].iov_len = va_arg(va, size_t);
    854 	}
    855 
    856 	if (!l_checksum_updatev(hmac, iov, num_args)) {
    857 		l_checksum_free(hmac);
    858 		va_end(va);
    859 		return false;
    860 	}
    861 
    862 	ret = l_checksum_get_digest(hmac, out, dlen);
    863 	l_checksum_free(hmac);
    864 
    865 	va_end(va);
    866 	return (ret == (int) dlen);
    867 }
    868 
    869 bool hkdf_expand(enum l_checksum_type type, const void *key, size_t key_len,
    870 			const char *info, void *out, size_t out_len)
    871 {
    872 	return prf_plus(type, key, key_len, out, out_len, 1,
    873 			info, strlen(info));
    874 }
    875 
    876 /*
    877  * 802.11, Section 11.6.6.7:
    878  * PTK = PRF-X(PMK, "Pairwise key expansion", Min(AA, SA) || Max(AA, SA) ||
    879  *		Min(ANonce, SNonce) || Max(ANonce, SNonce))
    880  *
    881  * 802.11, Section 11.6.1.3:
    882  * The PTK shall be derived from the PMK by
    883  *  PTK = PRF-X(PMK, "Pairwise key expansion", Min(AA,SPA) || Max(AA,SPA) ||
    884  *		Min(ANonce,SNonce) || Max(ANonce,SNonce))
    885  * where X = 256 + TK_bits. The value of TK_bits is cipher-suite dependent and
    886  * is defined in Table 11-4. The Min and Max operations for IEEE 802 addresses
    887  * are with the address converted to a positive integer treating the first
    888  * transmitted octet as the most significant octet of the integer. The Min and
    889  * Max operations for nonces are with the nonces treated as positive integers
    890  * converted as specified in 8.2.2.
    891  */
    892 static bool crypto_derive_ptk(const uint8_t *pmk, size_t pmk_len,
    893 				const char *label,
    894 				const uint8_t *addr1, const uint8_t *addr2,
    895 				const uint8_t *nonce1, const uint8_t *nonce2,
    896 				uint8_t *out_ptk, size_t ptk_len,
    897 				enum l_checksum_type type)
    898 {
    899 	/* Nonce length is 32 */
    900 	uint8_t data[ETH_ALEN * 2 + 64];
    901 	size_t pos = 0;
    902 
    903 	/* Address 1 is less than Address 2 */
    904 	if (memcmp(addr1, addr2, ETH_ALEN) < 0) {
    905 		memcpy(data, addr1, ETH_ALEN);
    906 		memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
    907 	} else {
    908 		memcpy(data, addr2, ETH_ALEN);
    909 		memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
    910 	}
    911 
    912 	pos += ETH_ALEN * 2;
    913 
    914 	/* Nonce1 is less than Nonce2 */
    915 	if (memcmp(nonce1, nonce2, 32) < 0) {
    916 		memcpy(data + pos, nonce1, 32);
    917 		memcpy(data + pos + 32, nonce2, 32);
    918 	} else {
    919 		memcpy(data + pos, nonce2, 32);
    920 		memcpy(data + pos + 32, nonce1, 32);
    921 	}
    922 
    923 	pos += 64;
    924 
    925 	if (type == L_CHECKSUM_SHA1)
    926 		return prf_sha1(pmk, pmk_len, label, strlen(label),
    927 					data, sizeof(data), out_ptk, ptk_len);
    928 	else
    929 		return crypto_kdf(type, pmk, pmk_len, label, strlen(label),
    930 					data, sizeof(data), out_ptk, ptk_len);
    931 }
    932 
    933 bool crypto_derive_pairwise_ptk(const uint8_t *pmk, size_t pmk_len,
    934 				const uint8_t *addr1, const uint8_t *addr2,
    935 				const uint8_t *nonce1, const uint8_t *nonce2,
    936 				uint8_t *out_ptk, size_t ptk_len,
    937 				enum l_checksum_type type)
    938 {
    939 	return crypto_derive_ptk(pmk, pmk_len, "Pairwise key expansion",
    940 					addr1, addr2, nonce1, nonce2,
    941 					out_ptk, ptk_len,
    942 					type);
    943 }
    944 
    945 /* Defined in 802.11-2012, Section 11.6.1.7.3 PMK-R0 */
    946 bool crypto_derive_pmk_r0(const uint8_t *xxkey, size_t xxkey_len,
    947 				const uint8_t *ssid, size_t ssid_len,
    948 				uint16_t mdid,
    949 				const uint8_t *r0khid, size_t r0kh_len,
    950 				const uint8_t *s0khid, bool sha384,
    951 				uint8_t *out_pmk_r0, uint8_t *out_pmk_r0_name)
    952 {
    953 	uint8_t context[512];
    954 	size_t pos = 0;
    955 	uint8_t output[64];
    956 	size_t offset = sha384 ? 48 : 32;
    957 	struct l_checksum *sha;
    958 	bool r = false;
    959 	struct iovec iov[2] = {
    960 		[0] = { .iov_base = "FT-R0N", .iov_len = 6 },
    961 		[1] = { .iov_base = output + offset, .iov_len = 16 },
    962 	};
    963 
    964 	context[pos++] = ssid_len;
    965 
    966 	memcpy(context + pos, ssid, ssid_len);
    967 	pos += ssid_len;
    968 
    969 	l_put_le16(mdid, context + pos);
    970 	pos += 2;
    971 
    972 	context[pos++] = r0kh_len;
    973 
    974 	memcpy(context + pos, r0khid, r0kh_len);
    975 	pos += r0kh_len;
    976 
    977 	memcpy(context + pos, s0khid, ETH_ALEN);
    978 	pos += ETH_ALEN;
    979 
    980 	if (sha384) {
    981 		if (!kdf_sha384(xxkey, xxkey_len, "FT-R0", 5, context, pos,
    982 					output, 64))
    983 			goto exit;
    984 	} else {
    985 		if (!kdf_sha256(xxkey, xxkey_len, "FT-R0", 5, context, pos,
    986 					output, 48))
    987 			goto exit;
    988 	}
    989 
    990 	sha = l_checksum_new((sha384) ? L_CHECKSUM_SHA384 : L_CHECKSUM_SHA256);
    991 	if (!sha)
    992 		goto exit;
    993 
    994 	l_checksum_updatev(sha, iov, 2);
    995 	l_checksum_get_digest(sha, out_pmk_r0_name, 16);
    996 
    997 	l_checksum_free(sha);
    998 
    999 	memcpy(out_pmk_r0, output, offset);
   1000 
   1001 	r = true;
   1002 
   1003 exit:
   1004 	explicit_bzero(context, pos);
   1005 	explicit_bzero(output, 64);
   1006 
   1007 	return r;
   1008 }
   1009 
   1010 /* Defined in 802.11-2012, Section 11.6.1.7.4 PMK-R1 */
   1011 bool crypto_derive_pmk_r1(const uint8_t *pmk_r0,
   1012 				const uint8_t *r1khid, const uint8_t *s1khid,
   1013 				const uint8_t *pmk_r0_name, bool sha384,
   1014 				uint8_t *out_pmk_r1,
   1015 				uint8_t *out_pmk_r1_name)
   1016 {
   1017 	uint8_t context[2 * ETH_ALEN];
   1018 	struct l_checksum *sha;
   1019 	bool r = false;
   1020 	struct iovec iov[3] = {
   1021 		[0] = { .iov_base = "FT-R1N", .iov_len = 6 },
   1022 		[1] = { .iov_base = (uint8_t *) pmk_r0_name, .iov_len = 16 },
   1023 		[2] = { .iov_base = context, .iov_len = sizeof(context) },
   1024 	};
   1025 
   1026 	memcpy(context, r1khid, ETH_ALEN);
   1027 
   1028 	memcpy(context + ETH_ALEN, s1khid, ETH_ALEN);
   1029 
   1030 	if (sha384) {
   1031 		if (!kdf_sha384(pmk_r0, 48, "FT-R1", 5, context,
   1032 					sizeof(context), out_pmk_r1, 48))
   1033 			goto exit;
   1034 	} else {
   1035 		if (!kdf_sha256(pmk_r0, 32, "FT-R1", 5, context,
   1036 					sizeof(context), out_pmk_r1, 32))
   1037 			goto exit;
   1038 	}
   1039 
   1040 	sha = l_checksum_new((sha384) ? L_CHECKSUM_SHA384 : L_CHECKSUM_SHA256);
   1041 	if (!sha) {
   1042 		explicit_bzero(out_pmk_r1, 48);
   1043 		goto exit;
   1044 	}
   1045 
   1046 	l_checksum_updatev(sha, iov, 3);
   1047 	l_checksum_get_digest(sha, out_pmk_r1_name, 16);
   1048 
   1049 	l_checksum_free(sha);
   1050 
   1051 	r = true;
   1052 
   1053 exit:
   1054 	explicit_bzero(context, sizeof(context));
   1055 
   1056 	return r;
   1057 }
   1058 
   1059 /* Defined in 802.11-2012, Section 11.6.1.7.5 PTK */
   1060 bool crypto_derive_ft_ptk(const uint8_t *pmk_r1, const uint8_t *pmk_r1_name,
   1061 				const uint8_t *addr1, const uint8_t *addr2,
   1062 				const uint8_t *nonce1, const uint8_t *nonce2,
   1063 				bool sha384, uint8_t *out_ptk, size_t ptk_len,
   1064 				uint8_t *out_ptk_name)
   1065 {
   1066 	uint8_t context[ETH_ALEN * 2 + 64];
   1067 	struct l_checksum *sha;
   1068 	bool r = false;
   1069 	struct iovec iov[3] = {
   1070 		[0] = { .iov_base = (uint8_t *) pmk_r1_name, .iov_len = 16 },
   1071 		[1] = { .iov_base = "FT-PTKN", .iov_len = 7 },
   1072 		[2] = { .iov_base = context, .iov_len = sizeof(context) },
   1073 	};
   1074 
   1075 	memcpy(context, nonce1, 32);
   1076 
   1077 	memcpy(context + 32, nonce2, 32);
   1078 
   1079 	memcpy(context + 64, addr1, ETH_ALEN);
   1080 
   1081 	memcpy(context + 64 + ETH_ALEN, addr2, ETH_ALEN);
   1082 
   1083 	if (sha384) {
   1084 		if (!kdf_sha384(pmk_r1, 48, "FT-PTK", 6, context,
   1085 					sizeof(context), out_ptk, ptk_len))
   1086 			goto exit;
   1087 	} else {
   1088 		if (!kdf_sha256(pmk_r1, 32, "FT-PTK", 6, context,
   1089 					sizeof(context), out_ptk, ptk_len))
   1090 			goto exit;
   1091 	}
   1092 
   1093 	sha = l_checksum_new((sha384) ? L_CHECKSUM_SHA384 : L_CHECKSUM_SHA256);
   1094 	if (!sha) {
   1095 		explicit_bzero(out_ptk, ptk_len);
   1096 		goto exit;
   1097 	}
   1098 
   1099 	l_checksum_updatev(sha, iov, 3);
   1100 	l_checksum_get_digest(sha, out_ptk_name, 16);
   1101 
   1102 	l_checksum_free(sha);
   1103 
   1104 	r = true;
   1105 
   1106 exit:
   1107 	explicit_bzero(context, sizeof(context));
   1108 
   1109 	return r;
   1110 }
   1111 
   1112 /* Defined in 802.11-2012, Section 11.6.1.3 Pairwise Key Hierarchy */
   1113 bool crypto_derive_pmkid(const uint8_t *pmk, size_t key_len,
   1114 				const uint8_t *addr1, const uint8_t *addr2,
   1115 				uint8_t *out_pmkid,
   1116 				enum l_checksum_type checksum)
   1117 {
   1118 	uint8_t data[20];
   1119 
   1120 	memcpy(data + 0, "PMK Name", 8);
   1121 	memcpy(data + 8, addr2, 6);
   1122 	memcpy(data + 14, addr1, 6);
   1123 
   1124 	return hmac_common(checksum, pmk, key_len, data, 20, out_pmkid, 16);
   1125 }
   1126 
   1127 enum l_checksum_type crypto_sae_hash_from_ecc_prime_len(enum crypto_sae type,
   1128 							size_t prime_len)
   1129 {
   1130 	/*
   1131 	 * If used with the looping technique described in 12.4.4.2.2 and
   1132 	 * 12.4.4.3.2, H and CN are instantiated with SHA-256.
   1133 	 */
   1134 	if (type == CRYPTO_SAE_LOOPING)
   1135 		return L_CHECKSUM_SHA256;
   1136 
   1137 	/* 802.11-2020, Table 12-1 Hash algorithm based on length of prime */
   1138 	if (prime_len <= 256 / 8)
   1139 		return L_CHECKSUM_SHA256;
   1140 
   1141 	if (prime_len <= 384 / 8)
   1142 		return L_CHECKSUM_SHA384;
   1143 
   1144 	return L_CHECKSUM_SHA512;
   1145 }
   1146 
   1147 struct l_ecc_point *crypto_derive_sae_pt_ecc(unsigned int group,
   1148 						const char *ssid,
   1149 						const char *password,
   1150 						const char *identifier)
   1151 {
   1152 	const struct l_ecc_curve *curve = l_ecc_curve_from_ike_group(group);
   1153 	enum l_checksum_type hash;
   1154 	size_t hash_len;
   1155 	uint8_t pwd_seed[64]; /* SHA512 is the biggest possible right now */
   1156 	uint8_t pwd_value[128];
   1157 	size_t pwd_value_len;
   1158 	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *u1 = NULL;
   1159 	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *u2 = NULL;
   1160 	_auto_(l_ecc_point_free) struct l_ecc_point *p1 = NULL;
   1161 	_auto_(l_ecc_point_free) struct l_ecc_point *p2 = NULL;
   1162 	_auto_(l_ecc_point_free) struct l_ecc_point *pt = NULL;
   1163 
   1164 	if (!curve)
   1165 		return NULL;
   1166 
   1167 	hash = crypto_sae_hash_from_ecc_prime_len(CRYPTO_SAE_HASH_TO_ELEMENT,
   1168 					l_ecc_curve_get_scalar_bytes(curve));
   1169 	hash_len = l_checksum_digest_length(hash);
   1170 
   1171 	/* pwd-seed = HKDF-Extract(ssid, password [|| identifier]) */
   1172 	hkdf_extract(hash, ssid, strlen(ssid), 2, pwd_seed,
   1173 			password, strlen(password),
   1174 			identifier, identifier ? strlen(identifier) : 0);
   1175 
   1176 	/* len = olen(p) + floor(olen(p)/2) */
   1177 	pwd_value_len = l_ecc_curve_get_scalar_bytes(curve);
   1178 	pwd_value_len += pwd_value_len / 2;
   1179 
   1180 	/*
   1181 	 * pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u1 P1", len)
   1182 	 */
   1183 	hkdf_expand(hash, pwd_seed, hash_len, "SAE Hash to Element u1 P1",
   1184 				pwd_value, pwd_value_len);
   1185 	u1 = l_ecc_scalar_new_modp(curve, pwd_value, pwd_value_len);
   1186 
   1187 	/*
   1188 	 * pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u2 P2", len)
   1189 	 */
   1190 	hkdf_expand(hash, pwd_seed, hash_len, "SAE Hash to Element u2 P2",
   1191 				pwd_value, pwd_value_len);
   1192 	u2 = l_ecc_scalar_new_modp(curve, pwd_value, pwd_value_len);
   1193 
   1194 	p1 = l_ecc_point_from_sswu(u1);
   1195 	p2 = l_ecc_point_from_sswu(u2);
   1196 
   1197 	pt = l_ecc_point_new(curve);
   1198 	l_ecc_point_add(pt, p1, p2);
   1199 
   1200 	return l_steal_ptr(pt);
   1201 }
   1202 
   1203 struct l_ecc_point *crypto_derive_sae_pwe_from_pt_ecc(const uint8_t *mac1,
   1204 						const uint8_t *mac2,
   1205 						const struct l_ecc_point *pt)
   1206 {
   1207 	const struct l_ecc_curve *curve = l_ecc_point_get_curve(pt);
   1208 	enum l_checksum_type hash;
   1209 	size_t hash_len;
   1210 	uint8_t sorted_macs[12];
   1211 	uint8_t val_buf[64]; /* Max for SHA-512 */
   1212 	struct l_ecc_scalar *val;
   1213 	struct l_ecc_point *pwe;
   1214 
   1215 	if (!pt || !curve)
   1216 		return NULL;
   1217 
   1218 	hash = crypto_sae_hash_from_ecc_prime_len(CRYPTO_SAE_HASH_TO_ELEMENT,
   1219 					l_ecc_curve_get_scalar_bytes(curve));
   1220 	hash_len = l_checksum_digest_length(hash);
   1221 
   1222 	/*
   1223 	 * val = H(0n, MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC))
   1224 	 */
   1225 	if (memcmp(mac1, mac2, 6) > 0) {
   1226 		memcpy(sorted_macs, mac1, 6);
   1227 		memcpy(sorted_macs + 6, mac2, 6);
   1228 	} else {
   1229 		memcpy(sorted_macs, mac2, 6);
   1230 		memcpy(sorted_macs + 6, mac1, 6);
   1231 	}
   1232 
   1233 	hkdf_extract(hash, NULL, 0, 1, val_buf,
   1234 					sorted_macs, sizeof(sorted_macs));
   1235 	val = l_ecc_scalar_new_reduced_1_to_n(curve, val_buf, hash_len);
   1236 	pwe = l_ecc_point_new(curve);
   1237 	l_ecc_point_multiply(pwe, val, pt);
   1238 	l_ecc_scalar_free(val);
   1239 
   1240 	return pwe;
   1241 }