1/* 2 * Cryptographic API 3 * 4 * Michael MIC (IEEE 802.11i/TKIP) keyed digest 5 * 6 * Copyright (c) 2004 Jouni Malinen <jkmaline@cc.hut.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. 11 */ 12 13#include <linux/init.h> 14#include <linux/module.h> 15#include <linux/string.h> 16//#include <linux/crypto.h> 17#include "rtl_crypto.h" 18 19 20struct michael_mic_ctx { 21 u8 pending[4]; 22 size_t pending_len; 23 24 u32 l, r; 25}; 26 27 28static inline u32 rotl(u32 val, int bits) 29{ 30 return (val << bits) | (val >> (32 - bits)); 31} 32 33 34static inline u32 rotr(u32 val, int bits) 35{ 36 return (val >> bits) | (val << (32 - bits)); 37} 38 39 40static inline u32 xswap(u32 val) 41{ 42 return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8); 43} 44 45 46#define michael_block(l, r) \ 47do { \ 48 r ^= rotl(l, 17); \ 49 l += r; \ 50 r ^= xswap(l); \ 51 l += r; \ 52 r ^= rotl(l, 3); \ 53 l += r; \ 54 r ^= rotr(l, 2); \ 55 l += r; \ 56} while (0) 57 58 59static inline u32 get_le32(const u8 *p) 60{ 61 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); 62} 63 64 65static inline void put_le32(u8 *p, u32 v) 66{ 67 p[0] = v; 68 p[1] = v >> 8; 69 p[2] = v >> 16; 70 p[3] = v >> 24; 71} 72 73 74static void michael_init(void *ctx) 75{ 76 struct michael_mic_ctx *mctx = ctx; 77 mctx->pending_len = 0; 78} 79 80 81static void michael_update(void *ctx, const u8 *data, unsigned int len) 82{ 83 struct michael_mic_ctx *mctx = ctx; 84 85 if (mctx->pending_len) { 86 int flen = 4 - mctx->pending_len; 87 if (flen > len) 88 flen = len; 89 memcpy(&mctx->pending[mctx->pending_len], data, flen); 90 mctx->pending_len += flen; 91 data += flen; 92 len -= flen; 93 94 if (mctx->pending_len < 4) 95 return; 96 97 mctx->l ^= get_le32(mctx->pending); 98 michael_block(mctx->l, mctx->r); 99 mctx->pending_len = 0; 100 } 101 102 while (len >= 4) { 103 mctx->l ^= get_le32(data); 104 michael_block(mctx->l, mctx->r); 105 data += 4; 106 len -= 4; 107 } 108 109 if (len > 0) { 110 mctx->pending_len = len; 111 memcpy(mctx->pending, data, len); 112 } 113} 114 115 116static void michael_final(void *ctx, u8 *out) 117{ 118 struct michael_mic_ctx *mctx = ctx; 119 u8 *data = mctx->pending; 120 121 /* Last block and padding (0x5a, 4..7 x 0) */ 122 switch (mctx->pending_len) { 123 case 0: 124 mctx->l ^= 0x5a; 125 break; 126 case 1: 127 mctx->l ^= data[0] | 0x5a00; 128 break; 129 case 2: 130 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000; 131 break; 132 case 3: 133 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) | 134 0x5a000000; 135 break; 136 } 137 michael_block(mctx->l, mctx->r); 138 /* l ^= 0; */ 139 michael_block(mctx->l, mctx->r); 140 141 put_le32(out, mctx->l); 142 put_le32(out + 4, mctx->r); 143} 144 145 146static int michael_setkey(void *ctx, const u8 *key, unsigned int keylen, 147 u32 *flags) 148{ 149 struct michael_mic_ctx *mctx = ctx; 150 if (keylen != 8) { 151 if (flags) 152 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN; 153 return -EINVAL; 154 } 155 mctx->l = get_le32(key); 156 mctx->r = get_le32(key + 4); 157 return 0; 158} 159 160 161static struct crypto_alg michael_mic_alg = { 162 .cra_name = "michael_mic", 163 .cra_flags = CRYPTO_ALG_TYPE_DIGEST, 164 .cra_blocksize = 8, 165 .cra_ctxsize = sizeof(struct michael_mic_ctx), 166 .cra_module = THIS_MODULE, 167 .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list), 168 .cra_u = { .digest = { 169 .dia_digestsize = 8, 170 .dia_init = michael_init, 171 .dia_update = michael_update, 172 .dia_final = michael_final, 173 .dia_setkey = michael_setkey } } 174}; 175 176 177static int __init michael_mic_init(void) 178{ 179 return crypto_register_alg(&michael_mic_alg); 180} 181 182 183static void __exit michael_mic_exit(void) 184{ 185 crypto_unregister_alg(&michael_mic_alg); 186} 187 188 189module_init(michael_mic_init); 190module_exit(michael_mic_exit); 191 192MODULE_LICENSE("GPL v2"); 193MODULE_DESCRIPTION("Michael MIC"); 194MODULE_AUTHOR("Jouni Malinen <jkmaline@cc.hut.fi>"); 195