LCOV - code coverage report
Current view: top level - src/eap_common - eap_pwd_common.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 132 174 75.9 %
Date: 2015-09-27 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * EAP server/peer: EAP-pwd shared routines
       3             :  * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : #include "common.h"
      11             : #include "crypto/sha256.h"
      12             : #include "crypto/crypto.h"
      13             : #include "eap_defs.h"
      14             : #include "eap_pwd_common.h"
      15             : 
      16             : /* The random function H(x) = HMAC-SHA256(0^32, x) */
      17         149 : struct crypto_hash * eap_pwd_h_init(void)
      18             : {
      19             :         u8 allzero[SHA256_MAC_LEN];
      20         149 :         os_memset(allzero, 0, SHA256_MAC_LEN);
      21         149 :         return crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256, allzero,
      22             :                                 SHA256_MAC_LEN);
      23             : }
      24             : 
      25             : 
      26         803 : void eap_pwd_h_update(struct crypto_hash *hash, const u8 *data, size_t len)
      27             : {
      28         803 :         crypto_hash_update(hash, data, len);
      29         803 : }
      30             : 
      31             : 
      32         149 : void eap_pwd_h_final(struct crypto_hash *hash, u8 *digest)
      33             : {
      34         149 :         size_t len = SHA256_MAC_LEN;
      35         149 :         crypto_hash_finish(hash, digest, &len);
      36         149 : }
      37             : 
      38             : 
      39             : /* a counter-based KDF based on NIST SP800-108 */
      40          69 : static int eap_pwd_kdf(const u8 *key, size_t keylen, const u8 *label,
      41             :                        size_t labellen, u8 *result, size_t resultbitlen)
      42             : {
      43             :         struct crypto_hash *hash;
      44             :         u8 digest[SHA256_MAC_LEN];
      45             :         u16 i, ctr, L;
      46          69 :         size_t resultbytelen, len = 0, mdlen;
      47             : 
      48          69 :         resultbytelen = (resultbitlen + 7) / 8;
      49          69 :         ctr = 0;
      50          69 :         L = htons(resultbitlen);
      51         293 :         while (len < resultbytelen) {
      52         155 :                 ctr++;
      53         155 :                 i = htons(ctr);
      54         155 :                 hash = crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256,
      55             :                                         key, keylen);
      56         155 :                 if (hash == NULL)
      57           0 :                         return -1;
      58         155 :                 if (ctr > 1)
      59          86 :                         crypto_hash_update(hash, digest, SHA256_MAC_LEN);
      60         155 :                 crypto_hash_update(hash, (u8 *) &i, sizeof(u16));
      61         155 :                 crypto_hash_update(hash, label, labellen);
      62         155 :                 crypto_hash_update(hash, (u8 *) &L, sizeof(u16));
      63         155 :                 mdlen = SHA256_MAC_LEN;
      64         155 :                 if (crypto_hash_finish(hash, digest, &mdlen) < 0)
      65           0 :                         return -1;
      66         155 :                 if ((len + mdlen) > resultbytelen)
      67          12 :                         os_memcpy(result + len, digest, resultbytelen - len);
      68             :                 else
      69         143 :                         os_memcpy(result + len, digest, mdlen);
      70         155 :                 len += mdlen;
      71             :         }
      72             : 
      73             :         /* since we're expanding to a bit length, mask off the excess */
      74          69 :         if (resultbitlen % 8) {
      75           2 :                 u8 mask = 0xff;
      76           2 :                 mask <<= (8 - (resultbitlen % 8));
      77           2 :                 result[resultbytelen - 1] &= mask;
      78             :         }
      79             : 
      80          69 :         return 0;
      81             : }
      82             : 
      83             : 
      84             : /*
      85             :  * compute a "random" secret point on an elliptic curve based
      86             :  * on the password and identities.
      87             :  */
      88          29 : int compute_password_element(EAP_PWD_group *grp, u16 num,
      89             :                              const u8 *password, size_t password_len,
      90             :                              const u8 *id_server, size_t id_server_len,
      91             :                              const u8 *id_peer, size_t id_peer_len,
      92             :                              const u8 *token)
      93             : {
      94          29 :         BIGNUM *x_candidate = NULL, *rnd = NULL, *cofactor = NULL;
      95             :         struct crypto_hash *hash;
      96          29 :         unsigned char pwe_digest[SHA256_MAC_LEN], *prfbuf = NULL, ctr;
      97          29 :         int nid, is_odd, ret = 0;
      98             :         size_t primebytelen, primebitlen;
      99             : 
     100          29 :         switch (num) { /* from IANA registry for IKE D-H groups */
     101             :         case 19:
     102          20 :                 nid = NID_X9_62_prime256v1;
     103          20 :                 break;
     104             :         case 20:
     105           2 :                 nid = NID_secp384r1;
     106           2 :                 break;
     107             :         case 21:
     108           2 :                 nid = NID_secp521r1;
     109           2 :                 break;
     110             : #ifndef OPENSSL_IS_BORINGSSL
     111             :         case 25:
     112           2 :                 nid = NID_X9_62_prime192v1;
     113           2 :                 break;
     114             : #endif /* OPENSSL_IS_BORINGSSL */
     115             :         case 26:
     116           2 :                 nid = NID_secp224r1;
     117           2 :                 break;
     118             :         default:
     119           1 :                 wpa_printf(MSG_INFO, "EAP-pwd: unsupported group %d", num);
     120           1 :                 return -1;
     121             :         }
     122             : 
     123          28 :         grp->pwe = NULL;
     124          28 :         grp->order = NULL;
     125          28 :         grp->prime = NULL;
     126             : 
     127          28 :         if ((grp->group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
     128           0 :                 wpa_printf(MSG_INFO, "EAP-pwd: unable to create EC_GROUP");
     129           0 :                 goto fail;
     130             :         }
     131             : 
     132          28 :         if (((rnd = BN_new()) == NULL) ||
     133          28 :             ((cofactor = BN_new()) == NULL) ||
     134          56 :             ((grp->pwe = EC_POINT_new(grp->group)) == NULL) ||
     135          56 :             ((grp->order = BN_new()) == NULL) ||
     136          56 :             ((grp->prime = BN_new()) == NULL) ||
     137             :             ((x_candidate = BN_new()) == NULL)) {
     138           0 :                 wpa_printf(MSG_INFO, "EAP-pwd: unable to create bignums");
     139           0 :                 goto fail;
     140             :         }
     141             : 
     142          28 :         if (!EC_GROUP_get_curve_GFp(grp->group, grp->prime, NULL, NULL, NULL))
     143             :         {
     144           0 :                 wpa_printf(MSG_INFO, "EAP-pwd: unable to get prime for GFp "
     145             :                            "curve");
     146           0 :                 goto fail;
     147             :         }
     148          28 :         if (!EC_GROUP_get_order(grp->group, grp->order, NULL)) {
     149           0 :                 wpa_printf(MSG_INFO, "EAP-pwd: unable to get order for curve");
     150           0 :                 goto fail;
     151             :         }
     152          28 :         if (!EC_GROUP_get_cofactor(grp->group, cofactor, NULL)) {
     153           0 :                 wpa_printf(MSG_INFO, "EAP-pwd: unable to get cofactor for "
     154             :                            "curve");
     155           0 :                 goto fail;
     156             :         }
     157          28 :         primebitlen = BN_num_bits(grp->prime);
     158          28 :         primebytelen = BN_num_bytes(grp->prime);
     159          28 :         if ((prfbuf = os_malloc(primebytelen)) == NULL) {
     160           0 :                 wpa_printf(MSG_INFO, "EAP-pwd: unable to malloc space for prf "
     161             :                            "buffer");
     162           0 :                 goto fail;
     163             :         }
     164          28 :         os_memset(prfbuf, 0, primebytelen);
     165          28 :         ctr = 0;
     166             :         while (1) {
     167          43 :                 if (ctr > 30) {
     168           0 :                         wpa_printf(MSG_INFO, "EAP-pwd: unable to find random "
     169             :                                    "point on curve for group %d, something's "
     170             :                                    "fishy", num);
     171           0 :                         goto fail;
     172             :                 }
     173          43 :                 ctr++;
     174             : 
     175             :                 /*
     176             :                  * compute counter-mode password value and stretch to prime
     177             :                  *    pwd-seed = H(token | peer-id | server-id | password |
     178             :                  *                 counter)
     179             :                  */
     180          43 :                 hash = eap_pwd_h_init();
     181          43 :                 if (hash == NULL)
     182           0 :                         goto fail;
     183          43 :                 eap_pwd_h_update(hash, token, sizeof(u32));
     184          43 :                 eap_pwd_h_update(hash, id_peer, id_peer_len);
     185          43 :                 eap_pwd_h_update(hash, id_server, id_server_len);
     186          43 :                 eap_pwd_h_update(hash, password, password_len);
     187          43 :                 eap_pwd_h_update(hash, &ctr, sizeof(ctr));
     188          43 :                 eap_pwd_h_final(hash, pwe_digest);
     189             : 
     190          43 :                 BN_bin2bn(pwe_digest, SHA256_MAC_LEN, rnd);
     191             : 
     192          43 :                 if (eap_pwd_kdf(pwe_digest, SHA256_MAC_LEN,
     193             :                                 (u8 *) "EAP-pwd Hunting And Pecking",
     194             :                                 os_strlen("EAP-pwd Hunting And Pecking"),
     195             :                                 prfbuf, primebitlen) < 0)
     196           0 :                         goto fail;
     197             : 
     198          43 :                 BN_bin2bn(prfbuf, primebytelen, x_candidate);
     199             : 
     200             :                 /*
     201             :                  * eap_pwd_kdf() returns a string of bits 0..primebitlen but
     202             :                  * BN_bin2bn will treat that string of bits as a big endian
     203             :                  * number. If the primebitlen is not an even multiple of 8
     204             :                  * then excessive bits-- those _after_ primebitlen-- so now
     205             :                  * we have to shift right the amount we masked off.
     206             :                  */
     207          43 :                 if (primebitlen % 8)
     208           2 :                         BN_rshift(x_candidate, x_candidate,
     209             :                                   (8 - (primebitlen % 8)));
     210             : 
     211          43 :                 if (BN_ucmp(x_candidate, grp->prime) >= 0)
     212           0 :                         continue;
     213             : 
     214          43 :                 wpa_hexdump(MSG_DEBUG, "EAP-pwd: x_candidate",
     215             :                             prfbuf, primebytelen);
     216             : 
     217             :                 /*
     218             :                  * need to unambiguously identify the solution, if there is
     219             :                  * one...
     220             :                  */
     221          43 :                 if (BN_is_odd(rnd))
     222          26 :                         is_odd = 1;
     223             :                 else
     224          17 :                         is_odd = 0;
     225             : 
     226             :                 /*
     227             :                  * solve the quadratic equation, if it's not solvable then we
     228             :                  * don't have a point
     229             :                  */
     230          43 :                 if (!EC_POINT_set_compressed_coordinates_GFp(grp->group,
     231             :                                                              grp->pwe,
     232             :                                                              x_candidate,
     233             :                                                              is_odd, NULL))
     234          15 :                         continue;
     235             :                 /*
     236             :                  * If there's a solution to the equation then the point must be
     237             :                  * on the curve so why check again explicitly? OpenSSL code
     238             :                  * says this is required by X9.62. We're not X9.62 but it can't
     239             :                  * hurt just to be sure.
     240             :                  */
     241          28 :                 if (!EC_POINT_is_on_curve(grp->group, grp->pwe, NULL)) {
     242           0 :                         wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve");
     243           0 :                         continue;
     244             :                 }
     245             : 
     246          28 :                 if (BN_cmp(cofactor, BN_value_one())) {
     247             :                         /* make sure the point is not in a small sub-group */
     248           0 :                         if (!EC_POINT_mul(grp->group, grp->pwe, NULL, grp->pwe,
     249             :                                           cofactor, NULL)) {
     250           0 :                                 wpa_printf(MSG_INFO, "EAP-pwd: cannot "
     251             :                                            "multiply generator by order");
     252           0 :                                 continue;
     253             :                         }
     254           0 :                         if (EC_POINT_is_at_infinity(grp->group, grp->pwe)) {
     255           0 :                                 wpa_printf(MSG_INFO, "EAP-pwd: point is at "
     256             :                                            "infinity");
     257           0 :                                 continue;
     258             :                         }
     259             :                 }
     260             :                 /* if we got here then we have a new generator. */
     261          28 :                 break;
     262          15 :         }
     263          28 :         wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %d tries", ctr);
     264          28 :         grp->group_num = num;
     265             :         if (0) {
     266             :  fail:
     267           0 :                 EC_GROUP_free(grp->group);
     268           0 :                 grp->group = NULL;
     269           0 :                 EC_POINT_clear_free(grp->pwe);
     270           0 :                 grp->pwe = NULL;
     271           0 :                 BN_clear_free(grp->order);
     272           0 :                 grp->order = NULL;
     273           0 :                 BN_clear_free(grp->prime);
     274           0 :                 grp->prime = NULL;
     275           0 :                 ret = 1;
     276             :         }
     277             :         /* cleanliness and order.... */
     278          28 :         BN_clear_free(cofactor);
     279          28 :         BN_clear_free(x_candidate);
     280          28 :         BN_clear_free(rnd);
     281          28 :         os_free(prfbuf);
     282             : 
     283          28 :         return ret;
     284             : }
     285             : 
     286             : 
     287          26 : int compute_keys(EAP_PWD_group *grp, BN_CTX *bnctx, const BIGNUM *k,
     288             :                  const BIGNUM *peer_scalar, const BIGNUM *server_scalar,
     289             :                  const u8 *confirm_peer, const u8 *confirm_server,
     290             :                  const u32 *ciphersuite, u8 *msk, u8 *emsk, u8 *session_id)
     291             : {
     292             :         struct crypto_hash *hash;
     293             :         u8 mk[SHA256_MAC_LEN], *cruft;
     294             :         u8 msk_emsk[EAP_MSK_LEN + EAP_EMSK_LEN];
     295             :         int offset;
     296             : 
     297          26 :         if ((cruft = os_malloc(BN_num_bytes(grp->prime))) == NULL)
     298           0 :                 return -1;
     299             : 
     300             :         /*
     301             :          * first compute the session-id = TypeCode | H(ciphersuite | scal_p |
     302             :          *      scal_s)
     303             :          */
     304          26 :         session_id[0] = EAP_TYPE_PWD;
     305          26 :         hash = eap_pwd_h_init();
     306          26 :         if (hash == NULL) {
     307           0 :                 os_free(cruft);
     308           0 :                 return -1;
     309             :         }
     310          26 :         eap_pwd_h_update(hash, (const u8 *) ciphersuite, sizeof(u32));
     311          26 :         offset = BN_num_bytes(grp->order) - BN_num_bytes(peer_scalar);
     312          26 :         os_memset(cruft, 0, BN_num_bytes(grp->prime));
     313          26 :         BN_bn2bin(peer_scalar, cruft + offset);
     314          26 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(grp->order));
     315          26 :         offset = BN_num_bytes(grp->order) - BN_num_bytes(server_scalar);
     316          26 :         os_memset(cruft, 0, BN_num_bytes(grp->prime));
     317          26 :         BN_bn2bin(server_scalar, cruft + offset);
     318          26 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(grp->order));
     319          26 :         eap_pwd_h_final(hash, &session_id[1]);
     320             : 
     321             :         /* then compute MK = H(k | confirm-peer | confirm-server) */
     322          26 :         hash = eap_pwd_h_init();
     323          26 :         if (hash == NULL) {
     324           0 :                 os_free(cruft);
     325           0 :                 return -1;
     326             :         }
     327          26 :         offset = BN_num_bytes(grp->prime) - BN_num_bytes(k);
     328          26 :         os_memset(cruft, 0, BN_num_bytes(grp->prime));
     329          26 :         BN_bn2bin(k, cruft + offset);
     330          26 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(grp->prime));
     331          26 :         os_free(cruft);
     332          26 :         eap_pwd_h_update(hash, confirm_peer, SHA256_MAC_LEN);
     333          26 :         eap_pwd_h_update(hash, confirm_server, SHA256_MAC_LEN);
     334          26 :         eap_pwd_h_final(hash, mk);
     335             : 
     336             :         /* stretch the mk with the session-id to get MSK | EMSK */
     337          26 :         if (eap_pwd_kdf(mk, SHA256_MAC_LEN,
     338             :                         session_id, SHA256_MAC_LEN + 1,
     339             :                         msk_emsk, (EAP_MSK_LEN + EAP_EMSK_LEN) * 8) < 0) {
     340           0 :                 return -1;
     341             :         }
     342             : 
     343          26 :         os_memcpy(msk, msk_emsk, EAP_MSK_LEN);
     344          26 :         os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN);
     345             : 
     346          26 :         return 1;
     347             : }

Generated by: LCOV version 1.10