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