+static int eap_aka_verify_mac(struct eap_aka_data *data,
+ const struct wpabuf *req,
+ const u8 *mac, const u8 *extra,
+ size_t extra_len)
+{
+ if (data->eap_method == EAP_TYPE_AKA_PRIME)
+ return eap_sim_verify_mac_sha256(data->k_aut, req, mac, extra,
+ extra_len);
+ return eap_sim_verify_mac(data->k_aut, req, mac, extra, extra_len);
+}
+
+
+#ifdef EAP_AKA_PRIME
+static struct wpabuf * eap_aka_prime_kdf_select(struct eap_aka_data *data,
+ u8 id, u16 kdf)
+{
+ struct eap_sim_msg *msg;
+
+ data->kdf = kdf;
+ wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d) (KDF "
+ "select)", id);
+ msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
+ EAP_AKA_SUBTYPE_CHALLENGE);
+ wpa_printf(MSG_DEBUG, " AT_KDF");
+ eap_sim_msg_add(msg, EAP_SIM_AT_KDF, kdf, NULL, 0);
+ return eap_sim_msg_finish(msg, NULL, NULL, 0);
+}
+
+
+static struct wpabuf * eap_aka_prime_kdf_neg(struct eap_aka_data *data,
+ u8 id, struct eap_sim_attrs *attr)
+{
+ size_t i;
+
+ for (i = 0; i < attr->kdf_count; i++) {
+ if (attr->kdf[i] == EAP_AKA_PRIME_KDF)
+ return eap_aka_prime_kdf_select(data, id,
+ EAP_AKA_PRIME_KDF);
+ }
+
+ /* No matching KDF found - fail authentication as if AUTN had been
+ * incorrect */
+ return eap_aka_authentication_reject(data, id);
+}
+
+
+static int eap_aka_prime_kdf_valid(struct eap_aka_data *data,
+ struct eap_sim_attrs *attr)
+{
+ size_t i, j;
+
+ if (attr->kdf_count == 0)
+ return 0;
+
+ /* The only allowed (and required) duplication of a KDF is the addition
+ * of the selected KDF into the beginning of the list. */
+
+ if (data->kdf) {
+ if (attr->kdf[0] != data->kdf) {
+ wpa_printf(MSG_WARNING, "EAP-AKA': The server did not "
+ "accept the selected KDF");
+ return 0;
+ }
+
+ for (i = 1; i < attr->kdf_count; i++) {
+ if (attr->kdf[i] == data->kdf)
+ break;
+ }
+ if (i == attr->kdf_count &&
+ attr->kdf_count < EAP_AKA_PRIME_KDF_MAX) {
+ wpa_printf(MSG_WARNING, "EAP-AKA': The server did not "
+ "duplicate the selected KDF");
+ return 0;
+ }
+
+ /* TODO: should check that the list is identical to the one
+ * used in the previous Challenge message apart from the added
+ * entry in the beginning. */
+ }
+
+ for (i = data->kdf ? 1 : 0; i < attr->kdf_count; i++) {
+ for (j = i + 1; j < attr->kdf_count; j++) {
+ if (attr->kdf[i] == attr->kdf[j]) {
+ wpa_printf(MSG_WARNING, "EAP-AKA': The server "
+ "included a duplicated KDF");
+ return 0;
+ }
+ }
+ }
+
+ return 1;
+}
+#endif /* EAP_AKA_PRIME */
+
+