Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Glue code for SHA1 hashing optimized for sparc64 crypto opcodes.
0003  *
0004  * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
0005  *
0006  * Copyright (c) Alan Smithee.
0007  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
0008  * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
0009  * Copyright (c) Mathias Krause <minipli@googlemail.com>
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <crypto/internal/hash.h>
0015 #include <linux/init.h>
0016 #include <linux/module.h>
0017 #include <linux/mm.h>
0018 #include <linux/types.h>
0019 #include <crypto/sha1.h>
0020 #include <crypto/sha1_base.h>
0021 
0022 #include <asm/pstate.h>
0023 #include <asm/elf.h>
0024 
0025 #include "opcodes.h"
0026 
0027 asmlinkage void sha1_sparc64_transform(u32 *digest, const char *data,
0028                        unsigned int rounds);
0029 
0030 static void __sha1_sparc64_update(struct sha1_state *sctx, const u8 *data,
0031                   unsigned int len, unsigned int partial)
0032 {
0033     unsigned int done = 0;
0034 
0035     sctx->count += len;
0036     if (partial) {
0037         done = SHA1_BLOCK_SIZE - partial;
0038         memcpy(sctx->buffer + partial, data, done);
0039         sha1_sparc64_transform(sctx->state, sctx->buffer, 1);
0040     }
0041     if (len - done >= SHA1_BLOCK_SIZE) {
0042         const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
0043 
0044         sha1_sparc64_transform(sctx->state, data + done, rounds);
0045         done += rounds * SHA1_BLOCK_SIZE;
0046     }
0047 
0048     memcpy(sctx->buffer, data + done, len - done);
0049 }
0050 
0051 static int sha1_sparc64_update(struct shash_desc *desc, const u8 *data,
0052                    unsigned int len)
0053 {
0054     struct sha1_state *sctx = shash_desc_ctx(desc);
0055     unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
0056 
0057     /* Handle the fast case right here */
0058     if (partial + len < SHA1_BLOCK_SIZE) {
0059         sctx->count += len;
0060         memcpy(sctx->buffer + partial, data, len);
0061     } else
0062         __sha1_sparc64_update(sctx, data, len, partial);
0063 
0064     return 0;
0065 }
0066 
0067 /* Add padding and return the message digest. */
0068 static int sha1_sparc64_final(struct shash_desc *desc, u8 *out)
0069 {
0070     struct sha1_state *sctx = shash_desc_ctx(desc);
0071     unsigned int i, index, padlen;
0072     __be32 *dst = (__be32 *)out;
0073     __be64 bits;
0074     static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
0075 
0076     bits = cpu_to_be64(sctx->count << 3);
0077 
0078     /* Pad out to 56 mod 64 and append length */
0079     index = sctx->count % SHA1_BLOCK_SIZE;
0080     padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
0081 
0082     /* We need to fill a whole block for __sha1_sparc64_update() */
0083     if (padlen <= 56) {
0084         sctx->count += padlen;
0085         memcpy(sctx->buffer + index, padding, padlen);
0086     } else {
0087         __sha1_sparc64_update(sctx, padding, padlen, index);
0088     }
0089     __sha1_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
0090 
0091     /* Store state in digest */
0092     for (i = 0; i < 5; i++)
0093         dst[i] = cpu_to_be32(sctx->state[i]);
0094 
0095     /* Wipe context */
0096     memset(sctx, 0, sizeof(*sctx));
0097 
0098     return 0;
0099 }
0100 
0101 static int sha1_sparc64_export(struct shash_desc *desc, void *out)
0102 {
0103     struct sha1_state *sctx = shash_desc_ctx(desc);
0104 
0105     memcpy(out, sctx, sizeof(*sctx));
0106 
0107     return 0;
0108 }
0109 
0110 static int sha1_sparc64_import(struct shash_desc *desc, const void *in)
0111 {
0112     struct sha1_state *sctx = shash_desc_ctx(desc);
0113 
0114     memcpy(sctx, in, sizeof(*sctx));
0115 
0116     return 0;
0117 }
0118 
0119 static struct shash_alg alg = {
0120     .digestsize =   SHA1_DIGEST_SIZE,
0121     .init       =   sha1_base_init,
0122     .update     =   sha1_sparc64_update,
0123     .final      =   sha1_sparc64_final,
0124     .export     =   sha1_sparc64_export,
0125     .import     =   sha1_sparc64_import,
0126     .descsize   =   sizeof(struct sha1_state),
0127     .statesize  =   sizeof(struct sha1_state),
0128     .base       =   {
0129         .cra_name   =   "sha1",
0130         .cra_driver_name=   "sha1-sparc64",
0131         .cra_priority   =   SPARC_CR_OPCODE_PRIORITY,
0132         .cra_blocksize  =   SHA1_BLOCK_SIZE,
0133         .cra_module =   THIS_MODULE,
0134     }
0135 };
0136 
0137 static bool __init sparc64_has_sha1_opcode(void)
0138 {
0139     unsigned long cfr;
0140 
0141     if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
0142         return false;
0143 
0144     __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
0145     if (!(cfr & CFR_SHA1))
0146         return false;
0147 
0148     return true;
0149 }
0150 
0151 static int __init sha1_sparc64_mod_init(void)
0152 {
0153     if (sparc64_has_sha1_opcode()) {
0154         pr_info("Using sparc64 sha1 opcode optimized SHA-1 implementation\n");
0155         return crypto_register_shash(&alg);
0156     }
0157     pr_info("sparc64 sha1 opcode not available.\n");
0158     return -ENODEV;
0159 }
0160 
0161 static void __exit sha1_sparc64_mod_fini(void)
0162 {
0163     crypto_unregister_shash(&alg);
0164 }
0165 
0166 module_init(sha1_sparc64_mod_init);
0167 module_exit(sha1_sparc64_mod_fini);
0168 
0169 MODULE_LICENSE("GPL");
0170 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, sparc64 sha1 opcode accelerated");
0171 
0172 MODULE_ALIAS_CRYPTO("sha1");
0173 
0174 #include "crop_devid.c"