michael.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Michael MIC implementation - optimized for TKIP MIC operations
  3. * Copyright 2002-2003, Instant802 Networks, 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. #include <linux/types.h>
  10. #include <linux/bitops.h>
  11. #include <linux/ieee80211.h>
  12. #include <asm/unaligned.h>
  13. #include "michael.h"
  14. static void michael_block(struct michael_mic_ctx *mctx, u32 val)
  15. {
  16. mctx->l ^= val;
  17. mctx->r ^= rol32(mctx->l, 17);
  18. mctx->l += mctx->r;
  19. mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) |
  20. ((mctx->l & 0x00ff00ff) << 8);
  21. mctx->l += mctx->r;
  22. mctx->r ^= rol32(mctx->l, 3);
  23. mctx->l += mctx->r;
  24. mctx->r ^= ror32(mctx->l, 2);
  25. mctx->l += mctx->r;
  26. }
  27. static void michael_mic_hdr(struct michael_mic_ctx *mctx, const u8 *key,
  28. struct ieee80211_hdr *hdr)
  29. {
  30. u8 *da, *sa, tid;
  31. da = ieee80211_get_DA(hdr);
  32. sa = ieee80211_get_SA(hdr);
  33. if (ieee80211_is_data_qos(hdr->frame_control))
  34. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  35. else
  36. tid = 0;
  37. mctx->l = get_unaligned_le32(key);
  38. mctx->r = get_unaligned_le32(key + 4);
  39. /*
  40. * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC
  41. * calculation, but it is _not_ transmitted
  42. */
  43. michael_block(mctx, get_unaligned_le32(da));
  44. michael_block(mctx, get_unaligned_le16(&da[4]) |
  45. (get_unaligned_le16(sa) << 16));
  46. michael_block(mctx, get_unaligned_le32(&sa[2]));
  47. michael_block(mctx, tid);
  48. }
  49. void michael_mic(const u8 *key, struct ieee80211_hdr *hdr,
  50. const u8 *data, size_t data_len, u8 *mic)
  51. {
  52. u32 val;
  53. size_t block, blocks, left;
  54. struct michael_mic_ctx mctx;
  55. michael_mic_hdr(&mctx, key, hdr);
  56. /* Real data */
  57. blocks = data_len / 4;
  58. left = data_len % 4;
  59. for (block = 0; block < blocks; block++)
  60. michael_block(&mctx, get_unaligned_le32(&data[block * 4]));
  61. /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make
  62. * total length a multiple of 4. */
  63. val = 0x5a;
  64. while (left > 0) {
  65. val <<= 8;
  66. left--;
  67. val |= data[blocks * 4 + left];
  68. }
  69. michael_block(&mctx, val);
  70. michael_block(&mctx, 0);
  71. put_unaligned_le32(mctx.l, mic);
  72. put_unaligned_le32(mctx.r, mic + 4);
  73. }