2 * EAP server/peer: EAP-pwd shared routines
3 * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the BSD license.
8 * Alternatively, this software may be distributed under the terms of the
9 * GNU General Public License version 2 as published by the Free Software
12 * See README and COPYING for more details.
18 #include "eap_pwd_common.h"
20 /* The random function H(x) = HMAC-SHA256(0^32, x) */
21 void H_Init(HMAC_CTX *ctx)
23 u8 allzero[SHA256_DIGEST_LENGTH];
25 os_memset(allzero, 0, SHA256_DIGEST_LENGTH);
26 HMAC_Init(ctx, allzero, SHA256_DIGEST_LENGTH, EVP_sha256());
30 void H_Update(HMAC_CTX *ctx, const u8 *data, int len)
32 HMAC_Update(ctx, data, len);
36 void H_Final(HMAC_CTX *ctx, u8 *digest)
38 unsigned int mdlen = SHA256_DIGEST_LENGTH;
40 HMAC_Final(ctx, digest, &mdlen);
41 HMAC_CTX_cleanup(ctx);
45 /* a counter-based KDF based on NIST SP800-108 */
46 void eap_pwd_kdf(u8 *key, int keylen, u8 *label, int labellen,
47 u8 *result, int resultbitlen)
50 unsigned char digest[SHA256_DIGEST_LENGTH];
52 int resultbytelen, len = 0;
53 unsigned int mdlen = SHA256_DIGEST_LENGTH;
54 unsigned char mask = 0xff;
56 resultbytelen = (resultbitlen + 7)/8;
58 L = htons(resultbitlen);
59 while (len < resultbytelen) {
60 ctr++; i = htons(ctr);
61 HMAC_Init(&hctx, key, keylen, EVP_sha256());
63 HMAC_Update(&hctx, digest, mdlen);
64 HMAC_Update(&hctx, (u8 *) &i, sizeof(u16));
65 HMAC_Update(&hctx, label, labellen);
66 HMAC_Update(&hctx, (u8 *) &L, sizeof(u16));
67 HMAC_Final(&hctx, digest, &mdlen);
68 if ((len + (int) mdlen) > resultbytelen)
69 os_memcpy(result + len, digest, resultbytelen - len);
71 os_memcpy(result + len, digest, mdlen);
73 HMAC_CTX_cleanup(&hctx);
76 /* since we're expanding to a bit length, mask off the excess */
77 if (resultbitlen % 8) {
78 mask >>= ((resultbytelen * 8) - resultbitlen);
85 * compute a "random" secret point on an elliptic curve based
86 * on the password and identities.
88 int compute_password_element(EAP_PWD_group *grp, u16 num,
89 u8 *password, int password_len,
90 u8 *id_server, int id_server_len,
91 u8 *id_peer, int id_peer_len, u8 *token)
93 BIGNUM *x_candidate = NULL, *rnd = NULL, *cofactor = NULL;
95 unsigned char pwe_digest[SHA256_DIGEST_LENGTH], *prfbuf = NULL, ctr;
96 int nid, is_odd, primebitlen, primebytelen, ret = 0;
98 switch (num) { /* from IANA registry for IKE D-H groups */
100 nid = NID_X9_62_prime256v1;
109 nid = NID_X9_62_prime192v1;
115 wpa_printf(MSG_INFO, "EAP-pwd: unsupported group %d", num);
123 if ((grp->group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
124 wpa_printf(MSG_INFO, "EAP-pwd: unable to create EC_GROUP");
128 if (((rnd = BN_new()) == NULL) ||
129 ((cofactor = BN_new()) == NULL) ||
130 ((grp->pwe = EC_POINT_new(grp->group)) == NULL) ||
131 ((grp->order = BN_new()) == NULL) ||
132 ((grp->prime = BN_new()) == NULL) ||
133 ((x_candidate = BN_new()) == NULL)) {
134 wpa_printf(MSG_INFO, "EAP-pwd: unable to create bignums");
138 if (!EC_GROUP_get_curve_GFp(grp->group, grp->prime, NULL, NULL, NULL))
140 wpa_printf(MSG_INFO, "EAP-pwd: unable to get prime for GFp "
144 if (!EC_GROUP_get_order(grp->group, grp->order, NULL)) {
145 wpa_printf(MSG_INFO, "EAP-pwd: unable to get order for curve");
148 if (!EC_GROUP_get_cofactor(grp->group, cofactor, NULL)) {
149 wpa_printf(MSG_INFO, "EAP-pwd: unable to get cofactor for "
153 primebitlen = BN_num_bits(grp->prime);
154 primebytelen = BN_num_bytes(grp->prime);
155 if ((prfbuf = os_malloc(primebytelen)) == NULL) {
156 wpa_printf(MSG_INFO, "EAP-pwd: unable to malloc space for prf "
160 os_memset(prfbuf, 0, primebytelen);
164 wpa_printf(MSG_INFO, "EAP-pwd: unable to find random "
165 "point on curve for group %d, something's "
172 * compute counter-mode password value and stretch to prime
173 * pwd-seed = H(token | peer-id | server-id | password |
177 H_Update(&ctx, token, sizeof(u32));
178 H_Update(&ctx, id_peer, id_peer_len);
179 H_Update(&ctx, id_server, id_server_len);
180 H_Update(&ctx, password, password_len);
181 H_Update(&ctx, &ctr, sizeof(ctr));
182 H_Final(&ctx, pwe_digest);
184 BN_bin2bn(pwe_digest, SHA256_DIGEST_LENGTH, rnd);
186 eap_pwd_kdf(pwe_digest, SHA256_DIGEST_LENGTH,
187 (unsigned char *) "EAP-pwd Hunting and Pecking",
188 os_strlen("EAP-pwd Hunting and Pecking"),
189 prfbuf, primebitlen);
191 BN_bin2bn(prfbuf, primebytelen, x_candidate);
192 if (BN_ucmp(x_candidate, grp->prime) >= 0)
195 wpa_hexdump(MSG_DEBUG, "EAP-pwd: x_candidate",
196 prfbuf, primebytelen);
199 * need to unambiguously identify the solution, if there is
208 * solve the quadratic equation, if it's not solvable then we
211 if (!EC_POINT_set_compressed_coordinates_GFp(grp->group,
217 * If there's a solution to the equation then the point must be
218 * on the curve so why check again explicitly? OpenSSL code
219 * says this is required by X9.62. We're not X9.62 but it can't
220 * hurt just to be sure.
222 if (!EC_POINT_is_on_curve(grp->group, grp->pwe, NULL)) {
223 wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve");
227 if (BN_cmp(cofactor, BN_value_one())) {
228 /* make sure the point is not in a small sub-group */
229 if (!EC_POINT_mul(grp->group, grp->pwe, NULL, grp->pwe,
231 wpa_printf(MSG_INFO, "EAP-pwd: cannot "
232 "multiply generator by order");
235 if (EC_POINT_is_at_infinity(grp->group, grp->pwe)) {
236 wpa_printf(MSG_INFO, "EAP-pwd: point is at "
241 /* if we got here then we have a new generator. */
244 wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %d tries", ctr);
245 grp->group_num = num;
248 EC_GROUP_free(grp->group);
249 EC_POINT_free(grp->pwe);
256 /* cleanliness and order.... */
258 BN_free(x_candidate);
266 int compute_keys(EAP_PWD_group *grp, BN_CTX *bnctx, BIGNUM *k,
267 EC_POINT *server_element, EC_POINT *peer_element,
268 BIGNUM *server_scalar, BIGNUM *peer_scalar, u32 *ciphersuite,
271 BIGNUM *scalar_sum, *x;
272 EC_POINT *element_sum;
274 u8 mk[SHA256_DIGEST_LENGTH], *cruft;
275 u8 session_id[SHA256_DIGEST_LENGTH + 1];
276 u8 msk_emsk[EAP_MSK_LEN + EAP_EMSK_LEN];
279 if (((cruft = os_malloc(BN_num_bytes(grp->prime))) == NULL) ||
280 ((x = BN_new()) == NULL) ||
281 ((scalar_sum = BN_new()) == NULL) ||
282 ((element_sum = EC_POINT_new(grp->group)) == NULL))
286 * first compute the session-id = TypeCode | H(ciphersuite | scal_p |
289 session_id[0] = EAP_TYPE_PWD;
291 H_Update(&ctx, (u8 *)ciphersuite, sizeof(u32));
292 BN_bn2bin(peer_scalar, cruft);
293 H_Update(&ctx, cruft, BN_num_bytes(grp->order));
294 BN_bn2bin(server_scalar, cruft);
295 H_Update(&ctx, cruft, BN_num_bytes(grp->order));
296 H_Final(&ctx, &session_id[1]);
299 * then compute MK = H(k | F(elem_p + elem_s) |
300 * (scal_p + scal_s) mod r)
305 os_memset(cruft, 0, BN_num_bytes(grp->prime));
307 H_Update(&ctx, cruft, BN_num_bytes(grp->prime));
309 /* x = F(elem_p + elem_s) */
310 if ((!EC_POINT_add(grp->group, element_sum, server_element,
311 peer_element, bnctx)) ||
312 (!EC_POINT_get_affine_coordinates_GFp(grp->group, element_sum, x,
316 os_memset(cruft, 0, BN_num_bytes(grp->prime));
318 H_Update(&ctx, cruft, BN_num_bytes(grp->prime));
320 /* (scal_p + scal_s) mod r */
321 BN_add(scalar_sum, server_scalar, peer_scalar);
322 BN_mod(scalar_sum, scalar_sum, grp->order, bnctx);
323 os_memset(cruft, 0, BN_num_bytes(grp->prime));
324 BN_bn2bin(scalar_sum, cruft);
325 H_Update(&ctx, cruft, BN_num_bytes(grp->order));
328 /* stretch the mk with the session-id to get MSK | EMSK */
329 eap_pwd_kdf(mk, SHA256_DIGEST_LENGTH,
330 session_id, SHA256_DIGEST_LENGTH+1,
331 msk_emsk, (EAP_MSK_LEN + EAP_EMSK_LEN) * 8);
333 os_memcpy(msk, msk_emsk, EAP_MSK_LEN);
334 os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN);
341 EC_POINT_free(element_sum);