ia64/linux-2.6.18-xen.hg

annotate crypto/michael_mic.c @ 854:950b9eb27661

usbback: fix urb interval value for interrupt urbs.

Signed-off-by: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 06 13:51:20 2009 +0100 (2009-04-06)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * Cryptographic API
ian@0 3 *
ian@0 4 * Michael MIC (IEEE 802.11i/TKIP) keyed digest
ian@0 5 *
ian@0 6 * Copyright (c) 2004 Jouni Malinen <jkmaline@cc.hut.fi>
ian@0 7 *
ian@0 8 * This program is free software; you can redistribute it and/or modify
ian@0 9 * it under the terms of the GNU General Public License version 2 as
ian@0 10 * published by the Free Software Foundation.
ian@0 11 */
ian@0 12
ian@0 13 #include <asm/byteorder.h>
ian@0 14 #include <linux/init.h>
ian@0 15 #include <linux/module.h>
ian@0 16 #include <linux/string.h>
ian@0 17 #include <linux/crypto.h>
ian@0 18 #include <linux/types.h>
ian@0 19
ian@0 20
ian@0 21 struct michael_mic_ctx {
ian@0 22 u8 pending[4];
ian@0 23 size_t pending_len;
ian@0 24
ian@0 25 u32 l, r;
ian@0 26 };
ian@0 27
ian@0 28
ian@0 29 static inline u32 xswap(u32 val)
ian@0 30 {
ian@0 31 return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
ian@0 32 }
ian@0 33
ian@0 34
ian@0 35 #define michael_block(l, r) \
ian@0 36 do { \
ian@0 37 r ^= rol32(l, 17); \
ian@0 38 l += r; \
ian@0 39 r ^= xswap(l); \
ian@0 40 l += r; \
ian@0 41 r ^= rol32(l, 3); \
ian@0 42 l += r; \
ian@0 43 r ^= ror32(l, 2); \
ian@0 44 l += r; \
ian@0 45 } while (0)
ian@0 46
ian@0 47
ian@0 48 static void michael_init(struct crypto_tfm *tfm)
ian@0 49 {
ian@0 50 struct michael_mic_ctx *mctx = crypto_tfm_ctx(tfm);
ian@0 51 mctx->pending_len = 0;
ian@0 52 }
ian@0 53
ian@0 54
ian@0 55 static void michael_update(struct crypto_tfm *tfm, const u8 *data,
ian@0 56 unsigned int len)
ian@0 57 {
ian@0 58 struct michael_mic_ctx *mctx = crypto_tfm_ctx(tfm);
ian@0 59 const __le32 *src;
ian@0 60
ian@0 61 if (mctx->pending_len) {
ian@0 62 int flen = 4 - mctx->pending_len;
ian@0 63 if (flen > len)
ian@0 64 flen = len;
ian@0 65 memcpy(&mctx->pending[mctx->pending_len], data, flen);
ian@0 66 mctx->pending_len += flen;
ian@0 67 data += flen;
ian@0 68 len -= flen;
ian@0 69
ian@0 70 if (mctx->pending_len < 4)
ian@0 71 return;
ian@0 72
ian@0 73 src = (const __le32 *)mctx->pending;
ian@0 74 mctx->l ^= le32_to_cpup(src);
ian@0 75 michael_block(mctx->l, mctx->r);
ian@0 76 mctx->pending_len = 0;
ian@0 77 }
ian@0 78
ian@0 79 src = (const __le32 *)data;
ian@0 80
ian@0 81 while (len >= 4) {
ian@0 82 mctx->l ^= le32_to_cpup(src++);
ian@0 83 michael_block(mctx->l, mctx->r);
ian@0 84 len -= 4;
ian@0 85 }
ian@0 86
ian@0 87 if (len > 0) {
ian@0 88 mctx->pending_len = len;
ian@0 89 memcpy(mctx->pending, src, len);
ian@0 90 }
ian@0 91 }
ian@0 92
ian@0 93
ian@0 94 static void michael_final(struct crypto_tfm *tfm, u8 *out)
ian@0 95 {
ian@0 96 struct michael_mic_ctx *mctx = crypto_tfm_ctx(tfm);
ian@0 97 u8 *data = mctx->pending;
ian@0 98 __le32 *dst = (__le32 *)out;
ian@0 99
ian@0 100 /* Last block and padding (0x5a, 4..7 x 0) */
ian@0 101 switch (mctx->pending_len) {
ian@0 102 case 0:
ian@0 103 mctx->l ^= 0x5a;
ian@0 104 break;
ian@0 105 case 1:
ian@0 106 mctx->l ^= data[0] | 0x5a00;
ian@0 107 break;
ian@0 108 case 2:
ian@0 109 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
ian@0 110 break;
ian@0 111 case 3:
ian@0 112 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
ian@0 113 0x5a000000;
ian@0 114 break;
ian@0 115 }
ian@0 116 michael_block(mctx->l, mctx->r);
ian@0 117 /* l ^= 0; */
ian@0 118 michael_block(mctx->l, mctx->r);
ian@0 119
ian@0 120 dst[0] = cpu_to_le32(mctx->l);
ian@0 121 dst[1] = cpu_to_le32(mctx->r);
ian@0 122 }
ian@0 123
ian@0 124
ian@0 125 static int michael_setkey(struct crypto_tfm *tfm, const u8 *key,
ian@0 126 unsigned int keylen, u32 *flags)
ian@0 127 {
ian@0 128 struct michael_mic_ctx *mctx = crypto_tfm_ctx(tfm);
ian@0 129 const __le32 *data = (const __le32 *)key;
ian@0 130
ian@0 131 if (keylen != 8) {
ian@0 132 if (flags)
ian@0 133 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
ian@0 134 return -EINVAL;
ian@0 135 }
ian@0 136
ian@0 137 mctx->l = le32_to_cpu(data[0]);
ian@0 138 mctx->r = le32_to_cpu(data[1]);
ian@0 139 return 0;
ian@0 140 }
ian@0 141
ian@0 142
ian@0 143 static struct crypto_alg michael_mic_alg = {
ian@0 144 .cra_name = "michael_mic",
ian@0 145 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
ian@0 146 .cra_blocksize = 8,
ian@0 147 .cra_ctxsize = sizeof(struct michael_mic_ctx),
ian@0 148 .cra_module = THIS_MODULE,
ian@0 149 .cra_alignmask = 3,
ian@0 150 .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list),
ian@0 151 .cra_u = { .digest = {
ian@0 152 .dia_digestsize = 8,
ian@0 153 .dia_init = michael_init,
ian@0 154 .dia_update = michael_update,
ian@0 155 .dia_final = michael_final,
ian@0 156 .dia_setkey = michael_setkey } }
ian@0 157 };
ian@0 158
ian@0 159
ian@0 160 static int __init michael_mic_init(void)
ian@0 161 {
ian@0 162 return crypto_register_alg(&michael_mic_alg);
ian@0 163 }
ian@0 164
ian@0 165
ian@0 166 static void __exit michael_mic_exit(void)
ian@0 167 {
ian@0 168 crypto_unregister_alg(&michael_mic_alg);
ian@0 169 }
ian@0 170
ian@0 171
ian@0 172 module_init(michael_mic_init);
ian@0 173 module_exit(michael_mic_exit);
ian@0 174
ian@0 175 MODULE_LICENSE("GPL v2");
ian@0 176 MODULE_DESCRIPTION("Michael MIC");
ian@0 177 MODULE_AUTHOR("Jouni Malinen <jkmaline@cc.hut.fi>");