Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Glue code for SHA-1 implementation for SPE instructions (PPC)
0004  *
0005  * Based on generic implementation.
0006  *
0007  * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
0008  */
0009 
0010 #include <crypto/internal/hash.h>
0011 #include <linux/init.h>
0012 #include <linux/module.h>
0013 #include <linux/mm.h>
0014 #include <linux/types.h>
0015 #include <crypto/sha1.h>
0016 #include <crypto/sha1_base.h>
0017 #include <asm/byteorder.h>
0018 #include <asm/switch_to.h>
0019 #include <linux/hardirq.h>
0020 
0021 /*
0022  * MAX_BYTES defines the number of bytes that are allowed to be processed
0023  * between preempt_disable() and preempt_enable(). SHA1 takes ~1000
0024  * operations per 64 bytes. e500 cores can issue two arithmetic instructions
0025  * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).
0026  * Thus 2KB of input data will need an estimated maximum of 18,000 cycles.
0027  * Headroom for cache misses included. Even with the low end model clocked
0028  * at 667 MHz this equals to a critical time window of less than 27us.
0029  *
0030  */
0031 #define MAX_BYTES 2048
0032 
0033 extern void ppc_spe_sha1_transform(u32 *state, const u8 *src, u32 blocks);
0034 
0035 static void spe_begin(void)
0036 {
0037     /* We just start SPE operations and will save SPE registers later. */
0038     preempt_disable();
0039     enable_kernel_spe();
0040 }
0041 
0042 static void spe_end(void)
0043 {
0044     disable_kernel_spe();
0045     /* reenable preemption */
0046     preempt_enable();
0047 }
0048 
0049 static inline void ppc_sha1_clear_context(struct sha1_state *sctx)
0050 {
0051     int count = sizeof(struct sha1_state) >> 2;
0052     u32 *ptr = (u32 *)sctx;
0053 
0054     /* make sure we can clear the fast way */
0055     BUILD_BUG_ON(sizeof(struct sha1_state) % 4);
0056     do { *ptr++ = 0; } while (--count);
0057 }
0058 
0059 static int ppc_spe_sha1_update(struct shash_desc *desc, const u8 *data,
0060             unsigned int len)
0061 {
0062     struct sha1_state *sctx = shash_desc_ctx(desc);
0063     const unsigned int offset = sctx->count & 0x3f;
0064     const unsigned int avail = 64 - offset;
0065     unsigned int bytes;
0066     const u8 *src = data;
0067 
0068     if (avail > len) {
0069         sctx->count += len;
0070         memcpy((char *)sctx->buffer + offset, src, len);
0071         return 0;
0072     }
0073 
0074     sctx->count += len;
0075 
0076     if (offset) {
0077         memcpy((char *)sctx->buffer + offset, src, avail);
0078 
0079         spe_begin();
0080         ppc_spe_sha1_transform(sctx->state, (const u8 *)sctx->buffer, 1);
0081         spe_end();
0082 
0083         len -= avail;
0084         src += avail;
0085     }
0086 
0087     while (len > 63) {
0088         bytes = (len > MAX_BYTES) ? MAX_BYTES : len;
0089         bytes = bytes & ~0x3f;
0090 
0091         spe_begin();
0092         ppc_spe_sha1_transform(sctx->state, src, bytes >> 6);
0093         spe_end();
0094 
0095         src += bytes;
0096         len -= bytes;
0097     }
0098 
0099     memcpy((char *)sctx->buffer, src, len);
0100     return 0;
0101 }
0102 
0103 static int ppc_spe_sha1_final(struct shash_desc *desc, u8 *out)
0104 {
0105     struct sha1_state *sctx = shash_desc_ctx(desc);
0106     const unsigned int offset = sctx->count & 0x3f;
0107     char *p = (char *)sctx->buffer + offset;
0108     int padlen;
0109     __be64 *pbits = (__be64 *)(((char *)&sctx->buffer) + 56);
0110     __be32 *dst = (__be32 *)out;
0111 
0112     padlen = 55 - offset;
0113     *p++ = 0x80;
0114 
0115     spe_begin();
0116 
0117     if (padlen < 0) {
0118         memset(p, 0x00, padlen + sizeof (u64));
0119         ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
0120         p = (char *)sctx->buffer;
0121         padlen = 56;
0122     }
0123 
0124     memset(p, 0, padlen);
0125     *pbits = cpu_to_be64(sctx->count << 3);
0126     ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
0127 
0128     spe_end();
0129 
0130     dst[0] = cpu_to_be32(sctx->state[0]);
0131     dst[1] = cpu_to_be32(sctx->state[1]);
0132     dst[2] = cpu_to_be32(sctx->state[2]);
0133     dst[3] = cpu_to_be32(sctx->state[3]);
0134     dst[4] = cpu_to_be32(sctx->state[4]);
0135 
0136     ppc_sha1_clear_context(sctx);
0137     return 0;
0138 }
0139 
0140 static int ppc_spe_sha1_export(struct shash_desc *desc, void *out)
0141 {
0142     struct sha1_state *sctx = shash_desc_ctx(desc);
0143 
0144     memcpy(out, sctx, sizeof(*sctx));
0145     return 0;
0146 }
0147 
0148 static int ppc_spe_sha1_import(struct shash_desc *desc, const void *in)
0149 {
0150     struct sha1_state *sctx = shash_desc_ctx(desc);
0151 
0152     memcpy(sctx, in, sizeof(*sctx));
0153     return 0;
0154 }
0155 
0156 static struct shash_alg alg = {
0157     .digestsize =   SHA1_DIGEST_SIZE,
0158     .init       =   sha1_base_init,
0159     .update     =   ppc_spe_sha1_update,
0160     .final      =   ppc_spe_sha1_final,
0161     .export     =   ppc_spe_sha1_export,
0162     .import     =   ppc_spe_sha1_import,
0163     .descsize   =   sizeof(struct sha1_state),
0164     .statesize  =   sizeof(struct sha1_state),
0165     .base       =   {
0166         .cra_name   =   "sha1",
0167         .cra_driver_name=   "sha1-ppc-spe",
0168         .cra_priority   =   300,
0169         .cra_blocksize  =   SHA1_BLOCK_SIZE,
0170         .cra_module =   THIS_MODULE,
0171     }
0172 };
0173 
0174 static int __init ppc_spe_sha1_mod_init(void)
0175 {
0176     return crypto_register_shash(&alg);
0177 }
0178 
0179 static void __exit ppc_spe_sha1_mod_fini(void)
0180 {
0181     crypto_unregister_shash(&alg);
0182 }
0183 
0184 module_init(ppc_spe_sha1_mod_init);
0185 module_exit(ppc_spe_sha1_mod_fini);
0186 
0187 MODULE_LICENSE("GPL");
0188 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, SPE optimized");
0189 
0190 MODULE_ALIAS_CRYPTO("sha1");
0191 MODULE_ALIAS_CRYPTO("sha1-ppc-spe");