michael.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <asm/unaligned.h>
  12. #include "michael.h"
  13. static void michael_block(struct michael_mic_ctx *mctx, u32 val)
  14. {
  15. mctx->l ^= val;
  16. mctx->r ^= rol32(mctx->l, 17);
  17. mctx->l += mctx->r;
  18. mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) |
  19. ((mctx->l & 0x00ff00ff) << 8);
  20. mctx->l += mctx->r;
  21. mctx->r ^= rol32(mctx->l, 3);
  22. mctx->l += mctx->r;
  23. mctx->r ^= ror32(mctx->l, 2);
  24. mctx->l += mctx->r;
  25. }
  26. static void michael_mic_hdr(struct michael_mic_ctx *mctx,
  27. u8 *key, u8 *da, u8 *sa, u8 priority)
  28. {
  29. mctx->l = get_unaligned_le32(key);
  30. mctx->r = get_unaligned_le32(key + 4);
  31. /*
  32. * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC
  33. * calculation, but it is _not_ transmitted
  34. */
  35. michael_block(mctx, get_unaligned_le32(da));
  36. michael_block(mctx, get_unaligned_le16(&da[4]) |
  37. (get_unaligned_le16(sa) << 16));
  38. michael_block(mctx, get_unaligned_le32(&sa[2]));
  39. michael_block(mctx, priority);
  40. }
  41. void michael_mic(u8 *key, u8 *da, u8 *sa, u8 priority,
  42. u8 *data, size_t data_len, u8 *mic)
  43. {
  44. u32 val;
  45. size_t block, blocks, left;
  46. struct michael_mic_ctx mctx;
  47. michael_mic_hdr(&mctx, key, da, sa, priority);
  48. /* Real data */
  49. blocks = data_len / 4;
  50. left = data_len % 4;
  51. for (block = 0; block < blocks; block++)
  52. michael_block(&mctx, get_unaligned_le32(&data[block * 4]));
  53. /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make
  54. * total length a multiple of 4. */
  55. val = 0x5a;
  56. while (left > 0) {
  57. val <<= 8;
  58. left--;
  59. val |= data[blocks * 4 + left];
  60. }
  61. michael_block(&mctx, val);
  62. michael_block(&mctx, 0);
  63. put_unaligned_le32(mctx.l, mic);
  64. put_unaligned_le32(mctx.r, mic + 4);
  65. }