2019-06-04 15:11:33 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 05:20:36 +07:00
|
|
|
/*
|
|
|
|
* Cryptographic API
|
|
|
|
*
|
|
|
|
* Michael MIC (IEEE 802.11i/TKIP) keyed digest
|
|
|
|
*
|
2007-03-25 07:15:30 +07:00
|
|
|
* Copyright (c) 2004 Jouni Malinen <j@w1.fi>
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2008-12-07 18:35:38 +07:00
|
|
|
#include <crypto/internal/hash.h>
|
2005-10-30 17:25:15 +07:00
|
|
|
#include <asm/byteorder.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
2005-10-30 17:25:15 +07:00
|
|
|
#include <linux/types.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
|
|
|
|
struct michael_mic_ctx {
|
2008-12-07 18:35:38 +07:00
|
|
|
u32 l, r;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct michael_mic_desc_ctx {
|
2005-04-17 05:20:36 +07:00
|
|
|
u8 pending[4];
|
|
|
|
size_t pending_len;
|
|
|
|
|
|
|
|
u32 l, r;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline u32 xswap(u32 val)
|
|
|
|
{
|
|
|
|
return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define michael_block(l, r) \
|
|
|
|
do { \
|
|
|
|
r ^= rol32(l, 17); \
|
|
|
|
l += r; \
|
|
|
|
r ^= xswap(l); \
|
|
|
|
l += r; \
|
|
|
|
r ^= rol32(l, 3); \
|
|
|
|
l += r; \
|
|
|
|
r ^= ror32(l, 2); \
|
|
|
|
l += r; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2008-12-07 18:35:38 +07:00
|
|
|
static int michael_init(struct shash_desc *desc)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-07 18:35:38 +07:00
|
|
|
struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
|
|
|
|
struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
|
2005-04-17 05:20:36 +07:00
|
|
|
mctx->pending_len = 0;
|
2008-12-07 18:35:38 +07:00
|
|
|
mctx->l = ctx->l;
|
|
|
|
mctx->r = ctx->r;
|
|
|
|
|
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-07 18:35:38 +07:00
|
|
|
static int michael_update(struct shash_desc *desc, const u8 *data,
|
2006-05-16 19:09:29 +07:00
|
|
|
unsigned int len)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-07 18:35:38 +07:00
|
|
|
struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
|
2005-10-30 17:25:15 +07:00
|
|
|
const __le32 *src;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (mctx->pending_len) {
|
|
|
|
int flen = 4 - mctx->pending_len;
|
|
|
|
if (flen > len)
|
|
|
|
flen = len;
|
|
|
|
memcpy(&mctx->pending[mctx->pending_len], data, flen);
|
|
|
|
mctx->pending_len += flen;
|
|
|
|
data += flen;
|
|
|
|
len -= flen;
|
|
|
|
|
|
|
|
if (mctx->pending_len < 4)
|
2008-12-07 18:35:38 +07:00
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2005-10-30 17:25:15 +07:00
|
|
|
src = (const __le32 *)mctx->pending;
|
|
|
|
mctx->l ^= le32_to_cpup(src);
|
2005-04-17 05:20:36 +07:00
|
|
|
michael_block(mctx->l, mctx->r);
|
|
|
|
mctx->pending_len = 0;
|
|
|
|
}
|
|
|
|
|
2005-10-30 17:25:15 +07:00
|
|
|
src = (const __le32 *)data;
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
while (len >= 4) {
|
2005-10-30 17:25:15 +07:00
|
|
|
mctx->l ^= le32_to_cpup(src++);
|
2005-04-17 05:20:36 +07:00
|
|
|
michael_block(mctx->l, mctx->r);
|
|
|
|
len -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
mctx->pending_len = len;
|
2005-10-30 17:25:15 +07:00
|
|
|
memcpy(mctx->pending, src, len);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
2008-12-07 18:35:38 +07:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-07 18:35:38 +07:00
|
|
|
static int michael_final(struct shash_desc *desc, u8 *out)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-07 18:35:38 +07:00
|
|
|
struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
|
2005-04-17 05:20:36 +07:00
|
|
|
u8 *data = mctx->pending;
|
2005-10-30 17:25:15 +07:00
|
|
|
__le32 *dst = (__le32 *)out;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/* Last block and padding (0x5a, 4..7 x 0) */
|
|
|
|
switch (mctx->pending_len) {
|
|
|
|
case 0:
|
|
|
|
mctx->l ^= 0x5a;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mctx->l ^= data[0] | 0x5a00;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
|
|
|
|
0x5a000000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
michael_block(mctx->l, mctx->r);
|
|
|
|
/* l ^= 0; */
|
|
|
|
michael_block(mctx->l, mctx->r);
|
|
|
|
|
2005-10-30 17:25:15 +07:00
|
|
|
dst[0] = cpu_to_le32(mctx->l);
|
|
|
|
dst[1] = cpu_to_le32(mctx->r);
|
2008-12-07 18:35:38 +07:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-07 18:35:38 +07:00
|
|
|
static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
|
2006-08-13 11:16:39 +07:00
|
|
|
unsigned int keylen)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-07 18:35:38 +07:00
|
|
|
struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
|
|
|
|
|
2005-10-30 17:25:15 +07:00
|
|
|
const __le32 *data = (const __le32 *)key;
|
|
|
|
|
crypto: remove CRYPTO_TFM_RES_BAD_KEY_LEN
The CRYPTO_TFM_RES_BAD_KEY_LEN flag was apparently meant as a way to
make the ->setkey() functions provide more information about errors.
However, no one actually checks for this flag, which makes it pointless.
Also, many algorithms fail to set this flag when given a bad length key.
Reviewing just the generic implementations, this is the case for
aes-fixed-time, cbcmac, echainiv, nhpoly1305, pcrypt, rfc3686, rfc4309,
rfc7539, rfc7539esp, salsa20, seqiv, and xcbc. But there are probably
many more in arch/*/crypto/ and drivers/crypto/.
Some algorithms can even set this flag when the key is the correct
length. For example, authenc and authencesn set it when the key payload
is malformed in any way (not just a bad length), the atmel-sha and ccree
drivers can set it if a memory allocation fails, and the chelsio driver
sets it for bad auth tag lengths, not just bad key lengths.
So even if someone actually wanted to start checking this flag (which
seems unlikely, since it's been unused for a long time), there would be
a lot of work needed to get it working correctly. But it would probably
be much better to go back to the drawing board and just define different
return values, like -EINVAL if the key is invalid for the algorithm vs.
-EKEYREJECTED if the key was rejected by a policy like "no weak keys".
That would be much simpler, less error-prone, and easier to test.
So just remove this flag.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Horia Geantă <horia.geanta@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-12-31 10:19:36 +07:00
|
|
|
if (keylen != 8)
|
2005-04-17 05:20:36 +07:00
|
|
|
return -EINVAL;
|
2005-10-30 17:25:15 +07:00
|
|
|
|
|
|
|
mctx->l = le32_to_cpu(data[0]);
|
|
|
|
mctx->r = le32_to_cpu(data[1]);
|
2005-04-17 05:20:36 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-07 18:35:38 +07:00
|
|
|
static struct shash_alg alg = {
|
|
|
|
.digestsize = 8,
|
|
|
|
.setkey = michael_setkey,
|
|
|
|
.init = michael_init,
|
|
|
|
.update = michael_update,
|
|
|
|
.final = michael_final,
|
|
|
|
.descsize = sizeof(struct michael_mic_desc_ctx),
|
|
|
|
.base = {
|
|
|
|
.cra_name = "michael_mic",
|
2019-06-03 12:40:57 +07:00
|
|
|
.cra_driver_name = "michael_mic-generic",
|
2008-12-07 18:35:38 +07:00
|
|
|
.cra_blocksize = 8,
|
|
|
|
.cra_alignmask = 3,
|
|
|
|
.cra_ctxsize = sizeof(struct michael_mic_ctx),
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init michael_mic_init(void)
|
|
|
|
{
|
2008-12-07 18:35:38 +07:00
|
|
|
return crypto_register_shash(&alg);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void __exit michael_mic_exit(void)
|
|
|
|
{
|
2008-12-07 18:35:38 +07:00
|
|
|
crypto_unregister_shash(&alg);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-12 11:57:42 +07:00
|
|
|
subsys_initcall(michael_mic_init);
|
2005-04-17 05:20:36 +07:00
|
|
|
module_exit(michael_mic_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_DESCRIPTION("Michael MIC");
|
2007-03-25 07:15:30 +07:00
|
|
|
MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
|
2014-11-21 08:05:53 +07:00
|
|
|
MODULE_ALIAS_CRYPTO("michael_mic");
|