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 SHA-1 implementation for SPE instructions (PPC)
^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/sha.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) #include <asm/switch_to.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/hardirq.h>
^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)  * MAX_BYTES defines the number of bytes that are allowed to be processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * between preempt_disable() and preempt_enable(). SHA1 takes ~1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * operations per 64 bytes. e500 cores can issue two arithmetic instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * Thus 2KB of input data will need an estimated maximum of 18,000 cycles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Headroom for cache misses included. Even with the low end model clocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * at 667 MHz this equals to a critical time window of less than 27us.
^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) #define MAX_BYTES 2048
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) extern void ppc_spe_sha1_transform(u32 *state, const u8 *src, u32 blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static void spe_begin(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	/* We just start SPE operations and will save SPE registers later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	enable_kernel_spe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static void spe_end(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	disable_kernel_spe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	/* reenable preemption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static inline void ppc_sha1_clear_context(struct sha1_state *sctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int count = sizeof(struct sha1_state) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	u32 *ptr = (u32 *)sctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/* make sure we can clear the fast way */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	BUILD_BUG_ON(sizeof(struct sha1_state) % 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	do { *ptr++ = 0; } while (--count);
^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) static int ppc_spe_sha1_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct sha1_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	sctx->state[0] = SHA1_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	sctx->state[1] = SHA1_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	sctx->state[2] = SHA1_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	sctx->state[3] = SHA1_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	sctx->state[4] = SHA1_H4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	sctx->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int ppc_spe_sha1_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct sha1_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	const unsigned int offset = sctx->count & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	const unsigned int avail = 64 - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unsigned int bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	const u8 *src = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (avail > len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		sctx->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		memcpy((char *)sctx->buffer + offset, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	sctx->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		memcpy((char *)sctx->buffer + offset, src, avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		ppc_spe_sha1_transform(sctx->state, (const u8 *)sctx->buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		len -= avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		src += avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	while (len > 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		bytes = (len > MAX_BYTES) ? MAX_BYTES : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		bytes = bytes & ~0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		ppc_spe_sha1_transform(sctx->state, src, bytes >> 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		src += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		len -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	memcpy((char *)sctx->buffer, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int ppc_spe_sha1_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct sha1_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	const unsigned int offset = sctx->count & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	char *p = (char *)sctx->buffer + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int padlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	__be64 *pbits = (__be64 *)(((char *)&sctx->buffer) + 56);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	__be32 *dst = (__be32 *)out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	padlen = 55 - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	*p++ = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	spe_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (padlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		memset(p, 0x00, padlen + sizeof (u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		p = (char *)sctx->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		padlen = 56;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	memset(p, 0, padlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	*pbits = cpu_to_be64(sctx->count << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	spe_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	dst[0] = cpu_to_be32(sctx->state[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	dst[1] = cpu_to_be32(sctx->state[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	dst[2] = cpu_to_be32(sctx->state[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	dst[3] = cpu_to_be32(sctx->state[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	dst[4] = cpu_to_be32(sctx->state[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	ppc_sha1_clear_context(sctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int ppc_spe_sha1_export(struct shash_desc *desc, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct sha1_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	memcpy(out, sctx, sizeof(*sctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int ppc_spe_sha1_import(struct shash_desc *desc, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct sha1_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	memcpy(sctx, in, sizeof(*sctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return 0;
^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) static struct shash_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	.digestsize	=	SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	.init		=	ppc_spe_sha1_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	.update		=	ppc_spe_sha1_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	.final		=	ppc_spe_sha1_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.export		=	ppc_spe_sha1_export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.import		=	ppc_spe_sha1_import,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.descsize	=	sizeof(struct sha1_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	.statesize	=	sizeof(struct sha1_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	.base		=	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		.cra_name	=	"sha1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		.cra_driver_name=	"sha1-ppc-spe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		.cra_priority	=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		.cra_blocksize	=	SHA1_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int __init ppc_spe_sha1_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return crypto_register_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void __exit ppc_spe_sha1_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	crypto_unregister_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) module_init(ppc_spe_sha1_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) module_exit(ppc_spe_sha1_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, SPE optimized");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) MODULE_ALIAS_CRYPTO("sha1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_ALIAS_CRYPTO("sha1-ppc-spe");