^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Glue code for MD5 hashing optimized for sparc64 crypto opcodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * and crypto/md5.c which are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) Alan Smithee.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (c) Mathias Krause <minipli@googlemail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (c) Cryptoapi developers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <crypto/md5.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/pstate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "opcodes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) asmlinkage void md5_sparc64_transform(u32 *digest, const char *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned int rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int md5_sparc64_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct md5_state *mctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) mctx->hash[0] = cpu_to_le32(MD5_H0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) mctx->hash[1] = cpu_to_le32(MD5_H1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) mctx->hash[2] = cpu_to_le32(MD5_H2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) mctx->hash[3] = cpu_to_le32(MD5_H3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) mctx->byte_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static void __md5_sparc64_update(struct md5_state *sctx, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int len, unsigned int partial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) sctx->byte_count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (partial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) done = MD5_HMAC_BLOCK_SIZE - partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) memcpy((u8 *)sctx->block + partial, data, done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) md5_sparc64_transform(sctx->hash, (u8 *)sctx->block, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (len - done >= MD5_HMAC_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) const unsigned int rounds = (len - done) / MD5_HMAC_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) md5_sparc64_transform(sctx->hash, data + done, rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) done += rounds * MD5_HMAC_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) memcpy(sctx->block, data + done, len - done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static int md5_sparc64_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned int partial = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Handle the fast case right here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (partial + len < MD5_HMAC_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) sctx->byte_count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) memcpy((u8 *)sctx->block + partial, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) __md5_sparc64_update(sctx, data, len, partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* Add padding and return the message digest. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int md5_sparc64_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned int i, index, padlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u32 *dst = (u32 *)out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) __le64 bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static const u8 padding[MD5_HMAC_BLOCK_SIZE] = { 0x80, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) bits = cpu_to_le64(sctx->byte_count << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* Pad out to 56 mod 64 and append length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) index = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) padlen = (index < 56) ? (56 - index) : ((MD5_HMAC_BLOCK_SIZE+56) - index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* We need to fill a whole block for __md5_sparc64_update() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (padlen <= 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) sctx->byte_count += padlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) memcpy((u8 *)sctx->block + index, padding, padlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __md5_sparc64_update(sctx, padding, padlen, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __md5_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Store state in digest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) for (i = 0; i < MD5_HASH_WORDS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dst[i] = sctx->hash[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Wipe context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) memset(sctx, 0, sizeof(*sctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int md5_sparc64_export(struct shash_desc *desc, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) memcpy(out, sctx, sizeof(*sctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int md5_sparc64_import(struct shash_desc *desc, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct md5_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) memcpy(sctx, in, sizeof(*sctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static struct shash_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .digestsize = MD5_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .init = md5_sparc64_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .update = md5_sparc64_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .final = md5_sparc64_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .export = md5_sparc64_export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .import = md5_sparc64_import,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .descsize = sizeof(struct md5_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .statesize = sizeof(struct md5_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .cra_name = "md5",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .cra_driver_name= "md5-sparc64",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .cra_priority = SPARC_CR_OPCODE_PRIORITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static bool __init sparc64_has_md5_opcode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long cfr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!(cfr & CFR_MD5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int __init md5_sparc64_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (sparc64_has_md5_opcode()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) pr_info("Using sparc64 md5 opcode optimized MD5 implementation\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return crypto_register_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pr_info("sparc64 md5 opcode not available.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void __exit md5_sparc64_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) crypto_unregister_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) module_init(md5_sparc64_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) module_exit(md5_sparc64_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) MODULE_DESCRIPTION("MD5 Message Digest Algorithm, sparc64 md5 opcode accelerated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) MODULE_ALIAS_CRYPTO("md5");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #include "crop_devid.c"