michael_mic.c 3.4 KB

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