simutil.h (8062B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2017-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 /* 24 * EAP-SIM/EAP-AKA shared values 25 */ 26 #define EAP_SIM_MK_LEN 20 27 #define EAP_SIM_K_ENCR_LEN 16 28 #define EAP_SIM_K_AUT_LEN 16 29 #define EAP_AKA_PRIME_K_AUT_LEN 32 30 #define EAP_SIM_MSK_LEN 64 31 #define EAP_SIM_EMSK_LEN 64 32 #define EAP_SIM_IV_LEN 16 33 #define EAP_SIM_MAC_LEN 16 34 #define EAP_SIM_RAND_LEN 16 35 #define EAP_AKA_K_RE_LEN 32 36 #define EAP_AKA_IK_LEN 16 37 #define EAP_AKA_CK_LEN 16 38 39 /* 40 * Possible pad types for EAP-SIM/EAP-AKA attributes 41 * 42 * NONE - No padding, data passed in immediately follows type/size 43 * ZERO - Zero pad where "real" length would be 44 * LENGTH - A "real" data length in bytes immediately follows type/size 45 * LENGTH_BITS - A "real" data length in bits follows type/size 46 */ 47 #define EAP_SIM_PAD_NONE 0 48 #define EAP_SIM_PAD_ZERO 1 49 #define EAP_SIM_PAD_LENGTH 2 50 #define EAP_SIM_PAD_LENGTH_BITS 3 51 52 /* 53 * Round up value to nearest word 54 */ 55 #define EAP_SIM_ROUND(x) ((x + 3) & ~0x3) 56 57 struct eap_sim_tlv_iter { 58 const uint8_t *pos; 59 const uint8_t *end; 60 uint8_t tag; 61 uint16_t len; 62 const uint8_t *data; 63 }; 64 65 /* 66 * RFC 4187, section 11 67 */ 68 enum eap_sim_at { 69 EAP_SIM_AT_RAND = 0x01, 70 EAP_SIM_AT_AUTN = 0x02, 71 EAP_SIM_AT_RES = 0x03, 72 EAP_SIM_AT_AUTS = 0x04, 73 EAP_SIM_AT_PADDING = 0x06, 74 EAP_SIM_AT_NONCE = 0x07, 75 EAP_SIM_AT_PERMANENT_ID_REQ = 0x0a, 76 EAP_SIM_AT_MAC = 0x0b, 77 EAP_SIM_AT_NOTIFICATION = 0x0c, 78 EAP_SIM_AT_ANY_ID_REQ = 0x0d, 79 EAP_SIM_AT_IDENTITY = 0x0e, 80 EAP_SIM_AT_VERSION_LIST = 0x0f, 81 EAP_SIM_AT_SELECTED_VERSION = 0x10, 82 EAP_SIM_AT_FULLAUTH_ID_REQ = 0x11, 83 EAP_SIM_AT_COUNTER = 0x13, 84 EAP_SIM_AT_NONCE_S = 0x15, 85 EAP_SIM_AT_CLIENT_ERROR_CODE = 0x16, 86 EAP_SIM_AT_KDF_INPUT = 0x17, 87 EAP_SIM_AT_KDF = 0x18, 88 EAP_SIM_AT_IV = 0x81, 89 EAP_SIM_AT_ENCR_DATA = 0x82, 90 EAP_SIM_AT_NEXT_PSEUDONYM = 0x84, 91 EAP_SIM_AT_NEXT_REAUTH_ID = 0x85, 92 EAP_SIM_AT_CHECKCODE = 0x86, 93 EAP_SIM_AT_RESULT_IND = 0x87, 94 EAP_SIM_AT_BIDDING = 0x88 95 }; 96 97 /* 98 * Possible client error's 99 */ 100 enum eap_sim_error { 101 EAP_SIM_ERROR_PROCESS = 0, 102 EAP_SIM_ERROR_VERSION_VERSION, 103 EAP_SIM_ERROR_CHALLENGE, 104 EAP_SIM_ERROR_RANDS 105 }; 106 107 /* 108 * Notification error codes (and success). 109 */ 110 enum eap_sim_fail { 111 EAP_SIM_FAIL_AFTER_AUTH = 0, 112 EAP_SIM_FAIL_DENIED_ACCESS = 1026, 113 EAP_SIM_FAIL_NOT_SUBSCRIBED = 1031, 114 EAP_SIM_FAIL_BEFORE_AUTH = 16384, 115 EAP_SIM_SUCCESS = 32768 116 }; 117 118 /* 119 * RFC 4186 Appendix B. FIPS 186 Pseudo-random number generator 120 * 121 * seed - PRF seed, e.g. the Master Key (MK) 122 * slen - seed length 123 * out - PRF output buffer 124 * olen - length of out 125 */ 126 void eap_sim_fips_prf(const void *seed, size_t slen, uint8_t *out, size_t olen); 127 128 /* 129 * 3GPP TS 35.206 130 * 131 * Algorithm for generating milenage parameters. 132 * 133 * opc - Input: OPc value 134 * k - Input: K key value 135 * rand - Input: Rand from server 136 * sqn - Input: Sequence number 137 * amf - Input: AMF value 138 * 139 * autn - Output: AUTN computed 140 * ck - Output: CK computed 141 * ik - Output: IK computed 142 * res - Output: RES computed 143 * 144 * Internal Functions used: 145 * f1 and f1* output MAC-A and MAC-S. MAC-A along with SQN/AK/AMF make AUTN 146 * f2 outputs RES where RES == OUT2[8-16] 147 * f3 outputs CK where CK == OUT3[0-16] 148 * f4 outputs IK where IK == OUT4[0-16] 149 * f5 outputs AK where AK == OUT2[0-6] 150 * 151 * f5* outputs AK', not used with EAP-AKA 152 */ 153 bool eap_aka_get_milenage(const uint8_t *opc, const uint8_t *k, 154 const uint8_t *rand, const uint8_t *sqn, const uint8_t *amf, 155 uint8_t *autn, uint8_t *ck, uint8_t *ik, uint8_t *res); 156 157 /* 158 * 3GPP TS 33.402 159 * Derivation of CK' and IK' from CK and IK 160 * 161 * FC = 0x20 162 * P0 = access network identity ('network') 163 * L0 = length of P0 164 * P1 = SQN ^ AK = first 6 bytes of AUTN 165 * L1 = length of P1 = 0x0006 166 * 167 * (CK' | IK') = HMAC_SHA256(FC |P0 | L0 | P1 | L1) 168 */ 169 bool eap_aka_derive_primes(const uint8_t *ck, const uint8_t *ik, 170 const uint8_t *autn, const uint8_t *network, uint16_t net_len, 171 uint8_t *ck_p, uint8_t *ik_p); 172 173 /* 174 * RFC 5448, Section 3.4.1 175 * 176 * PRF'(K,S) = T1 | T2 | T3 | T4 | ... 177 * 178 * where: 179 * T1 = HMAC-SHA-256 (K, S | 0x01) 180 * T2 = HMAC-SHA-256 (K, T1 | S | 0x02) 181 * T3 = HMAC-SHA-256 (K, T2 | S | 0x03) 182 * T4 = HMAC-SHA-256 (K, T3 | S | 0x04) 183 * ... 184 * 185 * RFC 5448, Section 3.3 186 * 187 * MK = PRF'(IK'|CK',"EAP-AKA'"|Identity) 188 * K_encr = MK[0..127] 189 * K_aut = MK[128..383] 190 * K_re = MK[384..639] 191 * MSK = MK[640..1151] 192 * EMSK = MK[1152..1663] 193 */ 194 bool eap_aka_prf_prime(const uint8_t *ik_p, const uint8_t *ck_p, 195 const char *identity, uint8_t *k_encr, uint8_t *k_aut, 196 uint8_t *k_re, uint8_t *msk, uint8_t *emsk); 197 198 /* 199 * Separate PRNG data into encryption keys. k_encr and k_aut may be NULL in the 200 * case of fast re-authentication. 201 * 202 * buf - output data from the PRNG, 160 bytes 203 * k_encr - first 16 bytes of buf 204 * k_aut - next 16 bytes of buf 205 * msk - next 64 bytes of buf 206 * emsk - next 64 bytes of buf 207 */ 208 bool eap_sim_get_encryption_keys(const uint8_t *buf, uint8_t *k_encr, 209 uint8_t *k_aut, uint8_t *msk, uint8_t *emsk); 210 211 /* 212 * Derive a packets MAC. This can be used to compute the packets MAC in place, 213 * by setting mac to the proper zero'ed location in buf. 214 * 215 * buf - the SIM packet, including MAC portion zero'ed, plus extra (e.g. SRES) 216 * len - the total length of buf 217 * key - encryption key to use (e.g. K_encr) 218 * mac - buffer for the 16 byte MAC 219 */ 220 bool eap_sim_derive_mac(enum eap_type type, const uint8_t *buf, size_t len, 221 const uint8_t *key, uint8_t *mac); 222 223 /* 224 * Helper to build the EAP packet header 225 * 226 * eap - eap_state, used to get the identifier 227 * method - EAP method (SIM or AKA) 228 * type - EAP-SIM subtype 229 * buf - EAP packet 230 * len - length of packet 231 */ 232 size_t eap_sim_build_header(struct eap_state *eap, enum eap_type method, 233 uint8_t type, uint8_t *buf, uint16_t len); 234 235 /* 236 * Signal that the client has detected an error 237 * 238 * eap - eap_state 239 * type - type of EAP method (SIM or AKA) 240 * code - error code to send 241 */ 242 void eap_sim_client_error(struct eap_state *eap, enum eap_type type, 243 uint16_t code); 244 245 /* 246 * Add an EAP-SIM attribute to a buffer. 247 * 248 * buf - pointer to start of EAP-SIM attribute 249 * attr - type of attribute 250 * ptype - Padding type AT_PAD_ZERO, AT_PAD_NONE or AT_PAD_LENGTH 251 * data - EAP-SIM attribute data, if NULL zeros will be written 252 * dlen - length of data pointer in bytes 253 * 254 * Returns the number of bytes written to buf. 255 */ 256 size_t eap_sim_add_attribute(uint8_t *buf, enum eap_sim_at attr, 257 uint8_t ptype, const uint8_t *data, uint16_t dlen); 258 259 /* 260 * Verify a packets MAC 261 * 262 * eap - eap_state pointer, used to rebuild the EAP header 263 * buf - should point to the start of the EAP-SIM packet 264 * len - length of EAP packet 265 * extra - Any extra block of data needed to compute the MAC 266 * elen - Length of 'extra' 267 */ 268 bool eap_sim_verify_mac(struct eap_state *eap, enum eap_type type, 269 const uint8_t *buf, uint16_t len, uint8_t *k_aut, 270 uint8_t *extra, size_t elen); 271 272 bool eap_sim_tlv_iter_init(struct eap_sim_tlv_iter *iter, const uint8_t *data, 273 uint32_t len); 274 275 bool eap_sim_tlv_iter_next(struct eap_sim_tlv_iter *iter); 276 277 uint8_t eap_sim_tlv_iter_get_type(struct eap_sim_tlv_iter *iter); 278 279 uint16_t eap_sim_tlv_iter_get_length(struct eap_sim_tlv_iter *iter); 280 281 const void *eap_sim_tlv_iter_get_data(struct eap_sim_tlv_iter *iter);