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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Glue code for MD5 implementation for PPC assembler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Based on generic implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <crypto/internal/hash.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/mm.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) #include <crypto/md5.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) extern void ppc_md5_transform(u32 *state, const u8 *src, u32 blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static inline void ppc_md5_clear_context(struct md5_state *sctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	int count = sizeof(struct md5_state) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	u32 *ptr = (u32 *)sctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	/* make sure we can clear the fast way */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	BUILD_BUG_ON(sizeof(struct md5_state) % 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	do { *ptr++ = 0; } while (--count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static int ppc_md5_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	sctx->hash[0] = MD5_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	sctx->hash[1] = MD5_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	sctx->hash[2] = MD5_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	sctx->hash[3] =	MD5_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	sctx->byte_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int ppc_md5_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	const unsigned int offset = sctx->byte_count & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	unsigned int avail = 64 - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	const u8 *src = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	sctx->byte_count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (avail > len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		memcpy((char *)sctx->block + offset, src, len);
^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) 	if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		memcpy((char *)sctx->block + offset, src, avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		ppc_md5_transform(sctx->hash, (const u8 *)sctx->block, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		len -= avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		src += avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (len > 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		ppc_md5_transform(sctx->hash, src, len >> 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		src += len & ~0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		len &= 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	memcpy((char *)sctx->block, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static int ppc_md5_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	const unsigned int offset = sctx->byte_count & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	const u8 *src = (const u8 *)sctx->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u8 *p = (u8 *)src + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int padlen = 55 - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	__le64 *pbits = (__le64 *)((char *)sctx->block + 56);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	__le32 *dst = (__le32 *)out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	*p++ = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (padlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		memset(p, 0x00, padlen + sizeof (u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		ppc_md5_transform(sctx->hash, src, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		p = (char *)sctx->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		padlen = 56;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	memset(p, 0, padlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	*pbits = cpu_to_le64(sctx->byte_count << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	ppc_md5_transform(sctx->hash, src, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	dst[0] = cpu_to_le32(sctx->hash[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	dst[1] = cpu_to_le32(sctx->hash[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	dst[2] = cpu_to_le32(sctx->hash[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	dst[3] = cpu_to_le32(sctx->hash[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ppc_md5_clear_context(sctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int ppc_md5_export(struct shash_desc *desc, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	memcpy(out, sctx, sizeof(*sctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int ppc_md5_import(struct shash_desc *desc, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	memcpy(sctx, in, sizeof(*sctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static struct shash_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	.digestsize	=	MD5_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.init		=	ppc_md5_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	.update		=	ppc_md5_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	.final		=	ppc_md5_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	.export		=	ppc_md5_export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	.import		=	ppc_md5_import,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	.descsize	=	sizeof(struct md5_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	.statesize	=	sizeof(struct md5_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	.base		=	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		.cra_name	=	"md5",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		.cra_driver_name=	"md5-ppc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		.cra_priority	=	200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		.cra_blocksize	=	MD5_HMAC_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int __init ppc_md5_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return crypto_register_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static void __exit ppc_md5_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	crypto_unregister_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) module_init(ppc_md5_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) module_exit(ppc_md5_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) MODULE_DESCRIPTION("MD5 Secure Hash Algorithm, PPC assembler");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) MODULE_ALIAS_CRYPTO("md5");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_ALIAS_CRYPTO("md5-ppc");