1 /*	$OpenBSD: ieee80211_crypto_bip.c,v 1.7 2015/11/24 13:45:06 mpi Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * This code implements the Broadcast/Multicast Integrity Protocol (BIP)
21  * defined in IEEE P802.11w/D7.0 section 8.3.4.
22  */
23 
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/mbuf.h>
27 #include <sys/malloc.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/endian.h>
31 
32 #include <net/if.h>
33 #include <net/if_dl.h>
34 #include <net/if_media.h>
35 
36 #include <netinet/in.h>
37 #include <netinet/if_ether.h>
38 
39 #include <net80211/ieee80211_var.h>
40 #include <net80211/ieee80211_crypto.h>
41 #include <net80211/ieee80211_priv.h>
42 
43 #include <crypto/rijndael.h>
44 #include <crypto/cmac.h>
45 
46 /* BIP software crypto context */
47 struct ieee80211_bip_ctx {
48 	AES_CMAC_CTX	cmac;
49 };
50 
51 /*
52  * Initialize software crypto context.  This function can be overridden
53  * by drivers doing hardware crypto.
54  */
55 int
56 ieee80211_bip_set_key(struct ieee80211com *ic, struct ieee80211_key *k)
57 {
58 	struct ieee80211_bip_ctx *ctx;
59 
60 	ctx = malloc(sizeof(*ctx), M_DEVBUF, M_NOWAIT | M_ZERO);
61 	if (ctx == NULL)
62 		return ENOMEM;
63 	AES_CMAC_SetKey(&ctx->cmac, k->k_key);
64 	k->k_priv = ctx;
65 	return 0;
66 }
67 
68 void
69 ieee80211_bip_delete_key(struct ieee80211com *ic, struct ieee80211_key *k)
70 {
71 	if (k->k_priv != NULL)
72 		free(k->k_priv, M_DEVBUF, 0);
73 	k->k_priv = NULL;
74 }
75 
76 /* pseudo-header used for BIP MIC computation */
77 struct ieee80211_bip_frame {
78 	u_int8_t	i_fc[2];
79 	u_int8_t	i_addr1[IEEE80211_ADDR_LEN];
80 	u_int8_t	i_addr2[IEEE80211_ADDR_LEN];
81 	u_int8_t	i_addr3[IEEE80211_ADDR_LEN];
82 } __packed;
83 
84 struct mbuf *
85 ieee80211_bip_encap(struct ieee80211com *ic, struct mbuf *m0,
86     struct ieee80211_key *k)
87 {
88 	struct ieee80211_bip_ctx *ctx = k->k_priv;
89 	struct ieee80211_bip_frame aad;
90 	struct ieee80211_frame *wh;
91 	u_int8_t *mmie, mic[AES_CMAC_DIGEST_LENGTH];
92 	struct mbuf *m;
93 
94 	wh = mtod(m0, struct ieee80211_frame *);
95 	KASSERT((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
96 	    IEEE80211_FC0_TYPE_MGT);
97 	/* clear Protected bit from group management frames */
98 	wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
99 
100 	/* construct AAD (additional authenticated data) */
101 	aad.i_fc[0] = wh->i_fc[0];
102 	aad.i_fc[1] = wh->i_fc[1] & ~(IEEE80211_FC1_RETRY |
103 	    IEEE80211_FC1_PWR_MGT | IEEE80211_FC1_MORE_DATA);
104 	/* XXX 11n may require clearing the Order bit too */
105 	IEEE80211_ADDR_COPY(aad.i_addr1, wh->i_addr1);
106 	IEEE80211_ADDR_COPY(aad.i_addr2, wh->i_addr2);
107 	IEEE80211_ADDR_COPY(aad.i_addr3, wh->i_addr3);
108 
109 	AES_CMAC_Init(&ctx->cmac);
110 	AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&aad, sizeof aad);
111 	AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&wh[1],
112 	    m0->m_len - sizeof(*wh));
113 
114 	m = m0;
115 	/* reserve trailing space for MMIE */
116 	if (M_TRAILINGSPACE(m) < IEEE80211_MMIE_LEN) {
117 		MGET(m->m_next, M_DONTWAIT, m->m_type);
118 		if (m->m_next == NULL)
119 			goto nospace;
120 		m = m->m_next;
121 		m->m_len = 0;
122 	}
123 
124 	/* construct Management MIC IE */
125 	mmie = mtod(m, u_int8_t *) + m->m_len;
126 	mmie[0] = IEEE80211_ELEMID_MMIE;
127 	mmie[1] = 16;
128 	LE_WRITE_2(&mmie[2], k->k_id);
129 	LE_WRITE_6(&mmie[4], k->k_tsc);
130 	memset(&mmie[10], 0, 8);	/* MMIE MIC field set to 0 */
131 
132 	AES_CMAC_Update(&ctx->cmac, mmie, IEEE80211_MMIE_LEN);
133 	AES_CMAC_Final(mic, &ctx->cmac);
134 	/* truncate AES-128-CMAC to 64-bit */
135 	memcpy(&mmie[10], mic, 8);
136 
137 	m->m_len += IEEE80211_MMIE_LEN;
138 	m0->m_pkthdr.len += IEEE80211_MMIE_LEN;
139 
140 	k->k_tsc++;
141 
142 	return m0;
143  nospace:
144 	ic->ic_stats.is_tx_nombuf++;
145 	m_freem(m0);
146 	return NULL;
147 }
148 
149 struct mbuf *
150 ieee80211_bip_decap(struct ieee80211com *ic, struct mbuf *m0,
151     struct ieee80211_key *k)
152 {
153 	struct ieee80211_bip_ctx *ctx = k->k_priv;
154 	struct ieee80211_frame *wh;
155 	struct ieee80211_bip_frame aad;
156 	u_int8_t *mmie, mic0[8], mic[AES_CMAC_DIGEST_LENGTH];
157 	u_int64_t ipn;
158 
159 	wh = mtod(m0, struct ieee80211_frame *);
160 	KASSERT((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
161 	    IEEE80211_FC0_TYPE_MGT);
162 
163 	/*
164 	 * It is assumed that management frames are contiguous and that
165 	 * the mbuf length has already been checked to contain at least
166 	 * a header and a MMIE (checked in ieee80211_decrypt()).
167 	 */
168 	KASSERT(m0->m_len >= sizeof(*wh) + IEEE80211_MMIE_LEN);
169 	mmie = mtod(m0, u_int8_t *) + m0->m_len - IEEE80211_MMIE_LEN;
170 
171 	ipn = LE_READ_6(&mmie[4]);
172 	if (ipn <= k->k_mgmt_rsc) {
173 		/* replayed frame, discard */
174 		ic->ic_stats.is_cmac_replays++;
175 		m_freem(m0);
176 		return NULL;
177 	}
178 
179 	/* save and mask MMIE MIC field to 0 */
180 	memcpy(mic0, &mmie[10], 8);
181 	memset(&mmie[10], 0, 8);
182 
183 	/* construct AAD (additional authenticated data) */
184 	aad.i_fc[0] = wh->i_fc[0];
185 	aad.i_fc[1] = wh->i_fc[1] & ~(IEEE80211_FC1_RETRY |
186 	    IEEE80211_FC1_PWR_MGT | IEEE80211_FC1_MORE_DATA);
187 	/* XXX 11n may require clearing the Order bit too */
188 	IEEE80211_ADDR_COPY(aad.i_addr1, wh->i_addr1);
189 	IEEE80211_ADDR_COPY(aad.i_addr2, wh->i_addr2);
190 	IEEE80211_ADDR_COPY(aad.i_addr3, wh->i_addr3);
191 
192 	/* compute MIC */
193 	AES_CMAC_Init(&ctx->cmac);
194 	AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&aad, sizeof aad);
195 	AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&wh[1],
196 	    m0->m_len - sizeof(*wh));
197 	AES_CMAC_Final(mic, &ctx->cmac);
198 
199 	/* check that MIC matches the one in MMIE */
200 	if (timingsafe_bcmp(mic, mic0, 8) != 0) {
201 		ic->ic_stats.is_cmac_icv_errs++;
202 		m_freem(m0);
203 		return NULL;
204 	}
205 	/*
206 	 * There is no need to trim the MMIE from the mbuf since it is
207 	 * an information element and will be ignored by upper layers.
208 	 * We do it anyway as it is cheap to do it here and because it
209 	 * may be confused with fixed fields by upper layers.
210 	 */
211 	m_adj(m0, -IEEE80211_MMIE_LEN);
212 
213 	/* update last seen packet number (MIC is validated) */
214 	k->k_mgmt_rsc = ipn;
215 
216 	return m0;
217 }
218