mac80211: fix use-after-free in defragmentation
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / mac80211 / aes_ccm.c
CommitLineData
f0706e82
JB
1/*
2 * Copyright 2003-2004, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
172589cc 10#include <linux/kernel.h>
f0706e82
JB
11#include <linux/types.h>
12#include <linux/crypto.h>
13#include <linux/err.h>
0cd20a27 14#include <crypto/aes.h>
f0706e82
JB
15
16#include <net/mac80211.h>
2c8dccc7 17#include "key.h"
f0706e82
JB
18#include "aes_ccm.h"
19
5fdae6b3 20static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *scratch, u8 *a)
f0706e82
JB
21{
22 int i;
5fdae6b3 23 u8 *b_0, *aad, *b, *s_0;
f0706e82 24
0cd20a27
JB
25 b_0 = scratch + 3 * AES_BLOCK_SIZE;
26 aad = scratch + 4 * AES_BLOCK_SIZE;
5fdae6b3 27 b = scratch;
0cd20a27 28 s_0 = scratch + AES_BLOCK_SIZE;
5fdae6b3
HH
29
30 crypto_cipher_encrypt_one(tfm, b, b_0);
f0706e82
JB
31
32 /* Extra Authenticate-only data (always two AES blocks) */
0cd20a27 33 for (i = 0; i < AES_BLOCK_SIZE; i++)
f0706e82 34 aad[i] ^= b[i];
5fdae6b3 35 crypto_cipher_encrypt_one(tfm, b, aad);
f0706e82 36
0cd20a27 37 aad += AES_BLOCK_SIZE;
f0706e82 38
0cd20a27 39 for (i = 0; i < AES_BLOCK_SIZE; i++)
f0706e82 40 aad[i] ^= b[i];
5fdae6b3 41 crypto_cipher_encrypt_one(tfm, a, aad);
f0706e82
JB
42
43 /* Mask out bits from auth-only-b_0 */
44 b_0[0] &= 0x07;
45
46 /* S_0 is used to encrypt T (= MIC) */
47 b_0[14] = 0;
48 b_0[15] = 0;
5fdae6b3 49 crypto_cipher_encrypt_one(tfm, s_0, b_0);
f0706e82
JB
50}
51
52
53void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
feccb466 54 u8 *data, size_t data_len,
f0706e82
JB
55 u8 *cdata, u8 *mic)
56{
57 int i, j, last_len, num_blocks;
0915cba3 58 u8 *pos, *cpos, *b, *s_0, *e, *b_0;
f0706e82
JB
59
60 b = scratch;
0cd20a27
JB
61 s_0 = scratch + AES_BLOCK_SIZE;
62 e = scratch + 2 * AES_BLOCK_SIZE;
63 b_0 = scratch + 3 * AES_BLOCK_SIZE;
f0706e82 64
0cd20a27
JB
65 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
66 last_len = data_len % AES_BLOCK_SIZE;
5fdae6b3 67 aes_ccm_prepare(tfm, scratch, b);
f0706e82
JB
68
69 /* Process payload blocks */
70 pos = data;
71 cpos = cdata;
72 for (j = 1; j <= num_blocks; j++) {
73 int blen = (j == num_blocks && last_len) ?
0cd20a27 74 last_len : AES_BLOCK_SIZE;
f0706e82
JB
75
76 /* Authentication followed by encryption */
77 for (i = 0; i < blen; i++)
78 b[i] ^= pos[i];
5fdae6b3 79 crypto_cipher_encrypt_one(tfm, b, b);
f0706e82
JB
80
81 b_0[14] = (j >> 8) & 0xff;
82 b_0[15] = j & 0xff;
5fdae6b3 83 crypto_cipher_encrypt_one(tfm, e, b_0);
f0706e82
JB
84 for (i = 0; i < blen; i++)
85 *cpos++ = *pos++ ^ e[i];
86 }
87
88 for (i = 0; i < CCMP_MIC_LEN; i++)
89 mic[i] = b[i] ^ s_0[i];
90}
91
92
93int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
feccb466 94 u8 *cdata, size_t data_len, u8 *mic, u8 *data)
f0706e82
JB
95{
96 int i, j, last_len, num_blocks;
0915cba3 97 u8 *pos, *cpos, *b, *s_0, *a, *b_0;
f0706e82
JB
98
99 b = scratch;
0cd20a27
JB
100 s_0 = scratch + AES_BLOCK_SIZE;
101 a = scratch + 2 * AES_BLOCK_SIZE;
102 b_0 = scratch + 3 * AES_BLOCK_SIZE;
f0706e82 103
0cd20a27
JB
104 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
105 last_len = data_len % AES_BLOCK_SIZE;
5fdae6b3 106 aes_ccm_prepare(tfm, scratch, a);
f0706e82
JB
107
108 /* Process payload blocks */
109 cpos = cdata;
110 pos = data;
111 for (j = 1; j <= num_blocks; j++) {
112 int blen = (j == num_blocks && last_len) ?
0cd20a27 113 last_len : AES_BLOCK_SIZE;
f0706e82
JB
114
115 /* Decryption followed by authentication */
116 b_0[14] = (j >> 8) & 0xff;
117 b_0[15] = j & 0xff;
5fdae6b3 118 crypto_cipher_encrypt_one(tfm, b, b_0);
f0706e82
JB
119 for (i = 0; i < blen; i++) {
120 *pos = *cpos++ ^ b[i];
121 a[i] ^= *pos++;
122 }
5fdae6b3 123 crypto_cipher_encrypt_one(tfm, a, a);
f0706e82
JB
124 }
125
126 for (i = 0; i < CCMP_MIC_LEN; i++) {
127 if ((mic[i] ^ s_0[i]) != a[i])
128 return -1;
129 }
130
131 return 0;
132}
133
134
988c0f72 135struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[])
f0706e82
JB
136{
137 struct crypto_cipher *tfm;
138
139 tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
1ac62ba7
BH
140 if (!IS_ERR(tfm))
141 crypto_cipher_setkey(tfm, key, ALG_CCMP_KEY_LEN);
f0706e82
JB
142
143 return tfm;
144}
145
146
147void ieee80211_aes_key_free(struct crypto_cipher *tfm)
148{
ffa56e54 149 crypto_free_cipher(tfm);
f0706e82 150}