Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Glue code for SHA512 hashing optimized for sparc64 crypto opcodes.
0003  *
0004  * This is based largely upon crypto/sha512_generic.c
0005  *
0006  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
0007  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
0008  * Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <crypto/internal/hash.h>
0014 #include <linux/init.h>
0015 #include <linux/module.h>
0016 #include <linux/mm.h>
0017 #include <linux/types.h>
0018 #include <crypto/sha2.h>
0019 #include <crypto/sha512_base.h>
0020 
0021 #include <asm/pstate.h>
0022 #include <asm/elf.h>
0023 
0024 #include "opcodes.h"
0025 
0026 asmlinkage void sha512_sparc64_transform(u64 *digest, const char *data,
0027                      unsigned int rounds);
0028 
0029 static void __sha512_sparc64_update(struct sha512_state *sctx, const u8 *data,
0030                     unsigned int len, unsigned int partial)
0031 {
0032     unsigned int done = 0;
0033 
0034     if ((sctx->count[0] += len) < len)
0035         sctx->count[1]++;
0036     if (partial) {
0037         done = SHA512_BLOCK_SIZE - partial;
0038         memcpy(sctx->buf + partial, data, done);
0039         sha512_sparc64_transform(sctx->state, sctx->buf, 1);
0040     }
0041     if (len - done >= SHA512_BLOCK_SIZE) {
0042         const unsigned int rounds = (len - done) / SHA512_BLOCK_SIZE;
0043 
0044         sha512_sparc64_transform(sctx->state, data + done, rounds);
0045         done += rounds * SHA512_BLOCK_SIZE;
0046     }
0047 
0048     memcpy(sctx->buf, data + done, len - done);
0049 }
0050 
0051 static int sha512_sparc64_update(struct shash_desc *desc, const u8 *data,
0052                  unsigned int len)
0053 {
0054     struct sha512_state *sctx = shash_desc_ctx(desc);
0055     unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
0056 
0057     /* Handle the fast case right here */
0058     if (partial + len < SHA512_BLOCK_SIZE) {
0059         if ((sctx->count[0] += len) < len)
0060             sctx->count[1]++;
0061         memcpy(sctx->buf + partial, data, len);
0062     } else
0063         __sha512_sparc64_update(sctx, data, len, partial);
0064 
0065     return 0;
0066 }
0067 
0068 static int sha512_sparc64_final(struct shash_desc *desc, u8 *out)
0069 {
0070     struct sha512_state *sctx = shash_desc_ctx(desc);
0071     unsigned int i, index, padlen;
0072     __be64 *dst = (__be64 *)out;
0073     __be64 bits[2];
0074     static const u8 padding[SHA512_BLOCK_SIZE] = { 0x80, };
0075 
0076     /* Save number of bits */
0077     bits[1] = cpu_to_be64(sctx->count[0] << 3);
0078     bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
0079 
0080     /* Pad out to 112 mod 128 and append length */
0081     index = sctx->count[0] % SHA512_BLOCK_SIZE;
0082     padlen = (index < 112) ? (112 - index) : ((SHA512_BLOCK_SIZE+112) - index);
0083 
0084     /* We need to fill a whole block for __sha512_sparc64_update() */
0085     if (padlen <= 112) {
0086         if ((sctx->count[0] += padlen) < padlen)
0087             sctx->count[1]++;
0088         memcpy(sctx->buf + index, padding, padlen);
0089     } else {
0090         __sha512_sparc64_update(sctx, padding, padlen, index);
0091     }
0092     __sha512_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 112);
0093 
0094     /* Store state in digest */
0095     for (i = 0; i < 8; i++)
0096         dst[i] = cpu_to_be64(sctx->state[i]);
0097 
0098     /* Wipe context */
0099     memset(sctx, 0, sizeof(*sctx));
0100 
0101     return 0;
0102 }
0103 
0104 static int sha384_sparc64_final(struct shash_desc *desc, u8 *hash)
0105 {
0106     u8 D[64];
0107 
0108     sha512_sparc64_final(desc, D);
0109 
0110     memcpy(hash, D, 48);
0111     memzero_explicit(D, 64);
0112 
0113     return 0;
0114 }
0115 
0116 static struct shash_alg sha512 = {
0117     .digestsize =   SHA512_DIGEST_SIZE,
0118     .init       =   sha512_base_init,
0119     .update     =   sha512_sparc64_update,
0120     .final      =   sha512_sparc64_final,
0121     .descsize   =   sizeof(struct sha512_state),
0122     .base       =   {
0123         .cra_name   =   "sha512",
0124         .cra_driver_name=   "sha512-sparc64",
0125         .cra_priority   =   SPARC_CR_OPCODE_PRIORITY,
0126         .cra_blocksize  =   SHA512_BLOCK_SIZE,
0127         .cra_module =   THIS_MODULE,
0128     }
0129 };
0130 
0131 static struct shash_alg sha384 = {
0132     .digestsize =   SHA384_DIGEST_SIZE,
0133     .init       =   sha384_base_init,
0134     .update     =   sha512_sparc64_update,
0135     .final      =   sha384_sparc64_final,
0136     .descsize   =   sizeof(struct sha512_state),
0137     .base       =   {
0138         .cra_name   =   "sha384",
0139         .cra_driver_name=   "sha384-sparc64",
0140         .cra_priority   =   SPARC_CR_OPCODE_PRIORITY,
0141         .cra_blocksize  =   SHA384_BLOCK_SIZE,
0142         .cra_module =   THIS_MODULE,
0143     }
0144 };
0145 
0146 static bool __init sparc64_has_sha512_opcode(void)
0147 {
0148     unsigned long cfr;
0149 
0150     if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
0151         return false;
0152 
0153     __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
0154     if (!(cfr & CFR_SHA512))
0155         return false;
0156 
0157     return true;
0158 }
0159 
0160 static int __init sha512_sparc64_mod_init(void)
0161 {
0162     if (sparc64_has_sha512_opcode()) {
0163         int ret = crypto_register_shash(&sha384);
0164         if (ret < 0)
0165             return ret;
0166 
0167         ret = crypto_register_shash(&sha512);
0168         if (ret < 0) {
0169             crypto_unregister_shash(&sha384);
0170             return ret;
0171         }
0172 
0173         pr_info("Using sparc64 sha512 opcode optimized SHA-512/SHA-384 implementation\n");
0174         return 0;
0175     }
0176     pr_info("sparc64 sha512 opcode not available.\n");
0177     return -ENODEV;
0178 }
0179 
0180 static void __exit sha512_sparc64_mod_fini(void)
0181 {
0182     crypto_unregister_shash(&sha384);
0183     crypto_unregister_shash(&sha512);
0184 }
0185 
0186 module_init(sha512_sparc64_mod_init);
0187 module_exit(sha512_sparc64_mod_fini);
0188 
0189 MODULE_LICENSE("GPL");
0190 MODULE_DESCRIPTION("SHA-384 and SHA-512 Secure Hash Algorithm, sparc64 sha512 opcode accelerated");
0191 
0192 MODULE_ALIAS_CRYPTO("sha384");
0193 MODULE_ALIAS_CRYPTO("sha512");
0194 
0195 #include "crop_devid.c"