blob: be7614b9ed27b7827cbae7a423e5957ec1302c25 [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>
Johannes Berg0cd20a22011-07-06 22:02:14 +020014#include <crypto/aes.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015
16#include <net/mac80211.h>
Johannes Berg2c8dccc2008-04-08 15:14:40 -040017#include "key.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include "aes_ccm.h"
19
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070020static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *scratch, u8 *a)
Jiri Bencf0706e82007-05-05 11:45:53 -070021{
22 int i;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070023 u8 *b_0, *aad, *b, *s_0;
Jiri Bencf0706e82007-05-05 11:45:53 -070024
Johannes Berg0cd20a22011-07-06 22:02:14 +020025 b_0 = scratch + 3 * AES_BLOCK_SIZE;
26 aad = scratch + 4 * AES_BLOCK_SIZE;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070027 b = scratch;
Johannes Berg0cd20a22011-07-06 22:02:14 +020028 s_0 = scratch + AES_BLOCK_SIZE;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070029
30 crypto_cipher_encrypt_one(tfm, b, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070031
32 /* Extra Authenticate-only data (always two AES blocks) */
Johannes Berg0cd20a22011-07-06 22:02:14 +020033 for (i = 0; i < AES_BLOCK_SIZE; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -070034 aad[i] ^= b[i];
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070035 crypto_cipher_encrypt_one(tfm, b, aad);
Jiri Bencf0706e82007-05-05 11:45:53 -070036
Johannes Berg0cd20a22011-07-06 22:02:14 +020037 aad += AES_BLOCK_SIZE;
Jiri Bencf0706e82007-05-05 11:45:53 -070038
Johannes Berg0cd20a22011-07-06 22:02:14 +020039 for (i = 0; i < AES_BLOCK_SIZE; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -070040 aad[i] ^= b[i];
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070041 crypto_cipher_encrypt_one(tfm, a, aad);
Jiri Bencf0706e82007-05-05 11:45:53 -070042
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;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070049 crypto_cipher_encrypt_one(tfm, s_0, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070050}
51
52
53void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
Harvey Harrisonfeccb462008-07-02 16:30:52 -070054 u8 *data, size_t data_len,
Jiri Bencf0706e82007-05-05 11:45:53 -070055 u8 *cdata, u8 *mic)
56{
57 int i, j, last_len, num_blocks;
Rajkumar Manoharan0915cba2011-04-25 15:56:17 +053058 u8 *pos, *cpos, *b, *s_0, *e, *b_0;
Jiri Bencf0706e82007-05-05 11:45:53 -070059
60 b = scratch;
Johannes Berg0cd20a22011-07-06 22:02:14 +020061 s_0 = scratch + AES_BLOCK_SIZE;
62 e = scratch + 2 * AES_BLOCK_SIZE;
63 b_0 = scratch + 3 * AES_BLOCK_SIZE;
Jiri Bencf0706e82007-05-05 11:45:53 -070064
Johannes Berg0cd20a22011-07-06 22:02:14 +020065 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
66 last_len = data_len % AES_BLOCK_SIZE;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070067 aes_ccm_prepare(tfm, scratch, b);
Jiri Bencf0706e82007-05-05 11:45:53 -070068
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) ?
Johannes Berg0cd20a22011-07-06 22:02:14 +020074 last_len : AES_BLOCK_SIZE;
Jiri Bencf0706e82007-05-05 11:45:53 -070075
76 /* Authentication followed by encryption */
77 for (i = 0; i < blen; i++)
78 b[i] ^= pos[i];
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070079 crypto_cipher_encrypt_one(tfm, b, b);
Jiri Bencf0706e82007-05-05 11:45:53 -070080
81 b_0[14] = (j >> 8) & 0xff;
82 b_0[15] = j & 0xff;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070083 crypto_cipher_encrypt_one(tfm, e, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070084 for (i = 0; i < blen; i++)
85 *cpos++ = *pos++ ^ e[i];
86 }
87
Johannes Berg4325f6c2013-05-08 13:09:08 +020088 for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -070089 mic[i] = b[i] ^ s_0[i];
90}
91
92
93int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
Harvey Harrisonfeccb462008-07-02 16:30:52 -070094 u8 *cdata, size_t data_len, u8 *mic, u8 *data)
Jiri Bencf0706e82007-05-05 11:45:53 -070095{
96 int i, j, last_len, num_blocks;
Rajkumar Manoharan0915cba2011-04-25 15:56:17 +053097 u8 *pos, *cpos, *b, *s_0, *a, *b_0;
Jiri Bencf0706e82007-05-05 11:45:53 -070098
99 b = scratch;
Johannes Berg0cd20a22011-07-06 22:02:14 +0200100 s_0 = scratch + AES_BLOCK_SIZE;
101 a = scratch + 2 * AES_BLOCK_SIZE;
102 b_0 = scratch + 3 * AES_BLOCK_SIZE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700103
Johannes Berg0cd20a22011-07-06 22:02:14 +0200104 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
105 last_len = data_len % AES_BLOCK_SIZE;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -0700106 aes_ccm_prepare(tfm, scratch, a);
Jiri Bencf0706e82007-05-05 11:45:53 -0700107
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) ?
Johannes Berg0cd20a22011-07-06 22:02:14 +0200113 last_len : AES_BLOCK_SIZE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700114
115 /* Decryption followed by authentication */
116 b_0[14] = (j >> 8) & 0xff;
117 b_0[15] = j & 0xff;
Harvey Harrison5fdae6b2008-07-02 16:30:53 -0700118 crypto_cipher_encrypt_one(tfm, b, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700119 for (i = 0; i < blen; i++) {
120 *pos = *cpos++ ^ b[i];
121 a[i] ^= *pos++;
122 }
Harvey Harrison5fdae6b2008-07-02 16:30:53 -0700123 crypto_cipher_encrypt_one(tfm, a, a);
Jiri Bencf0706e82007-05-05 11:45:53 -0700124 }
125
Johannes Berg4325f6c2013-05-08 13:09:08 +0200126 for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700127 if ((mic[i] ^ s_0[i]) != a[i])
128 return -1;
129 }
130
131 return 0;
132}
133
134
Johannes Berg988c0f72008-04-17 19:21:22 +0200135struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[])
Jiri Bencf0706e82007-05-05 11:45:53 -0700136{
137 struct crypto_cipher *tfm;
138
139 tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100140 if (!IS_ERR(tfm))
Johannes Berg4325f6c2013-05-08 13:09:08 +0200141 crypto_cipher_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
Jiri Bencf0706e82007-05-05 11:45:53 -0700142
143 return tfm;
144}
145
146
147void ieee80211_aes_key_free(struct crypto_cipher *tfm)
148{
Jesper Juhlffa56e542010-11-04 22:59:56 +0100149 crypto_free_cipher(tfm);
Jiri Bencf0706e82007-05-05 11:45:53 -0700150}