Back to home page

LXR

 
 

    


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