Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API |
| 4 | * |
| 5 | * Michael MIC (IEEE 802.11i/TKIP) keyed digest |
| 6 | * |
Jouni Malinen | 85d32e7 | 2007-03-24 17:15:30 -0700 | [diff] [blame] | 7 | * Copyright (c) 2004 Jouni Malinen <j@w1.fi> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 9 | #include <crypto/internal/hash.h> |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 10 | #include <asm/byteorder.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/string.h> |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 14 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | struct michael_mic_ctx { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 18 | u32 l, r; |
| 19 | }; |
| 20 | |
| 21 | struct michael_mic_desc_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | u8 pending[4]; |
| 23 | size_t pending_len; |
| 24 | |
| 25 | u32 l, r; |
| 26 | }; |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static inline u32 xswap(u32 val) |
| 29 | { |
| 30 | return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | #define michael_block(l, r) \ |
| 35 | do { \ |
| 36 | r ^= rol32(l, 17); \ |
| 37 | l += r; \ |
| 38 | r ^= xswap(l); \ |
| 39 | l += r; \ |
| 40 | r ^= rol32(l, 3); \ |
| 41 | l += r; \ |
| 42 | r ^= ror32(l, 2); \ |
| 43 | l += r; \ |
| 44 | } while (0) |
| 45 | |
| 46 | |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 47 | static int michael_init(struct shash_desc *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 49 | struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc); |
| 50 | struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | mctx->pending_len = 0; |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 52 | mctx->l = ctx->l; |
| 53 | mctx->r = ctx->r; |
| 54 | |
| 55 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 59 | static int michael_update(struct shash_desc *desc, const u8 *data, |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 60 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 62 | struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 63 | const __le32 *src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | if (mctx->pending_len) { |
| 66 | int flen = 4 - mctx->pending_len; |
| 67 | if (flen > len) |
| 68 | flen = len; |
| 69 | memcpy(&mctx->pending[mctx->pending_len], data, flen); |
| 70 | mctx->pending_len += flen; |
| 71 | data += flen; |
| 72 | len -= flen; |
| 73 | |
| 74 | if (mctx->pending_len < 4) |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 75 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 77 | src = (const __le32 *)mctx->pending; |
| 78 | mctx->l ^= le32_to_cpup(src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | michael_block(mctx->l, mctx->r); |
| 80 | mctx->pending_len = 0; |
| 81 | } |
| 82 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 83 | src = (const __le32 *)data; |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | while (len >= 4) { |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 86 | mctx->l ^= le32_to_cpup(src++); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | michael_block(mctx->l, mctx->r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | len -= 4; |
| 89 | } |
| 90 | |
| 91 | if (len > 0) { |
| 92 | mctx->pending_len = len; |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 93 | memcpy(mctx->pending, src, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 95 | |
| 96 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 100 | static int michael_final(struct shash_desc *desc, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 102 | struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | u8 *data = mctx->pending; |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 104 | __le32 *dst = (__le32 *)out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* Last block and padding (0x5a, 4..7 x 0) */ |
| 107 | switch (mctx->pending_len) { |
| 108 | case 0: |
| 109 | mctx->l ^= 0x5a; |
| 110 | break; |
| 111 | case 1: |
| 112 | mctx->l ^= data[0] | 0x5a00; |
| 113 | break; |
| 114 | case 2: |
| 115 | mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000; |
| 116 | break; |
| 117 | case 3: |
| 118 | mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) | |
| 119 | 0x5a000000; |
| 120 | break; |
| 121 | } |
| 122 | michael_block(mctx->l, mctx->r); |
| 123 | /* l ^= 0; */ |
| 124 | michael_block(mctx->l, mctx->r); |
| 125 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 126 | dst[0] = cpu_to_le32(mctx->l); |
| 127 | dst[1] = cpu_to_le32(mctx->r); |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 128 | |
| 129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 133 | static int michael_setkey(struct crypto_shash *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 134 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 136 | struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm); |
| 137 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 138 | const __le32 *data = (const __le32 *)key; |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (keylen != 8) { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 141 | crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return -EINVAL; |
| 143 | } |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 144 | |
| 145 | mctx->l = le32_to_cpu(data[0]); |
| 146 | mctx->r = le32_to_cpu(data[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 150 | static struct shash_alg alg = { |
| 151 | .digestsize = 8, |
| 152 | .setkey = michael_setkey, |
| 153 | .init = michael_init, |
| 154 | .update = michael_update, |
| 155 | .final = michael_final, |
| 156 | .descsize = sizeof(struct michael_mic_desc_ctx), |
| 157 | .base = { |
| 158 | .cra_name = "michael_mic", |
Eric Biggers | d6ebf52 | 2019-06-02 22:40:57 -0700 | [diff] [blame] | 159 | .cra_driver_name = "michael_mic-generic", |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 160 | .cra_blocksize = 8, |
| 161 | .cra_alignmask = 3, |
| 162 | .cra_ctxsize = sizeof(struct michael_mic_ctx), |
| 163 | .cra_module = THIS_MODULE, |
| 164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | static int __init michael_mic_init(void) |
| 168 | { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 169 | return crypto_register_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | static void __exit michael_mic_exit(void) |
| 174 | { |
Adrian-Ken Rueegsegger | 19e2bf1 | 2008-12-07 19:35:38 +0800 | [diff] [blame] | 175 | crypto_unregister_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 179 | subsys_initcall(michael_mic_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | module_exit(michael_mic_exit); |
| 181 | |
| 182 | MODULE_LICENSE("GPL v2"); |
| 183 | MODULE_DESCRIPTION("Michael MIC"); |
Jouni Malinen | 85d32e7 | 2007-03-24 17:15:30 -0700 | [diff] [blame] | 184 | MODULE_AUTHOR("Jouni Malinen <j@w1.fi>"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 185 | MODULE_ALIAS_CRYPTO("michael_mic"); |