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