Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: sparc/sha256 - implement library instead of shash

Instead of providing crypto_shash algorithms for the arch-optimized
SHA-256 code, instead implement the SHA-256 library. This is much
simpler, it makes the SHA-256 library functions be arch-optimized, and
it fixes the longstanding issue where the arch-optimized SHA-256 was
disabled by default. SHA-256 still remains available through
crypto_shash, but individual architectures no longer need to handle it.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
699618d4 77735920

+81 -141
-10
arch/sparc/crypto/Kconfig
··· 36 36 37 37 Architecture: sparc64 38 38 39 - config CRYPTO_SHA256_SPARC64 40 - tristate "Hash functions: SHA-224 and SHA-256" 41 - depends on SPARC64 42 - select CRYPTO_SHA256 43 - select CRYPTO_HASH 44 - help 45 - SHA-224 and SHA-256 secure hash algorithms (FIPS 180) 46 - 47 - Architecture: sparc64 using crypto instructions, when available 48 - 49 39 config CRYPTO_SHA512_SPARC64 50 40 tristate "Hash functions: SHA-384 and SHA-512" 51 41 depends on SPARC64
-2
arch/sparc/crypto/Makefile
··· 4 4 # 5 5 6 6 obj-$(CONFIG_CRYPTO_SHA1_SPARC64) += sha1-sparc64.o 7 - obj-$(CONFIG_CRYPTO_SHA256_SPARC64) += sha256-sparc64.o 8 7 obj-$(CONFIG_CRYPTO_SHA512_SPARC64) += sha512-sparc64.o 9 8 obj-$(CONFIG_CRYPTO_MD5_SPARC64) += md5-sparc64.o 10 9 ··· 12 13 obj-$(CONFIG_CRYPTO_CAMELLIA_SPARC64) += camellia-sparc64.o 13 14 14 15 sha1-sparc64-y := sha1_asm.o sha1_glue.o 15 - sha256-sparc64-y := sha256_asm.o sha256_glue.o 16 16 sha512-sparc64-y := sha512_asm.o sha512_glue.o 17 17 md5-sparc64-y := md5_asm.o md5_glue.o 18 18
+1 -1
arch/sparc/crypto/sha256_asm.S arch/sparc/lib/crypto/sha256_asm.S
··· 4 4 #include <asm/visasm.h> 5 5 6 6 ENTRY(sha256_sparc64_transform) 7 - /* %o0 = digest, %o1 = data, %o2 = rounds */ 7 + /* %o0 = state, %o1 = data, %o2 = nblocks */ 8 8 VISEntryHalf 9 9 ld [%o0 + 0x00], %f0 10 10 ld [%o0 + 0x04], %f1
-128
arch/sparc/crypto/sha256_glue.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* Glue code for SHA256 hashing optimized for sparc64 crypto opcodes. 3 - * 4 - * This is based largely upon crypto/sha256_generic.c 5 - * 6 - * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com> 7 - * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 8 - * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 9 - * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com> 10 - */ 11 - 12 - #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 13 - 14 - #include <asm/elf.h> 15 - #include <asm/opcodes.h> 16 - #include <asm/pstate.h> 17 - #include <crypto/internal/hash.h> 18 - #include <crypto/sha2.h> 19 - #include <crypto/sha256_base.h> 20 - #include <linux/kernel.h> 21 - #include <linux/module.h> 22 - 23 - asmlinkage void sha256_sparc64_transform(u32 *digest, const char *data, 24 - unsigned int rounds); 25 - 26 - static void sha256_block(struct crypto_sha256_state *sctx, const u8 *src, 27 - int blocks) 28 - { 29 - sha256_sparc64_transform(sctx->state, src, blocks); 30 - } 31 - 32 - static int sha256_sparc64_update(struct shash_desc *desc, const u8 *data, 33 - unsigned int len) 34 - { 35 - return sha256_base_do_update_blocks(desc, data, len, sha256_block); 36 - } 37 - 38 - static int sha256_sparc64_finup(struct shash_desc *desc, const u8 *src, 39 - unsigned int len, u8 *out) 40 - { 41 - sha256_base_do_finup(desc, src, len, sha256_block); 42 - return sha256_base_finish(desc, out); 43 - } 44 - 45 - static struct shash_alg sha256_alg = { 46 - .digestsize = SHA256_DIGEST_SIZE, 47 - .init = sha256_base_init, 48 - .update = sha256_sparc64_update, 49 - .finup = sha256_sparc64_finup, 50 - .descsize = sizeof(struct crypto_sha256_state), 51 - .base = { 52 - .cra_name = "sha256", 53 - .cra_driver_name= "sha256-sparc64", 54 - .cra_priority = SPARC_CR_OPCODE_PRIORITY, 55 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | 56 - CRYPTO_AHASH_ALG_FINUP_MAX, 57 - .cra_blocksize = SHA256_BLOCK_SIZE, 58 - .cra_module = THIS_MODULE, 59 - } 60 - }; 61 - 62 - static struct shash_alg sha224_alg = { 63 - .digestsize = SHA224_DIGEST_SIZE, 64 - .init = sha224_base_init, 65 - .update = sha256_sparc64_update, 66 - .finup = sha256_sparc64_finup, 67 - .descsize = sizeof(struct crypto_sha256_state), 68 - .base = { 69 - .cra_name = "sha224", 70 - .cra_driver_name= "sha224-sparc64", 71 - .cra_priority = SPARC_CR_OPCODE_PRIORITY, 72 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | 73 - CRYPTO_AHASH_ALG_FINUP_MAX, 74 - .cra_blocksize = SHA224_BLOCK_SIZE, 75 - .cra_module = THIS_MODULE, 76 - } 77 - }; 78 - 79 - static bool __init sparc64_has_sha256_opcode(void) 80 - { 81 - unsigned long cfr; 82 - 83 - if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO)) 84 - return false; 85 - 86 - __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr)); 87 - if (!(cfr & CFR_SHA256)) 88 - return false; 89 - 90 - return true; 91 - } 92 - 93 - static int __init sha256_sparc64_mod_init(void) 94 - { 95 - if (sparc64_has_sha256_opcode()) { 96 - int ret = crypto_register_shash(&sha224_alg); 97 - if (ret < 0) 98 - return ret; 99 - 100 - ret = crypto_register_shash(&sha256_alg); 101 - if (ret < 0) { 102 - crypto_unregister_shash(&sha224_alg); 103 - return ret; 104 - } 105 - 106 - pr_info("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n"); 107 - return 0; 108 - } 109 - pr_info("sparc64 sha256 opcode not available.\n"); 110 - return -ENODEV; 111 - } 112 - 113 - static void __exit sha256_sparc64_mod_fini(void) 114 - { 115 - crypto_unregister_shash(&sha224_alg); 116 - crypto_unregister_shash(&sha256_alg); 117 - } 118 - 119 - module_init(sha256_sparc64_mod_init); 120 - module_exit(sha256_sparc64_mod_fini); 121 - 122 - MODULE_LICENSE("GPL"); 123 - MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm, sparc64 sha256 opcode accelerated"); 124 - 125 - MODULE_ALIAS_CRYPTO("sha224"); 126 - MODULE_ALIAS_CRYPTO("sha256"); 127 - 128 - #include "crop_devid.c"
+1
arch/sparc/lib/Makefile
··· 4 4 5 5 asflags-y := -ansi -DST_DIV0=0x02 6 6 7 + obj-y += crypto/ 7 8 lib-$(CONFIG_SPARC32) += ashrdi3.o 8 9 lib-$(CONFIG_SPARC32) += memcpy.o memset.o 9 10 lib-y += strlen.o
+8
arch/sparc/lib/crypto/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + 3 + config CRYPTO_SHA256_SPARC64 4 + tristate 5 + depends on SPARC64 6 + default CRYPTO_LIB_SHA256 7 + select CRYPTO_ARCH_HAVE_LIB_SHA256 8 + select CRYPTO_LIB_SHA256_GENERIC
+4
arch/sparc/lib/crypto/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + 3 + obj-$(CONFIG_CRYPTO_SHA256_SPARC64) += sha256-sparc64.o 4 + sha256-sparc64-y := sha256.o sha256_asm.o
+64
arch/sparc/lib/crypto/sha256.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * SHA-256 accelerated using the sparc64 sha256 opcodes 4 + * 5 + * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com> 6 + * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 7 + * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 8 + * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com> 9 + */ 10 + 11 + #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 + 13 + #include <asm/elf.h> 14 + #include <asm/opcodes.h> 15 + #include <asm/pstate.h> 16 + #include <crypto/internal/sha2.h> 17 + #include <linux/kernel.h> 18 + #include <linux/module.h> 19 + 20 + static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_sha256_opcodes); 21 + 22 + asmlinkage void sha256_sparc64_transform(u32 state[SHA256_STATE_WORDS], 23 + const u8 *data, size_t nblocks); 24 + 25 + void sha256_blocks_arch(u32 state[SHA256_STATE_WORDS], 26 + const u8 *data, size_t nblocks) 27 + { 28 + if (static_branch_likely(&have_sha256_opcodes)) 29 + sha256_sparc64_transform(state, data, nblocks); 30 + else 31 + sha256_blocks_generic(state, data, nblocks); 32 + } 33 + EXPORT_SYMBOL(sha256_blocks_arch); 34 + 35 + bool sha256_is_arch_optimized(void) 36 + { 37 + return static_key_enabled(&have_sha256_opcodes); 38 + } 39 + EXPORT_SYMBOL(sha256_is_arch_optimized); 40 + 41 + static int __init sha256_sparc64_mod_init(void) 42 + { 43 + unsigned long cfr; 44 + 45 + if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO)) 46 + return 0; 47 + 48 + __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr)); 49 + if (!(cfr & CFR_SHA256)) 50 + return 0; 51 + 52 + static_branch_enable(&have_sha256_opcodes); 53 + pr_info("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n"); 54 + return 0; 55 + } 56 + arch_initcall(sha256_sparc64_mod_init); 57 + 58 + static void __exit sha256_sparc64_mod_exit(void) 59 + { 60 + } 61 + module_exit(sha256_sparc64_mod_exit); 62 + 63 + MODULE_LICENSE("GPL"); 64 + MODULE_DESCRIPTION("SHA-256 accelerated using the sparc64 sha256 opcodes");
+3
lib/crypto/Kconfig
··· 181 181 if S390 182 182 source "arch/s390/lib/crypto/Kconfig" 183 183 endif 184 + if SPARC 185 + source "arch/sparc/lib/crypto/Kconfig" 186 + endif 184 187 if X86 185 188 source "arch/x86/lib/crypto/Kconfig" 186 189 endif