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