blob: e756ed9311647b96776d8b1d6128c03d29748677 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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
Ilpo Järvinen172589c2007-08-28 15:50:33 -070010#include <linux/kernel.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070011#include <linux/types.h>
12#include <linux/crypto.h>
13#include <linux/err.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070014
15#include <net/mac80211.h>
Johannes Berg2c8dccc2008-04-08 15:14:40 -040016#include "key.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include "aes_ccm.h"
18
19
20static void ieee80211_aes_encrypt(struct crypto_cipher *tfm,
21 const u8 pt[16], u8 ct[16])
22{
23 crypto_cipher_encrypt_one(tfm, ct, pt);
24}
25
26
27static inline void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
28 u8 *b, u8 *s_0, u8 *a)
29{
30 int i;
31
32 ieee80211_aes_encrypt(tfm, b_0, b);
33
34 /* Extra Authenticate-only data (always two AES blocks) */
35 for (i = 0; i < AES_BLOCK_LEN; i++)
36 aad[i] ^= b[i];
37 ieee80211_aes_encrypt(tfm, aad, b);
38
39 aad += AES_BLOCK_LEN;
40
41 for (i = 0; i < AES_BLOCK_LEN; i++)
42 aad[i] ^= b[i];
43 ieee80211_aes_encrypt(tfm, aad, a);
44
45 /* Mask out bits from auth-only-b_0 */
46 b_0[0] &= 0x07;
47
48 /* S_0 is used to encrypt T (= MIC) */
49 b_0[14] = 0;
50 b_0[15] = 0;
51 ieee80211_aes_encrypt(tfm, b_0, s_0);
52}
53
54
55void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
Harvey Harrisonfeccb462008-07-02 16:30:52 -070056 u8 *data, size_t data_len,
Jiri Bencf0706e82007-05-05 11:45:53 -070057 u8 *cdata, u8 *mic)
58{
59 int i, j, last_len, num_blocks;
Harvey Harrisonfeccb462008-07-02 16:30:52 -070060 u8 *pos, *cpos, *b, *s_0, *e, *b_0, *aad;
Jiri Bencf0706e82007-05-05 11:45:53 -070061
62 b = scratch;
63 s_0 = scratch + AES_BLOCK_LEN;
64 e = scratch + 2 * AES_BLOCK_LEN;
Harvey Harrisonfeccb462008-07-02 16:30:52 -070065 b_0 = scratch + 3 * AES_BLOCK_LEN;
66 aad = scratch + 4 * AES_BLOCK_LEN;
Jiri Bencf0706e82007-05-05 11:45:53 -070067
Ilpo Järvinen172589c2007-08-28 15:50:33 -070068 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -070069 last_len = data_len % AES_BLOCK_LEN;
70 aes_ccm_prepare(tfm, b_0, aad, b, s_0, b);
71
72 /* Process payload blocks */
73 pos = data;
74 cpos = cdata;
75 for (j = 1; j <= num_blocks; j++) {
76 int blen = (j == num_blocks && last_len) ?
77 last_len : AES_BLOCK_LEN;
78
79 /* Authentication followed by encryption */
80 for (i = 0; i < blen; i++)
81 b[i] ^= pos[i];
82 ieee80211_aes_encrypt(tfm, b, b);
83
84 b_0[14] = (j >> 8) & 0xff;
85 b_0[15] = j & 0xff;
86 ieee80211_aes_encrypt(tfm, b_0, e);
87 for (i = 0; i < blen; i++)
88 *cpos++ = *pos++ ^ e[i];
89 }
90
91 for (i = 0; i < CCMP_MIC_LEN; i++)
92 mic[i] = b[i] ^ s_0[i];
93}
94
95
96int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
Harvey Harrisonfeccb462008-07-02 16:30:52 -070097 u8 *cdata, size_t data_len, u8 *mic, u8 *data)
Jiri Bencf0706e82007-05-05 11:45:53 -070098{
99 int i, j, last_len, num_blocks;
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700100 u8 *pos, *cpos, *b, *s_0, *a, *b_0, *aad;
Jiri Bencf0706e82007-05-05 11:45:53 -0700101
102 b = scratch;
103 s_0 = scratch + AES_BLOCK_LEN;
104 a = scratch + 2 * AES_BLOCK_LEN;
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700105 b_0 = scratch + 3 * AES_BLOCK_LEN;
106 aad = scratch + 4 * AES_BLOCK_LEN;
Jiri Bencf0706e82007-05-05 11:45:53 -0700107
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700108 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700109 last_len = data_len % AES_BLOCK_LEN;
110 aes_ccm_prepare(tfm, b_0, aad, b, s_0, a);
111
112 /* Process payload blocks */
113 cpos = cdata;
114 pos = data;
115 for (j = 1; j <= num_blocks; j++) {
116 int blen = (j == num_blocks && last_len) ?
117 last_len : AES_BLOCK_LEN;
118
119 /* Decryption followed by authentication */
120 b_0[14] = (j >> 8) & 0xff;
121 b_0[15] = j & 0xff;
122 ieee80211_aes_encrypt(tfm, b_0, b);
123 for (i = 0; i < blen; i++) {
124 *pos = *cpos++ ^ b[i];
125 a[i] ^= *pos++;
126 }
127
128 ieee80211_aes_encrypt(tfm, a, a);
129 }
130
131 for (i = 0; i < CCMP_MIC_LEN; i++) {
132 if ((mic[i] ^ s_0[i]) != a[i])
133 return -1;
134 }
135
136 return 0;
137}
138
139
Johannes Berg988c0f72008-04-17 19:21:22 +0200140struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[])
Jiri Bencf0706e82007-05-05 11:45:53 -0700141{
142 struct crypto_cipher *tfm;
143
144 tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
145 if (IS_ERR(tfm))
146 return NULL;
147
148 crypto_cipher_setkey(tfm, key, ALG_CCMP_KEY_LEN);
149
150 return tfm;
151}
152
153
154void ieee80211_aes_key_free(struct crypto_cipher *tfm)
155{
156 if (tfm)
157 crypto_free_cipher(tfm);
158}