upgrade madwifi to r1416-20060126, unite madwifi-tools and kmod-madwifi (madwifi...
[openwrt.git] / openwrt / target / linux / package / ieee80211-dscape / src / aes_ccm.c
1 /*
2 * Copyright 2003-2004, Instant802 Networks, Inc.
3 * Copyright 2005, 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
10 #include <linux/types.h>
11 #include <linux/netdevice.h>
12
13 #include <net/ieee80211.h>
14 #include "ieee80211_key.h"
15 #include "aes_ccm.h"
16
17 #include "aes.c"
18
19 static inline void aes_ccm_prepare(u32 *rk, u8 *b_0, u8 *aad, u8 *b,
20 u8 *s_0, u8 *a)
21 {
22 int i;
23
24 ieee80211_aes_encrypt(rk, b_0, b);
25
26 /* Extra Authenticate-only data (always two AES blocks) */
27 for (i = 0; i < AES_BLOCK_LEN; i++)
28 aad[i] ^= b[i];
29 ieee80211_aes_encrypt(rk, aad, b);
30
31 aad += AES_BLOCK_LEN;
32
33 for (i = 0; i < AES_BLOCK_LEN; i++)
34 aad[i] ^= b[i];
35 ieee80211_aes_encrypt(rk, aad, a);
36
37 /* Mask out bits from auth-only-b_0 */
38 b_0[0] &= 0x07;
39
40 /* S_0 is used to encrypt T (= MIC) */
41 b_0[14] = 0;
42 b_0[15] = 0;
43 ieee80211_aes_encrypt(rk, b_0, s_0);
44 }
45
46
47 void ieee80211_aes_ccm_encrypt(u32 *rk, u8 *b_0, u8 *aad, u8 *data,
48 size_t data_len, u8 *cdata, u8 *mic)
49 {
50 int i, j, last_len, num_blocks;
51 u8 *pos, *cpos;
52 u8 b[AES_BLOCK_LEN], s_0[AES_BLOCK_LEN], e[AES_BLOCK_LEN];
53
54 num_blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
55 last_len = data_len % AES_BLOCK_LEN;
56 aes_ccm_prepare(rk, b_0, aad, b, s_0, b);
57
58 /* Process payload blocks */
59 pos = data;
60 cpos = cdata;
61 for (j = 1; j <= num_blocks; j++) {
62 int blen = (j == num_blocks && last_len) ?
63 last_len : AES_BLOCK_LEN;
64
65 /* Authentication followed by encryption */
66 for (i = 0; i < blen; i++)
67 b[i] ^= pos[i];
68 ieee80211_aes_encrypt(rk, b, b);
69
70 b_0[14] = (j >> 8) & 0xff;
71 b_0[15] = j & 0xff;
72 ieee80211_aes_encrypt(rk, b_0, e);
73 for (i = 0; i < blen; i++)
74 *cpos++ = *pos++ ^ e[i];
75 }
76
77 for (i = 0; i < CCMP_MIC_LEN; i++)
78 mic[i] = b[i] ^ s_0[i];
79 }
80
81
82 int ieee80211_aes_ccm_decrypt(u32 *rk, u8 *b_0, u8 *aad, u8 *cdata,
83 size_t data_len, u8 *mic, u8 *data)
84 {
85 int i, j, last_len, num_blocks;
86 u8 *pos, *cpos;
87 u8 b[AES_BLOCK_LEN], s_0[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
88
89 num_blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
90 last_len = data_len % AES_BLOCK_LEN;
91 aes_ccm_prepare(rk, b_0, aad, b, s_0, a);
92
93 /* Process payload blocks */
94 cpos = cdata;
95 pos = data;
96 for (j = 1; j <= num_blocks; j++) {
97 int blen = (j == num_blocks && last_len) ?
98 last_len : AES_BLOCK_LEN;
99
100 /* Decryption followed by authentication */
101 b_0[14] = (j >> 8) & 0xff;
102 b_0[15] = j & 0xff;
103 ieee80211_aes_encrypt(rk, b_0, b);
104 for (i = 0; i < blen; i++) {
105 *pos = *cpos++ ^ b[i];
106 a[i] ^= *pos++;
107 }
108
109 ieee80211_aes_encrypt(rk, a, a);
110 }
111
112 for (i = 0; i < CCMP_MIC_LEN; i++) {
113 if ((mic[i] ^ s_0[i]) != a[i])
114 return -1;
115 }
116
117 return 0;
118 }
119
This page took 0.049394 seconds and 5 git commands to generate.