1 /*
2  * Simultaneous authentication of equals
3  * Copyright (c) 2012-2013, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 #ifdef CONFIG_WPA3_SAE
9 
10 #ifndef SAE_H
11 #define SAE_H
12 
13 #include "esp_err.h"
14 #include "utils/includes.h"
15 #include "utils/common.h"
16 #include "utils/wpa_debug.h"
17 
18 #define SAE_KCK_LEN 32
19 #define SAE_PMK_LEN 32
20 #define SAE_PMK_LEN_MAX 64
21 #define SAE_PMKID_LEN 16
22 #define SAE_KEYSEED_KEY_LEN 32
23 #define SAE_MAX_PRIME_LEN 512
24 #define SAE_MAX_ECC_PRIME_LEN 66
25 #define SAE_MAX_HASH_LEN 64
26 #define SAE_COMMIT_MAX_LEN (2 + 3 * SAE_MAX_PRIME_LEN + 255)
27 #ifdef CONFIG_SAE_PK
28 #define SAE_CONFIRM_MAX_LEN ((2 + SAE_MAX_HASH_LEN) + 1500)
29 #else /* CONFIG_SAE_PK */
30 #define SAE_CONFIRM_MAX_LEN (2 + SAE_MAX_HASH_LEN)
31 #endif /* CONFIG_SAE_PK */
32 #define SAE_PK_M_LEN 16
33 
34 /* Special value returned by sae_parse_commit() */
35 #define SAE_SILENTLY_DISCARD 65535
36 
37 struct sae_pk {
38 	struct wpabuf *m;
39 	struct crypto_ec_key *key;
40 	int group;
41 	struct wpabuf *pubkey; /* DER encoded subjectPublicKey */
42 };
43 
44 struct sae_temporary_data {
45 	u8 kck[SAE_MAX_HASH_LEN];
46 	size_t kck_len;
47 	struct crypto_bignum *own_commit_scalar;
48 	struct crypto_bignum *own_commit_element_ffc;
49 	struct crypto_ec_point *own_commit_element_ecc;
50 	struct crypto_bignum *peer_commit_element_ffc;
51 	struct crypto_ec_point *peer_commit_element_ecc;
52 	struct crypto_ec_point *pwe_ecc;
53 	struct crypto_bignum *pwe_ffc;
54 	struct crypto_bignum *sae_rand;
55 	struct crypto_ec *ec;
56 	int prime_len;
57 	const struct dh_group *dh;
58 	const struct crypto_bignum *prime;
59 	const struct crypto_bignum *order;
60 	struct crypto_bignum *prime_buf;
61 	struct crypto_bignum *order_buf;
62 	struct wpabuf *anti_clogging_token;
63 	char *pw_id;
64 	int order_len;
65 	struct wpabuf *own_rejected_groups;
66 	struct wpabuf *peer_rejected_groups;
67 	unsigned int own_addr_higher:1;
68 
69 #ifdef CONFIG_SAE_PK
70 	u8 kek[SAE_MAX_HASH_LEN];
71 	size_t kek_len;
72 	const struct sae_pk *ap_pk;
73 	u8 own_addr[ETH_ALEN];
74 	u8 peer_addr[ETH_ALEN];
75 	u8 fingerprint[SAE_MAX_HASH_LEN];
76 	size_t fingerprint_bytes;
77 	size_t fingerprint_bits;
78 	size_t lambda;
79 	unsigned int sec;
80 	u8 ssid[32];
81 	size_t ssid_len;
82 #endif
83 };
84 
85 struct sae_pt {
86 	struct sae_pt *next;
87 	int group;
88 	struct crypto_ec *ec;
89 	struct crypto_ec_point *ecc_pt;
90 
91 	const struct dh_group *dh;
92 	struct crypto_bignum *ffc_pt;
93 #ifdef CONFIG_SAE_PK
94 	u8 ssid[32];
95 	size_t ssid_len;
96 #endif /* CONFIG_SAE_PK */
97 };
98 
99 enum {
100 	SAE_MSG_COMMIT = 1,
101 	SAE_MSG_CONFIRM = 2,
102 };
103 
104 enum sae_state {
105 	SAE_NOTHING, SAE_COMMITTED, SAE_CONFIRMED, SAE_ACCEPTED
106 };
107 
108 struct sae_data {
109 	enum sae_state state;
110 	u16 send_confirm;
111 	u8 pmk[SAE_PMK_LEN];
112 	size_t pmk_len;
113 	u8 pmkid[SAE_PMKID_LEN];
114 
115 	struct crypto_bignum *peer_commit_scalar;
116 	struct crypto_bignum *peer_commit_scalar_accepted;
117 	int group;
118 	unsigned int sync; /* protocol instance variable: Sync */
119 	u16 rc; /* protocol instance variable: Rc (received send-confirm) */
120 	struct sae_temporary_data *tmp;
121 	unsigned int h2e:1;
122 	unsigned int pk:1;
123 };
124 
125 int sae_set_group(struct sae_data *sae, int group);
126 void sae_clear_temp_data(struct sae_data *sae);
127 void sae_clear_data(struct sae_data *sae);
128 
129 int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
130 		       const u8 *password, size_t password_len,
131 		       struct sae_data *sae);
132 int sae_prepare_commit_pt(struct sae_data *sae, const struct sae_pt *pt,
133 			  const u8 *addr1, const u8 *addr2,
134 			  int *rejected_groups, const struct sae_pk *pk);
135 int sae_process_commit(struct sae_data *sae);
136 int sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
137 		     const struct wpabuf *token, const char *identifier);
138 u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
139 		     const u8 **token, size_t *token_len, int *allowed_groups,
140 		     int h2e);
141 int sae_write_confirm(struct sae_data *sae, struct wpabuf *buf);
142 int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len);
143 u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group);
144 const char * sae_state_txt(enum sae_state state);
145 size_t sae_ecc_prime_len_2_hash_len(size_t prime_len);
146 size_t sae_ffc_prime_len_2_hash_len(size_t prime_len);
147 struct sae_pt * sae_derive_pt(int *groups, const u8 *ssid, size_t ssid_len,
148 			      const u8 *password, size_t password_len,
149 			      const char *identifier);
150 struct crypto_ec_point *
151 sae_derive_pwe_from_pt_ecc(const struct sae_pt *pt,
152 			   const u8 *addr1, const u8 *addr2);
153 struct crypto_bignum *
154 sae_derive_pwe_from_pt_ffc(const struct sae_pt *pt,
155 			   const u8 *addr1, const u8 *addr2);
156 void sae_deinit_pt(struct sae_pt *pt);
157 
158 /* sae_pk.c */
159 #ifdef CONFIG_SAE_PK
160 bool sae_pk_valid_password(const char *pw);
161 #else /* CONFIG_SAE_PK */
sae_pk_valid_password(const char * pw)162 static inline bool sae_pk_valid_password(const char *pw)
163 {
164 	return false;
165 }
166 #endif /* CONFIG_SAE_PK */
167 char * sae_pk_base32_encode(const u8 *src, size_t len_bits);
168 u8 * sae_pk_base32_decode(const char *src, size_t len, size_t *out_len);
169 int sae_pk_set_password(struct sae_data *sae, const char *password);
170 void sae_deinit_pk(struct sae_pk *pk);
171 struct sae_pk * sae_parse_pk(const char *val);
172 int sae_write_confirm_pk(struct sae_data *sae, struct wpabuf *buf);
173 int sae_check_confirm_pk(struct sae_data *sae, const u8 *ies, size_t ies_len);
174 int sae_hash(size_t hash_len, const u8 *data, size_t len, u8 *hash);
175 u32 sae_pk_get_be19(const u8 *buf);
176 void sae_pk_buf_shift_left_19(u8 *buf, size_t len);
177 
178 #endif /* SAE_H */
179 #endif /* CONFIG_WPA3_SAE */
180