Back to home page

OSCL-LXR

 
 

    


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