1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
4 <http://rt2x00.serialmonkey.com>
10 Abstract: rt2x00 crypto specific routines.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
17 #include "rt2x00lib.h"
19 enum cipher
rt2x00crypto_key_to_cipher(struct ieee80211_key_conf
*key
)
21 switch (key
->cipher
) {
22 case WLAN_CIPHER_SUITE_WEP40
:
24 case WLAN_CIPHER_SUITE_WEP104
:
26 case WLAN_CIPHER_SUITE_TKIP
:
28 case WLAN_CIPHER_SUITE_CCMP
:
35 void rt2x00crypto_create_tx_descriptor(struct rt2x00_dev
*rt2x00dev
,
37 struct txentry_desc
*txdesc
)
39 struct ieee80211_tx_info
*tx_info
= IEEE80211_SKB_CB(skb
);
40 struct ieee80211_key_conf
*hw_key
= tx_info
->control
.hw_key
;
42 if (!rt2x00_has_cap_hw_crypto(rt2x00dev
) || !hw_key
)
45 __set_bit(ENTRY_TXD_ENCRYPT
, &txdesc
->flags
);
47 txdesc
->cipher
= rt2x00crypto_key_to_cipher(hw_key
);
49 if (hw_key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
)
50 __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE
, &txdesc
->flags
);
52 txdesc
->key_idx
= hw_key
->hw_key_idx
;
53 txdesc
->iv_offset
= txdesc
->header_length
;
54 txdesc
->iv_len
= hw_key
->iv_len
;
56 if (!(hw_key
->flags
& IEEE80211_KEY_FLAG_GENERATE_IV
))
57 __set_bit(ENTRY_TXD_ENCRYPT_IV
, &txdesc
->flags
);
59 if (!(hw_key
->flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
))
60 __set_bit(ENTRY_TXD_ENCRYPT_MMIC
, &txdesc
->flags
);
63 unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev
*rt2x00dev
,
66 struct ieee80211_tx_info
*tx_info
= IEEE80211_SKB_CB(skb
);
67 struct ieee80211_key_conf
*key
= tx_info
->control
.hw_key
;
68 unsigned int overhead
= 0;
70 if (!rt2x00_has_cap_hw_crypto(rt2x00dev
) || !key
)
74 * Extend frame length to include IV/EIV/ICV/MMIC,
75 * note that these lengths should only be added when
76 * mac80211 does not generate it.
78 overhead
+= key
->icv_len
;
80 if (!(key
->flags
& IEEE80211_KEY_FLAG_GENERATE_IV
))
81 overhead
+= key
->iv_len
;
83 if (!(key
->flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
)) {
84 if (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
)
91 void rt2x00crypto_tx_copy_iv(struct sk_buff
*skb
, struct txentry_desc
*txdesc
)
93 struct skb_frame_desc
*skbdesc
= get_skb_frame_desc(skb
);
95 if (unlikely(!txdesc
->iv_len
))
98 /* Copy IV/EIV data */
99 memcpy(skbdesc
->iv
, skb
->data
+ txdesc
->iv_offset
, txdesc
->iv_len
);
102 void rt2x00crypto_tx_remove_iv(struct sk_buff
*skb
, struct txentry_desc
*txdesc
)
104 struct skb_frame_desc
*skbdesc
= get_skb_frame_desc(skb
);
106 if (unlikely(!txdesc
->iv_len
))
109 /* Copy IV/EIV data */
110 memcpy(skbdesc
->iv
, skb
->data
+ txdesc
->iv_offset
, txdesc
->iv_len
);
112 /* Move ieee80211 header */
113 memmove(skb
->data
+ txdesc
->iv_len
, skb
->data
, txdesc
->iv_offset
);
115 /* Pull buffer to correct size */
116 skb_pull(skb
, txdesc
->iv_len
);
117 txdesc
->length
-= txdesc
->iv_len
;
119 /* IV/EIV data has officially been stripped */
120 skbdesc
->flags
|= SKBDESC_IV_STRIPPED
;
123 void rt2x00crypto_tx_insert_iv(struct sk_buff
*skb
, unsigned int header_length
)
125 struct skb_frame_desc
*skbdesc
= get_skb_frame_desc(skb
);
126 const unsigned int iv_len
=
127 ((!!(skbdesc
->iv
[0])) * 4) + ((!!(skbdesc
->iv
[1])) * 4);
129 if (!(skbdesc
->flags
& SKBDESC_IV_STRIPPED
))
132 skb_push(skb
, iv_len
);
134 /* Move ieee80211 header */
135 memmove(skb
->data
, skb
->data
+ iv_len
, header_length
);
137 /* Copy IV/EIV data */
138 memcpy(skb
->data
+ header_length
, skbdesc
->iv
, iv_len
);
140 /* IV/EIV data has returned into the frame */
141 skbdesc
->flags
&= ~SKBDESC_IV_STRIPPED
;
144 void rt2x00crypto_rx_insert_iv(struct sk_buff
*skb
,
145 unsigned int header_length
,
146 struct rxdone_entry_desc
*rxdesc
)
148 unsigned int payload_len
= rxdesc
->size
- header_length
;
149 unsigned int align
= ALIGN_SIZE(skb
, header_length
);
151 unsigned int icv_len
;
152 unsigned int transfer
= 0;
155 * WEP64/WEP128: Provides IV & ICV
156 * TKIP: Provides IV/EIV & ICV
157 * AES: Provies IV/EIV & ICV
159 switch (rxdesc
->cipher
) {
179 * Make room for new data. There are 2 possibilities
180 * either the alignment is already present between
181 * the 802.11 header and payload. In that case we
182 * we have to move the header less then the iv_len
183 * since we can use the already available l2pad bytes
185 * When the alignment must be added manually we must
186 * move the header more then iv_len since we must
187 * make room for the payload move as well.
189 if (rxdesc
->dev_flags
& RXDONE_L2PAD
) {
190 skb_push(skb
, iv_len
- align
);
191 skb_put(skb
, icv_len
);
193 /* Move ieee80211 header */
194 memmove(skb
->data
+ transfer
,
195 skb
->data
+ transfer
+ (iv_len
- align
),
197 transfer
+= header_length
;
199 skb_push(skb
, iv_len
+ align
);
201 skb_put(skb
, icv_len
- align
);
202 else if (align
> icv_len
)
203 skb_trim(skb
, rxdesc
->size
+ iv_len
+ icv_len
);
205 /* Move ieee80211 header */
206 memmove(skb
->data
+ transfer
,
207 skb
->data
+ transfer
+ iv_len
+ align
,
209 transfer
+= header_length
;
212 /* Copy IV/EIV data */
213 memcpy(skb
->data
+ transfer
, rxdesc
->iv
, iv_len
);
217 * Move payload for alignment purposes. Note that
218 * this is only needed when no l2 padding is present.
220 if (!(rxdesc
->dev_flags
& RXDONE_L2PAD
)) {
221 memmove(skb
->data
+ transfer
,
222 skb
->data
+ transfer
+ align
,
227 * NOTE: Always count the payload as transferred,
228 * even when alignment was set to zero. This is required
229 * for determining the correct offset for the ICV data.
231 transfer
+= payload_len
;
235 * AES appends 8 bytes, we can't fill the upper
236 * 4 bytes, but mac80211 doesn't care about what
237 * we provide here anyway and strips it immediately.
239 memcpy(skb
->data
+ transfer
, &rxdesc
->icv
, 4);
242 /* IV/EIV/ICV has been inserted into frame */
243 rxdesc
->size
= transfer
;
244 rxdesc
->flags
&= ~RX_FLAG_IV_STRIPPED
;