1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue code for SHA-1 implementation for SPE instructions (PPC)
5 * Based on generic implementation.
7 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
10 #include <crypto/internal/hash.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <crypto/sha1.h>
16 #include <crypto/sha1_base.h>
17 #include <asm/byteorder.h>
18 #include <asm/switch_to.h>
19 #include <linux/hardirq.h>
22 * MAX_BYTES defines the number of bytes that are allowed to be processed
23 * between preempt_disable() and preempt_enable(). SHA1 takes ~1000
24 * operations per 64 bytes. e500 cores can issue two arithmetic instructions
25 * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).
26 * Thus 2KB of input data will need an estimated maximum of 18,000 cycles.
27 * Headroom for cache misses included. Even with the low end model clocked
28 * at 667 MHz this equals to a critical time window of less than 27us.
31 #define MAX_BYTES 2048
33 extern void ppc_spe_sha1_transform(u32 *state, const u8 *src, u32 blocks);
35 static void spe_begin(void)
37 /* We just start SPE operations and will save SPE registers later. */
42 static void spe_end(void)
45 /* reenable preemption */
49 static inline void ppc_sha1_clear_context(struct sha1_state *sctx)
51 int count = sizeof(struct sha1_state) >> 2;
52 u32 *ptr = (u32 *)sctx;
54 /* make sure we can clear the fast way */
55 BUILD_BUG_ON(sizeof(struct sha1_state) % 4);
56 do { *ptr++ = 0; } while (--count);
59 static int ppc_spe_sha1_update(struct shash_desc *desc, const u8 *data,
62 struct sha1_state *sctx = shash_desc_ctx(desc);
63 const unsigned int offset = sctx->count & 0x3f;
64 const unsigned int avail = 64 - offset;
70 memcpy((char *)sctx->buffer + offset, src, len);
77 memcpy((char *)sctx->buffer + offset, src, avail);
80 ppc_spe_sha1_transform(sctx->state, (const u8 *)sctx->buffer, 1);
88 bytes = (len > MAX_BYTES) ? MAX_BYTES : len;
89 bytes = bytes & ~0x3f;
92 ppc_spe_sha1_transform(sctx->state, src, bytes >> 6);
99 memcpy((char *)sctx->buffer, src, len);
103 static int ppc_spe_sha1_final(struct shash_desc *desc, u8 *out)
105 struct sha1_state *sctx = shash_desc_ctx(desc);
106 const unsigned int offset = sctx->count & 0x3f;
107 char *p = (char *)sctx->buffer + offset;
109 __be64 *pbits = (__be64 *)(((char *)&sctx->buffer) + 56);
110 __be32 *dst = (__be32 *)out;
112 padlen = 55 - offset;
118 memset(p, 0x00, padlen + sizeof (u64));
119 ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
120 p = (char *)sctx->buffer;
124 memset(p, 0, padlen);
125 *pbits = cpu_to_be64(sctx->count << 3);
126 ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
130 dst[0] = cpu_to_be32(sctx->state[0]);
131 dst[1] = cpu_to_be32(sctx->state[1]);
132 dst[2] = cpu_to_be32(sctx->state[2]);
133 dst[3] = cpu_to_be32(sctx->state[3]);
134 dst[4] = cpu_to_be32(sctx->state[4]);
136 ppc_sha1_clear_context(sctx);
140 static int ppc_spe_sha1_export(struct shash_desc *desc, void *out)
142 struct sha1_state *sctx = shash_desc_ctx(desc);
144 memcpy(out, sctx, sizeof(*sctx));
148 static int ppc_spe_sha1_import(struct shash_desc *desc, const void *in)
150 struct sha1_state *sctx = shash_desc_ctx(desc);
152 memcpy(sctx, in, sizeof(*sctx));
156 static struct shash_alg alg = {
157 .digestsize = SHA1_DIGEST_SIZE,
158 .init = sha1_base_init,
159 .update = ppc_spe_sha1_update,
160 .final = ppc_spe_sha1_final,
161 .export = ppc_spe_sha1_export,
162 .import = ppc_spe_sha1_import,
163 .descsize = sizeof(struct sha1_state),
164 .statesize = sizeof(struct sha1_state),
167 .cra_driver_name= "sha1-ppc-spe",
169 .cra_blocksize = SHA1_BLOCK_SIZE,
170 .cra_module = THIS_MODULE,
174 static int __init ppc_spe_sha1_mod_init(void)
176 return crypto_register_shash(&alg);
179 static void __exit ppc_spe_sha1_mod_fini(void)
181 crypto_unregister_shash(&alg);
184 module_init(ppc_spe_sha1_mod_init);
185 module_exit(ppc_spe_sha1_mod_fini);
187 MODULE_LICENSE("GPL");
188 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, SPE optimized");
190 MODULE_ALIAS_CRYPTO("sha1");
191 MODULE_ALIAS_CRYPTO("sha1-ppc-spe");