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 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 12 | #include <linux/err.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/random.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/if_ether.h> |
| 20 | #include <linux/if_arp.h> |
| 21 | #include <asm/string.h> |
| 22 | #include <linux/wireless.h> |
| 23 | |
| 24 | #include <net/ieee80211.h> |
| 25 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 26 | #include <linux/crypto.h> |
| 27 | #include <asm/scatterlist.h> |
| 28 | |
| 29 | MODULE_AUTHOR("Jouni Malinen"); |
| 30 | MODULE_DESCRIPTION("Host AP crypt: CCMP"); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | |
| 33 | #define AES_BLOCK_LEN 16 |
| 34 | #define CCMP_HDR_LEN 8 |
| 35 | #define CCMP_MIC_LEN 8 |
| 36 | #define CCMP_TK_LEN 16 |
| 37 | #define CCMP_PN_LEN 6 |
| 38 | |
| 39 | struct ieee80211_ccmp_data { |
| 40 | u8 key[CCMP_TK_LEN]; |
| 41 | int key_set; |
| 42 | |
| 43 | u8 tx_pn[CCMP_PN_LEN]; |
| 44 | u8 rx_pn[CCMP_PN_LEN]; |
| 45 | |
| 46 | u32 dot11RSNAStatsCCMPFormatErrors; |
| 47 | u32 dot11RSNAStatsCCMPReplays; |
| 48 | u32 dot11RSNAStatsCCMPDecryptErrors; |
| 49 | |
| 50 | int key_idx; |
| 51 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 52 | struct crypto_cipher *tfm; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 53 | |
| 54 | /* scratch buffers for virt_to_page() (crypto API) */ |
| 55 | u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN], |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 56 | tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN]; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 57 | u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN]; |
| 58 | }; |
| 59 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 60 | static inline void ieee80211_ccmp_aes_encrypt(struct crypto_cipher *tfm, |
| 61 | const u8 pt[16], u8 ct[16]) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 62 | { |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 63 | crypto_cipher_encrypt_one(tfm, ct, pt); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 64 | } |
| 65 | |
James Ketrenos | 6eb6edf | 2005-09-22 10:34:15 +0000 | [diff] [blame] | 66 | static void *ieee80211_ccmp_init(int key_idx) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 67 | { |
| 68 | struct ieee80211_ccmp_data *priv; |
| 69 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 70 | priv = kzalloc(sizeof(*priv), GFP_ATOMIC); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 71 | if (priv == NULL) |
| 72 | goto fail; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 73 | priv->key_idx = key_idx; |
| 74 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 75 | priv->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC); |
| 76 | if (IS_ERR(priv->tfm)) { |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 77 | printk(KERN_DEBUG "ieee80211_crypt_ccmp: could not allocate " |
| 78 | "crypto API aes\n"); |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 79 | priv->tfm = NULL; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 80 | goto fail; |
| 81 | } |
| 82 | |
| 83 | return priv; |
| 84 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 85 | fail: |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 86 | if (priv) { |
| 87 | if (priv->tfm) |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 88 | crypto_free_cipher(priv->tfm); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 89 | kfree(priv); |
| 90 | } |
| 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 95 | static void ieee80211_ccmp_deinit(void *priv) |
| 96 | { |
| 97 | struct ieee80211_ccmp_data *_priv = priv; |
| 98 | if (_priv && _priv->tfm) |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 99 | crypto_free_cipher(_priv->tfm); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 100 | kfree(priv); |
| 101 | } |
| 102 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 103 | static inline void xor_block(u8 * b, u8 * a, size_t len) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 104 | { |
| 105 | int i; |
| 106 | for (i = 0; i < len; i++) |
| 107 | b[i] ^= a[i]; |
| 108 | } |
| 109 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 110 | static void ccmp_init_blocks(struct crypto_cipher *tfm, |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 111 | struct ieee80211_hdr_4addr *hdr, |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 112 | u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 113 | { |
| 114 | u8 *pos, qc = 0; |
| 115 | size_t aad_len; |
| 116 | u16 fc; |
| 117 | int a4_included, qc_included; |
| 118 | u8 aad[2 * AES_BLOCK_LEN]; |
| 119 | |
| 120 | fc = le16_to_cpu(hdr->frame_ctl); |
| 121 | a4_included = ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == |
| 122 | (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)); |
| 123 | qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) && |
Zhu Yi | 0df7861 | 2006-02-28 15:10:36 +0800 | [diff] [blame] | 124 | (WLAN_FC_GET_STYPE(fc) & IEEE80211_STYPE_QOS_DATA)); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 125 | aad_len = 22; |
| 126 | if (a4_included) |
| 127 | aad_len += 6; |
| 128 | if (qc_included) { |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 129 | pos = (u8 *) & hdr->addr4; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 130 | 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 Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 156 | aad[0] = 0; /* aad_len >> 8 */ |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 157 | 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); |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 161 | pos = (u8 *) & hdr->seq_ctl; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 162 | aad[22] = pos[0] & 0x0f; |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 163 | aad[23] = 0; /* all bits masked */ |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 164 | 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 */ |
| 173 | ieee80211_ccmp_aes_encrypt(tfm, b0, auth); |
| 174 | xor_block(auth, aad, AES_BLOCK_LEN); |
| 175 | ieee80211_ccmp_aes_encrypt(tfm, auth, auth); |
| 176 | xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN); |
| 177 | ieee80211_ccmp_aes_encrypt(tfm, auth, auth); |
| 178 | b0[0] &= 0x07; |
| 179 | b0[14] = b0[15] = 0; |
| 180 | ieee80211_ccmp_aes_encrypt(tfm, b0, s0); |
| 181 | } |
| 182 | |
Zhu Yi | 9184d93 | 2006-01-19 16:22:32 +0800 | [diff] [blame] | 183 | static int ieee80211_ccmp_hdr(struct sk_buff *skb, int hdr_len, |
| 184 | u8 *aeskey, int keylen, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 185 | { |
| 186 | struct ieee80211_ccmp_data *key = priv; |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 187 | int i; |
| 188 | u8 *pos; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 189 | |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 190 | if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 191 | return -1; |
| 192 | |
Zhu Yi | 9184d93 | 2006-01-19 16:22:32 +0800 | [diff] [blame] | 193 | if (aeskey != NULL && keylen >= CCMP_TK_LEN) |
| 194 | memcpy(aeskey, key->key, CCMP_TK_LEN); |
| 195 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 196 | pos = skb_push(skb, CCMP_HDR_LEN); |
| 197 | memmove(pos, pos + CCMP_HDR_LEN, hdr_len); |
| 198 | pos += hdr_len; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 199 | |
| 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 Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 211 | *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 212 | *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 Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 217 | return CCMP_HDR_LEN; |
| 218 | } |
| 219 | |
| 220 | static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv) |
| 221 | { |
| 222 | struct ieee80211_ccmp_data *key = priv; |
| 223 | int data_len, i, blocks, last, len; |
| 224 | u8 *pos, *mic; |
| 225 | struct ieee80211_hdr_4addr *hdr; |
| 226 | 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; |
Zhu Yi | 9184d93 | 2006-01-19 16:22:32 +0800 | [diff] [blame] | 235 | len = ieee80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv); |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 236 | if (len < 0) |
| 237 | return -1; |
| 238 | |
| 239 | pos = skb->data + hdr_len + CCMP_HDR_LEN; |
| 240 | mic = skb_put(skb, CCMP_MIC_LEN); |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 241 | hdr = (struct ieee80211_hdr_4addr *)skb->data; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 242 | ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0); |
| 243 | |
| 244 | blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN; |
| 245 | last = data_len % AES_BLOCK_LEN; |
| 246 | |
| 247 | for (i = 1; i <= blocks; i++) { |
| 248 | len = (i == blocks && last) ? last : AES_BLOCK_LEN; |
| 249 | /* Authentication */ |
| 250 | xor_block(b, pos, len); |
| 251 | ieee80211_ccmp_aes_encrypt(key->tfm, b, b); |
| 252 | /* Encryption, with counter */ |
| 253 | b0[14] = (i >> 8) & 0xff; |
| 254 | b0[15] = i & 0xff; |
| 255 | ieee80211_ccmp_aes_encrypt(key->tfm, b0, e); |
| 256 | xor_block(pos, e, len); |
| 257 | pos += len; |
| 258 | } |
| 259 | |
| 260 | for (i = 0; i < CCMP_MIC_LEN; i++) |
| 261 | mic[i] = b[i] ^ s0[i]; |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Zhu Yi | b4328d8 | 2006-08-21 11:33:09 +0800 | [diff] [blame] | 266 | /* |
| 267 | * deal with seq counter wrapping correctly. |
| 268 | * refer to timer_after() for jiffies wrapping handling |
| 269 | */ |
| 270 | static 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 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 287 | static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv) |
| 288 | { |
| 289 | struct ieee80211_ccmp_data *key = priv; |
| 290 | u8 keyidx, *pos; |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 291 | struct ieee80211_hdr_4addr *hdr; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 292 | 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 | |
James Ketrenos | ee34af3 | 2005-09-21 11:54:36 -0500 | [diff] [blame] | 305 | hdr = (struct ieee80211_hdr_4addr *)skb->data; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 306 | 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" |
| 311 | " flag from " MAC_FMT "\n", MAC_ARG(hdr->addr2)); |
| 312 | } |
| 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()) { |
| 324 | printk(KERN_DEBUG "CCMP: received packet from " MAC_FMT |
| 325 | " with keyid=%d that does not have a configured" |
| 326 | " key\n", MAC_ARG(hdr->addr2), keyidx); |
| 327 | } |
| 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 Yi | b4328d8 | 2006-08-21 11:33:09 +0800 | [diff] [blame] | 339 | if (ccmp_replay_check(pn, key->rx_pn)) { |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 340 | if (net_ratelimit()) { |
| 341 | printk(KERN_DEBUG "CCMP: replay detected: STA=" MAC_FMT |
| 342 | " previous PN %02x%02x%02x%02x%02x%02x " |
| 343 | "received PN %02x%02x%02x%02x%02x%02x\n", |
| 344 | MAC_ARG(hdr->addr2), MAC_ARG(key->rx_pn), |
| 345 | MAC_ARG(pn)); |
| 346 | } |
| 347 | key->dot11RSNAStatsCCMPReplays++; |
| 348 | return -4; |
| 349 | } |
| 350 | |
| 351 | ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b); |
| 352 | xor_block(mic, b, CCMP_MIC_LEN); |
| 353 | |
| 354 | blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN; |
| 355 | last = data_len % AES_BLOCK_LEN; |
| 356 | |
| 357 | for (i = 1; i <= blocks; i++) { |
| 358 | len = (i == blocks && last) ? last : AES_BLOCK_LEN; |
| 359 | /* Decrypt, with counter */ |
| 360 | b0[14] = (i >> 8) & 0xff; |
| 361 | b0[15] = i & 0xff; |
| 362 | ieee80211_ccmp_aes_encrypt(key->tfm, b0, b); |
| 363 | xor_block(pos, b, len); |
| 364 | /* Authentication */ |
| 365 | xor_block(a, pos, len); |
| 366 | ieee80211_ccmp_aes_encrypt(key->tfm, a, a); |
| 367 | pos += len; |
| 368 | } |
| 369 | |
| 370 | if (memcmp(mic, a, CCMP_MIC_LEN) != 0) { |
| 371 | if (net_ratelimit()) { |
| 372 | printk(KERN_DEBUG "CCMP: decrypt failed: STA=" |
| 373 | MAC_FMT "\n", MAC_ARG(hdr->addr2)); |
| 374 | } |
| 375 | key->dot11RSNAStatsCCMPDecryptErrors++; |
| 376 | return -5; |
| 377 | } |
| 378 | |
| 379 | memcpy(key->rx_pn, pn, CCMP_PN_LEN); |
| 380 | |
| 381 | /* Remove hdr and MIC */ |
| 382 | memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len); |
| 383 | skb_pull(skb, CCMP_HDR_LEN); |
| 384 | skb_trim(skb, skb->len - CCMP_MIC_LEN); |
| 385 | |
| 386 | return keyidx; |
| 387 | } |
| 388 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 389 | 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] | 390 | { |
| 391 | struct ieee80211_ccmp_data *data = priv; |
| 392 | int keyidx; |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 393 | struct crypto_cipher *tfm = data->tfm; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 394 | |
| 395 | keyidx = data->key_idx; |
| 396 | memset(data, 0, sizeof(*data)); |
| 397 | data->key_idx = keyidx; |
| 398 | data->tfm = tfm; |
| 399 | if (len == CCMP_TK_LEN) { |
| 400 | memcpy(data->key, key, CCMP_TK_LEN); |
| 401 | data->key_set = 1; |
| 402 | if (seq) { |
| 403 | data->rx_pn[0] = seq[5]; |
| 404 | data->rx_pn[1] = seq[4]; |
| 405 | data->rx_pn[2] = seq[3]; |
| 406 | data->rx_pn[3] = seq[2]; |
| 407 | data->rx_pn[4] = seq[1]; |
| 408 | data->rx_pn[5] = seq[0]; |
| 409 | } |
| 410 | crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN); |
| 411 | } else if (len == 0) |
| 412 | data->key_set = 0; |
| 413 | else |
| 414 | return -1; |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 419 | 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] | 420 | { |
| 421 | struct ieee80211_ccmp_data *data = priv; |
| 422 | |
| 423 | if (len < CCMP_TK_LEN) |
| 424 | return -1; |
| 425 | |
| 426 | if (!data->key_set) |
| 427 | return 0; |
| 428 | memcpy(key, data->key, CCMP_TK_LEN); |
| 429 | |
| 430 | if (seq) { |
| 431 | seq[0] = data->tx_pn[5]; |
| 432 | seq[1] = data->tx_pn[4]; |
| 433 | seq[2] = data->tx_pn[3]; |
| 434 | seq[3] = data->tx_pn[2]; |
| 435 | seq[4] = data->tx_pn[1]; |
| 436 | seq[5] = data->tx_pn[0]; |
| 437 | } |
| 438 | |
| 439 | return CCMP_TK_LEN; |
| 440 | } |
| 441 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 442 | static char *ieee80211_ccmp_print_stats(char *p, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 443 | { |
| 444 | struct ieee80211_ccmp_data *ccmp = priv; |
| 445 | p += sprintf(p, "key[%d] alg=CCMP key_set=%d " |
| 446 | "tx_pn=%02x%02x%02x%02x%02x%02x " |
| 447 | "rx_pn=%02x%02x%02x%02x%02x%02x " |
| 448 | "format_errors=%d replays=%d decrypt_errors=%d\n", |
| 449 | ccmp->key_idx, ccmp->key_set, |
| 450 | MAC_ARG(ccmp->tx_pn), MAC_ARG(ccmp->rx_pn), |
| 451 | ccmp->dot11RSNAStatsCCMPFormatErrors, |
| 452 | ccmp->dot11RSNAStatsCCMPReplays, |
| 453 | ccmp->dot11RSNAStatsCCMPDecryptErrors); |
| 454 | |
| 455 | return p; |
| 456 | } |
| 457 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 458 | static struct ieee80211_crypto_ops ieee80211_crypt_ccmp = { |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 459 | .name = "CCMP", |
| 460 | .init = ieee80211_ccmp_init, |
| 461 | .deinit = ieee80211_ccmp_deinit, |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 462 | .build_iv = ieee80211_ccmp_hdr, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 463 | .encrypt_mpdu = ieee80211_ccmp_encrypt, |
| 464 | .decrypt_mpdu = ieee80211_ccmp_decrypt, |
| 465 | .encrypt_msdu = NULL, |
| 466 | .decrypt_msdu = NULL, |
| 467 | .set_key = ieee80211_ccmp_set_key, |
| 468 | .get_key = ieee80211_ccmp_get_key, |
| 469 | .print_stats = ieee80211_ccmp_print_stats, |
James Ketrenos | 1264fc0 | 2005-09-21 11:54:53 -0500 | [diff] [blame] | 470 | .extra_mpdu_prefix_len = CCMP_HDR_LEN, |
| 471 | .extra_mpdu_postfix_len = CCMP_MIC_LEN, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 472 | .owner = THIS_MODULE, |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 473 | }; |
| 474 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 475 | static int __init ieee80211_crypto_ccmp_init(void) |
| 476 | { |
| 477 | return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp); |
| 478 | } |
| 479 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 480 | static void __exit ieee80211_crypto_ccmp_exit(void) |
| 481 | { |
| 482 | ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp); |
| 483 | } |
| 484 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 485 | module_init(ieee80211_crypto_ccmp_init); |
| 486 | module_exit(ieee80211_crypto_ccmp_exit); |