Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 1 | /* |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 2 | * lib80211 crypt: host-based CCMP encryption implementation for lib80211 |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 3 | * |
Jouni Malinen | 85d32e7 | 2007-03-24 17:15:30 -0700 | [diff] [blame] | 4 | * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi> |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 5 | * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 6 | * |
| 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ärvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 14 | #include <linux/err.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 15 | #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. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 26 | #include <linux/ieee80211.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 27 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 28 | #include <linux/crypto.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 29 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 30 | #include <net/lib80211.h> |
| 31 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 32 | MODULE_AUTHOR("Jouni Malinen"); |
| 33 | MODULE_DESCRIPTION("Host AP crypt: CCMP"); |
| 34 | MODULE_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. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 42 | struct lib80211_ccmp_data { |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 43 | 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 Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 55 | struct crypto_cipher *tfm; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 56 | |
| 57 | /* scratch buffers for virt_to_page() (crypto API) */ |
| 58 | u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN], |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 59 | tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN]; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 60 | u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN]; |
| 61 | }; |
| 62 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 63 | static inline void lib80211_ccmp_aes_encrypt(struct crypto_cipher *tfm, |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 64 | const u8 pt[16], u8 ct[16]) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 65 | { |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 66 | crypto_cipher_encrypt_one(tfm, ct, pt); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 67 | } |
| 68 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 69 | static void *lib80211_ccmp_init(int key_idx) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 70 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 71 | struct lib80211_ccmp_data *priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 72 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 73 | priv = kzalloc(sizeof(*priv), GFP_ATOMIC); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 74 | if (priv == NULL) |
| 75 | goto fail; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 76 | priv->key_idx = key_idx; |
| 77 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 78 | priv->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC); |
| 79 | if (IS_ERR(priv->tfm)) { |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 80 | priv->tfm = NULL; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 81 | goto fail; |
| 82 | } |
| 83 | |
| 84 | return priv; |
| 85 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 86 | fail: |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 87 | if (priv) { |
| 88 | if (priv->tfm) |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 89 | crypto_free_cipher(priv->tfm); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 90 | kfree(priv); |
| 91 | } |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 96 | static void lib80211_ccmp_deinit(void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 97 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 98 | struct lib80211_ccmp_data *_priv = priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 99 | if (_priv && _priv->tfm) |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 100 | crypto_free_cipher(_priv->tfm); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 101 | kfree(priv); |
| 102 | } |
| 103 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 104 | static inline void xor_block(u8 * b, u8 * a, size_t len) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 105 | { |
| 106 | int i; |
| 107 | for (i = 0; i < len; i++) |
| 108 | b[i] ^= a[i]; |
| 109 | } |
| 110 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 111 | static void ccmp_init_blocks(struct crypto_cipher *tfm, |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 112 | struct ieee80211_hdr *hdr, |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 113 | u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 114 | { |
| 115 | u8 *pos, qc = 0; |
| 116 | size_t aad_len; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 117 | int a4_included, qc_included; |
| 118 | u8 aad[2 * AES_BLOCK_LEN]; |
| 119 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 120 | a4_included = ieee80211_has_a4(hdr->frame_control); |
| 121 | qc_included = ieee80211_is_data_qos(hdr->frame_control); |
| 122 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 123 | aad_len = 22; |
| 124 | if (a4_included) |
| 125 | aad_len += 6; |
| 126 | if (qc_included) { |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 127 | pos = (u8 *) & hdr->addr4; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 128 | if (a4_included) |
| 129 | pos += 6; |
| 130 | qc = *pos & 0x0f; |
| 131 | aad_len += 2; |
| 132 | } |
| 133 | |
| 134 | /* CCM Initial Block: |
| 135 | * Flag (Include authentication header, M=3 (8-octet MIC), |
| 136 | * L=1 (2-octet Dlen)) |
| 137 | * Nonce: 0x00 | A2 | PN |
| 138 | * Dlen */ |
| 139 | b0[0] = 0x59; |
| 140 | b0[1] = qc; |
| 141 | memcpy(b0 + 2, hdr->addr2, ETH_ALEN); |
| 142 | memcpy(b0 + 8, pn, CCMP_PN_LEN); |
| 143 | b0[14] = (dlen >> 8) & 0xff; |
| 144 | b0[15] = dlen & 0xff; |
| 145 | |
| 146 | /* AAD: |
| 147 | * FC with bits 4..6 and 11..13 masked to zero; 14 is always one |
| 148 | * A1 | A2 | A3 |
| 149 | * SC with bits 4..15 (seq#) masked to zero |
| 150 | * A4 (if present) |
| 151 | * QC (if present) |
| 152 | */ |
| 153 | pos = (u8 *) hdr; |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 154 | aad[0] = 0; /* aad_len >> 8 */ |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 155 | aad[1] = aad_len & 0xff; |
| 156 | aad[2] = pos[0] & 0x8f; |
| 157 | aad[3] = pos[1] & 0xc7; |
| 158 | memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN); |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 159 | pos = (u8 *) & hdr->seq_ctrl; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 160 | aad[22] = pos[0] & 0x0f; |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 161 | aad[23] = 0; /* all bits masked */ |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 162 | memset(aad + 24, 0, 8); |
| 163 | if (a4_included) |
| 164 | memcpy(aad + 24, hdr->addr4, ETH_ALEN); |
| 165 | if (qc_included) { |
| 166 | aad[a4_included ? 30 : 24] = qc; |
| 167 | /* rest of QC masked */ |
| 168 | } |
| 169 | |
| 170 | /* Start with the first block and AAD */ |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 171 | lib80211_ccmp_aes_encrypt(tfm, b0, auth); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 172 | xor_block(auth, aad, AES_BLOCK_LEN); |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 173 | lib80211_ccmp_aes_encrypt(tfm, auth, auth); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 174 | xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN); |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 175 | lib80211_ccmp_aes_encrypt(tfm, auth, auth); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 176 | b0[0] &= 0x07; |
| 177 | b0[14] = b0[15] = 0; |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 178 | lib80211_ccmp_aes_encrypt(tfm, b0, s0); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 179 | } |
| 180 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 181 | static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len, |
Zhu Yi | 9184d93 | 2006-01-19 16:22:32 +0800 | [diff] [blame] | 182 | u8 *aeskey, int keylen, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 183 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 184 | struct lib80211_ccmp_data *key = priv; |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 185 | int i; |
| 186 | u8 *pos; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 187 | |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 188 | if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 189 | return -1; |
| 190 | |
Zhu Yi | 9184d93 | 2006-01-19 16:22:32 +0800 | [diff] [blame] | 191 | if (aeskey != NULL && keylen >= CCMP_TK_LEN) |
| 192 | memcpy(aeskey, key->key, CCMP_TK_LEN); |
| 193 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 194 | pos = skb_push(skb, CCMP_HDR_LEN); |
| 195 | memmove(pos, pos + CCMP_HDR_LEN, hdr_len); |
| 196 | pos += hdr_len; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 197 | |
| 198 | i = CCMP_PN_LEN - 1; |
| 199 | while (i >= 0) { |
| 200 | key->tx_pn[i]++; |
| 201 | if (key->tx_pn[i] != 0) |
| 202 | break; |
| 203 | i--; |
| 204 | } |
| 205 | |
| 206 | *pos++ = key->tx_pn[5]; |
| 207 | *pos++ = key->tx_pn[4]; |
| 208 | *pos++ = 0; |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 209 | *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 210 | *pos++ = key->tx_pn[3]; |
| 211 | *pos++ = key->tx_pn[2]; |
| 212 | *pos++ = key->tx_pn[1]; |
| 213 | *pos++ = key->tx_pn[0]; |
| 214 | |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 215 | return CCMP_HDR_LEN; |
| 216 | } |
| 217 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 218 | static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv) |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 219 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 220 | struct lib80211_ccmp_data *key = priv; |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 221 | int data_len, i, blocks, last, len; |
| 222 | u8 *pos, *mic; |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 223 | struct ieee80211_hdr *hdr; |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 224 | u8 *b0 = key->tx_b0; |
| 225 | u8 *b = key->tx_b; |
| 226 | u8 *e = key->tx_e; |
| 227 | u8 *s0 = key->tx_s0; |
| 228 | |
| 229 | if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len) |
| 230 | return -1; |
| 231 | |
| 232 | data_len = skb->len - hdr_len; |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 233 | len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv); |
James Ketrenos | 31b59ea | 2005-09-21 11:58:49 -0500 | [diff] [blame] | 234 | if (len < 0) |
| 235 | return -1; |
| 236 | |
| 237 | pos = skb->data + hdr_len + CCMP_HDR_LEN; |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 238 | hdr = (struct ieee80211_hdr *)skb->data; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 239 | ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0); |
| 240 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 241 | blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 242 | last = data_len % AES_BLOCK_LEN; |
| 243 | |
| 244 | for (i = 1; i <= blocks; i++) { |
| 245 | len = (i == blocks && last) ? last : AES_BLOCK_LEN; |
| 246 | /* Authentication */ |
| 247 | xor_block(b, pos, len); |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 248 | lib80211_ccmp_aes_encrypt(key->tfm, b, b); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 249 | /* Encryption, with counter */ |
| 250 | b0[14] = (i >> 8) & 0xff; |
| 251 | b0[15] = i & 0xff; |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 252 | lib80211_ccmp_aes_encrypt(key->tfm, b0, e); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 253 | xor_block(pos, e, len); |
| 254 | pos += len; |
| 255 | } |
| 256 | |
Andriy Tkachuk | d0833a6 | 2010-02-02 15:58:53 +0200 | [diff] [blame] | 257 | mic = skb_put(skb, CCMP_MIC_LEN); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 258 | for (i = 0; i < CCMP_MIC_LEN; i++) |
| 259 | mic[i] = b[i] ^ s0[i]; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Zhu Yi | b4328d8 | 2006-08-21 11:33:09 +0800 | [diff] [blame] | 264 | /* |
| 265 | * deal with seq counter wrapping correctly. |
| 266 | * refer to timer_after() for jiffies wrapping handling |
| 267 | */ |
| 268 | static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o) |
| 269 | { |
| 270 | u32 iv32_n, iv16_n; |
| 271 | u32 iv32_o, iv16_o; |
| 272 | |
| 273 | iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3]; |
| 274 | iv16_n = (pn_n[4] << 8) | pn_n[5]; |
| 275 | |
| 276 | iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3]; |
| 277 | iv16_o = (pn_o[4] << 8) | pn_o[5]; |
| 278 | |
| 279 | if ((s32)iv32_n - (s32)iv32_o < 0 || |
| 280 | (iv32_n == iv32_o && iv16_n <= iv16_o)) |
| 281 | return 1; |
| 282 | return 0; |
| 283 | } |
| 284 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 285 | static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 286 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 287 | struct lib80211_ccmp_data *key = priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 288 | u8 keyidx, *pos; |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 289 | struct ieee80211_hdr *hdr; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 290 | u8 *b0 = key->rx_b0; |
| 291 | u8 *b = key->rx_b; |
| 292 | u8 *a = key->rx_a; |
| 293 | u8 pn[6]; |
| 294 | int i, blocks, last, len; |
| 295 | size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN; |
| 296 | u8 *mic = skb->data + skb->len - CCMP_MIC_LEN; |
| 297 | |
| 298 | if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) { |
| 299 | key->dot11RSNAStatsCCMPFormatErrors++; |
| 300 | return -1; |
| 301 | } |
| 302 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 303 | hdr = (struct ieee80211_hdr *)skb->data; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 304 | pos = skb->data + hdr_len; |
| 305 | keyidx = pos[3]; |
| 306 | if (!(keyidx & (1 << 5))) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 307 | net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n", |
| 308 | hdr->addr2); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 309 | key->dot11RSNAStatsCCMPFormatErrors++; |
| 310 | return -2; |
| 311 | } |
| 312 | keyidx >>= 6; |
| 313 | if (key->key_idx != keyidx) { |
| 314 | printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame " |
| 315 | "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv); |
| 316 | return -6; |
| 317 | } |
| 318 | if (!key->key_set) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 319 | net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n", |
| 320 | hdr->addr2, keyidx); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 321 | return -3; |
| 322 | } |
| 323 | |
| 324 | pn[0] = pos[7]; |
| 325 | pn[1] = pos[6]; |
| 326 | pn[2] = pos[5]; |
| 327 | pn[3] = pos[4]; |
| 328 | pn[4] = pos[1]; |
| 329 | pn[5] = pos[0]; |
| 330 | pos += 8; |
| 331 | |
Zhu Yi | b4328d8 | 2006-08-21 11:33:09 +0800 | [diff] [blame] | 332 | if (ccmp_replay_check(pn, key->rx_pn)) { |
John W. Linville | 6f16bf3 | 2009-03-11 11:05:25 -0400 | [diff] [blame] | 333 | #ifdef CONFIG_LIB80211_DEBUG |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 334 | net_dbg_ratelimited("CCMP: replay detected: STA=%pM previous PN %02x%02x%02x%02x%02x%02x received PN %02x%02x%02x%02x%02x%02x\n", |
| 335 | hdr->addr2, |
| 336 | key->rx_pn[0], key->rx_pn[1], key->rx_pn[2], |
| 337 | key->rx_pn[3], key->rx_pn[4], key->rx_pn[5], |
| 338 | pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]); |
John W. Linville | 6f16bf3 | 2009-03-11 11:05:25 -0400 | [diff] [blame] | 339 | #endif |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 340 | key->dot11RSNAStatsCCMPReplays++; |
| 341 | return -4; |
| 342 | } |
| 343 | |
| 344 | ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b); |
| 345 | xor_block(mic, b, CCMP_MIC_LEN); |
| 346 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 347 | blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 348 | last = data_len % AES_BLOCK_LEN; |
| 349 | |
| 350 | for (i = 1; i <= blocks; i++) { |
| 351 | len = (i == blocks && last) ? last : AES_BLOCK_LEN; |
| 352 | /* Decrypt, with counter */ |
| 353 | b0[14] = (i >> 8) & 0xff; |
| 354 | b0[15] = i & 0xff; |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 355 | lib80211_ccmp_aes_encrypt(key->tfm, b0, b); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 356 | xor_block(pos, b, len); |
| 357 | /* Authentication */ |
| 358 | xor_block(a, pos, len); |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 359 | lib80211_ccmp_aes_encrypt(key->tfm, a, a); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 360 | pos += len; |
| 361 | } |
| 362 | |
| 363 | if (memcmp(mic, a, CCMP_MIC_LEN) != 0) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 364 | net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM\n", |
| 365 | hdr->addr2); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 366 | key->dot11RSNAStatsCCMPDecryptErrors++; |
| 367 | return -5; |
| 368 | } |
| 369 | |
| 370 | memcpy(key->rx_pn, pn, CCMP_PN_LEN); |
| 371 | |
| 372 | /* Remove hdr and MIC */ |
| 373 | memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len); |
| 374 | skb_pull(skb, CCMP_HDR_LEN); |
| 375 | skb_trim(skb, skb->len - CCMP_MIC_LEN); |
| 376 | |
| 377 | return keyidx; |
| 378 | } |
| 379 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 380 | static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 381 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 382 | struct lib80211_ccmp_data *data = priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 383 | int keyidx; |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 384 | struct crypto_cipher *tfm = data->tfm; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 385 | |
| 386 | keyidx = data->key_idx; |
| 387 | memset(data, 0, sizeof(*data)); |
| 388 | data->key_idx = keyidx; |
| 389 | data->tfm = tfm; |
| 390 | if (len == CCMP_TK_LEN) { |
| 391 | memcpy(data->key, key, CCMP_TK_LEN); |
| 392 | data->key_set = 1; |
| 393 | if (seq) { |
| 394 | data->rx_pn[0] = seq[5]; |
| 395 | data->rx_pn[1] = seq[4]; |
| 396 | data->rx_pn[2] = seq[3]; |
| 397 | data->rx_pn[3] = seq[2]; |
| 398 | data->rx_pn[4] = seq[1]; |
| 399 | data->rx_pn[5] = seq[0]; |
| 400 | } |
| 401 | crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN); |
| 402 | } else if (len == 0) |
| 403 | data->key_set = 0; |
| 404 | else |
| 405 | return -1; |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 410 | static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 411 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 412 | struct lib80211_ccmp_data *data = priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 413 | |
| 414 | if (len < CCMP_TK_LEN) |
| 415 | return -1; |
| 416 | |
| 417 | if (!data->key_set) |
| 418 | return 0; |
| 419 | memcpy(key, data->key, CCMP_TK_LEN); |
| 420 | |
| 421 | if (seq) { |
| 422 | seq[0] = data->tx_pn[5]; |
| 423 | seq[1] = data->tx_pn[4]; |
| 424 | seq[2] = data->tx_pn[3]; |
| 425 | seq[3] = data->tx_pn[2]; |
| 426 | seq[4] = data->tx_pn[1]; |
| 427 | seq[5] = data->tx_pn[0]; |
| 428 | } |
| 429 | |
| 430 | return CCMP_TK_LEN; |
| 431 | } |
| 432 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 433 | static char *lib80211_ccmp_print_stats(char *p, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 434 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 435 | struct lib80211_ccmp_data *ccmp = priv; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 436 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 437 | p += sprintf(p, "key[%d] alg=CCMP key_set=%d " |
| 438 | "tx_pn=%02x%02x%02x%02x%02x%02x " |
| 439 | "rx_pn=%02x%02x%02x%02x%02x%02x " |
| 440 | "format_errors=%d replays=%d decrypt_errors=%d\n", |
| 441 | ccmp->key_idx, ccmp->key_set, |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 442 | ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2], |
| 443 | ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5], |
| 444 | ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2], |
| 445 | ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5], |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 446 | ccmp->dot11RSNAStatsCCMPFormatErrors, |
| 447 | ccmp->dot11RSNAStatsCCMPReplays, |
| 448 | ccmp->dot11RSNAStatsCCMPDecryptErrors); |
| 449 | |
| 450 | return p; |
| 451 | } |
| 452 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 453 | static struct lib80211_crypto_ops lib80211_crypt_ccmp = { |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 454 | .name = "CCMP", |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 455 | .init = lib80211_ccmp_init, |
| 456 | .deinit = lib80211_ccmp_deinit, |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 457 | .encrypt_mpdu = lib80211_ccmp_encrypt, |
| 458 | .decrypt_mpdu = lib80211_ccmp_decrypt, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 459 | .encrypt_msdu = NULL, |
| 460 | .decrypt_msdu = NULL, |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 461 | .set_key = lib80211_ccmp_set_key, |
| 462 | .get_key = lib80211_ccmp_get_key, |
| 463 | .print_stats = lib80211_ccmp_print_stats, |
James Ketrenos | 1264fc0 | 2005-09-21 11:54:53 -0500 | [diff] [blame] | 464 | .extra_mpdu_prefix_len = CCMP_HDR_LEN, |
| 465 | .extra_mpdu_postfix_len = CCMP_MIC_LEN, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 466 | .owner = THIS_MODULE, |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 467 | }; |
| 468 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 469 | static int __init lib80211_crypto_ccmp_init(void) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 470 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 471 | return lib80211_register_crypto_ops(&lib80211_crypt_ccmp); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 472 | } |
| 473 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 474 | static void __exit lib80211_crypto_ccmp_exit(void) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 475 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 476 | lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 477 | } |
| 478 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 479 | module_init(lib80211_crypto_ccmp_init); |
| 480 | module_exit(lib80211_crypto_ccmp_exit); |