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