michael_mic.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Cryptographic API
  3. *
  4. * Michael MIC (IEEE 802.11i/TKIP) keyed digest
  5. *
  6. * Copyright (c) 2004 Jouni Malinen <jkmaline@cc.hut.fi>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <linux/crypto.h>
  16. struct michael_mic_ctx {
  17. u8 pending[4];
  18. size_t pending_len;
  19. u32 l, r;
  20. };
  21. static inline u32 xswap(u32 val)
  22. {
  23. return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
  24. }
  25. #define michael_block(l, r) \
  26. do { \
  27. r ^= rol32(l, 17); \
  28. l += r; \
  29. r ^= xswap(l); \
  30. l += r; \
  31. r ^= rol32(l, 3); \
  32. l += r; \
  33. r ^= ror32(l, 2); \
  34. l += r; \
  35. } while (0)
  36. static inline u32 get_le32(const u8 *p)
  37. {
  38. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  39. }
  40. static inline void put_le32(u8 *p, u32 v)
  41. {
  42. p[0] = v;
  43. p[1] = v >> 8;
  44. p[2] = v >> 16;
  45. p[3] = v >> 24;
  46. }
  47. static void michael_init(void *ctx)
  48. {
  49. struct michael_mic_ctx *mctx = ctx;
  50. mctx->pending_len = 0;
  51. }
  52. static void michael_update(void *ctx, const u8 *data, unsigned int len)
  53. {
  54. struct michael_mic_ctx *mctx = ctx;
  55. if (mctx->pending_len) {
  56. int flen = 4 - mctx->pending_len;
  57. if (flen > len)
  58. flen = len;
  59. memcpy(&mctx->pending[mctx->pending_len], data, flen);
  60. mctx->pending_len += flen;
  61. data += flen;
  62. len -= flen;
  63. if (mctx->pending_len < 4)
  64. return;
  65. mctx->l ^= get_le32(mctx->pending);
  66. michael_block(mctx->l, mctx->r);
  67. mctx->pending_len = 0;
  68. }
  69. while (len >= 4) {
  70. mctx->l ^= get_le32(data);
  71. michael_block(mctx->l, mctx->r);
  72. data += 4;
  73. len -= 4;
  74. }
  75. if (len > 0) {
  76. mctx->pending_len = len;
  77. memcpy(mctx->pending, data, len);
  78. }
  79. }
  80. static void michael_final(void *ctx, u8 *out)
  81. {
  82. struct michael_mic_ctx *mctx = ctx;
  83. u8 *data = mctx->pending;
  84. /* Last block and padding (0x5a, 4..7 x 0) */
  85. switch (mctx->pending_len) {
  86. case 0:
  87. mctx->l ^= 0x5a;
  88. break;
  89. case 1:
  90. mctx->l ^= data[0] | 0x5a00;
  91. break;
  92. case 2:
  93. mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
  94. break;
  95. case 3:
  96. mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
  97. 0x5a000000;
  98. break;
  99. }
  100. michael_block(mctx->l, mctx->r);
  101. /* l ^= 0; */
  102. michael_block(mctx->l, mctx->r);
  103. put_le32(out, mctx->l);
  104. put_le32(out + 4, mctx->r);
  105. }
  106. static int michael_setkey(void *ctx, const u8 *key, unsigned int keylen,
  107. u32 *flags)
  108. {
  109. struct michael_mic_ctx *mctx = ctx;
  110. if (keylen != 8) {
  111. if (flags)
  112. *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
  113. return -EINVAL;
  114. }
  115. mctx->l = get_le32(key);
  116. mctx->r = get_le32(key + 4);
  117. return 0;
  118. }
  119. static struct crypto_alg michael_mic_alg = {
  120. .cra_name = "michael_mic",
  121. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  122. .cra_blocksize = 8,
  123. .cra_ctxsize = sizeof(struct michael_mic_ctx),
  124. .cra_module = THIS_MODULE,
  125. .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list),
  126. .cra_u = { .digest = {
  127. .dia_digestsize = 8,
  128. .dia_init = michael_init,
  129. .dia_update = michael_update,
  130. .dia_final = michael_final,
  131. .dia_setkey = michael_setkey } }
  132. };
  133. static int __init michael_mic_init(void)
  134. {
  135. return crypto_register_alg(&michael_mic_alg);
  136. }
  137. static void __exit michael_mic_exit(void)
  138. {
  139. crypto_unregister_alg(&michael_mic_alg);
  140. }
  141. module_init(michael_mic_init);
  142. module_exit(michael_mic_exit);
  143. MODULE_LICENSE("GPL v2");
  144. MODULE_DESCRIPTION("Michael MIC");
  145. MODULE_AUTHOR("Jouni Malinen <jkmaline@cc.hut.fi>");