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