Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / wpa_supplicant / src / eap_common / eap_sim_common.h
1 /*
2  * EAP peer/server: EAP-SIM/AKA/AKA' shared routines
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #ifndef EAP_SIM_COMMON_H
16 #define EAP_SIM_COMMON_H
17
18 #define EAP_SIM_NONCE_S_LEN 16
19 #define EAP_SIM_NONCE_MT_LEN 16
20 #define EAP_SIM_MAC_LEN 16
21 #define EAP_SIM_MK_LEN 20
22 #define EAP_SIM_K_AUT_LEN 16
23 #define EAP_SIM_K_ENCR_LEN 16
24 #define EAP_SIM_KEYING_DATA_LEN 64
25 #define EAP_SIM_IV_LEN 16
26 #define EAP_SIM_KC_LEN 8
27 #define EAP_SIM_SRES_LEN 4
28
29 #define GSM_RAND_LEN 16
30
31 #define EAP_SIM_VERSION 1
32
33 /* EAP-SIM Subtypes */
34 #define EAP_SIM_SUBTYPE_START 10
35 #define EAP_SIM_SUBTYPE_CHALLENGE 11
36 #define EAP_SIM_SUBTYPE_NOTIFICATION 12
37 #define EAP_SIM_SUBTYPE_REAUTHENTICATION 13
38 #define EAP_SIM_SUBTYPE_CLIENT_ERROR 14
39
40 /* AT_CLIENT_ERROR_CODE error codes */
41 #define EAP_SIM_UNABLE_TO_PROCESS_PACKET 0
42 #define EAP_SIM_UNSUPPORTED_VERSION 1
43 #define EAP_SIM_INSUFFICIENT_NUM_OF_CHAL 2
44 #define EAP_SIM_RAND_NOT_FRESH 3
45
46 #define EAP_SIM_MAX_FAST_REAUTHS 1000
47
48 #define EAP_SIM_MAX_CHAL 3
49
50
51 /* EAP-AKA Subtypes */
52 #define EAP_AKA_SUBTYPE_CHALLENGE 1
53 #define EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT 2
54 #define EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE 4
55 #define EAP_AKA_SUBTYPE_IDENTITY 5
56 #define EAP_AKA_SUBTYPE_NOTIFICATION 12
57 #define EAP_AKA_SUBTYPE_REAUTHENTICATION 13
58 #define EAP_AKA_SUBTYPE_CLIENT_ERROR 14
59
60 /* AT_CLIENT_ERROR_CODE error codes */
61 #define EAP_AKA_UNABLE_TO_PROCESS_PACKET 0
62
63 #define EAP_AKA_RAND_LEN 16
64 #define EAP_AKA_AUTN_LEN 16
65 #define EAP_AKA_AUTS_LEN 14
66 #define EAP_AKA_RES_MAX_LEN 16
67 #define EAP_AKA_IK_LEN 16
68 #define EAP_AKA_CK_LEN 16
69 #define EAP_AKA_MAX_FAST_REAUTHS 1000
70 #define EAP_AKA_MIN_RES_LEN 4
71 #define EAP_AKA_MAX_RES_LEN 16
72 #define EAP_AKA_CHECKCODE_LEN 20
73
74 #define EAP_AKA_PRIME_K_AUT_LEN 32
75 #define EAP_AKA_PRIME_CHECKCODE_LEN 32
76 #define EAP_AKA_PRIME_K_RE_LEN 32
77
78 struct wpabuf;
79
80 void eap_sim_derive_mk(const u8 *identity, size_t identity_len,
81                        const u8 *nonce_mt, u16 selected_version,
82                        const u8 *ver_list, size_t ver_list_len,
83                        int num_chal, const u8 *kc, u8 *mk);
84 void eap_aka_derive_mk(const u8 *identity, size_t identity_len,
85                        const u8 *ik, const u8 *ck, u8 *mk);
86 int eap_sim_derive_keys(const u8 *mk, u8 *k_encr, u8 *k_aut, u8 *msk,
87                         u8 *emsk);
88 int eap_sim_derive_keys_reauth(u16 _counter,
89                                const u8 *identity, size_t identity_len,
90                                const u8 *nonce_s, const u8 *mk, u8 *msk,
91                                u8 *emsk);
92 int eap_sim_verify_mac(const u8 *k_aut, const struct wpabuf *req,
93                        const u8 *mac, const u8 *extra, size_t extra_len);
94 void eap_sim_add_mac(const u8 *k_aut, const u8 *msg, size_t msg_len, u8 *mac,
95                      const u8 *extra, size_t extra_len);
96
97 #ifdef EAP_AKA_PRIME
98 void eap_aka_prime_derive_keys(const u8 *identity, size_t identity_len,
99                                const u8 *ik, const u8 *ck, u8 *k_encr,
100                                u8 *k_aut, u8 *k_re, u8 *msk, u8 *emsk);
101 int eap_aka_prime_derive_keys_reauth(const u8 *k_re, u16 counter,
102                                      const u8 *identity, size_t identity_len,
103                                      const u8 *nonce_s, u8 *msk, u8 *emsk);
104 int eap_sim_verify_mac_sha256(const u8 *k_aut, const struct wpabuf *req,
105                               const u8 *mac, const u8 *extra,
106                               size_t extra_len);
107 void eap_sim_add_mac_sha256(const u8 *k_aut, const u8 *msg, size_t msg_len,
108                             u8 *mac, const u8 *extra, size_t extra_len);
109
110 void eap_aka_prime_derive_ck_ik_prime(u8 *ck, u8 *ik, const u8 *sqn_ak,
111                                       const u8 *network_name,
112                                       size_t network_name_len);
113 #else /* EAP_AKA_PRIME */
114 static inline void eap_aka_prime_derive_keys(const u8 *identity,
115                                              size_t identity_len,
116                                              const u8 *ik, const u8 *ck,
117                                              u8 *k_encr, u8 *k_aut, u8 *k_re,
118                                              u8 *msk, u8 *emsk)
119 {
120 }
121
122 static inline int eap_aka_prime_derive_keys_reauth(const u8 *k_re, u16 counter,
123                                                    const u8 *identity,
124                                                    size_t identity_len,
125                                                    const u8 *nonce_s, u8 *msk,
126                                                    u8 *emsk)
127 {
128         return -1;
129 }
130
131 static inline int eap_sim_verify_mac_sha256(const u8 *k_aut,
132                                             const struct wpabuf *req,
133                                             const u8 *mac, const u8 *extra,
134                                             size_t extra_len)
135 {
136         return -1;
137 }
138 #endif /* EAP_AKA_PRIME */
139
140
141 /* EAP-SIM/AKA Attributes (0..127 non-skippable) */
142 #define EAP_SIM_AT_RAND 1
143 #define EAP_SIM_AT_AUTN 2 /* only AKA */
144 #define EAP_SIM_AT_RES 3 /* only AKA, only peer->server */
145 #define EAP_SIM_AT_AUTS 4 /* only AKA, only peer->server */
146 #define EAP_SIM_AT_PADDING 6 /* only encrypted */
147 #define EAP_SIM_AT_NONCE_MT 7 /* only SIM, only send */
148 #define EAP_SIM_AT_PERMANENT_ID_REQ 10
149 #define EAP_SIM_AT_MAC 11
150 #define EAP_SIM_AT_NOTIFICATION 12
151 #define EAP_SIM_AT_ANY_ID_REQ 13
152 #define EAP_SIM_AT_IDENTITY 14 /* only send */
153 #define EAP_SIM_AT_VERSION_LIST 15 /* only SIM */
154 #define EAP_SIM_AT_SELECTED_VERSION 16 /* only SIM */
155 #define EAP_SIM_AT_FULLAUTH_ID_REQ 17
156 #define EAP_SIM_AT_COUNTER 19 /* only encrypted */
157 #define EAP_SIM_AT_COUNTER_TOO_SMALL 20 /* only encrypted */
158 #define EAP_SIM_AT_NONCE_S 21 /* only encrypted */
159 #define EAP_SIM_AT_CLIENT_ERROR_CODE 22 /* only send */
160 #define EAP_SIM_AT_KDF_INPUT 23 /* only AKA' */
161 #define EAP_SIM_AT_KDF 24 /* only AKA' */
162 #define EAP_SIM_AT_IV 129
163 #define EAP_SIM_AT_ENCR_DATA 130
164 #define EAP_SIM_AT_NEXT_PSEUDONYM 132 /* only encrypted */
165 #define EAP_SIM_AT_NEXT_REAUTH_ID 133 /* only encrypted */
166 #define EAP_SIM_AT_CHECKCODE 134 /* only AKA */
167 #define EAP_SIM_AT_RESULT_IND 135
168 #define EAP_SIM_AT_BIDDING 136
169
170 /* AT_NOTIFICATION notification code values */
171 #define EAP_SIM_GENERAL_FAILURE_AFTER_AUTH 0
172 #define EAP_SIM_TEMPORARILY_DENIED 1026
173 #define EAP_SIM_NOT_SUBSCRIBED 1031
174 #define EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH 16384
175 #define EAP_SIM_SUCCESS 32768
176
177 /* EAP-AKA' AT_KDF Key Derivation Function values */
178 #define EAP_AKA_PRIME_KDF 1
179
180 /* AT_BIDDING flags */
181 #define EAP_AKA_BIDDING_FLAG_D 0x8000
182
183
184 enum eap_sim_id_req {
185         NO_ID_REQ, ANY_ID, FULLAUTH_ID, PERMANENT_ID
186 };
187
188
189 struct eap_sim_attrs {
190         const u8 *rand, *autn, *mac, *iv, *encr_data, *version_list, *nonce_s;
191         const u8 *next_pseudonym, *next_reauth_id;
192         const u8 *nonce_mt, *identity, *res, *auts;
193         const u8 *checkcode;
194         const u8 *kdf_input;
195         const u8 *bidding;
196         size_t num_chal, version_list_len, encr_data_len;
197         size_t next_pseudonym_len, next_reauth_id_len, identity_len, res_len;
198         size_t res_len_bits;
199         size_t checkcode_len;
200         size_t kdf_input_len;
201         enum eap_sim_id_req id_req;
202         int notification, counter, selected_version, client_error_code;
203         int counter_too_small;
204         int result_ind;
205 #define EAP_AKA_PRIME_KDF_MAX 10
206         u16 kdf[EAP_AKA_PRIME_KDF_MAX];
207         size_t kdf_count;
208 };
209
210 int eap_sim_parse_attr(const u8 *start, const u8 *end,
211                        struct eap_sim_attrs *attr, int aka, int encr);
212 u8 * eap_sim_parse_encr(const u8 *k_encr, const u8 *encr_data,
213                         size_t encr_data_len, const u8 *iv,
214                         struct eap_sim_attrs *attr, int aka);
215
216
217 struct eap_sim_msg;
218
219 struct eap_sim_msg * eap_sim_msg_init(int code, int id, int type, int subtype);
220 struct wpabuf * eap_sim_msg_finish(struct eap_sim_msg *msg, const u8 *k_aut,
221                                    const u8 *extra, size_t extra_len);
222 void eap_sim_msg_free(struct eap_sim_msg *msg);
223 u8 * eap_sim_msg_add_full(struct eap_sim_msg *msg, u8 attr,
224                           const u8 *data, size_t len);
225 u8 * eap_sim_msg_add(struct eap_sim_msg *msg, u8 attr,
226                      u16 value, const u8 *data, size_t len);
227 u8 * eap_sim_msg_add_mac(struct eap_sim_msg *msg, u8 attr);
228 int eap_sim_msg_add_encr_start(struct eap_sim_msg *msg, u8 attr_iv,
229                                u8 attr_encr);
230 int eap_sim_msg_add_encr_end(struct eap_sim_msg *msg, u8 *k_encr,
231                              int attr_pad);
232
233 void eap_sim_report_notification(void *msg_ctx, int notification, int aka);
234
235 #endif /* EAP_SIM_COMMON_H */