109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 24ff28d4cSDavid S. Miller /* Glue code for SHA1 hashing optimized for sparc64 crypto opcodes. 34ff28d4cSDavid S. Miller * 44ff28d4cSDavid S. Miller * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c 54ff28d4cSDavid S. Miller * 64ff28d4cSDavid S. Miller * Copyright (c) Alan Smithee. 74ff28d4cSDavid S. Miller * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 84ff28d4cSDavid S. Miller * Copyright (c) Jean-Francois Dive <jef@linuxbe.org> 94ff28d4cSDavid S. Miller * Copyright (c) Mathias Krause <minipli@googlemail.com> 104ff28d4cSDavid S. Miller */ 114ff28d4cSDavid S. Miller 124ff28d4cSDavid S. Miller #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 134ff28d4cSDavid S. Miller 144ff28d4cSDavid S. Miller #include <crypto/internal/hash.h> 154ff28d4cSDavid S. Miller #include <linux/init.h> 164ff28d4cSDavid S. Miller #include <linux/module.h> 174ff28d4cSDavid S. Miller #include <linux/mm.h> 184ff28d4cSDavid S. Miller #include <linux/types.h> 19a24d22b2SEric Biggers #include <crypto/sha1.h> 20*e0583b6aSTianjia Zhang #include <crypto/sha1_base.h> 214ff28d4cSDavid S. Miller 224ff28d4cSDavid S. Miller #include <asm/pstate.h> 234ff28d4cSDavid S. Miller #include <asm/elf.h> 244ff28d4cSDavid S. Miller 2510803624SDavid S. Miller #include "opcodes.h" 2610803624SDavid S. Miller 274ff28d4cSDavid S. Miller asmlinkage void sha1_sparc64_transform(u32 *digest, const char *data, 284ff28d4cSDavid S. Miller unsigned int rounds); 294ff28d4cSDavid S. Miller 304ff28d4cSDavid S. Miller static void __sha1_sparc64_update(struct sha1_state *sctx, const u8 *data, 314ff28d4cSDavid S. Miller unsigned int len, unsigned int partial) 324ff28d4cSDavid S. Miller { 334ff28d4cSDavid S. Miller unsigned int done = 0; 344ff28d4cSDavid S. Miller 354ff28d4cSDavid S. Miller sctx->count += len; 364ff28d4cSDavid S. Miller if (partial) { 374ff28d4cSDavid S. Miller done = SHA1_BLOCK_SIZE - partial; 384ff28d4cSDavid S. Miller memcpy(sctx->buffer + partial, data, done); 394ff28d4cSDavid S. Miller sha1_sparc64_transform(sctx->state, sctx->buffer, 1); 404ff28d4cSDavid S. Miller } 414ff28d4cSDavid S. Miller if (len - done >= SHA1_BLOCK_SIZE) { 424ff28d4cSDavid S. Miller const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE; 434ff28d4cSDavid S. Miller 444ff28d4cSDavid S. Miller sha1_sparc64_transform(sctx->state, data + done, rounds); 454ff28d4cSDavid S. Miller done += rounds * SHA1_BLOCK_SIZE; 464ff28d4cSDavid S. Miller } 474ff28d4cSDavid S. Miller 484ff28d4cSDavid S. Miller memcpy(sctx->buffer, data + done, len - done); 494ff28d4cSDavid S. Miller } 504ff28d4cSDavid S. Miller 514ff28d4cSDavid S. Miller static int sha1_sparc64_update(struct shash_desc *desc, const u8 *data, 524ff28d4cSDavid S. Miller unsigned int len) 534ff28d4cSDavid S. Miller { 544ff28d4cSDavid S. Miller struct sha1_state *sctx = shash_desc_ctx(desc); 554ff28d4cSDavid S. Miller unsigned int partial = sctx->count % SHA1_BLOCK_SIZE; 564ff28d4cSDavid S. Miller 574ff28d4cSDavid S. Miller /* Handle the fast case right here */ 584ff28d4cSDavid S. Miller if (partial + len < SHA1_BLOCK_SIZE) { 594ff28d4cSDavid S. Miller sctx->count += len; 604ff28d4cSDavid S. Miller memcpy(sctx->buffer + partial, data, len); 614ff28d4cSDavid S. Miller } else 624ff28d4cSDavid S. Miller __sha1_sparc64_update(sctx, data, len, partial); 634ff28d4cSDavid S. Miller 644ff28d4cSDavid S. Miller return 0; 654ff28d4cSDavid S. Miller } 664ff28d4cSDavid S. Miller 674ff28d4cSDavid S. Miller /* Add padding and return the message digest. */ 684ff28d4cSDavid S. Miller static int sha1_sparc64_final(struct shash_desc *desc, u8 *out) 694ff28d4cSDavid S. Miller { 704ff28d4cSDavid S. Miller struct sha1_state *sctx = shash_desc_ctx(desc); 714ff28d4cSDavid S. Miller unsigned int i, index, padlen; 724ff28d4cSDavid S. Miller __be32 *dst = (__be32 *)out; 734ff28d4cSDavid S. Miller __be64 bits; 744ff28d4cSDavid S. Miller static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, }; 754ff28d4cSDavid S. Miller 764ff28d4cSDavid S. Miller bits = cpu_to_be64(sctx->count << 3); 774ff28d4cSDavid S. Miller 784ff28d4cSDavid S. Miller /* Pad out to 56 mod 64 and append length */ 794ff28d4cSDavid S. Miller index = sctx->count % SHA1_BLOCK_SIZE; 804ff28d4cSDavid S. Miller padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index); 814ff28d4cSDavid S. Miller 824ff28d4cSDavid S. Miller /* We need to fill a whole block for __sha1_sparc64_update() */ 834ff28d4cSDavid S. Miller if (padlen <= 56) { 844ff28d4cSDavid S. Miller sctx->count += padlen; 854ff28d4cSDavid S. Miller memcpy(sctx->buffer + index, padding, padlen); 864ff28d4cSDavid S. Miller } else { 874ff28d4cSDavid S. Miller __sha1_sparc64_update(sctx, padding, padlen, index); 884ff28d4cSDavid S. Miller } 894ff28d4cSDavid S. Miller __sha1_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56); 904ff28d4cSDavid S. Miller 914ff28d4cSDavid S. Miller /* Store state in digest */ 924ff28d4cSDavid S. Miller for (i = 0; i < 5; i++) 934ff28d4cSDavid S. Miller dst[i] = cpu_to_be32(sctx->state[i]); 944ff28d4cSDavid S. Miller 954ff28d4cSDavid S. Miller /* Wipe context */ 964ff28d4cSDavid S. Miller memset(sctx, 0, sizeof(*sctx)); 974ff28d4cSDavid S. Miller 984ff28d4cSDavid S. Miller return 0; 994ff28d4cSDavid S. Miller } 1004ff28d4cSDavid S. Miller 1014ff28d4cSDavid S. Miller static int sha1_sparc64_export(struct shash_desc *desc, void *out) 1024ff28d4cSDavid S. Miller { 1034ff28d4cSDavid S. Miller struct sha1_state *sctx = shash_desc_ctx(desc); 1044ff28d4cSDavid S. Miller 1054ff28d4cSDavid S. Miller memcpy(out, sctx, sizeof(*sctx)); 1064ff28d4cSDavid S. Miller 1074ff28d4cSDavid S. Miller return 0; 1084ff28d4cSDavid S. Miller } 1094ff28d4cSDavid S. Miller 1104ff28d4cSDavid S. Miller static int sha1_sparc64_import(struct shash_desc *desc, const void *in) 1114ff28d4cSDavid S. Miller { 1124ff28d4cSDavid S. Miller struct sha1_state *sctx = shash_desc_ctx(desc); 1134ff28d4cSDavid S. Miller 1144ff28d4cSDavid S. Miller memcpy(sctx, in, sizeof(*sctx)); 1154ff28d4cSDavid S. Miller 1164ff28d4cSDavid S. Miller return 0; 1174ff28d4cSDavid S. Miller } 1184ff28d4cSDavid S. Miller 1194ff28d4cSDavid S. Miller static struct shash_alg alg = { 1204ff28d4cSDavid S. Miller .digestsize = SHA1_DIGEST_SIZE, 121*e0583b6aSTianjia Zhang .init = sha1_base_init, 1224ff28d4cSDavid S. Miller .update = sha1_sparc64_update, 1234ff28d4cSDavid S. Miller .final = sha1_sparc64_final, 1244ff28d4cSDavid S. Miller .export = sha1_sparc64_export, 1254ff28d4cSDavid S. Miller .import = sha1_sparc64_import, 1264ff28d4cSDavid S. Miller .descsize = sizeof(struct sha1_state), 1274ff28d4cSDavid S. Miller .statesize = sizeof(struct sha1_state), 1284ff28d4cSDavid S. Miller .base = { 1294ff28d4cSDavid S. Miller .cra_name = "sha1", 1304ff28d4cSDavid S. Miller .cra_driver_name= "sha1-sparc64", 13110803624SDavid S. Miller .cra_priority = SPARC_CR_OPCODE_PRIORITY, 1324ff28d4cSDavid S. Miller .cra_blocksize = SHA1_BLOCK_SIZE, 1334ff28d4cSDavid S. Miller .cra_module = THIS_MODULE, 1344ff28d4cSDavid S. Miller } 1354ff28d4cSDavid S. Miller }; 1364ff28d4cSDavid S. Miller 1374ff28d4cSDavid S. Miller static bool __init sparc64_has_sha1_opcode(void) 1384ff28d4cSDavid S. Miller { 1394ff28d4cSDavid S. Miller unsigned long cfr; 1404ff28d4cSDavid S. Miller 1414ff28d4cSDavid S. Miller if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO)) 1424ff28d4cSDavid S. Miller return false; 1434ff28d4cSDavid S. Miller 1444ff28d4cSDavid S. Miller __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr)); 1454ff28d4cSDavid S. Miller if (!(cfr & CFR_SHA1)) 1464ff28d4cSDavid S. Miller return false; 1474ff28d4cSDavid S. Miller 1484ff28d4cSDavid S. Miller return true; 1494ff28d4cSDavid S. Miller } 1504ff28d4cSDavid S. Miller 1514ff28d4cSDavid S. Miller static int __init sha1_sparc64_mod_init(void) 1524ff28d4cSDavid S. Miller { 1534ff28d4cSDavid S. Miller if (sparc64_has_sha1_opcode()) { 1544ff28d4cSDavid S. Miller pr_info("Using sparc64 sha1 opcode optimized SHA-1 implementation\n"); 1554ff28d4cSDavid S. Miller return crypto_register_shash(&alg); 1564ff28d4cSDavid S. Miller } 1574ff28d4cSDavid S. Miller pr_info("sparc64 sha1 opcode not available.\n"); 1584ff28d4cSDavid S. Miller return -ENODEV; 1594ff28d4cSDavid S. Miller } 1604ff28d4cSDavid S. Miller 1614ff28d4cSDavid S. Miller static void __exit sha1_sparc64_mod_fini(void) 1624ff28d4cSDavid S. Miller { 1634ff28d4cSDavid S. Miller crypto_unregister_shash(&alg); 1644ff28d4cSDavid S. Miller } 1654ff28d4cSDavid S. Miller 1664ff28d4cSDavid S. Miller module_init(sha1_sparc64_mod_init); 1674ff28d4cSDavid S. Miller module_exit(sha1_sparc64_mod_fini); 1684ff28d4cSDavid S. Miller 1694ff28d4cSDavid S. Miller MODULE_LICENSE("GPL"); 1704ff28d4cSDavid S. Miller MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, sparc64 sha1 opcode accelerated"); 1714ff28d4cSDavid S. Miller 1725d26a105SKees Cook MODULE_ALIAS_CRYPTO("sha1"); 173226f7ceaSDavid S. Miller 174226f7ceaSDavid S. Miller #include "crop_devid.c" 175