michael_mic.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cryptographic API
  4. *
  5. * Michael MIC (IEEE 802.11i/TKIP) keyed digest
  6. *
  7. * Copyright (c) 2004 Jouni Malinen <j@w1.fi>
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <asm/unaligned.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/types.h>
  15. struct michael_mic_ctx {
  16. u32 l, r;
  17. };
  18. struct michael_mic_desc_ctx {
  19. __le32 pending;
  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 int michael_init(struct shash_desc *desc)
  39. {
  40. struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  41. struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
  42. mctx->pending_len = 0;
  43. mctx->l = ctx->l;
  44. mctx->r = ctx->r;
  45. return 0;
  46. }
  47. static int michael_update(struct shash_desc *desc, const u8 *data,
  48. unsigned int len)
  49. {
  50. struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  51. if (mctx->pending_len) {
  52. int flen = 4 - mctx->pending_len;
  53. if (flen > len)
  54. flen = len;
  55. memcpy((u8 *)&mctx->pending + mctx->pending_len, data, flen);
  56. mctx->pending_len += flen;
  57. data += flen;
  58. len -= flen;
  59. if (mctx->pending_len < 4)
  60. return 0;
  61. mctx->l ^= le32_to_cpu(mctx->pending);
  62. michael_block(mctx->l, mctx->r);
  63. mctx->pending_len = 0;
  64. }
  65. while (len >= 4) {
  66. mctx->l ^= get_unaligned_le32(data);
  67. michael_block(mctx->l, mctx->r);
  68. data += 4;
  69. len -= 4;
  70. }
  71. if (len > 0) {
  72. mctx->pending_len = len;
  73. memcpy(&mctx->pending, data, len);
  74. }
  75. return 0;
  76. }
  77. static int michael_final(struct shash_desc *desc, u8 *out)
  78. {
  79. struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  80. u8 *data = (u8 *)&mctx->pending;
  81. /* Last block and padding (0x5a, 4..7 x 0) */
  82. switch (mctx->pending_len) {
  83. case 0:
  84. mctx->l ^= 0x5a;
  85. break;
  86. case 1:
  87. mctx->l ^= data[0] | 0x5a00;
  88. break;
  89. case 2:
  90. mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
  91. break;
  92. case 3:
  93. mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
  94. 0x5a000000;
  95. break;
  96. }
  97. michael_block(mctx->l, mctx->r);
  98. /* l ^= 0; */
  99. michael_block(mctx->l, mctx->r);
  100. put_unaligned_le32(mctx->l, out);
  101. put_unaligned_le32(mctx->r, out + 4);
  102. return 0;
  103. }
  104. static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
  105. unsigned int keylen)
  106. {
  107. struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
  108. if (keylen != 8)
  109. return -EINVAL;
  110. mctx->l = get_unaligned_le32(key);
  111. mctx->r = get_unaligned_le32(key + 4);
  112. return 0;
  113. }
  114. static struct shash_alg alg = {
  115. .digestsize = 8,
  116. .setkey = michael_setkey,
  117. .init = michael_init,
  118. .update = michael_update,
  119. .final = michael_final,
  120. .descsize = sizeof(struct michael_mic_desc_ctx),
  121. .base = {
  122. .cra_name = "michael_mic",
  123. .cra_driver_name = "michael_mic-generic",
  124. .cra_blocksize = 8,
  125. .cra_ctxsize = sizeof(struct michael_mic_ctx),
  126. .cra_module = THIS_MODULE,
  127. }
  128. };
  129. static int __init michael_mic_init(void)
  130. {
  131. return crypto_register_shash(&alg);
  132. }
  133. static void __exit michael_mic_exit(void)
  134. {
  135. crypto_unregister_shash(&alg);
  136. }
  137. subsys_initcall(michael_mic_init);
  138. module_exit(michael_mic_exit);
  139. MODULE_LICENSE("GPL v2");
  140. MODULE_DESCRIPTION("Michael MIC");
  141. MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
  142. MODULE_ALIAS_CRYPTO("michael_mic");