Blame view

net/mac80211/michael.c 2.01 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
f0706e828   Jiri Benc   [MAC80211]: Add m...
2
3
4
  /*
   * Michael MIC implementation - optimized for TKIP MIC operations
   * Copyright 2002-2003, Instant802 Networks, Inc.
f0706e828   Jiri Benc   [MAC80211]: Add m...
5
   */
f0706e828   Jiri Benc   [MAC80211]: Add m...
6
  #include <linux/types.h>
1bd3dff54   Harvey Harrison   mac80211: michael...
7
  #include <linux/bitops.h>
8e8862b79   Harvey Harrison   mac80211: remove ...
8
  #include <linux/ieee80211.h>
1bd3dff54   Harvey Harrison   mac80211: michael...
9
  #include <asm/unaligned.h>
f0706e828   Jiri Benc   [MAC80211]: Add m...
10
11
  
  #include "michael.h"
1b19ca396   Harvey Harrison   mac80211: introdu...
12
13
14
15
16
17
18
19
20
21
22
23
24
  static void michael_block(struct michael_mic_ctx *mctx, u32 val)
  {
  	mctx->l ^= val;
  	mctx->r ^= rol32(mctx->l, 17);
  	mctx->l += mctx->r;
  	mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) |
  		   ((mctx->l & 0x00ff00ff) << 8);
  	mctx->l += mctx->r;
  	mctx->r ^= rol32(mctx->l, 3);
  	mctx->l += mctx->r;
  	mctx->r ^= ror32(mctx->l, 2);
  	mctx->l += mctx->r;
  }
8e8862b79   Harvey Harrison   mac80211: remove ...
25
26
  static void michael_mic_hdr(struct michael_mic_ctx *mctx, const u8 *key,
  			    struct ieee80211_hdr *hdr)
1b19ca396   Harvey Harrison   mac80211: introdu...
27
  {
8e8862b79   Harvey Harrison   mac80211: remove ...
28
29
30
31
32
  	u8 *da, *sa, tid;
  
  	da = ieee80211_get_DA(hdr);
  	sa = ieee80211_get_SA(hdr);
  	if (ieee80211_is_data_qos(hdr->frame_control))
a1f2ba04c   Sara Sharon   mac80211: add get...
33
  		tid = ieee80211_get_tid(hdr);
8e8862b79   Harvey Harrison   mac80211: remove ...
34
35
  	else
  		tid = 0;
1b19ca396   Harvey Harrison   mac80211: introdu...
36
37
38
39
40
41
42
43
44
45
46
  	mctx->l = get_unaligned_le32(key);
  	mctx->r = get_unaligned_le32(key + 4);
  
  	/*
  	 * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC
  	 * calculation, but it is _not_ transmitted
  	 */
  	michael_block(mctx, get_unaligned_le32(da));
  	michael_block(mctx, get_unaligned_le16(&da[4]) |
  			    (get_unaligned_le16(sa) << 16));
  	michael_block(mctx, get_unaligned_le32(&sa[2]));
8e8862b79   Harvey Harrison   mac80211: remove ...
47
  	michael_block(mctx, tid);
1b19ca396   Harvey Harrison   mac80211: introdu...
48
  }
f0706e828   Jiri Benc   [MAC80211]: Add m...
49

8e8862b79   Harvey Harrison   mac80211: remove ...
50
  void michael_mic(const u8 *key, struct ieee80211_hdr *hdr,
a7b6f0c55   Harvey Harrison   mac80211: add con...
51
  		 const u8 *data, size_t data_len, u8 *mic)
f0706e828   Jiri Benc   [MAC80211]: Add m...
52
  {
1b19ca396   Harvey Harrison   mac80211: introdu...
53
  	u32 val;
f0706e828   Jiri Benc   [MAC80211]: Add m...
54
  	size_t block, blocks, left;
1b19ca396   Harvey Harrison   mac80211: introdu...
55
  	struct michael_mic_ctx mctx;
f0706e828   Jiri Benc   [MAC80211]: Add m...
56

8e8862b79   Harvey Harrison   mac80211: remove ...
57
  	michael_mic_hdr(&mctx, key, hdr);
f0706e828   Jiri Benc   [MAC80211]: Add m...
58
59
60
61
  
  	/* Real data */
  	blocks = data_len / 4;
  	left = data_len % 4;
1b19ca396   Harvey Harrison   mac80211: introdu...
62
63
  	for (block = 0; block < blocks; block++)
  		michael_block(&mctx, get_unaligned_le32(&data[block * 4]));
f0706e828   Jiri Benc   [MAC80211]: Add m...
64
65
66
67
68
69
70
71
72
  
  	/* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make
  	 * total length a multiple of 4. */
  	val = 0x5a;
  	while (left > 0) {
  		val <<= 8;
  		left--;
  		val |= data[blocks * 4 + left];
  	}
f0706e828   Jiri Benc   [MAC80211]: Add m...
73

1b19ca396   Harvey Harrison   mac80211: introdu...
74
75
76
77
78
  	michael_block(&mctx, val);
  	michael_block(&mctx, 0);
  
  	put_unaligned_le32(mctx.l, mic);
  	put_unaligned_le32(mctx.r, mic + 4);
f0706e828   Jiri Benc   [MAC80211]: Add m...
79
  }