Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Host AP crypt: host-based CCMP encryption implementation for Host AP driver |
| 3 | * |
| 4 | * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. See README and COPYING for |
| 9 | * more details. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/config.h> |
| 13 | #include <linux/version.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/random.h> |
| 18 | #include <linux/skbuff.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/if_ether.h> |
| 21 | #include <linux/if_arp.h> |
| 22 | #include <asm/string.h> |
| 23 | #include <linux/wireless.h> |
| 24 | |
| 25 | #include <net/ieee80211.h> |
| 26 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 27 | #include <linux/crypto.h> |
| 28 | #include <asm/scatterlist.h> |
| 29 | |
| 30 | MODULE_AUTHOR("Jouni Malinen"); |
| 31 | MODULE_DESCRIPTION("Host AP crypt: CCMP"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
| 34 | #define AES_BLOCK_LEN 16 |
| 35 | #define CCMP_HDR_LEN 8 |
| 36 | #define CCMP_MIC_LEN 8 |
| 37 | #define CCMP_TK_LEN 16 |
| 38 | #define CCMP_PN_LEN 6 |
| 39 | |
| 40 | struct ieee80211_ccmp_data { |
| 41 | u8 key[CCMP_TK_LEN]; |
| 42 | int key_set; |
| 43 | |
| 44 | u8 tx_pn[CCMP_PN_LEN]; |
| 45 | u8 rx_pn[CCMP_PN_LEN]; |
| 46 | |
| 47 | u32 dot11RSNAStatsCCMPFormatErrors; |
| 48 | u32 dot11RSNAStatsCCMPReplays; |
| 49 | u32 dot11RSNAStatsCCMPDecryptErrors; |
| 50 | |
| 51 | int key_idx; |
| 52 | |
| 53 | struct crypto_tfm *tfm; |
| 54 | |
| 55 | /* scratch buffers for virt_to_page() (crypto API) */ |
| 56 | u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN], |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 57 | tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN]; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 58 | u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN]; |
| 59 | }; |
| 60 | |
Adrian Bunk | e157249 | 2005-05-06 23:32:39 +0200 | [diff] [blame] | 61 | static void ieee80211_ccmp_aes_encrypt(struct crypto_tfm *tfm, |
| 62 | const u8 pt[16], u8 ct[16]) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 63 | { |
| 64 | struct scatterlist src, dst; |
| 65 | |
| 66 | src.page = virt_to_page(pt); |
| 67 | src.offset = offset_in_page(pt); |
| 68 | src.length = AES_BLOCK_LEN; |
| 69 | |
| 70 | dst.page = virt_to_page(ct); |
| 71 | dst.offset = offset_in_page(ct); |
| 72 | dst.length = AES_BLOCK_LEN; |
| 73 | |
| 74 | crypto_cipher_encrypt(tfm, &dst, &src, AES_BLOCK_LEN); |
| 75 | } |
| 76 | |
James Ketrenos | 6eb6edf | 2005-09-22 10:34:15 +0000 | [diff] [blame] | 77 | static void *ieee80211_ccmp_init(int key_idx) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 78 | { |
| 79 | struct ieee80211_ccmp_data *priv; |
| 80 | |
| 81 | priv = kmalloc(sizeof(*priv), GFP_ATOMIC); |
| 82 | if (priv == NULL) |
| 83 | goto fail; |
| 84 | memset(priv, 0, sizeof(*priv)); |
| 85 | priv->key_idx = key_idx; |
| 86 | |
| 87 | priv->tfm = crypto_alloc_tfm("aes", 0); |
| 88 | if (priv->tfm == NULL) { |
| 89 | printk(KERN_DEBUG "ieee80211_crypt_ccmp: could not allocate " |
| 90 | "crypto API aes\n"); |
| 91 | goto fail; |
| 92 | } |
| 93 | |
| 94 | return priv; |
| 95 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 96 | fail: |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 97 | if (priv) { |
| 98 | if (priv->tfm) |
| 99 | crypto_free_tfm(priv->tfm); |
| 100 | kfree(priv); |
| 101 | } |
| 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 106 | static void ieee80211_ccmp_deinit(void *priv) |
| 107 | { |
| 108 | struct ieee80211_ccmp_data *_priv = priv; |
| 109 | if (_priv && _priv->tfm) |
| 110 | crypto_free_tfm(_priv->tfm); |
| 111 | kfree(priv); |
| 112 | } |
| 113 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 114 | static inline void xor_block(u8 * b, u8 * a, size_t len) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 115 | { |
| 116 | int i; |
| 117 | for (i = 0; i < len; i++) |
| 118 | b[i] ^= a[i]; |
| 119 | } |
| 120 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 121 | static void ccmp_init_blocks(struct crypto_tfm *tfm, |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 122 | struct ieee80211_hdr_4addr *hdr, |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 123 | u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 124 | { |
| 125 | u8 *pos, qc = 0; |
| 126 | size_t aad_len; |
| 127 | u16 fc; |
| 128 | int a4_included, qc_included; |
| 129 | u8 aad[2 * AES_BLOCK_LEN]; |
| 130 | |
| 131 | fc = le16_to_cpu(hdr->frame_ctl); |
| 132 | a4_included = ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == |
| 133 | (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)); |
| 134 | qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) && |
| 135 | (WLAN_FC_GET_STYPE(fc) & 0x08)); |
| 136 | aad_len = 22; |
| 137 | if (a4_included) |
| 138 | aad_len += 6; |
| 139 | if (qc_included) { |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 140 | pos = (u8 *) & hdr->addr4; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 141 | if (a4_included) |
| 142 | pos += 6; |
| 143 | qc = *pos & 0x0f; |
| 144 | aad_len += 2; |
| 145 | } |
| 146 | |
| 147 | /* CCM Initial Block: |
| 148 | * Flag (Include authentication header, M=3 (8-octet MIC), |
| 149 | * L=1 (2-octet Dlen)) |
| 150 | * Nonce: 0x00 | A2 | PN |
| 151 | * Dlen */ |
| 152 | b0[0] = 0x59; |
| 153 | b0[1] = qc; |
| 154 | memcpy(b0 + 2, hdr->addr2, ETH_ALEN); |
| 155 | memcpy(b0 + 8, pn, CCMP_PN_LEN); |
| 156 | b0[14] = (dlen >> 8) & 0xff; |
| 157 | b0[15] = dlen & 0xff; |
| 158 | |
| 159 | /* AAD: |
| 160 | * FC with bits 4..6 and 11..13 masked to zero; 14 is always one |
| 161 | * A1 | A2 | A3 |
| 162 | * SC with bits 4..15 (seq#) masked to zero |
| 163 | * A4 (if present) |
| 164 | * QC (if present) |
| 165 | */ |
| 166 | pos = (u8 *) hdr; |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 167 | aad[0] = 0; /* aad_len >> 8 */ |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 168 | aad[1] = aad_len & 0xff; |
| 169 | aad[2] = pos[0] & 0x8f; |
| 170 | aad[3] = pos[1] & 0xc7; |
| 171 | memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN); |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 172 | pos = (u8 *) & hdr->seq_ctl; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 173 | aad[22] = pos[0] & 0x0f; |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 174 | aad[23] = 0; /* all bits masked */ |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 175 | memset(aad + 24, 0, 8); |
| 176 | if (a4_included) |
| 177 | memcpy(aad + 24, hdr->addr4, ETH_ALEN); |
| 178 | if (qc_included) { |
| 179 | aad[a4_included ? 30 : 24] = qc; |
| 180 | /* rest of QC masked */ |
| 181 | } |
| 182 | |
| 183 | /* Start with the first block and AAD */ |
| 184 | ieee80211_ccmp_aes_encrypt(tfm, b0, auth); |
| 185 | xor_block(auth, aad, AES_BLOCK_LEN); |
| 186 | ieee80211_ccmp_aes_encrypt(tfm, auth, auth); |
| 187 | xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN); |
| 188 | ieee80211_ccmp_aes_encrypt(tfm, auth, auth); |
| 189 | b0[0] &= 0x07; |
| 190 | b0[14] = b0[15] = 0; |
| 191 | ieee80211_ccmp_aes_encrypt(tfm, b0, s0); |
| 192 | } |
| 193 | |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 194 | static int ieee80211_ccmp_hdr(struct sk_buff *skb, int hdr_len, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 195 | { |
| 196 | struct ieee80211_ccmp_data *key = priv; |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 197 | int i; |
| 198 | u8 *pos; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 199 | |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 200 | if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 201 | return -1; |
| 202 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 203 | pos = skb_push(skb, CCMP_HDR_LEN); |
| 204 | memmove(pos, pos + CCMP_HDR_LEN, hdr_len); |
| 205 | pos += hdr_len; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 206 | |
| 207 | i = CCMP_PN_LEN - 1; |
| 208 | while (i >= 0) { |
| 209 | key->tx_pn[i]++; |
| 210 | if (key->tx_pn[i] != 0) |
| 211 | break; |
| 212 | i--; |
| 213 | } |
| 214 | |
| 215 | *pos++ = key->tx_pn[5]; |
| 216 | *pos++ = key->tx_pn[4]; |
| 217 | *pos++ = 0; |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 218 | *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 219 | *pos++ = key->tx_pn[3]; |
| 220 | *pos++ = key->tx_pn[2]; |
| 221 | *pos++ = key->tx_pn[1]; |
| 222 | *pos++ = key->tx_pn[0]; |
| 223 | |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 224 | return CCMP_HDR_LEN; |
| 225 | } |
| 226 | |
| 227 | static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv) |
| 228 | { |
| 229 | struct ieee80211_ccmp_data *key = priv; |
| 230 | int data_len, i, blocks, last, len; |
| 231 | u8 *pos, *mic; |
| 232 | struct ieee80211_hdr_4addr *hdr; |
| 233 | u8 *b0 = key->tx_b0; |
| 234 | u8 *b = key->tx_b; |
| 235 | u8 *e = key->tx_e; |
| 236 | u8 *s0 = key->tx_s0; |
| 237 | |
| 238 | if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len) |
| 239 | return -1; |
| 240 | |
| 241 | data_len = skb->len - hdr_len; |
| 242 | len = ieee80211_ccmp_hdr(skb, hdr_len, priv); |
| 243 | if (len < 0) |
| 244 | return -1; |
| 245 | |
| 246 | pos = skb->data + hdr_len + CCMP_HDR_LEN; |
| 247 | mic = skb_put(skb, CCMP_MIC_LEN); |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 248 | hdr = (struct ieee80211_hdr_4addr *)skb->data; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 249 | ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0); |
| 250 | |
| 251 | blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN; |
| 252 | last = data_len % AES_BLOCK_LEN; |
| 253 | |
| 254 | for (i = 1; i <= blocks; i++) { |
| 255 | len = (i == blocks && last) ? last : AES_BLOCK_LEN; |
| 256 | /* Authentication */ |
| 257 | xor_block(b, pos, len); |
| 258 | ieee80211_ccmp_aes_encrypt(key->tfm, b, b); |
| 259 | /* Encryption, with counter */ |
| 260 | b0[14] = (i >> 8) & 0xff; |
| 261 | b0[15] = i & 0xff; |
| 262 | ieee80211_ccmp_aes_encrypt(key->tfm, b0, e); |
| 263 | xor_block(pos, e, len); |
| 264 | pos += len; |
| 265 | } |
| 266 | |
| 267 | for (i = 0; i < CCMP_MIC_LEN; i++) |
| 268 | mic[i] = b[i] ^ s0[i]; |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 273 | static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv) |
| 274 | { |
| 275 | struct ieee80211_ccmp_data *key = priv; |
| 276 | u8 keyidx, *pos; |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 277 | struct ieee80211_hdr_4addr *hdr; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 278 | u8 *b0 = key->rx_b0; |
| 279 | u8 *b = key->rx_b; |
| 280 | u8 *a = key->rx_a; |
| 281 | u8 pn[6]; |
| 282 | int i, blocks, last, len; |
| 283 | size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN; |
| 284 | u8 *mic = skb->data + skb->len - CCMP_MIC_LEN; |
| 285 | |
| 286 | if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) { |
| 287 | key->dot11RSNAStatsCCMPFormatErrors++; |
| 288 | return -1; |
| 289 | } |
| 290 | |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 291 | hdr = (struct ieee80211_hdr_4addr *)skb->data; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 292 | pos = skb->data + hdr_len; |
| 293 | keyidx = pos[3]; |
| 294 | if (!(keyidx & (1 << 5))) { |
| 295 | if (net_ratelimit()) { |
| 296 | printk(KERN_DEBUG "CCMP: received packet without ExtIV" |
| 297 | " flag from " MAC_FMT "\n", MAC_ARG(hdr->addr2)); |
| 298 | } |
| 299 | key->dot11RSNAStatsCCMPFormatErrors++; |
| 300 | return -2; |
| 301 | } |
| 302 | keyidx >>= 6; |
| 303 | if (key->key_idx != keyidx) { |
| 304 | printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame " |
| 305 | "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv); |
| 306 | return -6; |
| 307 | } |
| 308 | if (!key->key_set) { |
| 309 | if (net_ratelimit()) { |
| 310 | printk(KERN_DEBUG "CCMP: received packet from " MAC_FMT |
| 311 | " with keyid=%d that does not have a configured" |
| 312 | " key\n", MAC_ARG(hdr->addr2), keyidx); |
| 313 | } |
| 314 | return -3; |
| 315 | } |
| 316 | |
| 317 | pn[0] = pos[7]; |
| 318 | pn[1] = pos[6]; |
| 319 | pn[2] = pos[5]; |
| 320 | pn[3] = pos[4]; |
| 321 | pn[4] = pos[1]; |
| 322 | pn[5] = pos[0]; |
| 323 | pos += 8; |
| 324 | |
| 325 | if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) { |
| 326 | if (net_ratelimit()) { |
| 327 | printk(KERN_DEBUG "CCMP: replay detected: STA=" MAC_FMT |
| 328 | " previous PN %02x%02x%02x%02x%02x%02x " |
| 329 | "received PN %02x%02x%02x%02x%02x%02x\n", |
| 330 | MAC_ARG(hdr->addr2), MAC_ARG(key->rx_pn), |
| 331 | MAC_ARG(pn)); |
| 332 | } |
| 333 | key->dot11RSNAStatsCCMPReplays++; |
| 334 | return -4; |
| 335 | } |
| 336 | |
| 337 | ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b); |
| 338 | xor_block(mic, b, CCMP_MIC_LEN); |
| 339 | |
| 340 | blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN; |
| 341 | last = data_len % AES_BLOCK_LEN; |
| 342 | |
| 343 | for (i = 1; i <= blocks; i++) { |
| 344 | len = (i == blocks && last) ? last : AES_BLOCK_LEN; |
| 345 | /* Decrypt, with counter */ |
| 346 | b0[14] = (i >> 8) & 0xff; |
| 347 | b0[15] = i & 0xff; |
| 348 | ieee80211_ccmp_aes_encrypt(key->tfm, b0, b); |
| 349 | xor_block(pos, b, len); |
| 350 | /* Authentication */ |
| 351 | xor_block(a, pos, len); |
| 352 | ieee80211_ccmp_aes_encrypt(key->tfm, a, a); |
| 353 | pos += len; |
| 354 | } |
| 355 | |
| 356 | if (memcmp(mic, a, CCMP_MIC_LEN) != 0) { |
| 357 | if (net_ratelimit()) { |
| 358 | printk(KERN_DEBUG "CCMP: decrypt failed: STA=" |
| 359 | MAC_FMT "\n", MAC_ARG(hdr->addr2)); |
| 360 | } |
| 361 | key->dot11RSNAStatsCCMPDecryptErrors++; |
| 362 | return -5; |
| 363 | } |
| 364 | |
| 365 | memcpy(key->rx_pn, pn, CCMP_PN_LEN); |
| 366 | |
| 367 | /* Remove hdr and MIC */ |
| 368 | memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len); |
| 369 | skb_pull(skb, CCMP_HDR_LEN); |
| 370 | skb_trim(skb, skb->len - CCMP_MIC_LEN); |
| 371 | |
| 372 | return keyidx; |
| 373 | } |
| 374 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 375 | static int ieee80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 376 | { |
| 377 | struct ieee80211_ccmp_data *data = priv; |
| 378 | int keyidx; |
| 379 | struct crypto_tfm *tfm = data->tfm; |
| 380 | |
| 381 | keyidx = data->key_idx; |
| 382 | memset(data, 0, sizeof(*data)); |
| 383 | data->key_idx = keyidx; |
| 384 | data->tfm = tfm; |
| 385 | if (len == CCMP_TK_LEN) { |
| 386 | memcpy(data->key, key, CCMP_TK_LEN); |
| 387 | data->key_set = 1; |
| 388 | if (seq) { |
| 389 | data->rx_pn[0] = seq[5]; |
| 390 | data->rx_pn[1] = seq[4]; |
| 391 | data->rx_pn[2] = seq[3]; |
| 392 | data->rx_pn[3] = seq[2]; |
| 393 | data->rx_pn[4] = seq[1]; |
| 394 | data->rx_pn[5] = seq[0]; |
| 395 | } |
| 396 | crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN); |
| 397 | } else if (len == 0) |
| 398 | data->key_set = 0; |
| 399 | else |
| 400 | return -1; |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 405 | static int ieee80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 406 | { |
| 407 | struct ieee80211_ccmp_data *data = priv; |
| 408 | |
| 409 | if (len < CCMP_TK_LEN) |
| 410 | return -1; |
| 411 | |
| 412 | if (!data->key_set) |
| 413 | return 0; |
| 414 | memcpy(key, data->key, CCMP_TK_LEN); |
| 415 | |
| 416 | if (seq) { |
| 417 | seq[0] = data->tx_pn[5]; |
| 418 | seq[1] = data->tx_pn[4]; |
| 419 | seq[2] = data->tx_pn[3]; |
| 420 | seq[3] = data->tx_pn[2]; |
| 421 | seq[4] = data->tx_pn[1]; |
| 422 | seq[5] = data->tx_pn[0]; |
| 423 | } |
| 424 | |
| 425 | return CCMP_TK_LEN; |
| 426 | } |
| 427 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 428 | static char *ieee80211_ccmp_print_stats(char *p, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 429 | { |
| 430 | struct ieee80211_ccmp_data *ccmp = priv; |
| 431 | p += sprintf(p, "key[%d] alg=CCMP key_set=%d " |
| 432 | "tx_pn=%02x%02x%02x%02x%02x%02x " |
| 433 | "rx_pn=%02x%02x%02x%02x%02x%02x " |
| 434 | "format_errors=%d replays=%d decrypt_errors=%d\n", |
| 435 | ccmp->key_idx, ccmp->key_set, |
| 436 | MAC_ARG(ccmp->tx_pn), MAC_ARG(ccmp->rx_pn), |
| 437 | ccmp->dot11RSNAStatsCCMPFormatErrors, |
| 438 | ccmp->dot11RSNAStatsCCMPReplays, |
| 439 | ccmp->dot11RSNAStatsCCMPDecryptErrors); |
| 440 | |
| 441 | return p; |
| 442 | } |
| 443 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 444 | static struct ieee80211_crypto_ops ieee80211_crypt_ccmp = { |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 445 | .name = "CCMP", |
| 446 | .init = ieee80211_ccmp_init, |
| 447 | .deinit = ieee80211_ccmp_deinit, |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 448 | .build_iv = ieee80211_ccmp_hdr, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 449 | .encrypt_mpdu = ieee80211_ccmp_encrypt, |
| 450 | .decrypt_mpdu = ieee80211_ccmp_decrypt, |
| 451 | .encrypt_msdu = NULL, |
| 452 | .decrypt_msdu = NULL, |
| 453 | .set_key = ieee80211_ccmp_set_key, |
| 454 | .get_key = ieee80211_ccmp_get_key, |
| 455 | .print_stats = ieee80211_ccmp_print_stats, |
James Ketrenos | 1264fc0 | 2005-09-21 11:54:53 -0500 | [diff] [blame] | 456 | .extra_mpdu_prefix_len = CCMP_HDR_LEN, |
| 457 | .extra_mpdu_postfix_len = CCMP_MIC_LEN, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 458 | .owner = THIS_MODULE, |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 459 | }; |
| 460 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 461 | static int __init ieee80211_crypto_ccmp_init(void) |
| 462 | { |
| 463 | return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp); |
| 464 | } |
| 465 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 466 | static void __exit ieee80211_crypto_ccmp_exit(void) |
| 467 | { |
| 468 | ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp); |
| 469 | } |
| 470 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 471 | module_init(ieee80211_crypto_ccmp_init); |
| 472 | module_exit(ieee80211_crypto_ccmp_exit); |