michael_mic.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Cryptographic API
  3. *
  4. * Michael MIC (IEEE 802.11i/TKIP) keyed digest
  5. *
  6. * Copyright (c) 2004 Jouni Malinen <j@w1.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 <crypto/internal/hash.h>
  13. #include <asm/byteorder.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. struct michael_mic_ctx {
  19. u32 l, r;
  20. };
  21. struct michael_mic_desc_ctx {
  22. u8 pending[4];
  23. size_t pending_len;
  24. u32 l, r;
  25. };
  26. static inline u32 xswap(u32 val)
  27. {
  28. return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
  29. }
  30. #define michael_block(l, r) \
  31. do { \
  32. r ^= rol32(l, 17); \
  33. l += r; \
  34. r ^= xswap(l); \
  35. l += r; \
  36. r ^= rol32(l, 3); \
  37. l += r; \
  38. r ^= ror32(l, 2); \
  39. l += r; \
  40. } while (0)
  41. static int michael_init(struct shash_desc *desc)
  42. {
  43. struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  44. struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
  45. mctx->pending_len = 0;
  46. mctx->l = ctx->l;
  47. mctx->r = ctx->r;
  48. return 0;
  49. }
  50. static int michael_update(struct shash_desc *desc, const u8 *data,
  51. unsigned int len)
  52. {
  53. struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  54. const __le32 *src;
  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 0;
  65. src = (const __le32 *)mctx->pending;
  66. mctx->l ^= le32_to_cpup(src);
  67. michael_block(mctx->l, mctx->r);
  68. mctx->pending_len = 0;
  69. }
  70. src = (const __le32 *)data;
  71. while (len >= 4) {
  72. mctx->l ^= le32_to_cpup(src++);
  73. michael_block(mctx->l, mctx->r);
  74. len -= 4;
  75. }
  76. if (len > 0) {
  77. mctx->pending_len = len;
  78. memcpy(mctx->pending, src, len);
  79. }
  80. return 0;
  81. }
  82. static int michael_final(struct shash_desc *desc, u8 *out)
  83. {
  84. struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  85. u8 *data = mctx->pending;
  86. __le32 *dst = (__le32 *)out;
  87. /* Last block and padding (0x5a, 4..7 x 0) */
  88. switch (mctx->pending_len) {
  89. case 0:
  90. mctx->l ^= 0x5a;
  91. break;
  92. case 1:
  93. mctx->l ^= data[0] | 0x5a00;
  94. break;
  95. case 2:
  96. mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
  97. break;
  98. case 3:
  99. mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
  100. 0x5a000000;
  101. break;
  102. }
  103. michael_block(mctx->l, mctx->r);
  104. /* l ^= 0; */
  105. michael_block(mctx->l, mctx->r);
  106. dst[0] = cpu_to_le32(mctx->l);
  107. dst[1] = cpu_to_le32(mctx->r);
  108. return 0;
  109. }
  110. static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
  111. unsigned int keylen)
  112. {
  113. struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
  114. const __le32 *data = (const __le32 *)key;
  115. if (keylen != 8) {
  116. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  117. return -EINVAL;
  118. }
  119. mctx->l = le32_to_cpu(data[0]);
  120. mctx->r = le32_to_cpu(data[1]);
  121. return 0;
  122. }
  123. static struct shash_alg alg = {
  124. .digestsize = 8,
  125. .setkey = michael_setkey,
  126. .init = michael_init,
  127. .update = michael_update,
  128. .final = michael_final,
  129. .descsize = sizeof(struct michael_mic_desc_ctx),
  130. .base = {
  131. .cra_name = "michael_mic",
  132. .cra_blocksize = 8,
  133. .cra_alignmask = 3,
  134. .cra_ctxsize = sizeof(struct michael_mic_ctx),
  135. .cra_module = THIS_MODULE,
  136. }
  137. };
  138. static int __init michael_mic_init(void)
  139. {
  140. return crypto_register_shash(&alg);
  141. }
  142. static void __exit michael_mic_exit(void)
  143. {
  144. crypto_unregister_shash(&alg);
  145. }
  146. module_init(michael_mic_init);
  147. module_exit(michael_mic_exit);
  148. MODULE_LICENSE("GPL v2");
  149. MODULE_DESCRIPTION("Michael MIC");
  150. MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
  151. MODULE_ALIAS_CRYPTO("michael_mic");