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