Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Glue code for SHA256 hashing optimized for sparc64 crypto opcodes.
0003  *
0004  * This is based largely upon crypto/sha256_generic.c
0005  *
0006  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
0007  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
0008  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0009  * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.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/sha2.h>
0020 #include <crypto/sha256_base.h>
0021 
0022 #include <asm/pstate.h>
0023 #include <asm/elf.h>
0024 
0025 #include "opcodes.h"
0026 
0027 asmlinkage void sha256_sparc64_transform(u32 *digest, const char *data,
0028                      unsigned int rounds);
0029 
0030 static void __sha256_sparc64_update(struct sha256_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 = SHA256_BLOCK_SIZE - partial;
0038         memcpy(sctx->buf + partial, data, done);
0039         sha256_sparc64_transform(sctx->state, sctx->buf, 1);
0040     }
0041     if (len - done >= SHA256_BLOCK_SIZE) {
0042         const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
0043 
0044         sha256_sparc64_transform(sctx->state, data + done, rounds);
0045         done += rounds * SHA256_BLOCK_SIZE;
0046     }
0047 
0048     memcpy(sctx->buf, data + done, len - done);
0049 }
0050 
0051 static int sha256_sparc64_update(struct shash_desc *desc, const u8 *data,
0052                  unsigned int len)
0053 {
0054     struct sha256_state *sctx = shash_desc_ctx(desc);
0055     unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
0056 
0057     /* Handle the fast case right here */
0058     if (partial + len < SHA256_BLOCK_SIZE) {
0059         sctx->count += len;
0060         memcpy(sctx->buf + partial, data, len);
0061     } else
0062         __sha256_sparc64_update(sctx, data, len, partial);
0063 
0064     return 0;
0065 }
0066 
0067 static int sha256_sparc64_final(struct shash_desc *desc, u8 *out)
0068 {
0069     struct sha256_state *sctx = shash_desc_ctx(desc);
0070     unsigned int i, index, padlen;
0071     __be32 *dst = (__be32 *)out;
0072     __be64 bits;
0073     static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
0074 
0075     bits = cpu_to_be64(sctx->count << 3);
0076 
0077     /* Pad out to 56 mod 64 and append length */
0078     index = sctx->count % SHA256_BLOCK_SIZE;
0079     padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56) - index);
0080 
0081     /* We need to fill a whole block for __sha256_sparc64_update() */
0082     if (padlen <= 56) {
0083         sctx->count += padlen;
0084         memcpy(sctx->buf + index, padding, padlen);
0085     } else {
0086         __sha256_sparc64_update(sctx, padding, padlen, index);
0087     }
0088     __sha256_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
0089 
0090     /* Store state in digest */
0091     for (i = 0; i < 8; i++)
0092         dst[i] = cpu_to_be32(sctx->state[i]);
0093 
0094     /* Wipe context */
0095     memset(sctx, 0, sizeof(*sctx));
0096 
0097     return 0;
0098 }
0099 
0100 static int sha224_sparc64_final(struct shash_desc *desc, u8 *hash)
0101 {
0102     u8 D[SHA256_DIGEST_SIZE];
0103 
0104     sha256_sparc64_final(desc, D);
0105 
0106     memcpy(hash, D, SHA224_DIGEST_SIZE);
0107     memzero_explicit(D, SHA256_DIGEST_SIZE);
0108 
0109     return 0;
0110 }
0111 
0112 static int sha256_sparc64_export(struct shash_desc *desc, void *out)
0113 {
0114     struct sha256_state *sctx = shash_desc_ctx(desc);
0115 
0116     memcpy(out, sctx, sizeof(*sctx));
0117     return 0;
0118 }
0119 
0120 static int sha256_sparc64_import(struct shash_desc *desc, const void *in)
0121 {
0122     struct sha256_state *sctx = shash_desc_ctx(desc);
0123 
0124     memcpy(sctx, in, sizeof(*sctx));
0125     return 0;
0126 }
0127 
0128 static struct shash_alg sha256_alg = {
0129     .digestsize =   SHA256_DIGEST_SIZE,
0130     .init       =   sha256_base_init,
0131     .update     =   sha256_sparc64_update,
0132     .final      =   sha256_sparc64_final,
0133     .export     =   sha256_sparc64_export,
0134     .import     =   sha256_sparc64_import,
0135     .descsize   =   sizeof(struct sha256_state),
0136     .statesize  =   sizeof(struct sha256_state),
0137     .base       =   {
0138         .cra_name   =   "sha256",
0139         .cra_driver_name=   "sha256-sparc64",
0140         .cra_priority   =   SPARC_CR_OPCODE_PRIORITY,
0141         .cra_blocksize  =   SHA256_BLOCK_SIZE,
0142         .cra_module =   THIS_MODULE,
0143     }
0144 };
0145 
0146 static struct shash_alg sha224_alg = {
0147     .digestsize =   SHA224_DIGEST_SIZE,
0148     .init       =   sha224_base_init,
0149     .update     =   sha256_sparc64_update,
0150     .final      =   sha224_sparc64_final,
0151     .descsize   =   sizeof(struct sha256_state),
0152     .base       =   {
0153         .cra_name   =   "sha224",
0154         .cra_driver_name=   "sha224-sparc64",
0155         .cra_priority   =   SPARC_CR_OPCODE_PRIORITY,
0156         .cra_blocksize  =   SHA224_BLOCK_SIZE,
0157         .cra_module =   THIS_MODULE,
0158     }
0159 };
0160 
0161 static bool __init sparc64_has_sha256_opcode(void)
0162 {
0163     unsigned long cfr;
0164 
0165     if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
0166         return false;
0167 
0168     __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
0169     if (!(cfr & CFR_SHA256))
0170         return false;
0171 
0172     return true;
0173 }
0174 
0175 static int __init sha256_sparc64_mod_init(void)
0176 {
0177     if (sparc64_has_sha256_opcode()) {
0178         int ret = crypto_register_shash(&sha224_alg);
0179         if (ret < 0)
0180             return ret;
0181 
0182         ret = crypto_register_shash(&sha256_alg);
0183         if (ret < 0) {
0184             crypto_unregister_shash(&sha224_alg);
0185             return ret;
0186         }
0187 
0188         pr_info("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n");
0189         return 0;
0190     }
0191     pr_info("sparc64 sha256 opcode not available.\n");
0192     return -ENODEV;
0193 }
0194 
0195 static void __exit sha256_sparc64_mod_fini(void)
0196 {
0197     crypto_unregister_shash(&sha224_alg);
0198     crypto_unregister_shash(&sha256_alg);
0199 }
0200 
0201 module_init(sha256_sparc64_mod_init);
0202 module_exit(sha256_sparc64_mod_fini);
0203 
0204 MODULE_LICENSE("GPL");
0205 MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm, sparc64 sha256 opcode accelerated");
0206 
0207 MODULE_ALIAS_CRYPTO("sha224");
0208 MODULE_ALIAS_CRYPTO("sha256");
0209 
0210 #include "crop_devid.c"