Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Cryptographic API
0004  *
0005  * Michael MIC (IEEE 802.11i/TKIP) keyed digest
0006  *
0007  * Copyright (c) 2004 Jouni Malinen <j@w1.fi>
0008  */
0009 #include <crypto/internal/hash.h>
0010 #include <asm/unaligned.h>
0011 #include <linux/init.h>
0012 #include <linux/module.h>
0013 #include <linux/string.h>
0014 #include <linux/types.h>
0015 
0016 
0017 struct michael_mic_ctx {
0018     u32 l, r;
0019 };
0020 
0021 struct michael_mic_desc_ctx {
0022     __le32 pending;
0023     size_t pending_len;
0024 
0025     u32 l, r;
0026 };
0027 
0028 static inline u32 xswap(u32 val)
0029 {
0030     return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
0031 }
0032 
0033 
0034 #define michael_block(l, r) \
0035 do {                \
0036     r ^= rol32(l, 17);  \
0037     l += r;         \
0038     r ^= xswap(l);      \
0039     l += r;         \
0040     r ^= rol32(l, 3);   \
0041     l += r;         \
0042     r ^= ror32(l, 2);   \
0043     l += r;         \
0044 } while (0)
0045 
0046 
0047 static int michael_init(struct shash_desc *desc)
0048 {
0049     struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
0050     struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
0051     mctx->pending_len = 0;
0052     mctx->l = ctx->l;
0053     mctx->r = ctx->r;
0054 
0055     return 0;
0056 }
0057 
0058 
0059 static int michael_update(struct shash_desc *desc, const u8 *data,
0060                unsigned int len)
0061 {
0062     struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
0063 
0064     if (mctx->pending_len) {
0065         int flen = 4 - mctx->pending_len;
0066         if (flen > len)
0067             flen = len;
0068         memcpy((u8 *)&mctx->pending + mctx->pending_len, data, flen);
0069         mctx->pending_len += flen;
0070         data += flen;
0071         len -= flen;
0072 
0073         if (mctx->pending_len < 4)
0074             return 0;
0075 
0076         mctx->l ^= le32_to_cpu(mctx->pending);
0077         michael_block(mctx->l, mctx->r);
0078         mctx->pending_len = 0;
0079     }
0080 
0081     while (len >= 4) {
0082         mctx->l ^= get_unaligned_le32(data);
0083         michael_block(mctx->l, mctx->r);
0084         data += 4;
0085         len -= 4;
0086     }
0087 
0088     if (len > 0) {
0089         mctx->pending_len = len;
0090         memcpy(&mctx->pending, data, len);
0091     }
0092 
0093     return 0;
0094 }
0095 
0096 
0097 static int michael_final(struct shash_desc *desc, u8 *out)
0098 {
0099     struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
0100     u8 *data = (u8 *)&mctx->pending;
0101 
0102     /* Last block and padding (0x5a, 4..7 x 0) */
0103     switch (mctx->pending_len) {
0104     case 0:
0105         mctx->l ^= 0x5a;
0106         break;
0107     case 1:
0108         mctx->l ^= data[0] | 0x5a00;
0109         break;
0110     case 2:
0111         mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
0112         break;
0113     case 3:
0114         mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
0115             0x5a000000;
0116         break;
0117     }
0118     michael_block(mctx->l, mctx->r);
0119     /* l ^= 0; */
0120     michael_block(mctx->l, mctx->r);
0121 
0122     put_unaligned_le32(mctx->l, out);
0123     put_unaligned_le32(mctx->r, out + 4);
0124 
0125     return 0;
0126 }
0127 
0128 
0129 static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
0130               unsigned int keylen)
0131 {
0132     struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
0133 
0134     if (keylen != 8)
0135         return -EINVAL;
0136 
0137     mctx->l = get_unaligned_le32(key);
0138     mctx->r = get_unaligned_le32(key + 4);
0139     return 0;
0140 }
0141 
0142 static struct shash_alg alg = {
0143     .digestsize     =   8,
0144     .setkey         =   michael_setkey,
0145     .init           =   michael_init,
0146     .update         =   michael_update,
0147     .final          =   michael_final,
0148     .descsize       =   sizeof(struct michael_mic_desc_ctx),
0149     .base           =   {
0150         .cra_name       =   "michael_mic",
0151         .cra_driver_name    =   "michael_mic-generic",
0152         .cra_blocksize      =   8,
0153         .cra_ctxsize        =   sizeof(struct michael_mic_ctx),
0154         .cra_module     =   THIS_MODULE,
0155     }
0156 };
0157 
0158 static int __init michael_mic_init(void)
0159 {
0160     return crypto_register_shash(&alg);
0161 }
0162 
0163 
0164 static void __exit michael_mic_exit(void)
0165 {
0166     crypto_unregister_shash(&alg);
0167 }
0168 
0169 
0170 subsys_initcall(michael_mic_init);
0171 module_exit(michael_mic_exit);
0172 
0173 MODULE_LICENSE("GPL v2");
0174 MODULE_DESCRIPTION("Michael MIC");
0175 MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
0176 MODULE_ALIAS_CRYPTO("michael_mic");