blob: dacb3b4b1bdbf5c786e18156064ef18253b2d46b [file] [log] [blame]
Jeff Garzikb4538722005-05-12 22:48:20 -04001/*
John W. Linville274bfb82008-10-29 11:35:05 -04002 * lib80211 crypt: host-based CCMP encryption implementation for lib80211
Jeff Garzikb4538722005-05-12 22:48:20 -04003 *
Jouni Malinen85d32e72007-03-24 17:15:30 -07004 * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
John W. Linville274bfb82008-10-29 11:35:05 -04005 * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
Jeff Garzikb4538722005-05-12 22:48:20 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. See README and COPYING for
10 * more details.
11 */
12
Ilpo Järvinen172589c2007-08-28 15:50:33 -070013#include <linux/kernel.h>
Herbert Xuf12cc202006-08-22 20:36:13 +100014#include <linux/err.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/random.h>
19#include <linux/skbuff.h>
20#include <linux/netdevice.h>
21#include <linux/if_ether.h>
22#include <linux/if_arp.h>
23#include <asm/string.h>
24#include <linux/wireless.h>
25
John W. Linville274bfb82008-10-29 11:35:05 -040026#include <linux/ieee80211.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040027
Jeff Garzikb4538722005-05-12 22:48:20 -040028#include <linux/crypto.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040029
John W. Linville274bfb82008-10-29 11:35:05 -040030#include <net/lib80211.h>
31
Jeff Garzikb4538722005-05-12 22:48:20 -040032MODULE_AUTHOR("Jouni Malinen");
33MODULE_DESCRIPTION("Host AP crypt: CCMP");
34MODULE_LICENSE("GPL");
35
36#define AES_BLOCK_LEN 16
37#define CCMP_HDR_LEN 8
38#define CCMP_MIC_LEN 8
39#define CCMP_TK_LEN 16
40#define CCMP_PN_LEN 6
41
John W. Linville274bfb82008-10-29 11:35:05 -040042struct lib80211_ccmp_data {
Jeff Garzikb4538722005-05-12 22:48:20 -040043 u8 key[CCMP_TK_LEN];
44 int key_set;
45
46 u8 tx_pn[CCMP_PN_LEN];
47 u8 rx_pn[CCMP_PN_LEN];
48
49 u32 dot11RSNAStatsCCMPFormatErrors;
50 u32 dot11RSNAStatsCCMPReplays;
51 u32 dot11RSNAStatsCCMPDecryptErrors;
52
53 int key_idx;
54
Herbert Xuf12cc202006-08-22 20:36:13 +100055 struct crypto_cipher *tfm;
Jeff Garzikb4538722005-05-12 22:48:20 -040056
57 /* scratch buffers for virt_to_page() (crypto API) */
58 u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
Jeff Garzik0edd5b42005-09-07 00:48:31 -040059 tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
Jeff Garzikb4538722005-05-12 22:48:20 -040060 u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
61};
62
John W. Linville274bfb82008-10-29 11:35:05 -040063static inline void lib80211_ccmp_aes_encrypt(struct crypto_cipher *tfm,
Herbert Xuf12cc202006-08-22 20:36:13 +100064 const u8 pt[16], u8 ct[16])
Jeff Garzikb4538722005-05-12 22:48:20 -040065{
Herbert Xuf12cc202006-08-22 20:36:13 +100066 crypto_cipher_encrypt_one(tfm, ct, pt);
Jeff Garzikb4538722005-05-12 22:48:20 -040067}
68
John W. Linville274bfb82008-10-29 11:35:05 -040069static void *lib80211_ccmp_init(int key_idx)
Jeff Garzikb4538722005-05-12 22:48:20 -040070{
John W. Linville274bfb82008-10-29 11:35:05 -040071 struct lib80211_ccmp_data *priv;
Jeff Garzikb4538722005-05-12 22:48:20 -040072
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070073 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
Jeff Garzikb4538722005-05-12 22:48:20 -040074 if (priv == NULL)
75 goto fail;
Jeff Garzikb4538722005-05-12 22:48:20 -040076 priv->key_idx = key_idx;
77
Herbert Xuf12cc202006-08-22 20:36:13 +100078 priv->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
79 if (IS_ERR(priv->tfm)) {
John W. Linville274bfb82008-10-29 11:35:05 -040080 printk(KERN_DEBUG "lib80211_crypt_ccmp: could not allocate "
Jeff Garzikb4538722005-05-12 22:48:20 -040081 "crypto API aes\n");
Herbert Xuf12cc202006-08-22 20:36:13 +100082 priv->tfm = NULL;
Jeff Garzikb4538722005-05-12 22:48:20 -040083 goto fail;
84 }
85
86 return priv;
87
Jeff Garzik0edd5b42005-09-07 00:48:31 -040088 fail:
Jeff Garzikb4538722005-05-12 22:48:20 -040089 if (priv) {
90 if (priv->tfm)
Herbert Xuf12cc202006-08-22 20:36:13 +100091 crypto_free_cipher(priv->tfm);
Jeff Garzikb4538722005-05-12 22:48:20 -040092 kfree(priv);
93 }
94
95 return NULL;
96}
97
John W. Linville274bfb82008-10-29 11:35:05 -040098static void lib80211_ccmp_deinit(void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -040099{
John W. Linville274bfb82008-10-29 11:35:05 -0400100 struct lib80211_ccmp_data *_priv = priv;
Jeff Garzikb4538722005-05-12 22:48:20 -0400101 if (_priv && _priv->tfm)
Herbert Xuf12cc202006-08-22 20:36:13 +1000102 crypto_free_cipher(_priv->tfm);
Jeff Garzikb4538722005-05-12 22:48:20 -0400103 kfree(priv);
104}
105
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400106static inline void xor_block(u8 * b, u8 * a, size_t len)
Jeff Garzikb4538722005-05-12 22:48:20 -0400107{
108 int i;
109 for (i = 0; i < len; i++)
110 b[i] ^= a[i];
111}
112
Herbert Xuf12cc202006-08-22 20:36:13 +1000113static void ccmp_init_blocks(struct crypto_cipher *tfm,
John W. Linville274bfb82008-10-29 11:35:05 -0400114 struct ieee80211_hdr *hdr,
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400115 u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0)
Jeff Garzikb4538722005-05-12 22:48:20 -0400116{
117 u8 *pos, qc = 0;
118 size_t aad_len;
Jeff Garzikb4538722005-05-12 22:48:20 -0400119 int a4_included, qc_included;
120 u8 aad[2 * AES_BLOCK_LEN];
121
John W. Linville274bfb82008-10-29 11:35:05 -0400122 a4_included = ieee80211_has_a4(hdr->frame_control);
123 qc_included = ieee80211_is_data_qos(hdr->frame_control);
124
Jeff Garzikb4538722005-05-12 22:48:20 -0400125 aad_len = 22;
126 if (a4_included)
127 aad_len += 6;
128 if (qc_included) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400129 pos = (u8 *) & hdr->addr4;
Jeff Garzikb4538722005-05-12 22:48:20 -0400130 if (a4_included)
131 pos += 6;
132 qc = *pos & 0x0f;
133 aad_len += 2;
134 }
135
136 /* CCM Initial Block:
137 * Flag (Include authentication header, M=3 (8-octet MIC),
138 * L=1 (2-octet Dlen))
139 * Nonce: 0x00 | A2 | PN
140 * Dlen */
141 b0[0] = 0x59;
142 b0[1] = qc;
143 memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
144 memcpy(b0 + 8, pn, CCMP_PN_LEN);
145 b0[14] = (dlen >> 8) & 0xff;
146 b0[15] = dlen & 0xff;
147
148 /* AAD:
149 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
150 * A1 | A2 | A3
151 * SC with bits 4..15 (seq#) masked to zero
152 * A4 (if present)
153 * QC (if present)
154 */
155 pos = (u8 *) hdr;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400156 aad[0] = 0; /* aad_len >> 8 */
Jeff Garzikb4538722005-05-12 22:48:20 -0400157 aad[1] = aad_len & 0xff;
158 aad[2] = pos[0] & 0x8f;
159 aad[3] = pos[1] & 0xc7;
160 memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
John W. Linville274bfb82008-10-29 11:35:05 -0400161 pos = (u8 *) & hdr->seq_ctrl;
Jeff Garzikb4538722005-05-12 22:48:20 -0400162 aad[22] = pos[0] & 0x0f;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400163 aad[23] = 0; /* all bits masked */
Jeff Garzikb4538722005-05-12 22:48:20 -0400164 memset(aad + 24, 0, 8);
165 if (a4_included)
166 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
167 if (qc_included) {
168 aad[a4_included ? 30 : 24] = qc;
169 /* rest of QC masked */
170 }
171
172 /* Start with the first block and AAD */
John W. Linville274bfb82008-10-29 11:35:05 -0400173 lib80211_ccmp_aes_encrypt(tfm, b0, auth);
Jeff Garzikb4538722005-05-12 22:48:20 -0400174 xor_block(auth, aad, AES_BLOCK_LEN);
John W. Linville274bfb82008-10-29 11:35:05 -0400175 lib80211_ccmp_aes_encrypt(tfm, auth, auth);
Jeff Garzikb4538722005-05-12 22:48:20 -0400176 xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
John W. Linville274bfb82008-10-29 11:35:05 -0400177 lib80211_ccmp_aes_encrypt(tfm, auth, auth);
Jeff Garzikb4538722005-05-12 22:48:20 -0400178 b0[0] &= 0x07;
179 b0[14] = b0[15] = 0;
John W. Linville274bfb82008-10-29 11:35:05 -0400180 lib80211_ccmp_aes_encrypt(tfm, b0, s0);
Jeff Garzikb4538722005-05-12 22:48:20 -0400181}
182
John W. Linville274bfb82008-10-29 11:35:05 -0400183static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
Zhu Yi9184d932006-01-19 16:22:32 +0800184 u8 *aeskey, int keylen, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400185{
John W. Linville274bfb82008-10-29 11:35:05 -0400186 struct lib80211_ccmp_data *key = priv;
James Ketrenos31b59ea2005-09-21 11:58:49 -0500187 int i;
188 u8 *pos;
Jeff Garzikb4538722005-05-12 22:48:20 -0400189
James Ketrenos31b59ea2005-09-21 11:58:49 -0500190 if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
Jeff Garzikb4538722005-05-12 22:48:20 -0400191 return -1;
192
Zhu Yi9184d932006-01-19 16:22:32 +0800193 if (aeskey != NULL && keylen >= CCMP_TK_LEN)
194 memcpy(aeskey, key->key, CCMP_TK_LEN);
195
Jeff Garzikb4538722005-05-12 22:48:20 -0400196 pos = skb_push(skb, CCMP_HDR_LEN);
197 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
198 pos += hdr_len;
Jeff Garzikb4538722005-05-12 22:48:20 -0400199
200 i = CCMP_PN_LEN - 1;
201 while (i >= 0) {
202 key->tx_pn[i]++;
203 if (key->tx_pn[i] != 0)
204 break;
205 i--;
206 }
207
208 *pos++ = key->tx_pn[5];
209 *pos++ = key->tx_pn[4];
210 *pos++ = 0;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400211 *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
Jeff Garzikb4538722005-05-12 22:48:20 -0400212 *pos++ = key->tx_pn[3];
213 *pos++ = key->tx_pn[2];
214 *pos++ = key->tx_pn[1];
215 *pos++ = key->tx_pn[0];
216
James Ketrenos31b59ea2005-09-21 11:58:49 -0500217 return CCMP_HDR_LEN;
218}
219
John W. Linville274bfb82008-10-29 11:35:05 -0400220static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
James Ketrenos31b59ea2005-09-21 11:58:49 -0500221{
John W. Linville274bfb82008-10-29 11:35:05 -0400222 struct lib80211_ccmp_data *key = priv;
James Ketrenos31b59ea2005-09-21 11:58:49 -0500223 int data_len, i, blocks, last, len;
224 u8 *pos, *mic;
John W. Linville274bfb82008-10-29 11:35:05 -0400225 struct ieee80211_hdr *hdr;
James Ketrenos31b59ea2005-09-21 11:58:49 -0500226 u8 *b0 = key->tx_b0;
227 u8 *b = key->tx_b;
228 u8 *e = key->tx_e;
229 u8 *s0 = key->tx_s0;
230
231 if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
232 return -1;
233
234 data_len = skb->len - hdr_len;
John W. Linville274bfb82008-10-29 11:35:05 -0400235 len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
James Ketrenos31b59ea2005-09-21 11:58:49 -0500236 if (len < 0)
237 return -1;
238
239 pos = skb->data + hdr_len + CCMP_HDR_LEN;
John W. Linville274bfb82008-10-29 11:35:05 -0400240 hdr = (struct ieee80211_hdr *)skb->data;
Jeff Garzikb4538722005-05-12 22:48:20 -0400241 ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
242
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700243 blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
Jeff Garzikb4538722005-05-12 22:48:20 -0400244 last = data_len % AES_BLOCK_LEN;
245
246 for (i = 1; i <= blocks; i++) {
247 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
248 /* Authentication */
249 xor_block(b, pos, len);
John W. Linville274bfb82008-10-29 11:35:05 -0400250 lib80211_ccmp_aes_encrypt(key->tfm, b, b);
Jeff Garzikb4538722005-05-12 22:48:20 -0400251 /* Encryption, with counter */
252 b0[14] = (i >> 8) & 0xff;
253 b0[15] = i & 0xff;
John W. Linville274bfb82008-10-29 11:35:05 -0400254 lib80211_ccmp_aes_encrypt(key->tfm, b0, e);
Jeff Garzikb4538722005-05-12 22:48:20 -0400255 xor_block(pos, e, len);
256 pos += len;
257 }
258
Andriy Tkachukd0833a62010-02-02 15:58:53 +0200259 mic = skb_put(skb, CCMP_MIC_LEN);
Jeff Garzikb4538722005-05-12 22:48:20 -0400260 for (i = 0; i < CCMP_MIC_LEN; i++)
261 mic[i] = b[i] ^ s0[i];
262
263 return 0;
264}
265
Zhu Yib4328d82006-08-21 11:33:09 +0800266/*
267 * deal with seq counter wrapping correctly.
268 * refer to timer_after() for jiffies wrapping handling
269 */
270static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
271{
272 u32 iv32_n, iv16_n;
273 u32 iv32_o, iv16_o;
274
275 iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
276 iv16_n = (pn_n[4] << 8) | pn_n[5];
277
278 iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
279 iv16_o = (pn_o[4] << 8) | pn_o[5];
280
281 if ((s32)iv32_n - (s32)iv32_o < 0 ||
282 (iv32_n == iv32_o && iv16_n <= iv16_o))
283 return 1;
284 return 0;
285}
286
John W. Linville274bfb82008-10-29 11:35:05 -0400287static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400288{
John W. Linville274bfb82008-10-29 11:35:05 -0400289 struct lib80211_ccmp_data *key = priv;
Jeff Garzikb4538722005-05-12 22:48:20 -0400290 u8 keyidx, *pos;
John W. Linville274bfb82008-10-29 11:35:05 -0400291 struct ieee80211_hdr *hdr;
Jeff Garzikb4538722005-05-12 22:48:20 -0400292 u8 *b0 = key->rx_b0;
293 u8 *b = key->rx_b;
294 u8 *a = key->rx_a;
295 u8 pn[6];
296 int i, blocks, last, len;
297 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
298 u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
299
300 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
301 key->dot11RSNAStatsCCMPFormatErrors++;
302 return -1;
303 }
304
John W. Linville274bfb82008-10-29 11:35:05 -0400305 hdr = (struct ieee80211_hdr *)skb->data;
Jeff Garzikb4538722005-05-12 22:48:20 -0400306 pos = skb->data + hdr_len;
307 keyidx = pos[3];
308 if (!(keyidx & (1 << 5))) {
309 if (net_ratelimit()) {
310 printk(KERN_DEBUG "CCMP: received packet without ExtIV"
Johannes Berge1749612008-10-27 15:59:26 -0700311 " flag from %pM\n", hdr->addr2);
Jeff Garzikb4538722005-05-12 22:48:20 -0400312 }
313 key->dot11RSNAStatsCCMPFormatErrors++;
314 return -2;
315 }
316 keyidx >>= 6;
317 if (key->key_idx != keyidx) {
318 printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame "
319 "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv);
320 return -6;
321 }
322 if (!key->key_set) {
323 if (net_ratelimit()) {
Johannes Berge1749612008-10-27 15:59:26 -0700324 printk(KERN_DEBUG "CCMP: received packet from %pM"
Jeff Garzikb4538722005-05-12 22:48:20 -0400325 " with keyid=%d that does not have a configured"
Johannes Berge1749612008-10-27 15:59:26 -0700326 " key\n", hdr->addr2, keyidx);
Jeff Garzikb4538722005-05-12 22:48:20 -0400327 }
328 return -3;
329 }
330
331 pn[0] = pos[7];
332 pn[1] = pos[6];
333 pn[2] = pos[5];
334 pn[3] = pos[4];
335 pn[4] = pos[1];
336 pn[5] = pos[0];
337 pos += 8;
338
Zhu Yib4328d82006-08-21 11:33:09 +0800339 if (ccmp_replay_check(pn, key->rx_pn)) {
John W. Linville6f16bf32009-03-11 11:05:25 -0400340#ifdef CONFIG_LIB80211_DEBUG
John W. Linville274bfb82008-10-29 11:35:05 -0400341 if (net_ratelimit()) {
342 printk(KERN_DEBUG "CCMP: replay detected: STA=%pM "
Joe Perches0795af52007-10-03 17:59:30 -0700343 "previous PN %02x%02x%02x%02x%02x%02x "
344 "received PN %02x%02x%02x%02x%02x%02x\n",
Johannes Berge1749612008-10-27 15:59:26 -0700345 hdr->addr2,
Joe Perches0795af52007-10-03 17:59:30 -0700346 key->rx_pn[0], key->rx_pn[1], key->rx_pn[2],
347 key->rx_pn[3], key->rx_pn[4], key->rx_pn[5],
348 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
Jeff Garzikb4538722005-05-12 22:48:20 -0400349 }
John W. Linville6f16bf32009-03-11 11:05:25 -0400350#endif
Jeff Garzikb4538722005-05-12 22:48:20 -0400351 key->dot11RSNAStatsCCMPReplays++;
352 return -4;
353 }
354
355 ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
356 xor_block(mic, b, CCMP_MIC_LEN);
357
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700358 blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
Jeff Garzikb4538722005-05-12 22:48:20 -0400359 last = data_len % AES_BLOCK_LEN;
360
361 for (i = 1; i <= blocks; i++) {
362 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
363 /* Decrypt, with counter */
364 b0[14] = (i >> 8) & 0xff;
365 b0[15] = i & 0xff;
John W. Linville274bfb82008-10-29 11:35:05 -0400366 lib80211_ccmp_aes_encrypt(key->tfm, b0, b);
Jeff Garzikb4538722005-05-12 22:48:20 -0400367 xor_block(pos, b, len);
368 /* Authentication */
369 xor_block(a, pos, len);
John W. Linville274bfb82008-10-29 11:35:05 -0400370 lib80211_ccmp_aes_encrypt(key->tfm, a, a);
Jeff Garzikb4538722005-05-12 22:48:20 -0400371 pos += len;
372 }
373
374 if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
375 if (net_ratelimit()) {
376 printk(KERN_DEBUG "CCMP: decrypt failed: STA="
Johannes Berge1749612008-10-27 15:59:26 -0700377 "%pM\n", hdr->addr2);
Jeff Garzikb4538722005-05-12 22:48:20 -0400378 }
379 key->dot11RSNAStatsCCMPDecryptErrors++;
380 return -5;
381 }
382
383 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
384
385 /* Remove hdr and MIC */
386 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
387 skb_pull(skb, CCMP_HDR_LEN);
388 skb_trim(skb, skb->len - CCMP_MIC_LEN);
389
390 return keyidx;
391}
392
John W. Linville274bfb82008-10-29 11:35:05 -0400393static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400394{
John W. Linville274bfb82008-10-29 11:35:05 -0400395 struct lib80211_ccmp_data *data = priv;
Jeff Garzikb4538722005-05-12 22:48:20 -0400396 int keyidx;
Herbert Xuf12cc202006-08-22 20:36:13 +1000397 struct crypto_cipher *tfm = data->tfm;
Jeff Garzikb4538722005-05-12 22:48:20 -0400398
399 keyidx = data->key_idx;
400 memset(data, 0, sizeof(*data));
401 data->key_idx = keyidx;
402 data->tfm = tfm;
403 if (len == CCMP_TK_LEN) {
404 memcpy(data->key, key, CCMP_TK_LEN);
405 data->key_set = 1;
406 if (seq) {
407 data->rx_pn[0] = seq[5];
408 data->rx_pn[1] = seq[4];
409 data->rx_pn[2] = seq[3];
410 data->rx_pn[3] = seq[2];
411 data->rx_pn[4] = seq[1];
412 data->rx_pn[5] = seq[0];
413 }
414 crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN);
415 } else if (len == 0)
416 data->key_set = 0;
417 else
418 return -1;
419
420 return 0;
421}
422
John W. Linville274bfb82008-10-29 11:35:05 -0400423static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400424{
John W. Linville274bfb82008-10-29 11:35:05 -0400425 struct lib80211_ccmp_data *data = priv;
Jeff Garzikb4538722005-05-12 22:48:20 -0400426
427 if (len < CCMP_TK_LEN)
428 return -1;
429
430 if (!data->key_set)
431 return 0;
432 memcpy(key, data->key, CCMP_TK_LEN);
433
434 if (seq) {
435 seq[0] = data->tx_pn[5];
436 seq[1] = data->tx_pn[4];
437 seq[2] = data->tx_pn[3];
438 seq[3] = data->tx_pn[2];
439 seq[4] = data->tx_pn[1];
440 seq[5] = data->tx_pn[0];
441 }
442
443 return CCMP_TK_LEN;
444}
445
John W. Linville274bfb82008-10-29 11:35:05 -0400446static char *lib80211_ccmp_print_stats(char *p, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400447{
John W. Linville274bfb82008-10-29 11:35:05 -0400448 struct lib80211_ccmp_data *ccmp = priv;
Joe Perches0795af52007-10-03 17:59:30 -0700449
Jeff Garzikb4538722005-05-12 22:48:20 -0400450 p += sprintf(p, "key[%d] alg=CCMP key_set=%d "
451 "tx_pn=%02x%02x%02x%02x%02x%02x "
452 "rx_pn=%02x%02x%02x%02x%02x%02x "
453 "format_errors=%d replays=%d decrypt_errors=%d\n",
454 ccmp->key_idx, ccmp->key_set,
Joe Perches0795af52007-10-03 17:59:30 -0700455 ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2],
456 ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5],
457 ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2],
458 ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5],
Jeff Garzikb4538722005-05-12 22:48:20 -0400459 ccmp->dot11RSNAStatsCCMPFormatErrors,
460 ccmp->dot11RSNAStatsCCMPReplays,
461 ccmp->dot11RSNAStatsCCMPDecryptErrors);
462
463 return p;
464}
465
John W. Linville274bfb82008-10-29 11:35:05 -0400466static struct lib80211_crypto_ops lib80211_crypt_ccmp = {
James Ketrenos74079fd2005-09-13 17:35:21 -0500467 .name = "CCMP",
John W. Linville274bfb82008-10-29 11:35:05 -0400468 .init = lib80211_ccmp_init,
469 .deinit = lib80211_ccmp_deinit,
John W. Linville274bfb82008-10-29 11:35:05 -0400470 .encrypt_mpdu = lib80211_ccmp_encrypt,
471 .decrypt_mpdu = lib80211_ccmp_decrypt,
James Ketrenos74079fd2005-09-13 17:35:21 -0500472 .encrypt_msdu = NULL,
473 .decrypt_msdu = NULL,
John W. Linville274bfb82008-10-29 11:35:05 -0400474 .set_key = lib80211_ccmp_set_key,
475 .get_key = lib80211_ccmp_get_key,
476 .print_stats = lib80211_ccmp_print_stats,
James Ketrenos1264fc02005-09-21 11:54:53 -0500477 .extra_mpdu_prefix_len = CCMP_HDR_LEN,
478 .extra_mpdu_postfix_len = CCMP_MIC_LEN,
James Ketrenos74079fd2005-09-13 17:35:21 -0500479 .owner = THIS_MODULE,
Jeff Garzikb4538722005-05-12 22:48:20 -0400480};
481
John W. Linville274bfb82008-10-29 11:35:05 -0400482static int __init lib80211_crypto_ccmp_init(void)
Jeff Garzikb4538722005-05-12 22:48:20 -0400483{
John W. Linville274bfb82008-10-29 11:35:05 -0400484 return lib80211_register_crypto_ops(&lib80211_crypt_ccmp);
Jeff Garzikb4538722005-05-12 22:48:20 -0400485}
486
John W. Linville274bfb82008-10-29 11:35:05 -0400487static void __exit lib80211_crypto_ccmp_exit(void)
Jeff Garzikb4538722005-05-12 22:48:20 -0400488{
John W. Linville274bfb82008-10-29 11:35:05 -0400489 lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp);
Jeff Garzikb4538722005-05-12 22:48:20 -0400490}
491
John W. Linville274bfb82008-10-29 11:35:05 -0400492module_init(lib80211_crypto_ccmp_init);
493module_exit(lib80211_crypto_ccmp_exit);