Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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