2 * Wi-Fi Protected Setup - common functionality
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "dh_groups.h"
23 #include "wps_dev_attr.h"
26 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
27 const char *label, u8 *res, size_t res_len)
29 u8 i_buf[4], key_bits[4];
33 u8 hash[SHA256_MAC_LEN], *opos;
36 WPA_PUT_BE32(key_bits, res_len * 8);
39 len[0] = sizeof(i_buf);
40 addr[1] = label_prefix;
41 len[1] = label_prefix_len;
42 addr[2] = (const u8 *) label;
43 len[2] = os_strlen(label);
45 len[3] = sizeof(key_bits);
47 iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
51 for (i = 1; i <= iter; i++) {
52 WPA_PUT_BE32(i_buf, i);
53 hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
55 os_memcpy(opos, hash, SHA256_MAC_LEN);
56 opos += SHA256_MAC_LEN;
57 left -= SHA256_MAC_LEN;
59 os_memcpy(opos, hash, left);
64 int wps_derive_keys(struct wps_data *wps)
66 struct wpabuf *pubkey, *dh_shared;
67 u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
70 u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
72 if (wps->dh_privkey == NULL) {
73 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
77 pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
79 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
83 dh_shared = dh_derive_shared(pubkey, wps->dh_privkey,
84 dh_groups_get(WPS_DH_GROUP));
85 if (dh_shared == NULL) {
86 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
90 /* Own DH private key is not needed anymore */
91 wpabuf_free(wps->dh_privkey);
92 wps->dh_privkey = NULL;
94 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
96 /* DHKey = SHA-256(g^AB mod p) */
97 addr[0] = wpabuf_head(dh_shared);
98 len[0] = wpabuf_len(dh_shared);
99 sha256_vector(1, addr, len, dhkey);
100 wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
101 wpabuf_free(dh_shared);
103 /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
104 addr[0] = wps->nonce_e;
105 len[0] = WPS_NONCE_LEN;
106 addr[1] = wps->mac_addr_e;
108 addr[2] = wps->nonce_r;
109 len[2] = WPS_NONCE_LEN;
110 hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
111 wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
113 wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
115 os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
116 os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
117 os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
120 wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
121 wps->authkey, WPS_AUTHKEY_LEN);
122 wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
123 wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
124 wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
130 int wps_derive_mgmt_keys(struct wps_data *wps)
132 u8 nonces[2 * WPS_NONCE_LEN];
133 u8 keys[WPS_MGMTAUTHKEY_LEN + WPS_MGMTENCKEY_LEN];
134 u8 hash[SHA256_MAC_LEN];
137 const char *auth_label = "WFA-WLAN-Management-MgmtAuthKey";
138 const char *enc_label = "WFA-WLAN-Management-MgmtEncKey";
140 /* MgmtAuthKey || MgmtEncKey =
141 * kdf(EMSK, N1 || N2 || "WFA-WLAN-Management-Keys", 384) */
142 os_memcpy(nonces, wps->nonce_e, WPS_NONCE_LEN);
143 os_memcpy(nonces + WPS_NONCE_LEN, wps->nonce_r, WPS_NONCE_LEN);
144 wps_kdf(wps->emsk, nonces, sizeof(nonces), "WFA-WLAN-Management-Keys",
146 os_memcpy(wps->mgmt_auth_key, keys, WPS_MGMTAUTHKEY_LEN);
147 os_memcpy(wps->mgmt_enc_key, keys + WPS_MGMTAUTHKEY_LEN,
151 len[0] = sizeof(nonces);
153 /* MgmtEncKeyID = first 128 bits of
154 * SHA-256(N1 || N2 || "WFA-WLAN-Management-MgmtAuthKey") */
155 addr[1] = (const u8 *) auth_label;
156 len[1] = os_strlen(auth_label);
157 sha256_vector(2, addr, len, hash);
158 os_memcpy(wps->mgmt_auth_key_id, hash, WPS_MGMT_KEY_ID_LEN);
160 /* MgmtEncKeyID = first 128 bits of
161 * SHA-256(N1 || N2 || "WFA-WLAN-Management-MgmtEncKey") */
162 addr[1] = (const u8 *) enc_label;
163 len[1] = os_strlen(enc_label);
164 sha256_vector(2, addr, len, hash);
165 os_memcpy(wps->mgmt_enc_key_id, hash, WPS_MGMT_KEY_ID_LEN);
167 wpa_hexdump_key(MSG_DEBUG, "WPS: MgmtAuthKey",
168 wps->mgmt_auth_key, WPS_MGMTAUTHKEY_LEN);
169 wpa_hexdump(MSG_DEBUG, "WPS: MgmtAuthKeyID",
170 wps->mgmt_auth_key_id, WPS_MGMT_KEY_ID_LEN);
171 wpa_hexdump_key(MSG_DEBUG, "WPS: MgmtEncKey",
172 wps->mgmt_enc_key, WPS_MGMTENCKEY_LEN);
173 wpa_hexdump(MSG_DEBUG, "WPS: MgmtEncKeyID",
174 wps->mgmt_enc_key_id, WPS_MGMT_KEY_ID_LEN);
180 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
181 size_t dev_passwd_len)
183 u8 hash[SHA256_MAC_LEN];
185 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
186 (dev_passwd_len + 1) / 2, hash);
187 os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
188 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
189 dev_passwd + (dev_passwd_len + 1) / 2,
190 dev_passwd_len / 2, hash);
191 os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
193 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
194 dev_passwd, dev_passwd_len);
195 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
196 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
200 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
203 struct wpabuf *decrypted;
204 const size_t block_size = 16;
210 if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
212 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
216 decrypted = wpabuf_alloc(encr_len - block_size);
217 if (decrypted == NULL)
220 wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
221 wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
222 if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
223 wpabuf_len(decrypted))) {
224 wpabuf_free(decrypted);
228 wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
231 pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
233 if (pad > wpabuf_len(decrypted)) {
234 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
235 wpabuf_free(decrypted);
238 for (i = 0; i < pad; i++) {
240 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
242 wpabuf_free(decrypted);
246 decrypted->used -= pad;
253 * wps_pin_checksum - Compute PIN checksum
254 * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
255 * Returns: Checksum digit
257 unsigned int wps_pin_checksum(unsigned int pin)
259 unsigned int accum = 0;
261 accum += 3 * (pin % 10);
267 return (10 - accum % 10) % 10;
272 * wps_pin_valid - Check whether a PIN has a valid checksum
273 * @pin: Eight digit PIN (i.e., including the checksum digit)
274 * Returns: 1 if checksum digit is valid, or 0 if not
276 unsigned int wps_pin_valid(unsigned int pin)
278 return wps_pin_checksum(pin / 10) == (pin % 10);
283 * wps_generate_pin - Generate a random PIN
284 * Returns: Eight digit PIN (i.e., including the checksum digit)
286 unsigned int wps_generate_pin(void)
290 /* Generate seven random digits for the PIN */
291 if (os_get_random((unsigned char *) &val, sizeof(val)) < 0) {
294 val = os_random() ^ now.sec ^ now.usec;
298 /* Append checksum digit */
299 return val * 10 + wps_pin_checksum(val);
303 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg)
305 union wps_event_data data;
307 if (wps->event_cb == NULL)
310 os_memset(&data, 0, sizeof(data));
312 wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);