1274bfb8dSJohn W. Linville /* 2274bfb8dSJohn W. Linville * lib80211 crypt: host-based CCMP encryption implementation for lib80211 3274bfb8dSJohn W. Linville * 4274bfb8dSJohn W. Linville * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi> 5274bfb8dSJohn W. Linville * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com> 6274bfb8dSJohn W. Linville * 7274bfb8dSJohn W. Linville * This program is free software; you can redistribute it and/or modify 8274bfb8dSJohn W. Linville * it under the terms of the GNU General Public License version 2 as 9274bfb8dSJohn W. Linville * published by the Free Software Foundation. See README and COPYING for 10274bfb8dSJohn W. Linville * more details. 11274bfb8dSJohn W. Linville */ 12274bfb8dSJohn W. Linville 13274bfb8dSJohn W. Linville #include <linux/kernel.h> 14274bfb8dSJohn W. Linville #include <linux/err.h> 15274bfb8dSJohn W. Linville #include <linux/module.h> 16274bfb8dSJohn W. Linville #include <linux/init.h> 17274bfb8dSJohn W. Linville #include <linux/slab.h> 18274bfb8dSJohn W. Linville #include <linux/random.h> 19274bfb8dSJohn W. Linville #include <linux/skbuff.h> 20274bfb8dSJohn W. Linville #include <linux/netdevice.h> 21274bfb8dSJohn W. Linville #include <linux/if_ether.h> 22274bfb8dSJohn W. Linville #include <linux/if_arp.h> 23274bfb8dSJohn W. Linville #include <asm/string.h> 24274bfb8dSJohn W. Linville #include <linux/wireless.h> 25274bfb8dSJohn W. Linville 26274bfb8dSJohn W. Linville #include <linux/ieee80211.h> 27274bfb8dSJohn W. Linville 28274bfb8dSJohn W. Linville #include <linux/crypto.h> 29274bfb8dSJohn W. Linville 30274bfb8dSJohn W. Linville #include <net/lib80211.h> 31274bfb8dSJohn W. Linville 32274bfb8dSJohn W. Linville MODULE_AUTHOR("Jouni Malinen"); 33274bfb8dSJohn W. Linville MODULE_DESCRIPTION("Host AP crypt: CCMP"); 34274bfb8dSJohn W. Linville MODULE_LICENSE("GPL"); 35274bfb8dSJohn W. Linville 36274bfb8dSJohn W. Linville #define AES_BLOCK_LEN 16 37274bfb8dSJohn W. Linville #define CCMP_HDR_LEN 8 38274bfb8dSJohn W. Linville #define CCMP_MIC_LEN 8 39274bfb8dSJohn W. Linville #define CCMP_TK_LEN 16 40274bfb8dSJohn W. Linville #define CCMP_PN_LEN 6 41274bfb8dSJohn W. Linville 42274bfb8dSJohn W. Linville struct lib80211_ccmp_data { 43274bfb8dSJohn W. Linville u8 key[CCMP_TK_LEN]; 44274bfb8dSJohn W. Linville int key_set; 45274bfb8dSJohn W. Linville 46274bfb8dSJohn W. Linville u8 tx_pn[CCMP_PN_LEN]; 47274bfb8dSJohn W. Linville u8 rx_pn[CCMP_PN_LEN]; 48274bfb8dSJohn W. Linville 49274bfb8dSJohn W. Linville u32 dot11RSNAStatsCCMPFormatErrors; 50274bfb8dSJohn W. Linville u32 dot11RSNAStatsCCMPReplays; 51274bfb8dSJohn W. Linville u32 dot11RSNAStatsCCMPDecryptErrors; 52274bfb8dSJohn W. Linville 53274bfb8dSJohn W. Linville int key_idx; 54274bfb8dSJohn W. Linville 55274bfb8dSJohn W. Linville struct crypto_cipher *tfm; 56274bfb8dSJohn W. Linville 57274bfb8dSJohn W. Linville /* scratch buffers for virt_to_page() (crypto API) */ 58274bfb8dSJohn W. Linville u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN], 59274bfb8dSJohn W. Linville tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN]; 60274bfb8dSJohn W. Linville u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN]; 61274bfb8dSJohn W. Linville }; 62274bfb8dSJohn W. Linville 63274bfb8dSJohn W. Linville static inline void lib80211_ccmp_aes_encrypt(struct crypto_cipher *tfm, 64274bfb8dSJohn W. Linville const u8 pt[16], u8 ct[16]) 65274bfb8dSJohn W. Linville { 66274bfb8dSJohn W. Linville crypto_cipher_encrypt_one(tfm, ct, pt); 67274bfb8dSJohn W. Linville } 68274bfb8dSJohn W. Linville 69274bfb8dSJohn W. Linville static void *lib80211_ccmp_init(int key_idx) 70274bfb8dSJohn W. Linville { 71274bfb8dSJohn W. Linville struct lib80211_ccmp_data *priv; 72274bfb8dSJohn W. Linville 73274bfb8dSJohn W. Linville priv = kzalloc(sizeof(*priv), GFP_ATOMIC); 74274bfb8dSJohn W. Linville if (priv == NULL) 75274bfb8dSJohn W. Linville goto fail; 76274bfb8dSJohn W. Linville priv->key_idx = key_idx; 77274bfb8dSJohn W. Linville 78274bfb8dSJohn W. Linville priv->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC); 79274bfb8dSJohn W. Linville if (IS_ERR(priv->tfm)) { 80274bfb8dSJohn W. Linville priv->tfm = NULL; 81274bfb8dSJohn W. Linville goto fail; 82274bfb8dSJohn W. Linville } 83274bfb8dSJohn W. Linville 84274bfb8dSJohn W. Linville return priv; 85274bfb8dSJohn W. Linville 86274bfb8dSJohn W. Linville fail: 87274bfb8dSJohn W. Linville if (priv) { 88274bfb8dSJohn W. Linville if (priv->tfm) 89274bfb8dSJohn W. Linville crypto_free_cipher(priv->tfm); 90274bfb8dSJohn W. Linville kfree(priv); 91274bfb8dSJohn W. Linville } 92274bfb8dSJohn W. Linville 93274bfb8dSJohn W. Linville return NULL; 94274bfb8dSJohn W. Linville } 95274bfb8dSJohn W. Linville 96274bfb8dSJohn W. Linville static void lib80211_ccmp_deinit(void *priv) 97274bfb8dSJohn W. Linville { 98274bfb8dSJohn W. Linville struct lib80211_ccmp_data *_priv = priv; 99274bfb8dSJohn W. Linville if (_priv && _priv->tfm) 100274bfb8dSJohn W. Linville crypto_free_cipher(_priv->tfm); 101274bfb8dSJohn W. Linville kfree(priv); 102274bfb8dSJohn W. Linville } 103274bfb8dSJohn W. Linville 104274bfb8dSJohn W. Linville static inline void xor_block(u8 * b, u8 * a, size_t len) 105274bfb8dSJohn W. Linville { 106274bfb8dSJohn W. Linville int i; 107274bfb8dSJohn W. Linville for (i = 0; i < len; i++) 108274bfb8dSJohn W. Linville b[i] ^= a[i]; 109274bfb8dSJohn W. Linville } 110274bfb8dSJohn W. Linville 111274bfb8dSJohn W. Linville static void ccmp_init_blocks(struct crypto_cipher *tfm, 112274bfb8dSJohn W. Linville struct ieee80211_hdr *hdr, 113274bfb8dSJohn W. Linville u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0) 114274bfb8dSJohn W. Linville { 115274bfb8dSJohn W. Linville u8 *pos, qc = 0; 116274bfb8dSJohn W. Linville size_t aad_len; 117274bfb8dSJohn W. Linville int a4_included, qc_included; 118274bfb8dSJohn W. Linville u8 aad[2 * AES_BLOCK_LEN]; 119274bfb8dSJohn W. Linville 120274bfb8dSJohn W. Linville a4_included = ieee80211_has_a4(hdr->frame_control); 121274bfb8dSJohn W. Linville qc_included = ieee80211_is_data_qos(hdr->frame_control); 122274bfb8dSJohn W. Linville 123274bfb8dSJohn W. Linville aad_len = 22; 124274bfb8dSJohn W. Linville if (a4_included) 125274bfb8dSJohn W. Linville aad_len += 6; 126274bfb8dSJohn W. Linville if (qc_included) { 127274bfb8dSJohn W. Linville pos = (u8 *) & hdr->addr4; 128274bfb8dSJohn W. Linville if (a4_included) 129274bfb8dSJohn W. Linville pos += 6; 130274bfb8dSJohn W. Linville qc = *pos & 0x0f; 131274bfb8dSJohn W. Linville aad_len += 2; 132274bfb8dSJohn W. Linville } 133274bfb8dSJohn W. Linville 134274bfb8dSJohn W. Linville /* CCM Initial Block: 135274bfb8dSJohn W. Linville * Flag (Include authentication header, M=3 (8-octet MIC), 136274bfb8dSJohn W. Linville * L=1 (2-octet Dlen)) 137274bfb8dSJohn W. Linville * Nonce: 0x00 | A2 | PN 138274bfb8dSJohn W. Linville * Dlen */ 139274bfb8dSJohn W. Linville b0[0] = 0x59; 140274bfb8dSJohn W. Linville b0[1] = qc; 141274bfb8dSJohn W. Linville memcpy(b0 + 2, hdr->addr2, ETH_ALEN); 142274bfb8dSJohn W. Linville memcpy(b0 + 8, pn, CCMP_PN_LEN); 143274bfb8dSJohn W. Linville b0[14] = (dlen >> 8) & 0xff; 144274bfb8dSJohn W. Linville b0[15] = dlen & 0xff; 145274bfb8dSJohn W. Linville 146274bfb8dSJohn W. Linville /* AAD: 147274bfb8dSJohn W. Linville * FC with bits 4..6 and 11..13 masked to zero; 14 is always one 148274bfb8dSJohn W. Linville * A1 | A2 | A3 149274bfb8dSJohn W. Linville * SC with bits 4..15 (seq#) masked to zero 150274bfb8dSJohn W. Linville * A4 (if present) 151274bfb8dSJohn W. Linville * QC (if present) 152274bfb8dSJohn W. Linville */ 153274bfb8dSJohn W. Linville pos = (u8 *) hdr; 154274bfb8dSJohn W. Linville aad[0] = 0; /* aad_len >> 8 */ 155274bfb8dSJohn W. Linville aad[1] = aad_len & 0xff; 156274bfb8dSJohn W. Linville aad[2] = pos[0] & 0x8f; 157274bfb8dSJohn W. Linville aad[3] = pos[1] & 0xc7; 158274bfb8dSJohn W. Linville memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN); 159274bfb8dSJohn W. Linville pos = (u8 *) & hdr->seq_ctrl; 160274bfb8dSJohn W. Linville aad[22] = pos[0] & 0x0f; 161274bfb8dSJohn W. Linville aad[23] = 0; /* all bits masked */ 162274bfb8dSJohn W. Linville memset(aad + 24, 0, 8); 163274bfb8dSJohn W. Linville if (a4_included) 164274bfb8dSJohn W. Linville memcpy(aad + 24, hdr->addr4, ETH_ALEN); 165274bfb8dSJohn W. Linville if (qc_included) { 166274bfb8dSJohn W. Linville aad[a4_included ? 30 : 24] = qc; 167274bfb8dSJohn W. Linville /* rest of QC masked */ 168274bfb8dSJohn W. Linville } 169274bfb8dSJohn W. Linville 170274bfb8dSJohn W. Linville /* Start with the first block and AAD */ 171274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(tfm, b0, auth); 172274bfb8dSJohn W. Linville xor_block(auth, aad, AES_BLOCK_LEN); 173274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(tfm, auth, auth); 174274bfb8dSJohn W. Linville xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN); 175274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(tfm, auth, auth); 176274bfb8dSJohn W. Linville b0[0] &= 0x07; 177274bfb8dSJohn W. Linville b0[14] = b0[15] = 0; 178274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(tfm, b0, s0); 179274bfb8dSJohn W. Linville } 180274bfb8dSJohn W. Linville 181274bfb8dSJohn W. Linville static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len, 182274bfb8dSJohn W. Linville u8 *aeskey, int keylen, void *priv) 183274bfb8dSJohn W. Linville { 184274bfb8dSJohn W. Linville struct lib80211_ccmp_data *key = priv; 185274bfb8dSJohn W. Linville int i; 186274bfb8dSJohn W. Linville u8 *pos; 187274bfb8dSJohn W. Linville 188274bfb8dSJohn W. Linville if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len) 189274bfb8dSJohn W. Linville return -1; 190274bfb8dSJohn W. Linville 191274bfb8dSJohn W. Linville if (aeskey != NULL && keylen >= CCMP_TK_LEN) 192274bfb8dSJohn W. Linville memcpy(aeskey, key->key, CCMP_TK_LEN); 193274bfb8dSJohn W. Linville 194274bfb8dSJohn W. Linville pos = skb_push(skb, CCMP_HDR_LEN); 195274bfb8dSJohn W. Linville memmove(pos, pos + CCMP_HDR_LEN, hdr_len); 196274bfb8dSJohn W. Linville pos += hdr_len; 197274bfb8dSJohn W. Linville 198274bfb8dSJohn W. Linville i = CCMP_PN_LEN - 1; 199274bfb8dSJohn W. Linville while (i >= 0) { 200274bfb8dSJohn W. Linville key->tx_pn[i]++; 201274bfb8dSJohn W. Linville if (key->tx_pn[i] != 0) 202274bfb8dSJohn W. Linville break; 203274bfb8dSJohn W. Linville i--; 204274bfb8dSJohn W. Linville } 205274bfb8dSJohn W. Linville 206274bfb8dSJohn W. Linville *pos++ = key->tx_pn[5]; 207274bfb8dSJohn W. Linville *pos++ = key->tx_pn[4]; 208274bfb8dSJohn W. Linville *pos++ = 0; 209274bfb8dSJohn W. Linville *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ; 210274bfb8dSJohn W. Linville *pos++ = key->tx_pn[3]; 211274bfb8dSJohn W. Linville *pos++ = key->tx_pn[2]; 212274bfb8dSJohn W. Linville *pos++ = key->tx_pn[1]; 213274bfb8dSJohn W. Linville *pos++ = key->tx_pn[0]; 214274bfb8dSJohn W. Linville 215274bfb8dSJohn W. Linville return CCMP_HDR_LEN; 216274bfb8dSJohn W. Linville } 217274bfb8dSJohn W. Linville 218274bfb8dSJohn W. Linville static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv) 219274bfb8dSJohn W. Linville { 220274bfb8dSJohn W. Linville struct lib80211_ccmp_data *key = priv; 221274bfb8dSJohn W. Linville int data_len, i, blocks, last, len; 222274bfb8dSJohn W. Linville u8 *pos, *mic; 223274bfb8dSJohn W. Linville struct ieee80211_hdr *hdr; 224274bfb8dSJohn W. Linville u8 *b0 = key->tx_b0; 225274bfb8dSJohn W. Linville u8 *b = key->tx_b; 226274bfb8dSJohn W. Linville u8 *e = key->tx_e; 227274bfb8dSJohn W. Linville u8 *s0 = key->tx_s0; 228274bfb8dSJohn W. Linville 229274bfb8dSJohn W. Linville if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len) 230274bfb8dSJohn W. Linville return -1; 231274bfb8dSJohn W. Linville 232274bfb8dSJohn W. Linville data_len = skb->len - hdr_len; 233274bfb8dSJohn W. Linville len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv); 234274bfb8dSJohn W. Linville if (len < 0) 235274bfb8dSJohn W. Linville return -1; 236274bfb8dSJohn W. Linville 237274bfb8dSJohn W. Linville pos = skb->data + hdr_len + CCMP_HDR_LEN; 238274bfb8dSJohn W. Linville hdr = (struct ieee80211_hdr *)skb->data; 239274bfb8dSJohn W. Linville ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0); 240274bfb8dSJohn W. Linville 241274bfb8dSJohn W. Linville blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN); 242274bfb8dSJohn W. Linville last = data_len % AES_BLOCK_LEN; 243274bfb8dSJohn W. Linville 244274bfb8dSJohn W. Linville for (i = 1; i <= blocks; i++) { 245274bfb8dSJohn W. Linville len = (i == blocks && last) ? last : AES_BLOCK_LEN; 246274bfb8dSJohn W. Linville /* Authentication */ 247274bfb8dSJohn W. Linville xor_block(b, pos, len); 248274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(key->tfm, b, b); 249274bfb8dSJohn W. Linville /* Encryption, with counter */ 250274bfb8dSJohn W. Linville b0[14] = (i >> 8) & 0xff; 251274bfb8dSJohn W. Linville b0[15] = i & 0xff; 252274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(key->tfm, b0, e); 253274bfb8dSJohn W. Linville xor_block(pos, e, len); 254274bfb8dSJohn W. Linville pos += len; 255274bfb8dSJohn W. Linville } 256274bfb8dSJohn W. Linville 257d0833a6aSAndriy Tkachuk mic = skb_put(skb, CCMP_MIC_LEN); 258274bfb8dSJohn W. Linville for (i = 0; i < CCMP_MIC_LEN; i++) 259274bfb8dSJohn W. Linville mic[i] = b[i] ^ s0[i]; 260274bfb8dSJohn W. Linville 261274bfb8dSJohn W. Linville return 0; 262274bfb8dSJohn W. Linville } 263274bfb8dSJohn W. Linville 264274bfb8dSJohn W. Linville /* 265274bfb8dSJohn W. Linville * deal with seq counter wrapping correctly. 266274bfb8dSJohn W. Linville * refer to timer_after() for jiffies wrapping handling 267274bfb8dSJohn W. Linville */ 268274bfb8dSJohn W. Linville static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o) 269274bfb8dSJohn W. Linville { 270274bfb8dSJohn W. Linville u32 iv32_n, iv16_n; 271274bfb8dSJohn W. Linville u32 iv32_o, iv16_o; 272274bfb8dSJohn W. Linville 273274bfb8dSJohn W. Linville iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3]; 274274bfb8dSJohn W. Linville iv16_n = (pn_n[4] << 8) | pn_n[5]; 275274bfb8dSJohn W. Linville 276274bfb8dSJohn W. Linville iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3]; 277274bfb8dSJohn W. Linville iv16_o = (pn_o[4] << 8) | pn_o[5]; 278274bfb8dSJohn W. Linville 279274bfb8dSJohn W. Linville if ((s32)iv32_n - (s32)iv32_o < 0 || 280274bfb8dSJohn W. Linville (iv32_n == iv32_o && iv16_n <= iv16_o)) 281274bfb8dSJohn W. Linville return 1; 282274bfb8dSJohn W. Linville return 0; 283274bfb8dSJohn W. Linville } 284274bfb8dSJohn W. Linville 285274bfb8dSJohn W. Linville static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv) 286274bfb8dSJohn W. Linville { 287274bfb8dSJohn W. Linville struct lib80211_ccmp_data *key = priv; 288274bfb8dSJohn W. Linville u8 keyidx, *pos; 289274bfb8dSJohn W. Linville struct ieee80211_hdr *hdr; 290274bfb8dSJohn W. Linville u8 *b0 = key->rx_b0; 291274bfb8dSJohn W. Linville u8 *b = key->rx_b; 292274bfb8dSJohn W. Linville u8 *a = key->rx_a; 293274bfb8dSJohn W. Linville u8 pn[6]; 294274bfb8dSJohn W. Linville int i, blocks, last, len; 295274bfb8dSJohn W. Linville size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN; 296274bfb8dSJohn W. Linville u8 *mic = skb->data + skb->len - CCMP_MIC_LEN; 297274bfb8dSJohn W. Linville 298274bfb8dSJohn W. Linville if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) { 299274bfb8dSJohn W. Linville key->dot11RSNAStatsCCMPFormatErrors++; 300274bfb8dSJohn W. Linville return -1; 301274bfb8dSJohn W. Linville } 302274bfb8dSJohn W. Linville 303274bfb8dSJohn W. Linville hdr = (struct ieee80211_hdr *)skb->data; 304274bfb8dSJohn W. Linville pos = skb->data + hdr_len; 305274bfb8dSJohn W. Linville keyidx = pos[3]; 306274bfb8dSJohn W. Linville if (!(keyidx & (1 << 5))) { 307e87cc472SJoe Perches net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n", 308e87cc472SJoe Perches hdr->addr2); 309274bfb8dSJohn W. Linville key->dot11RSNAStatsCCMPFormatErrors++; 310274bfb8dSJohn W. Linville return -2; 311274bfb8dSJohn W. Linville } 312274bfb8dSJohn W. Linville keyidx >>= 6; 313274bfb8dSJohn W. Linville if (key->key_idx != keyidx) { 314274bfb8dSJohn W. Linville printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame " 315274bfb8dSJohn W. Linville "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv); 316274bfb8dSJohn W. Linville return -6; 317274bfb8dSJohn W. Linville } 318274bfb8dSJohn W. Linville if (!key->key_set) { 319e87cc472SJoe Perches net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n", 320e87cc472SJoe Perches hdr->addr2, keyidx); 321274bfb8dSJohn W. Linville return -3; 322274bfb8dSJohn W. Linville } 323274bfb8dSJohn W. Linville 324274bfb8dSJohn W. Linville pn[0] = pos[7]; 325274bfb8dSJohn W. Linville pn[1] = pos[6]; 326274bfb8dSJohn W. Linville pn[2] = pos[5]; 327274bfb8dSJohn W. Linville pn[3] = pos[4]; 328274bfb8dSJohn W. Linville pn[4] = pos[1]; 329274bfb8dSJohn W. Linville pn[5] = pos[0]; 330274bfb8dSJohn W. Linville pos += 8; 331274bfb8dSJohn W. Linville 332274bfb8dSJohn W. Linville if (ccmp_replay_check(pn, key->rx_pn)) { 3336f16bf3bSJohn W. Linville #ifdef CONFIG_LIB80211_DEBUG 334e87cc472SJoe Perches 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", 335274bfb8dSJohn W. Linville hdr->addr2, 336274bfb8dSJohn W. Linville key->rx_pn[0], key->rx_pn[1], key->rx_pn[2], 337274bfb8dSJohn W. Linville key->rx_pn[3], key->rx_pn[4], key->rx_pn[5], 338274bfb8dSJohn W. Linville pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]); 3396f16bf3bSJohn W. Linville #endif 340274bfb8dSJohn W. Linville key->dot11RSNAStatsCCMPReplays++; 341274bfb8dSJohn W. Linville return -4; 342274bfb8dSJohn W. Linville } 343274bfb8dSJohn W. Linville 344274bfb8dSJohn W. Linville ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b); 345274bfb8dSJohn W. Linville xor_block(mic, b, CCMP_MIC_LEN); 346274bfb8dSJohn W. Linville 347274bfb8dSJohn W. Linville blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN); 348274bfb8dSJohn W. Linville last = data_len % AES_BLOCK_LEN; 349274bfb8dSJohn W. Linville 350274bfb8dSJohn W. Linville for (i = 1; i <= blocks; i++) { 351274bfb8dSJohn W. Linville len = (i == blocks && last) ? last : AES_BLOCK_LEN; 352274bfb8dSJohn W. Linville /* Decrypt, with counter */ 353274bfb8dSJohn W. Linville b0[14] = (i >> 8) & 0xff; 354274bfb8dSJohn W. Linville b0[15] = i & 0xff; 355274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(key->tfm, b0, b); 356274bfb8dSJohn W. Linville xor_block(pos, b, len); 357274bfb8dSJohn W. Linville /* Authentication */ 358274bfb8dSJohn W. Linville xor_block(a, pos, len); 359274bfb8dSJohn W. Linville lib80211_ccmp_aes_encrypt(key->tfm, a, a); 360274bfb8dSJohn W. Linville pos += len; 361274bfb8dSJohn W. Linville } 362274bfb8dSJohn W. Linville 363274bfb8dSJohn W. Linville if (memcmp(mic, a, CCMP_MIC_LEN) != 0) { 364e87cc472SJoe Perches net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM\n", 365e87cc472SJoe Perches hdr->addr2); 366274bfb8dSJohn W. Linville key->dot11RSNAStatsCCMPDecryptErrors++; 367274bfb8dSJohn W. Linville return -5; 368274bfb8dSJohn W. Linville } 369274bfb8dSJohn W. Linville 370274bfb8dSJohn W. Linville memcpy(key->rx_pn, pn, CCMP_PN_LEN); 371274bfb8dSJohn W. Linville 372274bfb8dSJohn W. Linville /* Remove hdr and MIC */ 373274bfb8dSJohn W. Linville memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len); 374274bfb8dSJohn W. Linville skb_pull(skb, CCMP_HDR_LEN); 375274bfb8dSJohn W. Linville skb_trim(skb, skb->len - CCMP_MIC_LEN); 376274bfb8dSJohn W. Linville 377274bfb8dSJohn W. Linville return keyidx; 378274bfb8dSJohn W. Linville } 379274bfb8dSJohn W. Linville 380274bfb8dSJohn W. Linville static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv) 381274bfb8dSJohn W. Linville { 382274bfb8dSJohn W. Linville struct lib80211_ccmp_data *data = priv; 383274bfb8dSJohn W. Linville int keyidx; 384274bfb8dSJohn W. Linville struct crypto_cipher *tfm = data->tfm; 385274bfb8dSJohn W. Linville 386274bfb8dSJohn W. Linville keyidx = data->key_idx; 387274bfb8dSJohn W. Linville memset(data, 0, sizeof(*data)); 388274bfb8dSJohn W. Linville data->key_idx = keyidx; 389274bfb8dSJohn W. Linville data->tfm = tfm; 390274bfb8dSJohn W. Linville if (len == CCMP_TK_LEN) { 391274bfb8dSJohn W. Linville memcpy(data->key, key, CCMP_TK_LEN); 392274bfb8dSJohn W. Linville data->key_set = 1; 393274bfb8dSJohn W. Linville if (seq) { 394274bfb8dSJohn W. Linville data->rx_pn[0] = seq[5]; 395274bfb8dSJohn W. Linville data->rx_pn[1] = seq[4]; 396274bfb8dSJohn W. Linville data->rx_pn[2] = seq[3]; 397274bfb8dSJohn W. Linville data->rx_pn[3] = seq[2]; 398274bfb8dSJohn W. Linville data->rx_pn[4] = seq[1]; 399274bfb8dSJohn W. Linville data->rx_pn[5] = seq[0]; 400274bfb8dSJohn W. Linville } 401274bfb8dSJohn W. Linville crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN); 402274bfb8dSJohn W. Linville } else if (len == 0) 403274bfb8dSJohn W. Linville data->key_set = 0; 404274bfb8dSJohn W. Linville else 405274bfb8dSJohn W. Linville return -1; 406274bfb8dSJohn W. Linville 407274bfb8dSJohn W. Linville return 0; 408274bfb8dSJohn W. Linville } 409274bfb8dSJohn W. Linville 410274bfb8dSJohn W. Linville static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv) 411274bfb8dSJohn W. Linville { 412274bfb8dSJohn W. Linville struct lib80211_ccmp_data *data = priv; 413274bfb8dSJohn W. Linville 414274bfb8dSJohn W. Linville if (len < CCMP_TK_LEN) 415274bfb8dSJohn W. Linville return -1; 416274bfb8dSJohn W. Linville 417274bfb8dSJohn W. Linville if (!data->key_set) 418274bfb8dSJohn W. Linville return 0; 419274bfb8dSJohn W. Linville memcpy(key, data->key, CCMP_TK_LEN); 420274bfb8dSJohn W. Linville 421274bfb8dSJohn W. Linville if (seq) { 422274bfb8dSJohn W. Linville seq[0] = data->tx_pn[5]; 423274bfb8dSJohn W. Linville seq[1] = data->tx_pn[4]; 424274bfb8dSJohn W. Linville seq[2] = data->tx_pn[3]; 425274bfb8dSJohn W. Linville seq[3] = data->tx_pn[2]; 426274bfb8dSJohn W. Linville seq[4] = data->tx_pn[1]; 427274bfb8dSJohn W. Linville seq[5] = data->tx_pn[0]; 428274bfb8dSJohn W. Linville } 429274bfb8dSJohn W. Linville 430274bfb8dSJohn W. Linville return CCMP_TK_LEN; 431274bfb8dSJohn W. Linville } 432274bfb8dSJohn W. Linville 433*6bbefe86SDavid Howells static void lib80211_ccmp_print_stats(struct seq_file *m, void *priv) 434274bfb8dSJohn W. Linville { 435274bfb8dSJohn W. Linville struct lib80211_ccmp_data *ccmp = priv; 436274bfb8dSJohn W. Linville 437*6bbefe86SDavid Howells seq_printf(m, 438*6bbefe86SDavid Howells "key[%d] alg=CCMP key_set=%d " 439274bfb8dSJohn W. Linville "tx_pn=%02x%02x%02x%02x%02x%02x " 440274bfb8dSJohn W. Linville "rx_pn=%02x%02x%02x%02x%02x%02x " 441274bfb8dSJohn W. Linville "format_errors=%d replays=%d decrypt_errors=%d\n", 442274bfb8dSJohn W. Linville ccmp->key_idx, ccmp->key_set, 443274bfb8dSJohn W. Linville ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2], 444274bfb8dSJohn W. Linville ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5], 445274bfb8dSJohn W. Linville ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2], 446274bfb8dSJohn W. Linville ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5], 447274bfb8dSJohn W. Linville ccmp->dot11RSNAStatsCCMPFormatErrors, 448274bfb8dSJohn W. Linville ccmp->dot11RSNAStatsCCMPReplays, 449274bfb8dSJohn W. Linville ccmp->dot11RSNAStatsCCMPDecryptErrors); 450274bfb8dSJohn W. Linville } 451274bfb8dSJohn W. Linville 452274bfb8dSJohn W. Linville static struct lib80211_crypto_ops lib80211_crypt_ccmp = { 453274bfb8dSJohn W. Linville .name = "CCMP", 454274bfb8dSJohn W. Linville .init = lib80211_ccmp_init, 455274bfb8dSJohn W. Linville .deinit = lib80211_ccmp_deinit, 456274bfb8dSJohn W. Linville .encrypt_mpdu = lib80211_ccmp_encrypt, 457274bfb8dSJohn W. Linville .decrypt_mpdu = lib80211_ccmp_decrypt, 458274bfb8dSJohn W. Linville .encrypt_msdu = NULL, 459274bfb8dSJohn W. Linville .decrypt_msdu = NULL, 460274bfb8dSJohn W. Linville .set_key = lib80211_ccmp_set_key, 461274bfb8dSJohn W. Linville .get_key = lib80211_ccmp_get_key, 462274bfb8dSJohn W. Linville .print_stats = lib80211_ccmp_print_stats, 463274bfb8dSJohn W. Linville .extra_mpdu_prefix_len = CCMP_HDR_LEN, 464274bfb8dSJohn W. Linville .extra_mpdu_postfix_len = CCMP_MIC_LEN, 465274bfb8dSJohn W. Linville .owner = THIS_MODULE, 466274bfb8dSJohn W. Linville }; 467274bfb8dSJohn W. Linville 468274bfb8dSJohn W. Linville static int __init lib80211_crypto_ccmp_init(void) 469274bfb8dSJohn W. Linville { 470274bfb8dSJohn W. Linville return lib80211_register_crypto_ops(&lib80211_crypt_ccmp); 471274bfb8dSJohn W. Linville } 472274bfb8dSJohn W. Linville 473274bfb8dSJohn W. Linville static void __exit lib80211_crypto_ccmp_exit(void) 474274bfb8dSJohn W. Linville { 475274bfb8dSJohn W. Linville lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp); 476274bfb8dSJohn W. Linville } 477274bfb8dSJohn W. Linville 478274bfb8dSJohn W. Linville module_init(lib80211_crypto_ccmp_init); 479274bfb8dSJohn W. Linville module_exit(lib80211_crypto_ccmp_exit); 480