linux/crypto/michael_mic.c
<<
>>
Prefs
   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/byteorder.h>
  11#include <linux/init.h>
  12#include <linux/module.h>
  13#include <linux/string.h>
  14#include <linux/types.h>
  15
  16
  17struct michael_mic_ctx {
  18        u32 l, r;
  19};
  20
  21struct michael_mic_desc_ctx {
  22        u8 pending[4];
  23        size_t pending_len;
  24
  25        u32 l, r;
  26};
  27
  28static inline u32 xswap(u32 val)
  29{
  30        return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
  31}
  32
  33
  34#define michael_block(l, r)     \
  35do {                            \
  36        r ^= rol32(l, 17);      \
  37        l += r;                 \
  38        r ^= xswap(l);          \
  39        l += r;                 \
  40        r ^= rol32(l, 3);       \
  41        l += r;                 \
  42        r ^= ror32(l, 2);       \
  43        l += r;                 \
  44} while (0)
  45
  46
  47static int michael_init(struct shash_desc *desc)
  48{
  49        struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  50        struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
  51        mctx->pending_len = 0;
  52        mctx->l = ctx->l;
  53        mctx->r = ctx->r;
  54
  55        return 0;
  56}
  57
  58
  59static int michael_update(struct shash_desc *desc, const u8 *data,
  60                           unsigned int len)
  61{
  62        struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
  63        const __le32 *src;
  64
  65        if (mctx->pending_len) {
  66                int flen = 4 - mctx->pending_len;
  67                if (flen > len)
  68                        flen = len;
  69                memcpy(&mctx->pending[mctx->pending_len], data, flen);
  70                mctx->pending_len += flen;
  71                data += flen;
  72                len -= flen;
  73
  74                if (mctx->pending_len < 4)
  75                        return 0;
  76
  77                src = (const __le32 *)mctx->pending;
  78                mctx->l ^= le32_to_cpup(src);
  79                michael_block(mctx->l, mctx->r);
  80                mctx->pending_len = 0;
  81        }
  82
  83        src = (const __le32 *)data;
  84
  85        while (len >= 4) {
  86                mctx->l ^= le32_to_cpup(src++);
  87                michael_block(mctx->l, mctx->r);
  88                len -= 4;
  89        }
  90
  91        if (len > 0) {
  92                mctx->pending_len = len;
  93                memcpy(mctx->pending, src, len);
  94        }
  95
  96        return 0;
  97}
  98
  99
 100static int michael_final(struct shash_desc *desc, u8 *out)
 101{
 102        struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
 103        u8 *data = mctx->pending;
 104        __le32 *dst = (__le32 *)out;
 105
 106        /* Last block and padding (0x5a, 4..7 x 0) */
 107        switch (mctx->pending_len) {
 108        case 0:
 109                mctx->l ^= 0x5a;
 110                break;
 111        case 1:
 112                mctx->l ^= data[0] | 0x5a00;
 113                break;
 114        case 2:
 115                mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
 116                break;
 117        case 3:
 118                mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
 119                        0x5a000000;
 120                break;
 121        }
 122        michael_block(mctx->l, mctx->r);
 123        /* l ^= 0; */
 124        michael_block(mctx->l, mctx->r);
 125
 126        dst[0] = cpu_to_le32(mctx->l);
 127        dst[1] = cpu_to_le32(mctx->r);
 128
 129        return 0;
 130}
 131
 132
 133static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
 134                          unsigned int keylen)
 135{
 136        struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
 137
 138        const __le32 *data = (const __le32 *)key;
 139
 140        if (keylen != 8)
 141                return -EINVAL;
 142
 143        mctx->l = le32_to_cpu(data[0]);
 144        mctx->r = le32_to_cpu(data[1]);
 145        return 0;
 146}
 147
 148static struct shash_alg alg = {
 149        .digestsize             =       8,
 150        .setkey                 =       michael_setkey,
 151        .init                   =       michael_init,
 152        .update                 =       michael_update,
 153        .final                  =       michael_final,
 154        .descsize               =       sizeof(struct michael_mic_desc_ctx),
 155        .base                   =       {
 156                .cra_name               =       "michael_mic",
 157                .cra_driver_name        =       "michael_mic-generic",
 158                .cra_blocksize          =       8,
 159                .cra_alignmask          =       3,
 160                .cra_ctxsize            =       sizeof(struct michael_mic_ctx),
 161                .cra_module             =       THIS_MODULE,
 162        }
 163};
 164
 165static int __init michael_mic_init(void)
 166{
 167        return crypto_register_shash(&alg);
 168}
 169
 170
 171static void __exit michael_mic_exit(void)
 172{
 173        crypto_unregister_shash(&alg);
 174}
 175
 176
 177subsys_initcall(michael_mic_init);
 178module_exit(michael_mic_exit);
 179
 180MODULE_LICENSE("GPL v2");
 181MODULE_DESCRIPTION("Michael MIC");
 182MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
 183MODULE_ALIAS_CRYPTO("michael_mic");
 184