Wireless Daemon for Linux
 help / color / mirror / Atom feed
From: Tim Kourt <tim.a.kourt@linux.intel.com>
To: iwd@lists.01.org
Subject: [PATCH 5/6] crypto: Add support for PRF+ SHA1
Date: Thu, 05 Dec 2019 13:13:53 -0800	[thread overview]
Message-ID: <20191205211354.19075-5-tim.a.kourt@linux.intel.com> (raw)
In-Reply-To: <20191205211354.19075-1-tim.a.kourt@linux.intel.com>

[-- Attachment #1: Type: text/plain, Size: 2860 bytes --]

The PRF+ algorithm is based Internet Key Exchange (IKEv2) Protocol:
	https://www.ietf.org/rfc/rfc4306.txt
---
 src/crypto.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/crypto.h |  3 +++
 2 files changed, 67 insertions(+)

diff --git a/src/crypto.c b/src/crypto.c
index 67a042be..c1fe83a2 100644
--- a/src/crypto.c
+++ b/src/crypto.c
@@ -581,6 +581,70 @@ bool prf_sha1(const void *key, size_t key_len,
 	return true;
 }
 
+bool prf_plus_sha1(const void *key, size_t key_len,
+					const void *label, size_t label_len,
+					const void *seed, size_t seed_len,
+					void *output, size_t size)
+{
+	/*
+	 * PRF+ (K, S, LEN) = T1 | T2 | T3 | T4 | ... where:
+	 *
+	 * T1 = HMAC-SHA1 (K, S | LEN | 0x01 | 0x00 | 0x00)
+	 *
+	 * T2 = HMAC-SHA1 (K, T1 | S | LEN | 0x02 | 0x00 | 0x00)
+	 *
+	 * T3 = HMAC-SHA1 (K, T2 | S | LEN | 0x03 | 0x00 | 0x00)
+	 *
+	 * T4 = HMAC-SHA1 (K, T3 | S | LEN | 0x04 | 0x00 | 0x00)
+	 *
+	 * ...
+	 */
+
+	static const uint8_t SHA1_MAC_LEN = 20;
+	static const uint8_t nil_bytes[2] = { 0, 0 };
+	struct l_checksum *hmac;
+	uint8_t t[SHA1_MAC_LEN];
+	uint8_t counter;
+	struct iovec iov[5] = {
+		[0] = { .iov_base = (void *) t, .iov_len = 0 },
+		[1] = { .iov_base = (void *) label, .iov_len = label_len },
+		[2] = { .iov_base = (void *) seed, .iov_len = seed_len },
+		[3] = { .iov_base = &counter, .iov_len = 1 },
+		[4] = { .iov_base = (void *) nil_bytes, .iov_len = 2 },
+	};
+
+	hmac = l_checksum_new_hmac(L_CHECKSUM_SHA1, key, key_len);
+	if (!hmac)
+		return false;
+
+	/* PRF processes in 160-bit chunks (20 bytes) */
+	for (counter = 1;; counter++) {
+		size_t len;
+
+		if (size > SHA1_MAC_LEN)
+			len = SHA1_MAC_LEN;
+		else
+			len = size;
+
+		l_checksum_updatev(hmac, iov, 5);
+		l_checksum_get_digest(hmac, t, len);
+
+		memcpy(output, t, len);
+
+		size -= len;
+
+		if (!size)
+			break;
+
+		output += len;
+		iov[0].iov_len = len;
+	}
+
+	l_checksum_free(hmac);
+
+	return true;
+}
+
 /* Defined in 802.11-2012, Section 11.6.1.7.2 Key derivation function (KDF) */
 bool kdf_sha256(const void *key, size_t key_len,
 		const void *prefix, size_t prefix_len,
diff --git a/src/crypto.h b/src/crypto.h
index 23ca2450..89e2402c 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -106,6 +106,9 @@ bool kdf_sha384(const void *key, size_t key_len,
 bool prf_sha1(const void *key, size_t key_len,
 		const void *prefix, size_t prefix_len,
 		const void *data, size_t data_len, void *output, size_t size);
+bool prf_plus_sha1(const void *key, size_t key_len,
+		const void *prefix, size_t prefix_len,
+		const void *data, size_t data_len, void *output, size_t size);
 bool hkdf_extract(enum l_checksum_type type, const uint8_t *key, size_t key_len,
 				uint8_t num_args, uint8_t *out, ...);
 
-- 
2.13.6

  parent reply	other threads:[~2019-12-05 21:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-05 21:13 [PATCH 1/6] peap: Introduce PEAP state Tim Kourt
2019-12-05 21:13 ` [PATCH 2/6] peap: Delay key installation until success of Phase 2 Tim Kourt
2019-12-05 21:13 ` [PATCH 3/6] peap: Rename AVPs to TLVs Tim Kourt
2019-12-05 21:13 ` [PATCH 4/6] peap: Extend EAP Extensions to handle multiple TLVs Tim Kourt
2019-12-09  7:46   ` Denis Kenzior
2019-12-05 21:13 ` Tim Kourt [this message]
2019-12-09  7:47   ` [PATCH 5/6] crypto: Add support for PRF+ SHA1 Denis Kenzior
2019-12-05 21:13 ` [PATCH 6/6] peap: Add support for Crypto-Binding in PEAPv0 Tim Kourt
2019-12-09  8:00   ` Denis Kenzior
2019-12-09  7:40 ` [PATCH 1/6] peap: Introduce PEAP state Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191205211354.19075-5-tim.a.kourt@linux.intel.com \
    --to=tim.a.kourt@linux.intel.com \
    --cc=iwd@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox