michael.c 2.0 KB

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