crypto: shash - Handle partial blocks in API
Subsystems (14)
Herbert Xu
maintainercover crypto: shash - Handle partial blocks in API
This series is based on
https://lore.kernel.org/linux-crypto/[email protected]
Most hash algorithms are block-based and data is held back until a whole block can be fed into the algorithm. Currently, this retention is entirely handled in the low-level algorithm code, with the API and the user oblivious to it.
As a comparison, the block ciphers defer this retention entirely to the user and the API, who is expected to provide input with no partial data until the very end.
The result of this is that every shash algorithm has to implement partial block handling, and it gets many times worse when you look at ahash drivers.
This patch series adds optional handling of partial blocks to the shash API and converts some of the shash algorithms to it. In particular, all the algorithms used by ahash drivers have been converted. This ensures that when the ahash drivers themselves are converted the export format will be consistent.
As this touches so many shash algorithms, two extra changes have been made at the same time: removal of SIMD fallback paths on arm/arm64/x86 and exporting in the same format as that of the generic algorithm.
Herbert Xu (67):
crypto: shash - Handle partial blocks in API
crypto: blake2b-generic - Use API partial block handling
crypto: arm/blake2b - Use API partial block handling
crypto: ghash-generic - Use API partial block handling
crypto: powerpc/ghash - Use API partial block handling
crypto: arm/ghash - Use API partial block handling
crypto: arm64/ghash - Use API partial block handling
crypto: riscv/ghash - Use API partial block handling
crypto: s390/ghash - Use API partial block handling
crypto: x86/ghash - Use API partial block handling
crypto: md5-generic - Use API partial block handling
crypto: mips/octeon-md5 - Use API partial block handling
crypto: powerpc/md5 - Use API partial block handling
crypto: sparc/md5 - Use API partial block handling
crypto: x86/sha1 - Use API partial block handling
crypto: arm64/sha1 - Use API partial block handling
crypto: mips/octeon-sha1 - Use API partial block handling
crypto: sha1-generic - Use API partial block handling
crypto: arm/sha1-ce - Use API partial block handling
crypto: arm/sha1-neon - Use API partial block handling
crypto: arm/sha1-asm - Use API partial block handling
crypto: powerpc/sha1 - Use API partial block handling
crypto: powerpc/sha1-spe - Use API partial block handling
crypto: s390/sha1 - Use API partial block handling
crypto: sparc/sha1 - Use API partial block handling
crypto: sha1_base - Remove partial block helpers
crypto: x86/sha256 - Use API partial block handling
crypto: mips/octeon-sha256 - Use API partial block handling
crypto: riscv/sha256 - Use API partial block handling
crypto: sha256-generic - Use API partial block handling
crypto: arm/sha256-ce - Use API partial block handling
crypto: arm/sha256-neon - Use API partial block handling
crypto: arm/sha256-asm - Use API partial block handling
crypto: arm64/sha256-ce - Use API partial block handling
crypto: arm64/sha256 - Use API partial block handling
crypto: powerpc/sha256-spe - Use API partial block handling
crypto: s390/sha256 - Use API partial block handling
crypto: sparc/sha256 - Use API partial block handling
crypto: sha256_base - Remove partial block helpers
crypto: arm64/sha3-ce - Use API partial block handling
crypto: s390/sha3 - Use API partial block handling
crypto: sha3-generic - Use API partial block handling
crypto: zynqmp-sha - Use API partial block handling
crypto: x86/sha512 - Use API partial block handling
crypto: mips/octeon-sha512 - Use API partial block handling
crypto: riscv/sha512 - Use API partial block handling
crypto: sha512-generic - Use API partial block handling
crypto: arm/sha512-neon - Use API partial block handling
crypto: arm/sha512-asm - Use API partial block handling
crypto: arm64/sha512-ce - Use API partial block handling
crypto: arm/sha512 - Use API partial block handling
crypto: s390/sha512 - Use API partial block handling
crypto: sparc/sha512 - Use API partial block handling
crypto: sha512_base - Remove partial block helpers
crypto: sm3-generic - Use API partial block handling
crypto: arm64/sm3-ce - Use API partial block handling
crypto: arm64/sm3-neon - Use API partial block handling
crypto: riscv/sm3 - Use API partial block handling
crypto: x86/sm3 - Use API partial block handling
crypto: lib/sm3 - Remove partial block helpers
crypto: cbcmac - Use API partial block handling
crypto: cmac - Use API partial block handling
crypto: xcbc - Use API partial block handling
crypto: arm64/aes - Use API partial block handling
crypto: arm64/sm4 - Use API partial block handling
crypto: nx - Use API partial block handling
crypto: padlock-sha - Use API partial block handling
arch/arm/crypto/blake2b-neon-glue.c | 20 +- arch/arm/crypto/ghash-ce-glue.c | 110 ++--- arch/arm/crypto/sha1-ce-glue.c | 36 +- arch/arm/crypto/sha1.h | 14 - arch/arm/crypto/sha1_glue.c | 33 +- arch/arm/crypto/sha1_neon_glue.c | 39 +- arch/arm/crypto/sha2-ce-glue.c | 52 +- arch/arm/crypto/sha256_glue.c | 46 +- arch/arm/crypto/sha256_glue.h | 8 +- arch/arm/crypto/sha256_neon_glue.c | 49 +- arch/arm/crypto/sha512-glue.c | 36 +- arch/arm/crypto/sha512-neon-glue.c | 43 +- arch/arm/crypto/sha512.h | 6 - arch/arm64/crypto/aes-glue.c | 122 ++--- arch/arm64/crypto/ghash-ce-glue.c | 151 +++--- arch/arm64/crypto/sha1-ce-glue.c | 66 +-- arch/arm64/crypto/sha2-ce-glue.c | 90 +--- arch/arm64/crypto/sha256-glue.c | 97 ++-- arch/arm64/crypto/sha3-ce-glue.c | 107 ++-- arch/arm64/crypto/sha512-ce-glue.c | 49 +- arch/arm64/crypto/sha512-glue.c | 28 +- arch/arm64/crypto/sm3-ce-glue.c | 48 +- arch/arm64/crypto/sm3-neon-glue.c | 48 +- arch/arm64/crypto/sm4-ce-glue.c | 98 ++-- arch/mips/cavium-octeon/crypto/octeon-md5.c | 119 ++--- arch/mips/cavium-octeon/crypto/octeon-sha1.c | 136 ++--- .../mips/cavium-octeon/crypto/octeon-sha256.c | 161 ++---- .../mips/cavium-octeon/crypto/octeon-sha512.c | 155 ++---- arch/powerpc/crypto/ghash.c | 87 ++-- arch/powerpc/crypto/md5-glue.c | 99 +--- arch/powerpc/crypto/sha1-spe-glue.c | 132 +---- arch/powerpc/crypto/sha1.c | 101 +--- arch/powerpc/crypto/sha256-spe-glue.c | 167 ++----- arch/riscv/crypto/ghash-riscv64-glue.c | 58 +-- arch/riscv/crypto/sha256-riscv64-glue.c | 68 ++- arch/riscv/crypto/sha512-riscv64-glue.c | 47 +- arch/riscv/crypto/sm3-riscv64-glue.c | 49 +- arch/s390/crypto/ghash_s390.c | 110 ++--- arch/s390/crypto/sha.h | 22 +- arch/s390/crypto/sha1_s390.c | 22 +- arch/s390/crypto/sha256_s390.c | 35 +- arch/s390/crypto/sha3_256_s390.c | 60 +-- arch/s390/crypto/sha3_512_s390.c | 67 +-- arch/s390/crypto/sha512_s390.c | 45 +- arch/s390/crypto/sha_common.c | 84 ++-- arch/sparc/crypto/md5_glue.c | 141 +++--- arch/sparc/crypto/sha1_glue.c | 109 +--- arch/sparc/crypto/sha256_glue.c | 121 +---- arch/sparc/crypto/sha512_glue.c | 102 +--- arch/x86/crypto/ghash-clmulni-intel_asm.S | 5 +- arch/x86/crypto/ghash-clmulni-intel_glue.c | 301 ++---------- arch/x86/crypto/sha1_ssse3_glue.c | 81 +-- arch/x86/crypto/sha256_ssse3_glue.c | 104 ++-- arch/x86/crypto/sha512_ssse3_glue.c | 79 +-- arch/x86/crypto/sm3_avx_glue.c | 54 +- crypto/blake2b_generic.c | 31 +- crypto/ccm.c | 59 +-- crypto/cmac.c | 92 +--- crypto/ghash-generic.c | 56 +-- crypto/md5.c | 102 ++-- crypto/sha1_generic.c | 33 +- crypto/sha256_generic.c | 50 +- crypto/sha3_generic.c | 101 ++-- crypto/sha512_generic.c | 56 +-- crypto/shash.c | 229 +++++++-- crypto/sm3_generic.c | 31 +- crypto/xcbc.c | 92 +--- drivers/crypto/nx/nx-aes-xcbc.c | 128 ++--- drivers/crypto/nx/nx-sha256.c | 130 ++--- drivers/crypto/nx/nx-sha512.c | 143 +++--- drivers/crypto/nx/nx.c | 15 +- drivers/crypto/nx/nx.h | 6 +- drivers/crypto/padlock-sha.c | 464 ++++++------------ drivers/crypto/xilinx/zynqmp-sha.c | 71 +-- include/crypto/blake2b.h | 31 +- include/crypto/ghash.h | 4 +- include/crypto/hash.h | 104 ++-- include/crypto/internal/blake2b.h | 94 ++-- include/crypto/internal/hash.h | 15 + include/crypto/md5.h | 3 +- include/crypto/sha1.h | 9 +- include/crypto/sha1_base.h | 79 +-- include/crypto/sha2.h | 20 +- include/crypto/sha256_base.h | 111 +++-- include/crypto/sha3.h | 16 +- include/crypto/sha512_base.h | 88 ++-- include/crypto/sm3.h | 3 +- include/crypto/sm3_base.h | 79 ++- include/linux/crypto.h | 2 + lib/crypto/sha256.c | 7 +- lib/crypto/sm3.c | 68 +-- 91 files changed, 2375 insertions(+), 4564 deletions(-) delete mode 100644 arch/arm/crypto/sha1.h
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 1/67 crypto: shash - Handle partial blocks in API
Provide an option to handle the partial blocks in the shash API. Almost every hash algorithm has a block size and are only able to hash partial blocks on finalisation.
Rather than duplicating the partial block handling many times, add this functionality to the shash API.
It is optional (e.g., hmac would never need this by relying on the partial block handling of the underlying hash), and to enable it set the bit CRYPTO_AHASH_ALG_BLOCK_ONLY.
The export format is always that of the underlying hash export, plus the partial block buffer, followed by a single-byte for the partial block length.
Set the bit CRYPTO_AHASH_ALG_FINAL_NONZERO to withhold an extra byte in the partial block. This will come in handy when this is extended to ahash where hardware often can't deal with a zero-length final.
It will also be used for algorithms requiring an extra block for finalisation (e.g., cmac).
As an optimisation, set the bit CRYPTO_AHASH_ALG_FINUP_MAX if the algorithm wishes to get as much data as possible instead of just the last partial block.
The descriptor will be zeroed after finalisation.
--- crypto/shash.c | 229 +++++++++++++++++++++++++++------ include/crypto/hash.h | 104 +++++++++------ include/crypto/internal/hash.h | 15 +++ include/linux/crypto.h | 2 + 4 files changed, 271 insertions(+), 79 deletions(-)
crypto/shash.c +190 -39
@@ -16,6 +16,24 @@#include "hash.h" +static inline bool crypto_shash_block_only (struct crypto_shash *tfm) +{ +return crypto_shash_alg (tfm)->base.cra_flags & +CRYPTO_AHASH_ALG_BLOCK_ONLY ; +} + +static inline bool crypto_shash_final_nonzero (struct crypto_shash *tfm) +{ +return crypto_shash_alg (tfm)->base.cra_flags & +CRYPTO_AHASH_ALG_FINAL_NONZERO ; +} + +static inline bool crypto_shash_finup_max (struct crypto_shash *tfm) +{ +return crypto_shash_alg (tfm)->base.cra_flags & +CRYPTO_AHASH_ALG_FINUP_MAX ; +} +int shash_no_setkey (struct crypto_shash *tfm,const u8 *key,unsigned int keylen) {
@@ -46,18 +64,27 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, }EXPORT_SYMBOL_GPL (crypto_shash_setkey); -int crypto_shash_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static int __crypto_shash_init (struct shash_desc *desc) { -return crypto_shash_alg (desc->tfm)->update (desc, data, len); -} -EXPORT_SYMBOL_GPL (crypto_shash_update); +struct crypto_shash *tfm = desc->tfm; -int crypto_shash_final (struct shash_desc *desc,u8 *out) -{ -return crypto_shash_alg (desc->tfm)->final (desc, out); +if (crypto_shash_block_only (tfm)) { +u8 *buf =shash_desc_ctx (desc); + + buf +=crypto_shash_descsize (tfm) -1 ; + *buf =0 ; + } + +return crypto_shash_alg (tfm)->init (desc); } -EXPORT_SYMBOL_GPL (crypto_shash_final); + +int crypto_shash_init (struct shash_desc *desc) +{ +if (crypto_shash_get_flags (desc->tfm) &CRYPTO_TFM_NEED_KEY ) +return -ENOKEY ; +return __crypto_shash_init (desc); +} +EXPORT_SYMBOL_GPL (crypto_shash_init);static int shash_default_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out)
@@ -68,20 +95,89 @@ static int shash_default_finup(struct shash_desc *desc, const u8 *data, shash->final (desc, out); } -int crypto_shash_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out) +static int crypto_shash_op_and_zero ( +int (*op)(struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out), +struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -return crypto_shash_alg (desc->tfm)->finup (desc, data, len, out); +int err; + + err =op (desc, data, len, out); +memset (shash_desc_ctx (desc),0 ,crypto_shash_descsize (desc->tfm)); +return err; +} + +int crypto_shash_finup (struct shash_desc *restrict desc,const u8 *data, +unsigned int len,u8 *restrict out) +{ +struct crypto_shash *tfm = desc->tfm; +u8 *blenp =shash_desc_ctx (desc); +bool finup_max, nonzero; +unsigned int bs; +int err; +u8 *buf; + +if (!crypto_shash_block_only (tfm)) { +if (out) + goto finup; +return crypto_shash_alg (tfm)->update (desc, data, len); + } + + finup_max = out &&crypto_shash_finup_max (tfm); + +/* Retain extra block for final nonzero algorithms. */ + nonzero =crypto_shash_final_nonzero (tfm); + +/* +* The partial block buffer follows the algorithm desc context. +* The byte following that contains the length. +*/ + blenp +=crypto_shash_descsize (tfm) -1 ; + bs =crypto_shash_blocksize (tfm); + buf = blenp - bs; + +if (likely (!*blenp && finup_max)) + goto finup; + +while ((*blenp + len) >= bs + nonzero) { +unsigned int nbytes = len - nonzero; +const u8 *src = data; + +if (*blenp) { +memcpy (buf + *blenp, data, bs - *blenp); + nbytes = bs; + src = buf; + } + + err =crypto_shash_alg (tfm)->update (desc, src, nbytes); +if (err <0 ) +return err; + + data += nbytes - err - *blenp; + len -= nbytes - err - *blenp; + *blenp =0 ; + } + +if (*blenp || !out) { +memcpy (buf + *blenp, data, len); + *blenp += len; +if (!out) +return 0 ; + data = buf; + len = *blenp; + } + +finup: +return crypto_shash_op_and_zero (crypto_shash_alg (tfm)->finup, desc, + data, len, out); }EXPORT_SYMBOL_GPL (crypto_shash_finup);static int shash_default_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -struct shash_alg *shash =crypto_shash_alg (desc->tfm); - -return shash->init (desc) ?: - shash->finup (desc, data, len, out); +return __crypto_shash_init (desc) ?: +crypto_shash_finup (desc, data, len, out); } int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
@@ -92,7 +188,8 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data,if (crypto_shash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )return -ENOKEY ; -return crypto_shash_alg (tfm)->digest (desc, data, len, out); +return crypto_shash_op_and_zero (crypto_shash_alg (tfm)->digest, desc, + data, len, out); }EXPORT_SYMBOL_GPL (crypto_shash_digest);
@@ -100,44 +197,92 @@ int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, unsigned int len, u8 *out) {SHASH_DESC_ON_STACK (desc ,tfm ); -int err; desc->tfm = tfm; - - err =crypto_shash_digest (desc, data, len, out); - -shash_desc_zero (desc); - -return err; +return crypto_shash_digest (desc, data, len, out); }EXPORT_SYMBOL_GPL (crypto_shash_tfm_digest); +int crypto_shash_export_core (struct shash_desc *desc,void *out) +{ +int (*export)(struct shash_desc *desc,void *out); +struct crypto_shash *tfm = desc->tfm; +u8 *buf =shash_desc_ctx (desc); +unsigned int plen, ss; + + plen =crypto_shash_blocksize (tfm) +1 ; + ss =crypto_shash_statesize (tfm); +if (crypto_shash_block_only (tfm)) + ss -= plen; + export =crypto_shash_alg (tfm)->export; +if (!export) { +memcpy (out, buf, ss); +return 0 ; + } + +return export (desc, out); +} +EXPORT_SYMBOL_GPL (crypto_shash_export_core); +int crypto_shash_export (struct shash_desc *desc,void *out) {struct crypto_shash *tfm = desc->tfm; -struct shash_alg *shash =crypto_shash_alg (tfm); -if (shash->export) -return shash->export (desc, out); +if (crypto_shash_block_only (tfm)) { +unsigned int plen =crypto_shash_blocksize (tfm) +1 ; +unsigned int descsize =crypto_shash_descsize (tfm); +unsigned int ss =crypto_shash_statesize (tfm); +u8 *buf =shash_desc_ctx (desc); -memcpy (out,shash_desc_ctx (desc),crypto_shash_descsize (tfm)); -return 0 ; +memcpy (out + ss - plen, buf + descsize - plen, plen); + } +return crypto_shash_export_core (desc, out); }EXPORT_SYMBOL_GPL (crypto_shash_export); -int crypto_shash_import (struct shash_desc *desc,const void *in) +int crypto_shash_import_core (struct shash_desc *desc,const void *in) { +int (*import)(struct shash_desc *desc,const void *in);struct crypto_shash *tfm = desc->tfm; -struct shash_alg *shash =crypto_shash_alg (tfm); +unsigned int descsize, plen, ss; +u8 *buf =shash_desc_ctx (desc);if (crypto_shash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )return -ENOKEY ; -if (shash->import) -return shash->import (desc, in); + plen =crypto_shash_blocksize (tfm) +1 ; + descsize =crypto_shash_descsize (tfm); + ss =crypto_shash_statesize (tfm); + buf[descsize -1 ] =0 ; +if (crypto_shash_block_only (tfm)) + ss -= plen; + import =crypto_shash_alg (tfm)->import; +if (!import) { +memcpy (buf, in, ss); +return 0 ; + } -memcpy (shash_desc_ctx (desc), in,crypto_shash_descsize (tfm)); -return 0 ; +return import (desc, in); +} +EXPORT_SYMBOL_GPL (crypto_shash_import_core); + +int crypto_shash_import (struct shash_desc *desc,const void *in) +{ +struct crypto_shash *tfm = desc->tfm; +int err; + + err =crypto_shash_import_core (desc, in); +if (crypto_shash_block_only (tfm)) { +unsigned int plen =crypto_shash_blocksize (tfm) +1 ; +unsigned int descsize =crypto_shash_descsize (tfm); +unsigned int ss =crypto_shash_statesize (tfm); +u8 *buf =shash_desc_ctx (desc); + +memcpy (buf + descsize - plen, in + ss - plen, plen); +if (buf[descsize -1 ] >= plen) + err = -EOVERFLOW ; + } +return err; }EXPORT_SYMBOL_GPL (crypto_shash_import);
@@ -293,9 +438,6 @@ static int shash_prepare_alg(struct shash_alg *alg)struct crypto_alg *base = &alg->halg.base;int err; -if (alg->descsize >HASH_MAX_DESCSIZE ) -return -EINVAL ; -if ((alg->export && !alg->import) || (alg->import && !alg->export))return -EINVAL ;
@@ -321,11 +463,20 @@ static int shash_prepare_alg(struct shash_alg *alg) alg->finup = shash_default_finup;if (!alg->digest) alg->digest = shash_default_digest; -if (!alg->export) +if (!alg->export && !alg->halg.statesize) alg->halg.statesize = alg->descsize;if (!alg->setkey) alg->setkey = shash_no_setkey; +if (base->cra_flags &CRYPTO_AHASH_ALG_BLOCK_ONLY ) { +BUILD_BUG_ON (MAX_ALGAPI_BLOCKSIZE >=256 ); + alg->descsize += base->cra_blocksize +1 ; + alg->statesize += base->cra_blocksize +1 ; + } + +if (alg->descsize >HASH_MAX_DESCSIZE ) +return -EINVAL ; +return 0 ; }
include/crypto/hash.h +64 -40
134 lines not on this page · raw message
include/crypto/internal/hash.h +15 -0
@@ -11,6 +11,15 @@#include <crypto/algapi.h> #include <crypto/hash.h> +/* Set this bit to handle partial blocks in the API. */ +#define CRYPTO_AHASH_ALG_BLOCK_ONLY 0x01000000 + +/* Set this bit if final requires at least one byte. */ +#define CRYPTO_AHASH_ALG_FINAL_NONZERO 0x02000000 + +/* Set this bit if finup can deal with multiple blocks. */ +#define CRYPTO_AHASH_ALG_FINUP_MAX 0x04000000 +#define HASH_FBREQ_ON_STACK (name, req) \ char __##name##_req[sizeof(struct ahash_request) + \ MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \
@@ -281,5 +290,11 @@ static inline struct ahash_request *ahash_fbreq_on_stack_init( return req; } +/* Return the state size without partial block for block-only algorithms. */ +static inline unsigned int crypto_shash_coresize (struct crypto_shash *tfm) +{ +return crypto_shash_statesize (tfm) -crypto_shash_blocksize (tfm) -1 ; +} +#endif /* _CRYPTO_INTERNAL_HASH_H */
include/linux/crypto.h +2 -0
@@ -136,6 +136,8 @@/* Set if the algorithm supports request chains and virtual addresses. */ #define CRYPTO_ALG_REQ_CHAIN 0x00040000 +/* The high bits 0xff000000 are reserved for type-specific flags. */ +/* * Transform masks and values (for crt_flags). */
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 2/67 crypto: blake2b-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/blake2b_generic.c | 31 ++++++++------ include/crypto/blake2b.h | 14 ++----- include/crypto/internal/blake2b.h | 70 ++++++++++++++++++++++++++----- 3 files changed, 80 insertions(+), 35 deletions(-)
crypto/blake2b_generic.c +18 -13
@@ -15,12 +15,12 @@* More information about BLAKE2 can be found at https://blake2.net. */ -#include <linux/unaligned.h> -#include <linux/module.h> -#include <linux/kernel.h> -#include <linux/bitops.h> #include <crypto/internal/blake2b.h> #include <crypto/internal/hash.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> +#include <linux/unaligned.h> static const u8 blake2b_sigma[12 ][16 ] = { {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 },
@@ -111,8 +111,8 @@ static void blake2b_compress_one_generic(struct blake2b_state *S,#undef G#undef ROUND -void blake2b_compress_generic (struct blake2b_state *state, -const u8 *block,size_t nblocks,u32 inc) +static void blake2b_compress_generic (struct blake2b_state *state, +const u8 *block,size_t nblocks,u32 inc) { do { blake2b_increment_counter(state, inc);
@@ -120,17 +120,19 @@ void blake2b_compress_generic(struct blake2b_state *state, block +=BLAKE2B_BLOCK_SIZE ; }while (--nblocks); } -EXPORT_SYMBOL (blake2b_compress_generic);static int crypto_blake2b_update_generic (struct shash_desc *desc,const u8 *in,unsigned int inlen) { -return crypto_blake2b_update (desc, in, inlen, blake2b_compress_generic); +return crypto_blake2b_update_bo (desc, in, inlen, + blake2b_compress_generic); } -static int crypto_blake2b_final_generic (struct shash_desc *desc,u8 *out) +static int crypto_blake2b_finup_generic (struct shash_desc *desc,const u8 *in, +unsigned int inlen,u8 *out) { -return crypto_blake2b_final (desc, out, blake2b_compress_generic); +return crypto_blake2b_finup (desc, in, inlen, out, + blake2b_compress_generic); }#define BLAKE2B_ALG (name, driver_name, digest_size) \
@@ -138,7 +140,9 @@ static int crypto_blake2b_final_generic(struct shash_desc *desc, u8 *out) .base.cra_name = name, \ .base.cra_driver_name = driver_name, \ .base.cra_priority = 100, \ - .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, \ + .base.cra_flags =CRYPTO_ALG_OPTIONAL_KEY | \ +CRYPTO_AHASH_ALG_BLOCK_ONLY | \ + CRYPTO_AHASH_ALG_FINAL_NONZERO, \ .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, \ .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), \ .base.cra_module = THIS_MODULE, \
@@ -146,8 +150,9 @@ static int crypto_blake2b_final_generic(struct shash_desc *desc, u8 *out) .setkey = crypto_blake2b_setkey, \ .init = crypto_blake2b_init, \ .update = crypto_blake2b_update_generic, \ - .final = crypto_blake2b_final_generic, \ - .descsize = sizeof(struct blake2b_state), \ + .finup = crypto_blake2b_finup_generic, \ + .descsize = BLAKE2B_DESC_SIZE, \ + .statesize = BLAKE2B_STATE_SIZE, \ }static struct shash_alg blake2b_algs[] = {
include/crypto/blake2b.h +3 -11
@@ -11,6 +11,8 @@ enum blake2b_lengths {BLAKE2B_BLOCK_SIZE =128 ,BLAKE2B_HASH_SIZE =64 ,BLAKE2B_KEY_SIZE =64 , +BLAKE2B_STATE_SIZE =80 , +BLAKE2B_DESC_SIZE =96 ,BLAKE2B_160_HASH_SIZE =20 , BLAKE2B_256_HASH_SIZE = 32,
@@ -25,7 +27,6 @@ struct blake2b_state {u64 f[2 ];u8 buf[BLAKE2B_BLOCK_SIZE ];unsigned int buflen; -unsigned int outlen; };enum blake2b_iv {
@@ -40,7 +41,7 @@ enum blake2b_iv { };static inline void __blake2b_init (struct blake2b_state *state,size_t outlen, -const void *key,size_t keylen) +size_t keylen) { state->h[0 ] =BLAKE2B_IV0 ^ (0x01010000 | keylen <<8 | outlen); state->h[1 ] =BLAKE2B_IV1 ;
@@ -52,15 +53,6 @@ static inline void __blake2b_init(struct blake2b_state *state, size_t outlen, state->h[7 ] =BLAKE2B_IV7 ; state->t[0 ] =0 ; state->t[1] = 0; - state->f[0 ] =0 ; - state->f[1 ] =0 ; - state->buflen =0 ; - state->outlen = outlen; -if (keylen) { -memcpy (state->buf, key, keylen); -memset (&state->buf[keylen],0 ,BLAKE2B_BLOCK_SIZE - keylen); - state->buflen =BLAKE2B_BLOCK_SIZE ; - } }#endif /* _CRYPTO_BLAKE2B_H */
include/crypto/internal/blake2b.h +59 -11
130 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 3/67 crypto: arm/blake2b - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm/crypto/blake2b-neon-glue.c | 20 +++++----- include/crypto/blake2b.h | 21 +++++----- include/crypto/internal/blake2b.h | 62 ----------------------------- 3 files changed, 19 insertions(+), 84 deletions(-)
arch/arm/crypto/blake2b-neon-glue.c +9 -11
@@ -7,7 +7,6 @@#include <crypto/internal/blake2b.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <linux/module.h> #include <linux/sizes.h>
@@ -21,11 +20,6 @@ asmlinkage void blake2b_compress_neon(struct blake2b_state *state,static void blake2b_compress_arch (struct blake2b_state *state,const u8 *block,size_t nblocks,u32 inc) { -if (!crypto_simd_usable ()) { -blake2b_compress_generic (state, block, nblocks, inc); -return ; - } - do { const size_t blocks = min_t(size_t, nblocks, SZ_4K / BLAKE2B_BLOCK_SIZE);
@@ -42,12 +36,14 @@ static void blake2b_compress_arch(struct blake2b_state *state,static int crypto_blake2b_update_neon (struct shash_desc *desc,const u8 *in,unsigned int inlen) { -return crypto_blake2b_update (desc, in, inlen, blake2b_compress_arch); +return crypto_blake2b_update_bo (desc, in, inlen, blake2b_compress_arch); } -static int crypto_blake2b_final_neon (struct shash_desc *desc,u8 *out) +static int crypto_blake2b_finup_neon (struct shash_desc *desc,const u8 *in, +unsigned int inlen,u8 *out) { -return crypto_blake2b_final (desc, out, blake2b_compress_arch); +return crypto_blake2b_finup (desc, in, inlen, out, + blake2b_compress_arch); }#define BLAKE2B_ALG (name, driver_name, digest_size) \
@@ -55,7 +51,8 @@ static int crypto_blake2b_final_neon(struct shash_desc *desc, u8 *out) .base.cra_name = name, \ .base.cra_driver_name = driver_name, \ .base.cra_priority = 200, \ - .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, \ + .base.cra_flags =CRYPTO_ALG_OPTIONAL_KEY | \ + CRYPTO_AHASH_ALG_BLOCK_ONLY, \ .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, \ .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), \ .base.cra_module = THIS_MODULE, \
@@ -63,8 +60,9 @@ static int crypto_blake2b_final_neon(struct shash_desc *desc, u8 *out) .setkey = crypto_blake2b_setkey, \ .init = crypto_blake2b_init, \ .update = crypto_blake2b_update_neon, \ - .final = crypto_blake2b_final_neon, \ + .finup = crypto_blake2b_finup_neon, \ .descsize = sizeof(struct blake2b_state), \ + .statesize = BLAKE2B_STATE_SIZE, \ }static struct shash_alg blake2b_neon_algs[] = {
include/crypto/blake2b.h +10 -11
@@ -7,12 +7,20 @@#include <linux/types.h> #include <linux/string.h> +struct blake2b_state { +/* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */ +u64 h[8 ]; +u64 t[2 ]; +/* The true state ends here. The rest is temporary storage. */ +u64 f[2 ]; +}; +enum blake2b_lengths {BLAKE2B_BLOCK_SIZE =128 ,BLAKE2B_HASH_SIZE =64 ,BLAKE2B_KEY_SIZE =64 , -BLAKE2B_STATE_SIZE =80 , -BLAKE2B_DESC_SIZE =96 , +BLAKE2B_STATE_SIZE = offsetof(struct blake2b_state , f), +BLAKE2B_DESC_SIZE =sizeof (struct blake2b_state ),BLAKE2B_160_HASH_SIZE =20 ,BLAKE2B_256_HASH_SIZE =32 ,
@@ -20,15 +28,6 @@ enum blake2b_lengths { BLAKE2B_512_HASH_SIZE = 64, }; -struct blake2b_state { -/* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */ -u64 h[8 ]; -u64 t[2 ]; -u64 f[2 ]; -u8 buf[BLAKE2B_BLOCK_SIZE ]; -unsigned int buflen; -}; -enum blake2b_iv {BLAKE2B_IV0 =0x6A09E667F3BCC908ULL ,BLAKE2B_IV1 =0xBB67AE8584CAA73BULL ,
include/crypto/internal/blake2b.h +0 -62
80 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 4/67 crypto: ghash-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/ghash-generic.c | 56 +++++++++++++----------------------------- include/crypto/ghash.h | 3 ++- 2 files changed, 19 insertions(+), 40 deletions(-)
crypto/ghash-generic.c +17 -39
@@ -34,14 +34,14 @@* (https://csrc.nist.gov/publications/detail/sp/800-38d/final) */ -#include <crypto/algapi.h> #include <crypto/gf128mul.h> #include <crypto/ghash.h> #include <crypto/internal/hash.h> -#include <linux/crypto.h> -#include <linux/init.h> +#include <crypto/utils.h> +#include <linux/err.h> #include <linux/kernel.h> #include <linux/module.h> +#include <linux/string.h> static int ghash_init (struct shash_desc *desc) {
@@ -82,59 +82,36 @@ static int ghash_update(struct shash_desc *desc,struct ghash_ctx *ctx =crypto_shash_ctx (desc->tfm);u8 *dst = dctx->buffer; -if (dctx->bytes) { -int n =min (srclen, dctx->bytes); -u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes); - - dctx->bytes -= n; - srclen -= n; - -while (n--) - *pos++ ^= *src++; - -if (!dctx->bytes) -gf128mul_4k_lle ((be128 *)dst, ctx->gf128); - } - -while (srclen >=GHASH_BLOCK_SIZE ) { +do {crypto_xor (dst, src,GHASH_BLOCK_SIZE );gf128mul_4k_lle ((be128 *)dst, ctx->gf128); src +=GHASH_BLOCK_SIZE ; srclen -=GHASH_BLOCK_SIZE ; - } + }while (srclen >=GHASH_BLOCK_SIZE ); -if (srclen) { - dctx->bytes =GHASH_BLOCK_SIZE - srclen; -while (srclen--) - *dst++ ^= *src++; - } - -return 0 ; +return srclen; } -static void ghash_flush (struct ghash_ctx *ctx,struct ghash_desc_ctx *dctx) +static void ghash_flush (struct shash_desc *desc,const u8 *src, +unsigned int len) { +struct ghash_ctx *ctx =crypto_shash_ctx (desc->tfm); +struct ghash_desc_ctx *dctx =shash_desc_ctx (desc);u8 *dst = dctx->buffer; -if (dctx->bytes) { -u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes); - -while (dctx->bytes--) - *tmp++ ^=0 ; - +if (len) { +crypto_xor (dst, src, len);gf128mul_4k_lle ((be128 *)dst, ctx->gf128); } - - dctx->bytes =0 ; } -static int ghash_final (struct shash_desc *desc,u8 *dst) +static int ghash_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *dst) {struct ghash_desc_ctx *dctx =shash_desc_ctx (desc); -struct ghash_ctx *ctx =crypto_shash_ctx (desc->tfm);u8 *buf = dctx->buffer; -ghash_flush (ctx, dctx); +ghash_flush (desc, src, len);memcpy (dst, buf,GHASH_BLOCK_SIZE );return 0 ;
@@ -151,13 +128,14 @@ static struct shash_alg ghash_alg = { .digestsize = GHASH_DIGEST_SIZE, .init = ghash_init, .update = ghash_update, - .final = ghash_final, + .finup = ghash_finup, .setkey = ghash_setkey, .descsize = sizeof(struct ghash_desc_ctx), .base = { .cra_name = "ghash", .cra_driver_name = "ghash-generic", .cra_priority = 100, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_blocksize = GHASH_BLOCK_SIZE, .cra_ctxsize = sizeof(struct ghash_ctx), .cra_module = THIS_MODULE,
include/crypto/ghash.h +2 -1
@@ -12,13 +12,14 @@#define GHASH_BLOCK_SIZE 16#define GHASH_DIGEST_SIZE 16 +struct gf128mul_4k ; +struct ghash_ctx {struct gf128mul_4k *gf128; };struct ghash_desc_ctx {u8 buffer[GHASH_BLOCK_SIZE ]; -u32 bytes; };#endif
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 5/67 crypto: powerpc/ghash - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/powerpc/crypto/ghash.c | 87 +++++++++++++------------------------ 1 file changed, 31 insertions(+), 56 deletions(-)
arch/powerpc/crypto/ghash.c +31 -56
@@ -11,18 +11,17 @@* Copyright (C) 2014 - 2018 Linaro Ltd. <[email protected]> */ -#include <linux/types.h> -#include <linux/err.h> -#include <linux/crypto.h> -#include <linux/delay.h> #include <asm/simd.h> #include <asm/switch_to.h> #include <crypto/aes.h> +#include <crypto/gf128mul.h> #include <crypto/ghash.h> -#include <crypto/scatterwalk.h> #include <crypto/internal/hash.h> #include <crypto/internal/simd.h> -#include <crypto/b128ops.h> +#include <linux/err.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> #include "aesp8-ppc.h" void gcm_init_p8 (u128 htable[16 ],const u64 Xi[2 ]);
@@ -39,15 +38,12 @@ struct p8_ghash_ctx {struct p8_ghash_desc_ctx {u64 shash[2 ]; -u8 buffer[GHASH_DIGEST_SIZE ]; -int bytes; };static int p8_ghash_init (struct shash_desc *desc) {struct p8_ghash_desc_ctx *dctx =shash_desc_ctx (desc); - dctx->bytes =0 ;memset (dctx->shash,0 ,GHASH_DIGEST_SIZE );return 0 ; }
@@ -74,27 +70,30 @@ static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key, }static inline void __ghash_block (struct p8_ghash_ctx *ctx, -struct p8_ghash_desc_ctx *dctx) +struct p8_ghash_desc_ctx *dctx, +const u8 *src) {if (crypto_simd_usable ()) {preempt_disable ();pagefault_disable ();enable_kernel_vsx (); -gcm_ghash_p8 (dctx->shash, ctx->htable, - dctx->buffer,GHASH_DIGEST_SIZE ); +gcm_ghash_p8 (dctx->shash, ctx->htable, src,GHASH_BLOCK_SIZE );disable_kernel_vsx ();pagefault_enable ();preempt_enable (); }else { -crypto_xor ((u8 *)dctx->shash, dctx->buffer,GHASH_BLOCK_SIZE ); +crypto_xor ((u8 *)dctx->shash, src,GHASH_BLOCK_SIZE );gf128mul_lle ((be128 *)dctx->shash, &ctx->key); } } -static inline void __ghash_blocks (struct p8_ghash_ctx *ctx, -struct p8_ghash_desc_ctx *dctx, -const u8 *src,unsigned int srclen) +static inline int __ghash_blocks (struct p8_ghash_ctx *ctx, +struct p8_ghash_desc_ctx *dctx, +const u8 *src,unsigned int srclen) { +int remain = srclen -round_down (srclen,GHASH_BLOCK_SIZE ); + + srclen -= remain;if (crypto_simd_usable ()) {preempt_disable ();pagefault_disable ();
@@ -105,62 +104,38 @@ static inline void __ghash_blocks(struct p8_ghash_ctx *ctx,pagefault_enable ();preempt_enable (); }else { -while (srclen >=GHASH_BLOCK_SIZE ) { +do {crypto_xor ((u8 *)dctx->shash, src,GHASH_BLOCK_SIZE );gf128mul_lle ((be128 *)dctx->shash, &ctx->key); srclen -=GHASH_BLOCK_SIZE ; src +=GHASH_BLOCK_SIZE ; - } + }while (srclen); } + +return remain; }static int p8_ghash_update (struct shash_desc *desc,const u8 *src,unsigned int srclen) { -unsigned int len;struct p8_ghash_ctx *ctx =crypto_tfm_ctx (crypto_shash_tfm (desc->tfm));struct p8_ghash_desc_ctx *dctx =shash_desc_ctx (desc); -if (dctx->bytes) { -if (dctx->bytes + srclen <GHASH_DIGEST_SIZE ) { -memcpy (dctx->buffer + dctx->bytes, src, - srclen); - dctx->bytes += srclen; -return 0 ; - } -memcpy (dctx->buffer + dctx->bytes, src, -GHASH_DIGEST_SIZE - dctx->bytes); - -__ghash_block (ctx, dctx); - - src +=GHASH_DIGEST_SIZE - dctx->bytes; - srclen -=GHASH_DIGEST_SIZE - dctx->bytes; - dctx->bytes =0 ; - } - len = srclen & ~(GHASH_DIGEST_SIZE -1 ); -if (len) { -__ghash_blocks (ctx, dctx, src, len); - src += len; - srclen -= len; - } -if (srclen) { -memcpy (dctx->buffer, src, srclen); - dctx->bytes = srclen; - } -return 0 ; +return __ghash_blocks (ctx, dctx, src, srclen); } -static int p8_ghash_final (struct shash_desc *desc,u8 *out) +static int p8_ghash_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -int i;struct p8_ghash_ctx *ctx =crypto_tfm_ctx (crypto_shash_tfm (desc->tfm));struct p8_ghash_desc_ctx *dctx =shash_desc_ctx (desc); -if (dctx->bytes) { -for (i = dctx->bytes; i <GHASH_DIGEST_SIZE ; i++) - dctx->buffer[i] =0 ; -__ghash_block (ctx, dctx); - dctx->bytes =0 ; +if (len) { +u8 buf[GHASH_BLOCK_SIZE ] = {}; + +memcpy (buf, src, len); +__ghash_block (ctx, dctx, buf); +memzero_explicit (buf,sizeof (buf)); }memcpy (out, dctx->shash,GHASH_DIGEST_SIZE );return 0 ;
@@ -170,14 +145,14 @@ struct shash_alg p8_ghash_alg = { .digestsize = GHASH_DIGEST_SIZE, .init = p8_ghash_init, .update = p8_ghash_update, - .final = p8_ghash_final, + .finup = p8_ghash_finup, .setkey = p8_ghash_setkey, - .descsize =sizeof (struct p8_ghash_desc_ctx ) - + sizeof(struct ghash_desc_ctx), + .descsize = sizeof(struct p8_ghash_desc_ctx), .base = { .cra_name = "ghash", .cra_driver_name = "p8_ghash", .cra_priority = 1000, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_blocksize = GHASH_BLOCK_SIZE, .cra_ctxsize = sizeof(struct p8_ghash_ctx), .cra_module = THIS_MODULE,
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 6/67 crypto: arm/ghash - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also switch to the generic export format.
Finally remove a couple of stray may_use_simd() calls in gcm.
--- arch/arm/crypto/ghash-ce-glue.c | 110 +++++++++++++++----------------- 1 file changed, 50 insertions(+), 60 deletions(-)
arch/arm/crypto/ghash-ce-glue.c +50 -60
@@ -8,22 +8,22 @@#include <asm/hwcap.h> #include <asm/neon.h> -#include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/aes.h> -#include <crypto/gcm.h> #include <crypto/b128ops.h> -#include <crypto/cryptd.h> +#include <crypto/gcm.h> +#include <crypto/gf128mul.h> +#include <crypto/ghash.h> #include <crypto/internal/aead.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/internal/skcipher.h> -#include <crypto/gf128mul.h> #include <crypto/scatterwalk.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/errno.h> #include <linux/jump_label.h> +#include <linux/kernel.h> #include <linux/module.h> +#include <linux/string.h> +#include <linux/unaligned.h> MODULE_DESCRIPTION ("GHASH hash function using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );
@@ -32,9 +32,6 @@ MODULE_ALIAS_CRYPTO("ghash");MODULE_ALIAS_CRYPTO ("gcm(aes)" );MODULE_ALIAS_CRYPTO ("rfc4106(gcm(aes))" ); -#define GHASH_BLOCK_SIZE 16 -#define GHASH_DIGEST_SIZE 16 -#define RFC4106_NONCE_SIZE 4struct ghash_key {
@@ -49,10 +46,8 @@ struct gcm_key {u8 nonce[];// for RFC4106 nonce }; -struct ghash_desc_ctx { +struct arm_ghash_desc_ctx {u64 digest[GHASH_DIGEST_SIZE /sizeof (u64)]; -u8 buf[GHASH_BLOCK_SIZE ]; -u32 count; }; asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
@@ -65,9 +60,9 @@ static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_p64);static int ghash_init (struct shash_desc *desc) { -struct ghash_desc_ctx *ctx =shash_desc_ctx (desc); +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); - *ctx = (struct ghash_desc_ctx ){}; + *ctx = (struct arm_ghash_desc_ctx ){};return 0 ; }
@@ -85,54 +80,51 @@ static void ghash_do_update(int blocks, u64 dg[], const char *src,static int ghash_update (struct shash_desc *desc,const u8 *src,unsigned int len) { -struct ghash_desc_ctx *ctx =shash_desc_ctx (desc); -unsigned int partial = ctx->count %GHASH_BLOCK_SIZE ; +struct ghash_key *key =crypto_shash_ctx (desc->tfm); +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); +int blocks; - ctx->count += len; + blocks = len /GHASH_BLOCK_SIZE ; +ghash_do_update (blocks, ctx->digest, src, key,NULL ); +return len - blocks *GHASH_BLOCK_SIZE ; +} -if ((partial + len) >=GHASH_BLOCK_SIZE ) { -struct ghash_key *key =crypto_shash_ctx (desc->tfm); -int blocks; +static int ghash_export (struct shash_desc *desc,void *out) +{ +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); +u8 *dst = out; -if (partial) { -int p =GHASH_BLOCK_SIZE - partial; - -memcpy (ctx->buf + partial, src, p); - src += p; - len -= p; - } - - blocks = len /GHASH_BLOCK_SIZE ; - len %=GHASH_BLOCK_SIZE ; - -ghash_do_update (blocks, ctx->digest, src, key, - partial ? ctx->buf :NULL ); - src += blocks *GHASH_BLOCK_SIZE ; - partial =0 ; - } -if (len) -memcpy (ctx->buf + partial, src, len); +put_unaligned_be64 (ctx->digest[1 ], dst); +put_unaligned_be64 (ctx->digest[0 ], dst +8 );return 0 ; } -static int ghash_final (struct shash_desc *desc,u8 *dst) +static int ghash_import (struct shash_desc *desc,const void *in) { -struct ghash_desc_ctx *ctx =shash_desc_ctx (desc); -unsigned int partial = ctx->count %GHASH_BLOCK_SIZE ; +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); +const u8 *src = in; -if (partial) { -struct ghash_key *key =crypto_shash_ctx (desc->tfm); - -memset (ctx->buf + partial,0 ,GHASH_BLOCK_SIZE - partial); -ghash_do_update (1 , ctx->digest, ctx->buf, key,NULL ); - } -put_unaligned_be64 (ctx->digest[1 ], dst); -put_unaligned_be64 (ctx->digest[0 ], dst +8 ); - - *ctx = (struct ghash_desc_ctx ){}; + ctx->digest[1 ] =get_unaligned_be64 (src); + ctx->digest[0 ] =get_unaligned_be64 (src +8 );return 0 ; } +static int ghash_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *dst) +{ +struct ghash_key *key =crypto_shash_ctx (desc->tfm); +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); + +if (len) { +u8 buf[GHASH_BLOCK_SIZE ] = {}; + +memcpy (buf, src, len); +ghash_do_update (1 , ctx->digest, buf, key,NULL ); +memzero_explicit (buf,sizeof (buf)); + } +return ghash_export (desc, dst); +} +static void ghash_reflect (u64 h[],const be128 *k) {u64 carry =be64_to_cpu (k->a) >>63 ;
@@ -175,13 +167,17 @@ static struct shash_alg ghash_alg = { .digestsize = GHASH_DIGEST_SIZE, .init = ghash_init, .update = ghash_update, - .final = ghash_final, + .finup = ghash_finup, .setkey = ghash_setkey, - .descsize = sizeof(struct ghash_desc_ctx), + .export = ghash_export, + .import = ghash_import, + .descsize = sizeof(struct arm_ghash_desc_ctx), + .statesize = sizeof(struct ghash_desc_ctx), .base.cra_name = "ghash", .base.cra_driver_name = "ghash-ce", .base.cra_priority = 300, + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .base.cra_blocksize = GHASH_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct ghash_key) + sizeof(u64[2]), .base.cra_module = THIS_MODULE,
@@ -317,9 +313,6 @@ static int gcm_encrypt(struct aead_request *req, const u8 *iv, u32 assoclen)u8 *tag, *dst;int tail, err; -if (WARN_ON_ONCE (!may_use_simd ())) -return -EBUSY ; - err =skcipher_walk_aead_encrypt (&walk, req, false);kernel_neon_begin ();
@@ -409,9 +402,6 @@ static int gcm_decrypt(struct aead_request *req, const u8 *iv, u32 assoclen)u8 *tag, *dst;int tail, err, ret; -if (WARN_ON_ONCE (!may_use_simd ())) -return -EBUSY ; -scatterwalk_map_and_copy (otag, req->src, req->assoclen + req->cryptlen - authsize, authsize,0 );
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 7/67 crypto: arm64/ghash - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/ghash-ce-glue.c | 151 ++++++++++++------------------ 1 file changed, 60 insertions(+), 91 deletions(-)
arch/arm64/crypto/ghash-ce-glue.c +60 -91
@@ -6,30 +6,27 @@*/ #include <asm/neon.h> -#include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/aes.h> -#include <crypto/gcm.h> -#include <crypto/algapi.h> #include <crypto/b128ops.h> +#include <crypto/gcm.h> +#include <crypto/ghash.h> #include <crypto/gf128mul.h> #include <crypto/internal/aead.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/internal/skcipher.h> #include <crypto/scatterwalk.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/errno.h> +#include <linux/kernel.h> #include <linux/module.h> +#include <linux/string.h> +#include <linux/unaligned.h> MODULE_DESCRIPTION ("GHASH and AES-GCM using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_LICENSE ("GPL v2" );MODULE_ALIAS_CRYPTO ("ghash" ); -#define GHASH_BLOCK_SIZE 16 -#define GHASH_DIGEST_SIZE 16 -#define RFC4106_NONCE_SIZE 4struct ghash_key {
@@ -37,10 +34,8 @@ struct ghash_key {u64 h[][2 ]; }; -struct ghash_desc_ctx { +struct arm_ghash_desc_ctx {u64 digest[GHASH_DIGEST_SIZE /sizeof (u64)]; -u8 buf[GHASH_BLOCK_SIZE ]; -u32 count; };struct gcm_aes_ctx {
@@ -65,36 +60,12 @@ asmlinkage int pmull_gcm_decrypt(int bytes, u8 dst[], const u8 src[],static int ghash_init (struct shash_desc *desc) { -struct ghash_desc_ctx *ctx =shash_desc_ctx (desc); +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); - *ctx = (struct ghash_desc_ctx ){}; + *ctx = (struct arm_ghash_desc_ctx ){};return 0 ; } -static void ghash_do_update (int blocks,u64 dg[],const char *src, -struct ghash_key *key,const char *head) -{ -be128 dst = {cpu_to_be64 (dg[1 ]),cpu_to_be64 (dg[0 ]) }; - -do { -const u8 *in = src; - -if (head) { - in = head; - blocks++; - head =NULL ; - }else { - src +=GHASH_BLOCK_SIZE ; - } - -crypto_xor ((u8 *)&dst, in,GHASH_BLOCK_SIZE ); -gf128mul_lle (&dst, &key->k); - }while (--blocks); - - dg[0 ] =be64_to_cpu (dst.b); - dg[1 ] =be64_to_cpu (dst.a); -} - static __always_inline void ghash_do_simd_update(int blocks, u64 dg[], const char *src, struct ghash_key *key, const char *head,
@@ -103,13 +74,9 @@ void ghash_do_simd_update(int blocks, u64 dg[], const char *src,u64 const h[][2 ], const char *head)) { - if (likely(crypto_simd_usable())) { -kernel_neon_begin (); -simd_update (blocks, dg, src, key->h, head); -kernel_neon_end (); - } else { -ghash_do_update (blocks, dg, src, key, head); - } +kernel_neon_begin (); +simd_update (blocks, dg, src, key->h, head); +kernel_neon_end (); }/* avoid hogging the CPU for too long */
@@ -118,63 +85,61 @@ void ghash_do_simd_update(int blocks, u64 dg[], const char *src,static int ghash_update (struct shash_desc *desc,const u8 *src,unsigned int len) { -struct ghash_desc_ctx *ctx =shash_desc_ctx (desc); -unsigned int partial = ctx->count %GHASH_BLOCK_SIZE ; +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); +struct ghash_key *key =crypto_shash_ctx (desc->tfm); +int blocks; - ctx->count += len; + blocks = len /GHASH_BLOCK_SIZE ; + len -= blocks *GHASH_BLOCK_SIZE ; -if ((partial + len) >=GHASH_BLOCK_SIZE ) { -struct ghash_key *key =crypto_shash_ctx (desc->tfm); -int blocks; +do { +int chunk =min (blocks,MAX_BLOCKS ); -if (partial) { -int p =GHASH_BLOCK_SIZE - partial; +ghash_do_simd_update (chunk, ctx->digest, src, key,NULL , + pmull_ghash_update_p8); + blocks -= chunk; + src += chunk *GHASH_BLOCK_SIZE ; + }while (unlikely (blocks >0 )); +return len; +} -memcpy (ctx->buf + partial, src, p); - src += p; - len -= p; - } +static int ghash_export (struct shash_desc *desc,void *out) +{ +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); +u8 *dst = out; - blocks = len /GHASH_BLOCK_SIZE ; - len %=GHASH_BLOCK_SIZE ; - -do { -int chunk =min (blocks,MAX_BLOCKS ); - -ghash_do_simd_update (chunk, ctx->digest, src, key, - partial ? ctx->buf :NULL , - pmull_ghash_update_p8); - - blocks -= chunk; - src += chunk *GHASH_BLOCK_SIZE ; - partial =0 ; - }while (unlikely (blocks >0 )); - } -if (len) -memcpy (ctx->buf + partial, src, len); +put_unaligned_be64 (ctx->digest[1 ], dst); +put_unaligned_be64 (ctx->digest[0 ], dst +8 );return 0 ; } -static int ghash_final (struct shash_desc *desc,u8 *dst) +static int ghash_import (struct shash_desc *desc,const void *in) { -struct ghash_desc_ctx *ctx =shash_desc_ctx (desc); -unsigned int partial = ctx->count %GHASH_BLOCK_SIZE ; +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); +const u8 *src = in; -if (partial) { -struct ghash_key *key =crypto_shash_ctx (desc->tfm); - -memset (ctx->buf + partial,0 ,GHASH_BLOCK_SIZE - partial); - -ghash_do_simd_update (1 , ctx->digest, ctx->buf, key,NULL , - pmull_ghash_update_p8); - } -put_unaligned_be64 (ctx->digest[1 ], dst); -put_unaligned_be64 (ctx->digest[0 ], dst +8 ); - -memzero_explicit (ctx,sizeof (*ctx)); + ctx->digest[1 ] =get_unaligned_be64 (src); + ctx->digest[0 ] =get_unaligned_be64 (src +8 );return 0 ; } +static int ghash_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *dst) +{ +struct arm_ghash_desc_ctx *ctx =shash_desc_ctx (desc); +struct ghash_key *key =crypto_shash_ctx (desc->tfm); + +if (len) { +u8 buf[GHASH_BLOCK_SIZE ] = {}; + +memcpy (buf, src, len); +ghash_do_simd_update (1 , ctx->digest, src, key,NULL , + pmull_ghash_update_p8); +memzero_explicit (buf,sizeof (buf)); + } +return ghash_export (desc, dst); +} +static void ghash_reflect (u64 h[],const be128 *k) {u64 carry =be64_to_cpu (k->a) &BIT (63 ) ?1 :0 ;
@@ -205,6 +170,7 @@ static struct shash_alg ghash_alg = { .base.cra_name = "ghash", .base.cra_driver_name = "ghash-neon", .base.cra_priority = 150, + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .base.cra_blocksize = GHASH_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct ghash_key) + sizeof(u64[2]), .base.cra_module = THIS_MODULE,
@@ -212,9 +178,12 @@ static struct shash_alg ghash_alg = { .digestsize = GHASH_DIGEST_SIZE, .init = ghash_init, .update = ghash_update, - .final = ghash_final, + .finup = ghash_finup, .setkey = ghash_setkey, - .descsize = sizeof(struct ghash_desc_ctx), + .export = ghash_export, + .import = ghash_import, + .descsize = sizeof(struct arm_ghash_desc_ctx), + .statesize = sizeof(struct ghash_desc_ctx), };static int num_rounds (struct crypto_aes_ctx *ctx)
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 8/67 crypto: riscv/ghash - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
As this was the last user relying on crypto/ghash.h for gf128mul.h, remove the unnecessary inclusion of gf128mul.h from crypto/ghash.h.
--- arch/riscv/crypto/ghash-riscv64-glue.c | 58 ++++++++------------------ include/crypto/ghash.h | 1 - 2 files changed, 18 insertions(+), 41 deletions(-)
arch/riscv/crypto/ghash-riscv64-glue.c +18 -40
@@ -11,11 +11,16 @@#include <asm/simd.h> #include <asm/vector.h> +#include <crypto/b128ops.h> +#include <crypto/gf128mul.h> #include <crypto/ghash.h> #include <crypto/internal/hash.h> #include <crypto/internal/simd.h> -#include <linux/linkage.h> +#include <crypto/utils.h> +#include <linux/errno.h> +#include <linux/kernel.h> #include <linux/module.h> +#include <linux/string.h> asmlinkage void ghash_zvkg(be128 *accumulator, const be128 *key, const u8 *data,size_t len);
@@ -26,8 +31,6 @@ struct riscv64_ghash_tfm_ctx {struct riscv64_ghash_desc_ctx {be128 accumulator; -u8 buffer[GHASH_BLOCK_SIZE ]; -u32 bytes; }; static int riscv64_ghash_setkey(struct crypto_shash *tfm, const u8 *key,
@@ -78,50 +81,24 @@ static int riscv64_ghash_update(struct shash_desc *desc, const u8 *src, {const struct riscv64_ghash_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct riscv64_ghash_desc_ctx *dctx =shash_desc_ctx (desc); -unsigned int len; -if (dctx->bytes) { -if (dctx->bytes + srclen <GHASH_BLOCK_SIZE ) { -memcpy (dctx->buffer + dctx->bytes, src, srclen); - dctx->bytes += srclen; -return 0 ; - } -memcpy (dctx->buffer + dctx->bytes, src, -GHASH_BLOCK_SIZE - dctx->bytes); -riscv64_ghash_blocks (tctx, dctx, dctx->buffer, -GHASH_BLOCK_SIZE ); - src +=GHASH_BLOCK_SIZE - dctx->bytes; - srclen -=GHASH_BLOCK_SIZE - dctx->bytes; - dctx->bytes =0 ; - } - - len =round_down (srclen,GHASH_BLOCK_SIZE ); -if (len) { -riscv64_ghash_blocks (tctx, dctx, src, len); - src += len; - srclen -= len; - } - -if (srclen) { -memcpy (dctx->buffer, src, srclen); - dctx->bytes = srclen; - } - -return 0 ; +riscv64_ghash_blocks (tctx, dctx, src, +round_down (srclen,GHASH_BLOCK_SIZE )); +return srclen -round_down (srclen,GHASH_BLOCK_SIZE ); } -static int riscv64_ghash_final (struct shash_desc *desc,u8 *out) +static int riscv64_ghash_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) {const struct riscv64_ghash_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct riscv64_ghash_desc_ctx *dctx =shash_desc_ctx (desc); -int i; -if (dctx->bytes) { -for (i = dctx->bytes; i <GHASH_BLOCK_SIZE ; i++) - dctx->buffer[i] =0 ; +if (len) { +u8 buf[GHASH_BLOCK_SIZE ] = {}; -riscv64_ghash_blocks (tctx, dctx, dctx->buffer, -GHASH_BLOCK_SIZE ); +memcpy (buf, src, len); +riscv64_ghash_blocks (tctx, dctx, buf,GHASH_BLOCK_SIZE ); +memzero_explicit (buf,sizeof (buf)); }memcpy (out, &dctx->accumulator,GHASH_DIGEST_SIZE );
@@ -131,7 +108,7 @@ static int riscv64_ghash_final(struct shash_desc *desc, u8 *out)static struct shash_alg riscv64_ghash_alg = { .init = riscv64_ghash_init, .update = riscv64_ghash_update, - .final = riscv64_ghash_final, + .finup = riscv64_ghash_finup, .setkey = riscv64_ghash_setkey, .descsize =sizeof (struct riscv64_ghash_desc_ctx ), .digestsize =GHASH_DIGEST_SIZE ,
@@ -139,6 +116,7 @@ static struct shash_alg riscv64_ghash_alg = { .cra_blocksize = GHASH_BLOCK_SIZE, .cra_ctxsize = sizeof(struct riscv64_ghash_tfm_ctx), .cra_priority = 300, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_name = "ghash", .cra_driver_name = "ghash-riscv64-zvkg", .cra_module = THIS_MODULE,
include/crypto/ghash.h +0 -1
@@ -7,7 +7,6 @@#define __CRYPTO_GHASH_H__#include <linux/types.h> -#include <crypto/gf128mul.h> #define GHASH_BLOCK_SIZE 16#define GHASH_DIGEST_SIZE 16
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 9/67 crypto: s390/ghash - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also switch to the generic export format.
--- arch/s390/crypto/ghash_s390.c | 110 ++++++++++++++++------------------ 1 file changed, 50 insertions(+), 60 deletions(-)
arch/s390/crypto/ghash_s390.c +50 -60
@@ -8,29 +8,28 @@* Author(s): Gerald Schaefer <[email protected]> */ -#include <crypto/internal/hash.h> -#include <linux/module.h> -#include <linux/cpufeature.h> #include <asm/cpacf.h> +#include <crypto/ghash.h> +#include <crypto/internal/hash.h> +#include <linux/cpufeature.h> +#include <linux/err.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> -#define GHASH_BLOCK_SIZE 16 -#define GHASH_DIGEST_SIZE 16 - -struct ghash_ctx { +struct s390_ghash_ctx {u8 key[GHASH_BLOCK_SIZE ]; }; -struct ghash_desc_ctx { +struct s390_ghash_desc_ctx {u8 icv[GHASH_BLOCK_SIZE ];u8 key[GHASH_BLOCK_SIZE ]; -u8 buffer[GHASH_BLOCK_SIZE ]; -u32 bytes; };static int ghash_init (struct shash_desc *desc) { -struct ghash_desc_ctx *dctx =shash_desc_ctx (desc); -struct ghash_ctx *ctx =crypto_shash_ctx (desc->tfm); +struct s390_ghash_ctx *ctx =crypto_shash_ctx (desc->tfm); +struct s390_ghash_desc_ctx *dctx =shash_desc_ctx (desc);memset (dctx,0 ,sizeof (*dctx));memcpy (dctx->key, ctx->key,GHASH_BLOCK_SIZE );
@@ -41,7 +40,7 @@ static int ghash_init(struct shash_desc *desc)static int ghash_setkey (struct crypto_shash *tfm,const u8 *key,unsigned int keylen) { -struct ghash_ctx *ctx =crypto_shash_ctx (tfm); +struct s390_ghash_ctx *ctx =crypto_shash_ctx (tfm);if (keylen !=GHASH_BLOCK_SIZE )return -EINVAL ;
@@ -54,80 +53,71 @@ static int ghash_setkey(struct crypto_shash *tfm,static int ghash_update (struct shash_desc *desc,const u8 *src,unsigned int srclen) { -struct ghash_desc_ctx *dctx =shash_desc_ctx (desc); +struct s390_ghash_desc_ctx *dctx =shash_desc_ctx (desc);unsigned int n; -u8 *buf = dctx->buffer; - -if (dctx->bytes) { -u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes); - - n =min (srclen, dctx->bytes); - dctx->bytes -= n; - srclen -= n; - -memcpy (pos, src, n); - src += n; - -if (!dctx->bytes) { -cpacf_kimd (CPACF_KIMD_GHASH , dctx, buf, -GHASH_BLOCK_SIZE ); - } - } n = srclen & ~(GHASH_BLOCK_SIZE -1 ); -if (n) { -cpacf_kimd (CPACF_KIMD_GHASH , dctx, src, n); - src += n; - srclen -= n; - } - -if (srclen) { - dctx->bytes =GHASH_BLOCK_SIZE - srclen; -memcpy (buf, src, srclen); - } - -return 0 ; +cpacf_kimd (CPACF_KIMD_GHASH , dctx, src, n); +return srclen - n; } -static int ghash_flush (struct ghash_desc_ctx *dctx) +static void ghash_flush (struct s390_ghash_desc_ctx *dctx,const u8 *src, +unsigned int len) { -u8 *buf = dctx->buffer; +if (len) { +u8 buf[GHASH_BLOCK_SIZE ] = {}; -if (dctx->bytes) { -u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes); - -memset (pos,0 , dctx->bytes); +memcpy (buf, src, len);cpacf_kimd (CPACF_KIMD_GHASH , dctx, buf,GHASH_BLOCK_SIZE ); - dctx->bytes =0 ; +memzero_explicit (buf,sizeof (buf)); } +} +static int ghash_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *dst) +{ +struct s390_ghash_desc_ctx *dctx =shash_desc_ctx (desc); + +ghash_flush (dctx, src, len); +memcpy (dst, dctx->icv,GHASH_BLOCK_SIZE );return 0 ; } -static int ghash_final (struct shash_desc *desc,u8 *dst) +static int ghash_export (struct shash_desc *desc,void *out) { -struct ghash_desc_ctx *dctx =shash_desc_ctx (desc); -int ret; +struct s390_ghash_desc_ctx *dctx =shash_desc_ctx (desc); - ret =ghash_flush (dctx); -if (!ret) -memcpy (dst, dctx->icv,GHASH_BLOCK_SIZE ); -return ret; +memcpy (out, dctx->icv,GHASH_DIGEST_SIZE ); +return 0 ; +} + +static int ghash_import (struct shash_desc *desc,const void *in) +{ +struct s390_ghash_ctx *ctx =crypto_shash_ctx (desc->tfm); +struct s390_ghash_desc_ctx *dctx =shash_desc_ctx (desc); + +memcpy (dctx->icv, in,GHASH_DIGEST_SIZE ); +memcpy (dctx->key, ctx->key,GHASH_BLOCK_SIZE ); +return 0 ; }static struct shash_alg ghash_alg = { .digestsize =GHASH_DIGEST_SIZE , .init = ghash_init, .update = ghash_update, - .final = ghash_final, + .finup = ghash_finup, .setkey = ghash_setkey, - .descsize =sizeof (struct ghash_desc_ctx ), + .export = ghash_export, + .import = ghash_import, + .statesize =sizeof (struct ghash_desc_ctx ), + .descsize =sizeof (struct s390_ghash_desc_ctx ), .base = { .cra_name ="ghash" , .cra_driver_name ="ghash-s390" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =GHASH_BLOCK_SIZE , - .cra_ctxsize =sizeof (struct ghash_ctx ), + .cra_ctxsize =sizeof (struct s390_ghash_ctx ), .cra_module =THIS_MODULE , }, };
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 10/67 crypto: x86/ghash - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/x86/crypto/ghash-clmulni-intel_asm.S | 5 +- arch/x86/crypto/ghash-clmulni-intel_glue.c | 301 +++------------------ 2 files changed, 43 insertions(+), 263 deletions(-)
arch/x86/crypto/ghash-clmulni-intel_asm.S +3 -2
@@ -103,8 +103,8 @@ SYM_FUNC_START(clmul_ghash_mul) SYM_FUNC_END(clmul_ghash_mul) /* - * void clmul_ghash_update(char *dst, const char *src, unsigned int srclen, - * const le128 *shash); + * int clmul_ghash_update(char *dst, const char *src, unsigned int srclen, + * const le128 *shash); */ SYM_FUNC_START(clmul_ghash_update) FRAME_BEGIN
@@ -127,6 +127,7 @@ SYM_FUNC_START(clmul_ghash_update) pshufb BSWAP, DATA movups DATA, (%rdi) .Lupdate_just_ret: + mov %rdx, %rax FRAME_END RET SYM_FUNC_END(clmul_ghash_update)
arch/x86/crypto/ghash-clmulni-intel_glue.c +40 -261
367 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 11/67 crypto: md5-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/md5.c | 102 ++++++++++++++++--------------------------- include/crypto/md5.h | 3 +- 2 files changed, 40 insertions(+), 65 deletions(-)
crypto/md5.c +38 -64
@@ -17,11 +17,9 @@*/ #include <crypto/internal/hash.h> #include <crypto/md5.h> -#include <linux/init.h> +#include <linux/kernel.h> #include <linux/module.h> #include <linux/string.h> -#include <linux/types.h> -#include <asm/byteorder.h> const u8 md5_zero_message_hash[MD5_DIGEST_SIZE ] = {0xd4 ,0x1d ,0x8c ,0xd9 ,0x8f ,0x00 ,0xb2 ,0x04 ,
@@ -120,10 +118,11 @@ static void md5_transform(__u32 *hash, __u32 const *in) hash[3 ] += d; } -static inline void md5_transform_helper (struct md5_state *ctx) +static inline void md5_transform_helper (struct md5_state *ctx, +u32 block[MD5_BLOCK_WORDS ]) { -le32_to_cpu_array (ctx->block,sizeof (ctx->block) /sizeof (u32)); -md5_transform (ctx->hash, ctx->block); +le32_to_cpu_array (block,MD5_BLOCK_WORDS ); +md5_transform (ctx->hash, block); }static int md5_init (struct shash_desc *desc)
@@ -142,91 +141,66 @@ static int md5_init(struct shash_desc *desc)static int md5_update (struct shash_desc *desc,const u8 *data,unsigned int len) {struct md5_state *mctx =shash_desc_ctx (desc); -const u32 avail =sizeof (mctx->block) - (mctx->byte_count &0x3f ); +u32 block[MD5_BLOCK_WORDS ]; mctx->byte_count += len; - -if (avail > len) { -memcpy ((char *)mctx->block + (sizeof (mctx->block) - avail), - data, len); -return 0 ; - } - -memcpy ((char *)mctx->block + (sizeof (mctx->block) - avail), - data, avail); - -md5_transform_helper (mctx); - data += avail; - len -= avail; - -while (len >=sizeof (mctx->block)) { -memcpy (mctx->block, data,sizeof (mctx->block)); -md5_transform_helper (mctx); - data +=sizeof (mctx->block); - len -=sizeof (mctx->block); - } - -memcpy (mctx->block, data, len); - -return 0 ; +do { +memcpy (block, data,sizeof (block)); +md5_transform_helper (mctx, block); + data +=sizeof (block); + len -=sizeof (block); + }while (len >=sizeof (block)); +memzero_explicit (block,sizeof (block)); + mctx->byte_count -= len; +return len; } -static int md5_final (struct shash_desc *desc,u8 *out) +static int md5_finup (struct shash_desc *desc,const u8 *data,unsigned int len, +u8 *out) {struct md5_state *mctx =shash_desc_ctx (desc); -const unsigned int offset = mctx->byte_count &0x3f ; -char *p = (char *)mctx->block + offset; -int padding =56 - (offset +1 ); +u32 block[MD5_BLOCK_WORDS ]; +unsigned int offset; +int padding; +char *p; + +memcpy (block, data, len); + + offset = len; + p = (char *)block + offset; + padding =56 - (offset +1 ); *p++ =0x80 ;if (padding <0 ) {memset (p,0x00 , padding +sizeof (u64)); -md5_transform_helper (mctx); - p = (char *)mctx->block; +md5_transform_helper (mctx, block); + p = (char *)block; padding =56 ; }memset (p,0 , padding); - mctx->block[14 ] = mctx->byte_count <<3 ; - mctx->block[15 ] = mctx->byte_count >>29 ; -le32_to_cpu_array (mctx->block, (sizeof (mctx->block) - -sizeof (u64)) /sizeof (u32)); -md5_transform (mctx->hash, mctx->block); + mctx->byte_count += len; + block[14 ] = mctx->byte_count <<3 ; + block[15 ] = mctx->byte_count >>29 ; +le32_to_cpu_array (block, (sizeof (block) -sizeof (u64)) /sizeof (u32)); +md5_transform (mctx->hash, block); +memzero_explicit (block,sizeof (block));cpu_to_le32_array (mctx->hash,sizeof (mctx->hash) /sizeof (u32));memcpy (out, mctx->hash,sizeof (mctx->hash)); -memset (mctx,0 ,sizeof (*mctx));return 0 ; } -static int md5_export (struct shash_desc *desc,void *out) -{ -struct md5_state *ctx =shash_desc_ctx (desc); - -memcpy (out, ctx,sizeof (*ctx)); -return 0 ; -} - -static int md5_import (struct shash_desc *desc,const void *in) -{ -struct md5_state *ctx =shash_desc_ctx (desc); - -memcpy (ctx, in,sizeof (*ctx)); -return 0 ; -} -static struct shash_alg alg = { .digestsize =MD5_DIGEST_SIZE , .init = md5_init, .update = md5_update, - .final = md5_final, - .export = md5_export, - .import = md5_import, - .descsize =sizeof (struct md5_state ), - .statesize =sizeof (struct md5_state ), + .finup = md5_finup, + .descsize =MD5_STATE_SIZE , .base = { .cra_name ="md5" , .cra_driver_name ="md5-generic" , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =MD5_HMAC_BLOCK_SIZE , .cra_module =THIS_MODULE , }
include/crypto/md5.h +2 -1
@@ -8,6 +8,7 @@#define MD5_HMAC_BLOCK_SIZE 64#define MD5_BLOCK_WORDS 16#define MD5_HASH_WORDS 4 +#define MD5_STATE_SIZE 24#define MD5_H0 0x67452301UL#define MD5_H1 0xefcdab89UL
@@ -18,8 +19,8 @@ extern const u8 md5_zero_message_hash[MD5_DIGEST_SIZE];struct md5_state {u32 hash[MD5_HASH_WORDS ]; -u32 block[MD5_BLOCK_WORDS ];u64 byte_count; +u32 block[MD5_BLOCK_WORDS ]; };#endif
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 12/67 crypto: mips/octeon-md5 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also switch to the generic export format.
--- arch/mips/cavium-octeon/crypto/octeon-md5.c | 119 +++++++++++--------- 1 file changed, 63 insertions(+), 56 deletions(-)
arch/mips/cavium-octeon/crypto/octeon-md5.c +63 -56
@@ -19,22 +19,26 @@* any later version. */ -#include <crypto/md5.h> -#include <linux/init.h> -#include <linux/types.h> -#include <linux/module.h> -#include <linux/string.h> -#include <asm/byteorder.h> #include <asm/octeon/octeon.h> #include <crypto/internal/hash.h> +#include <crypto/md5.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> +#include <linux/unaligned.h> #include "octeon-crypto.h" +struct octeon_md5_state { +__le32 hash[MD5_HASH_WORDS ]; +u64 byte_count; +}; +/* * We pass everything as 64-bit. OCTEON can handle misaligned data. */ -static void octeon_md5_store_hash (struct md5_state *ctx) +static void octeon_md5_store_hash (struct octeon_md5_state *ctx) {u64 *hash = (u64 *)ctx->hash;
@@ -42,7 +46,7 @@ static void octeon_md5_store_hash(struct md5_state *ctx)write_octeon_64bit_hash_dword (hash[1 ],1 ); } -static void octeon_md5_read_hash (struct md5_state *ctx) +static void octeon_md5_read_hash (struct octeon_md5_state *ctx) {u64 *hash = (u64 *)ctx->hash;
@@ -66,13 +70,12 @@ static void octeon_md5_transform(const void *_block)static int octeon_md5_init (struct shash_desc *desc) { -struct md5_state *mctx =shash_desc_ctx (desc); +struct octeon_md5_state *mctx =shash_desc_ctx (desc); - mctx->hash[0 ] =MD5_H0 ; - mctx->hash[1 ] =MD5_H1 ; - mctx->hash[2 ] =MD5_H2 ; - mctx->hash[3 ] =MD5_H3 ; -cpu_to_le32_array (mctx->hash,4 ); + mctx->hash[0 ] =cpu_to_le32 (MD5_H0 ); + mctx->hash[1 ] =cpu_to_le32 (MD5_H1 ); + mctx->hash[2 ] =cpu_to_le32 (MD5_H2 ); + mctx->hash[3 ] =cpu_to_le32 (MD5_H3 ); mctx->byte_count =0 ;return 0 ;
@@ -81,52 +84,38 @@ static int octeon_md5_init(struct shash_desc *desc)static int octeon_md5_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct md5_state *mctx =shash_desc_ctx (desc); -const u32 avail =sizeof (mctx->block) - (mctx->byte_count &0x3f ); +struct octeon_md5_state *mctx =shash_desc_ctx (desc);struct octeon_cop2_state state;unsigned long flags; mctx->byte_count += len; - -if (avail > len) { -memcpy ((char *)mctx->block + (sizeof (mctx->block) - avail), - data, len); -return 0 ; - } - -memcpy ((char *)mctx->block + (sizeof (mctx->block) - avail), data, - avail); - flags =octeon_crypto_enable (&state);octeon_md5_store_hash (mctx); -octeon_md5_transform (mctx->block); - data += avail; - len -= avail; - -while (len >=sizeof (mctx->block)) { +do {octeon_md5_transform (data); - data +=sizeof (mctx->block); - len -=sizeof (mctx->block); - } + data +=MD5_HMAC_BLOCK_SIZE ; + len -=MD5_HMAC_BLOCK_SIZE ; + }while (len >=MD5_HMAC_BLOCK_SIZE );octeon_md5_read_hash (mctx);octeon_crypto_disable (&state, flags); - -memcpy (mctx->block, data, len); - -return 0 ; + mctx->byte_count -= len; +return len; } -static int octeon_md5_final (struct shash_desc *desc,u8 *out) +static int octeon_md5_finup (struct shash_desc *desc,const u8 *src, +unsigned int offset,u8 *out) { -struct md5_state *mctx =shash_desc_ctx (desc); -const unsigned int offset = mctx->byte_count &0x3f ; -char *p = (char *)mctx->block + offset; +struct octeon_md5_state *mctx =shash_desc_ctx (desc);int padding =56 - (offset +1 );struct octeon_cop2_state state; +u32 block[MD5_BLOCK_WORDS ];unsigned long flags; +char *p; + p =memcpy (block, src, offset); + p += offset; *p++ =0x80 ; flags =octeon_crypto_enable (&state);
@@ -134,39 +123,56 @@ static int octeon_md5_final(struct shash_desc *desc, u8 *out)if (padding <0 ) {memset (p,0x00 , padding +sizeof (u64)); -octeon_md5_transform (mctx->block); - p = (char *)mctx->block; +octeon_md5_transform (block); + p = (char *)block; padding =56 ; }memset (p,0 , padding); - mctx->block[14 ] = mctx->byte_count <<3 ; - mctx->block[15 ] = mctx->byte_count >>29 ; -cpu_to_le32_array (mctx->block +14 ,2 ); -octeon_md5_transform (mctx->block); + mctx->byte_count += offset; + block[14 ] = mctx->byte_count <<3 ; + block[15 ] = mctx->byte_count >>29 ; +cpu_to_le32_array (block +14 ,2 ); +octeon_md5_transform (block);octeon_md5_read_hash (mctx);octeon_crypto_disable (&state, flags); +memzero_explicit (block,sizeof (block));memcpy (out, mctx->hash,sizeof (mctx->hash)); -memset (mctx,0 ,sizeof (*mctx));return 0 ; }static int octeon_md5_export (struct shash_desc *desc,void *out) { -struct md5_state *ctx =shash_desc_ctx (desc); +struct octeon_md5_state *ctx =shash_desc_ctx (desc); +union { +u8 *u8; +u32 *u32; +u64 *u64; + } p = { .u8 = out }; +int i; -memcpy (out, ctx,sizeof (*ctx)); +for (i =0 ; i <MD5_HASH_WORDS ; i++) +put_unaligned (le32_to_cpu (ctx->hash[i]), p.u32++); +put_unaligned (ctx->byte_count, p.u64);return 0 ; }static int octeon_md5_import (struct shash_desc *desc,const void *in) { -struct md5_state *ctx =shash_desc_ctx (desc); +struct octeon_md5_state *ctx =shash_desc_ctx (desc); +union { +const u8 *u8; +const u32 *u32; +const u64 *u64; + } p = { .u8 = in }; +int i; -memcpy (ctx, in,sizeof (*ctx)); +for (i =0 ; i <MD5_HASH_WORDS ; i++) + ctx->hash[i] =cpu_to_le32 (get_unaligned (p.u32++)); + ctx->byte_count =get_unaligned (p.u64);return 0 ; }
@@ -174,15 +180,16 @@ static struct shash_alg alg = { .digestsize = MD5_DIGEST_SIZE, .init = octeon_md5_init, .update = octeon_md5_update, - .final = octeon_md5_final, + .finup = octeon_md5_finup, .export = octeon_md5_export, .import = octeon_md5_import, - .descsize = sizeof(struct md5_state), - .statesize = sizeof(struct md5_state), + .statesize = MD5_STATE_SIZE, + .descsize = sizeof(struct octeon_md5_state), .base = { .cra_name = "md5", .cra_driver_name= "octeon-md5", .cra_priority = OCTEON_CR_OPCODE_PRIORITY, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_blocksize = MD5_HMAC_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 13/67 crypto: powerpc/md5 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/powerpc/crypto/md5-glue.c | 99 +++++++--------------------------- 1 file changed, 20 insertions(+), 79 deletions(-)
arch/powerpc/crypto/md5-glue.c +20 -79
@@ -8,25 +8,13 @@*/ #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/md5.h> -#include <asm/byteorder.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> extern void ppc_md5_transform (u32 *state,const u8 *src,u32 blocks); -static inline void ppc_md5_clear_context (struct md5_state *sctx) -{ -int count =sizeof (struct md5_state ) >>2 ; -u32 *ptr = (u32 *)sctx; - -/* make sure we can clear the fast way */ -BUILD_BUG_ON (sizeof (struct md5_state ) %4 ); -do { *ptr++ =0 ; }while (--count); -} -static int ppc_md5_init (struct shash_desc *desc) {struct md5_state *sctx =shash_desc_ctx (desc);
@@ -44,79 +32,34 @@ static int ppc_md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) {struct md5_state *sctx =shash_desc_ctx (desc); -const unsigned int offset = sctx->byte_count &0x3f ; -unsigned int avail =64 - offset; -const u8 *src = data; - sctx->byte_count += len; - -if (avail > len) { -memcpy ((char *)sctx->block + offset, src, len); -return 0 ; - } - -if (offset) { -memcpy ((char *)sctx->block + offset, src, avail); -ppc_md5_transform (sctx->hash, (const u8 *)sctx->block,1 ); - len -= avail; - src += avail; - } - -if (len >63 ) { -ppc_md5_transform (sctx->hash, src, len >>6 ); - src += len & ~0x3f ; - len &=0x3f ; - } - -memcpy ((char *)sctx->block, src, len); -return 0 ; + sctx->byte_count +=round_down (len,MD5_HMAC_BLOCK_SIZE ); +ppc_md5_transform (sctx->hash, data, len >>6 ); +return len -round_down (len,MD5_HMAC_BLOCK_SIZE ); } -static int ppc_md5_final (struct shash_desc *desc,u8 *out) +static int ppc_md5_finup (struct shash_desc *desc,const u8 *src, +unsigned int offset,u8 *out) {struct md5_state *sctx =shash_desc_ctx (desc); -const unsigned int offset = sctx->byte_count &0x3f ; -const u8 *src = (const u8 *)sctx->block; -u8 *p = (u8 *)src + offset; -int padlen =55 - offset; -__le64 *pbits = (__le64 *)((char *)sctx->block +56 ); +__le64 block[MD5_BLOCK_WORDS ] = {}; +u8 *p =memcpy (block, src, offset);__le32 *dst = (__le32 *)out; +__le64 *pbits; + src = p; + p += offset; *p++ =0x80 ; - -if (padlen <0 ) { -memset (p,0x00 , padlen +sizeof (u64)); -ppc_md5_transform (sctx->hash, src,1 ); - p = (char *)sctx->block; - padlen =56 ; - } - -memset (p,0 , padlen); + sctx->byte_count += offset; + pbits = &block[(MD5_BLOCK_WORDS / (offset >55 ?1 :2 )) -1 ]; *pbits =cpu_to_le64 (sctx->byte_count <<3 ); -ppc_md5_transform (sctx->hash, src,1 ); +ppc_md5_transform (sctx->hash, src, (pbits - block +1 ) /8 ); +memzero_explicit (block,sizeof (block)); dst[0 ] =cpu_to_le32 (sctx->hash[0 ]); dst[1 ] =cpu_to_le32 (sctx->hash[1 ]); dst[2 ] =cpu_to_le32 (sctx->hash[2 ]); dst[3 ] =cpu_to_le32 (sctx->hash[3 ]); - -ppc_md5_clear_context (sctx); -return 0 ; -} - -static int ppc_md5_export (struct shash_desc *desc,void *out) -{ -struct md5_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); -return 0 ; -} - -static int ppc_md5_import (struct shash_desc *desc,const void *in) -{ -struct md5_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx));return 0 ; }
@@ -124,15 +67,13 @@ static struct shash_alg alg = { .digestsize = MD5_DIGEST_SIZE, .init = ppc_md5_init, .update = ppc_md5_update, - .final = ppc_md5_final, - .export = ppc_md5_export, - .import = ppc_md5_import, - .descsize = sizeof(struct md5_state), - .statesize = sizeof(struct md5_state), + .finup = ppc_md5_finup, + .descsize = MD5_STATE_SIZE, .base = { .cra_name = "md5", .cra_driver_name= "md5-ppc", .cra_priority = 200, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_blocksize = MD5_HMAC_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 14/67 crypto: sparc/md5 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also switch to the generic export format.
--- arch/sparc/crypto/md5_glue.c | 141 ++++++++++++++++------------------- 1 file changed, 63 insertions(+), 78 deletions(-)
arch/sparc/crypto/md5_glue.c +63 -78
@@ -14,121 +14,105 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt -#include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> -#include <crypto/md5.h> - -#include <asm/pstate.h> #include <asm/elf.h> +#include <asm/pstate.h> +#include <crypto/internal/hash.h> +#include <crypto/md5.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> +#include <linux/unaligned.h> #include "opcodes.h" -asmlinkage void md5_sparc64_transform(u32 *digest, const char *data, +struct sparc_md5_state { +__le32 hash[MD5_HASH_WORDS ]; +u64 byte_count; +}; + +asmlinkage void md5_sparc64_transform(__le32 *digest, const char *data,unsigned int rounds);static int md5_sparc64_init (struct shash_desc *desc) { -struct md5_state *mctx =shash_desc_ctx (desc); +struct sparc_md5_state *mctx =shash_desc_ctx (desc); - mctx->hash[0 ] =MD5_H0 ; - mctx->hash[1 ] =MD5_H1 ; - mctx->hash[2 ] =MD5_H2 ; - mctx->hash[3 ] =MD5_H3 ; -le32_to_cpu_array (mctx->hash,4 ); + mctx->hash[0 ] =cpu_to_le32 (MD5_H0 ); + mctx->hash[1 ] =cpu_to_le32 (MD5_H1 ); + mctx->hash[2 ] =cpu_to_le32 (MD5_H2 ); + mctx->hash[3 ] =cpu_to_le32 (MD5_H3 ); mctx->byte_count =0 ;return 0 ; } -static void __md5_sparc64_update (struct md5_state *sctx,const u8 *data, -unsigned int len,unsigned int partial) -{ -unsigned int done =0 ; - - sctx->byte_count += len; -if (partial) { - done =MD5_HMAC_BLOCK_SIZE - partial; -memcpy ((u8 *)sctx->block + partial, data, done); -md5_sparc64_transform (sctx->hash, (u8 *)sctx->block,1 ); - } -if (len - done >=MD5_HMAC_BLOCK_SIZE ) { -const unsigned int rounds = (len - done) /MD5_HMAC_BLOCK_SIZE ; - -md5_sparc64_transform (sctx->hash, data + done, rounds); - done += rounds *MD5_HMAC_BLOCK_SIZE ; - } - -memcpy (sctx->block, data + done, len - done); -} -static int md5_sparc64_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct md5_state *sctx =shash_desc_ctx (desc); -unsigned int partial = sctx->byte_count %MD5_HMAC_BLOCK_SIZE ; +struct sparc_md5_state *sctx =shash_desc_ctx (desc); -/* Handle the fast case right here */ -if (partial + len <MD5_HMAC_BLOCK_SIZE ) { - sctx->byte_count += len; -memcpy ((u8 *)sctx->block + partial, data, len); - }else -__md5_sparc64_update (sctx, data, len, partial); - -return 0 ; + sctx->byte_count +=round_down (len,MD5_HMAC_BLOCK_SIZE ); +md5_sparc64_transform (sctx->hash, data, len /MD5_HMAC_BLOCK_SIZE ); +return len -round_down (len,MD5_HMAC_BLOCK_SIZE ); }/* Add padding and return the message digest. */ -static int md5_sparc64_final (struct shash_desc *desc,u8 *out) +static int md5_sparc64_finup (struct shash_desc *desc,const u8 *src, +unsigned int offset,u8 *out) { -struct md5_state *sctx =shash_desc_ctx (desc); -unsigned int i, index, padlen; -u32 *dst = (u32 *)out; -__le64 bits; -static const u8 padding[MD5_HMAC_BLOCK_SIZE ] = {0x80 , }; +struct sparc_md5_state *sctx =shash_desc_ctx (desc); +__le64 block[MD5_BLOCK_WORDS ] = {}; +u8 *p =memcpy (block, src, offset); +__le32 *dst = (__le32 *)out; +__le64 *pbits; +int i; - bits =cpu_to_le64 (sctx->byte_count <<3 ); - -/* Pad out to 56 mod 64 and append length */ - index = sctx->byte_count %MD5_HMAC_BLOCK_SIZE ; - padlen = (index <56 ) ? (56 - index) : ((MD5_HMAC_BLOCK_SIZE +56 ) - index); - -/* We need to fill a whole block for __md5_sparc64_update() */ -if (padlen <=56 ) { - sctx->byte_count += padlen; -memcpy ((u8 *)sctx->block + index, padding, padlen); - }else { -__md5_sparc64_update (sctx, padding, padlen, index); - } -__md5_sparc64_update (sctx, (const u8 *)&bits,sizeof (bits),56 ); + src = p; + p += offset; + *p++ =0x80 ; + sctx->byte_count += offset; + pbits = &block[(MD5_BLOCK_WORDS / (offset >55 ?1 :2 )) -1 ]; + *pbits =cpu_to_le64 (sctx->byte_count <<3 ); +md5_sparc64_transform (sctx->hash, src, (pbits - block +1 ) /8 ); +memzero_explicit (block,sizeof (block));/* Store state in digest */ for (i =0 ; i <MD5_HASH_WORDS ; i++) dst[i] = sctx->hash[i]; -/* Wipe context */ -memset (sctx,0 ,sizeof (*sctx)); -return 0 ; }static int md5_sparc64_export (struct shash_desc *desc,void *out) { -struct md5_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); +struct sparc_md5_state *sctx =shash_desc_ctx (desc); +union { +u8 *u8; +u32 *u32; +u64 *u64; + } p = { .u8 = out }; +int i; +for (i =0 ; i <MD5_HASH_WORDS ; i++) +put_unaligned (le32_to_cpu (sctx->hash[i]), p.u32++); +put_unaligned (sctx->byte_count, p.u64);return 0 ; }static int md5_sparc64_import (struct shash_desc *desc,const void *in) { -struct md5_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); +struct sparc_md5_state *sctx =shash_desc_ctx (desc); +union { +const u8 *u8; +const u32 *u32; +const u64 *u64; + } p = { .u8 = in }; +int i; +for (i =0 ; i <MD5_HASH_WORDS ; i++) + sctx->hash[i] =cpu_to_le32 (get_unaligned (p.u32++)); + sctx->byte_count =get_unaligned (p.u64);return 0 ; }
@@ -136,15 +120,16 @@ static struct shash_alg alg = { .digestsize = MD5_DIGEST_SIZE, .init = md5_sparc64_init, .update = md5_sparc64_update, - .final = md5_sparc64_final, + .finup = md5_sparc64_finup, .export = md5_sparc64_export, .import = md5_sparc64_import, - .descsize = sizeof(struct md5_state), - .statesize = sizeof(struct md5_state), + .descsize = sizeof(struct sparc_md5_state), + .statesize = sizeof(struct sparc_md5_state), .base = { .cra_name = "md5", .cra_driver_name= "md5-sparc64", .cra_priority = SPARC_CR_OPCODE_PRIORITY, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_blocksize = MD5_HMAC_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 15/67 crypto: x86/sha1 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/x86/crypto/sha1_ssse3_glue.c | 81 ++++++++++--------------------- include/crypto/sha1.h | 1 + include/crypto/sha1_base.h | 42 ++++++++++++++-- 3 files changed, 64 insertions(+), 60 deletions(-)
arch/x86/crypto/sha1_ssse3_glue.c +25 -56
@@ -16,16 +16,14 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt -#include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> -#include <crypto/sha1.h> -#include <crypto/sha1_base.h> #include <asm/cpu_device_id.h> #include <asm/simd.h> +#include <crypto/internal/hash.h> +#include <crypto/sha1.h> +#include <crypto/sha1_base.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/module.h> static const struct x86_cpu_id module_cpu_ids[] = {X86_MATCH_FEATURE (X86_FEATURE_SHA_NI ,NULL ),
@@ -36,14 +34,10 @@ static const struct x86_cpu_id module_cpu_ids[] = { };MODULE_DEVICE_TABLE (x86cpu, module_cpu_ids); -static int sha1_update (struct shash_desc *desc,const u8 *data, -unsigned int len,sha1_block_fn *sha1_xform) +static inline int sha1_update (struct shash_desc *desc,const u8 *data, +unsigned int len,sha1_block_fn *sha1_xform) { -struct sha1_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count %SHA1_BLOCK_SIZE ) + len <SHA1_BLOCK_SIZE ) -return crypto_sha1_update (desc, data, len); +int remain;/* * Make sure struct sha1_state begins directly with the SHA1
@@ -52,22 +46,18 @@ static int sha1_update(struct shash_desc *desc, const u8 *data,BUILD_BUG_ON (offsetof(struct sha1_state , state) !=0 );kernel_fpu_begin (); -sha1_base_do_update (desc, data, len, sha1_xform); + remain =sha1_base_do_update_blocks (desc, data, len, sha1_xform);kernel_fpu_end (); -return 0 ; +return remain; } -static int sha1_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out,sha1_block_fn *sha1_xform) +static inline int sha1_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out, +sha1_block_fn *sha1_xform) { -if (!crypto_simd_usable ()) -return crypto_sha1_finup (desc, data, len, out); -kernel_fpu_begin (); -if (len) -sha1_base_do_update (desc, data, len, sha1_xform); -sha1_base_do_finalize (desc, sha1_xform); +sha1_base_do_finup (desc, data, len, sha1_xform);kernel_fpu_end ();return sha1_base_finish (desc, out);
@@ -88,23 +78,17 @@ static int sha1_ssse3_finup(struct shash_desc *desc, const u8 *data,return sha1_finup (desc, data, len, out, sha1_transform_ssse3); } -/* Add padding and return the message digest. */ -static int sha1_ssse3_final (struct shash_desc *desc,u8 *out) -{ -return sha1_ssse3_finup (desc,NULL ,0 , out); -} -static struct shash_alg sha1_ssse3_alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = sha1_ssse3_update, - .final = sha1_ssse3_final, .finup = sha1_ssse3_finup, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-ssse3" , .cra_priority =150 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -138,22 +122,17 @@ static int sha1_avx_finup(struct shash_desc *desc, const u8 *data,return sha1_finup (desc, data, len, out, sha1_transform_avx); } -static int sha1_avx_final (struct shash_desc *desc,u8 *out) -{ -return sha1_avx_finup (desc,NULL ,0 , out); -} -static struct shash_alg sha1_avx_alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = sha1_avx_update, - .final = sha1_avx_final, .finup = sha1_avx_finup, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-avx" , .cra_priority =160 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -198,8 +177,8 @@ static bool avx2_usable(void)return false; } -static void sha1_apply_transform_avx2 (struct sha1_state *state, -const u8 *data,int blocks) +static inline void sha1_apply_transform_avx2 (struct sha1_state *state, +const u8 *data,int blocks) {/* Select the optimal transform based on data block size */ if (blocks >=SHA1_AVX2_BLOCK_OPTSIZE )
@@ -220,22 +199,17 @@ static int sha1_avx2_finup(struct shash_desc *desc, const u8 *data,return sha1_finup (desc, data, len, out, sha1_apply_transform_avx2); } -static int sha1_avx2_final (struct shash_desc *desc,u8 *out) -{ -return sha1_avx2_finup (desc,NULL ,0 , out); -} -static struct shash_alg sha1_avx2_alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = sha1_avx2_update, - .final = sha1_avx2_final, .finup = sha1_avx2_finup, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-avx2" , .cra_priority =170 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -269,22 +243,17 @@ static int sha1_ni_finup(struct shash_desc *desc, const u8 *data,return sha1_finup (desc, data, len, out, sha1_ni_transform); } -static int sha1_ni_final (struct shash_desc *desc,u8 *out) -{ -return sha1_ni_finup (desc,NULL ,0 , out); -} -static struct shash_alg sha1_ni_alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = sha1_ni_update, - .final = sha1_ni_final, .finup = sha1_ni_finup, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-ni" , .cra_priority =250 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
include/crypto/sha1.h +1 -0
@@ -10,6 +10,7 @@#define SHA1_DIGEST_SIZE 20#define SHA1_BLOCK_SIZE 64 +#define SHA1_STATE_SIZE offsetof(struct sha1_state, buffer)#define SHA1_H0 0x67452301UL#define SHA1_H1 0xefcdab89UL
include/crypto/sha1_base.h +38 -4
67 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 16/67 crypto: arm64/sha1 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/sha1-ce-glue.c | 66 ++++++++------------------------ 1 file changed, 17 insertions(+), 49 deletions(-)
arch/arm64/crypto/sha1-ce-glue.c +17 -49
@@ -7,14 +7,14 @@#include <asm/neon.h> #include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/internal/hash.h> #include <crypto/internal/simd.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> +#include <linux/string.h> MODULE_DESCRIPTION ("SHA1 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );
@@ -56,79 +56,47 @@ static int sha1_ce_update(struct shash_desc *desc, const u8 *data, {struct sha1_ce_state *sctx =shash_desc_ctx (desc); -if (!crypto_simd_usable ()) -return crypto_sha1_update (desc, data, len); - sctx->finalize =0 ; -sha1_base_do_update (desc, data, len, sha1_ce_transform); - -return 0 ; +return sha1_base_do_update_blocks (desc, data, len, sha1_ce_transform); }static int sha1_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {struct sha1_ce_state *sctx =shash_desc_ctx (desc); -bool finalize = !sctx->sst.count && !(len %SHA1_BLOCK_SIZE ) && len; - -if (!crypto_simd_usable ()) -return crypto_sha1_finup (desc, data, len, out); +bool finalized = false;/* * Allow the asm code to perform the finalization if there is no * partial data and the input is a round multiple of the block size. */ - sctx->finalize = finalize; +if (len >=SHA1_BLOCK_SIZE ) { +unsigned int remain = len -round_down (len,SHA1_BLOCK_SIZE ); -sha1_base_do_update (desc, data, len, sha1_ce_transform); -if (!finalize) -sha1_base_do_finalize (desc, sha1_ce_transform); + finalized = !remain; + sctx->finalize = finalized; +sha1_base_do_update_blocks (desc, data, len, sha1_ce_transform); + data += len - remain; + len = remain; + } +if (!finalized) +sha1_base_do_finup (desc, data, len, sha1_ce_transform);return sha1_base_finish (desc, out); } -static int sha1_ce_final (struct shash_desc *desc,u8 *out) -{ -struct sha1_ce_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable ()) -return crypto_sha1_finup (desc,NULL ,0 , out); - - sctx->finalize =0 ; -sha1_base_do_finalize (desc, sha1_ce_transform); -return sha1_base_finish (desc, out); -} - -static int sha1_ce_export (struct shash_desc *desc,void *out) -{ -struct sha1_ce_state *sctx =shash_desc_ctx (desc); - -memcpy (out, &sctx->sst,sizeof (struct sha1_state )); -return 0 ; -} - -static int sha1_ce_import (struct shash_desc *desc,const void *in) -{ -struct sha1_ce_state *sctx =shash_desc_ctx (desc); - -memcpy (&sctx->sst, in,sizeof (struct sha1_state )); - sctx->finalize =0 ; -return 0 ; -} -static struct shash_alg alg = { .init = sha1_base_init, .update = sha1_ce_update, - .final = sha1_ce_final, .finup = sha1_ce_finup, - .import = sha1_ce_import, - .export = sha1_ce_export, .descsize =sizeof (struct sha1_ce_state ), - .statesize =sizeof (struct sha1_state ), + .statesize =SHA1_STATE_SIZE , .digestsize =SHA1_DIGEST_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-ce" , .cra_priority =200 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 17/67 crypto: mips/octeon-sha1 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/mips/cavium-octeon/crypto/octeon-sha1.c | 136 ++++--------------- 1 file changed, 30 insertions(+), 106 deletions(-)
arch/mips/cavium-octeon/crypto/octeon-sha1.c +30 -106
@@ -13,15 +13,13 @@* Copyright (c) Jean-Francois Dive <[email protected]> */ -#include <linux/mm.h> -#include <crypto/sha1.h> -#include <crypto/sha1_base.h> -#include <linux/init.h> -#include <linux/types.h> -#include <linux/module.h> -#include <asm/byteorder.h> #include <asm/octeon/octeon.h> #include <crypto/internal/hash.h> +#include <crypto/sha1.h> +#include <crypto/sha1_base.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "octeon-crypto.h"
@@ -58,49 +56,23 @@ static void octeon_sha1_read_hash(struct sha1_state *sctx)memzero_explicit (&hash_tail.dword,sizeof (hash_tail.dword)); } -static void octeon_sha1_transform (const void *_block) +static void octeon_sha1_transform (struct sha1_state *sctx,const u8 *src, +int blocks) { -const u64 *block = _block; +do { +const u64 *block = (const u64 *)src; -write_octeon_64bit_block_dword (block[0 ],0 ); -write_octeon_64bit_block_dword (block[1 ],1 ); -write_octeon_64bit_block_dword (block[2 ],2 ); -write_octeon_64bit_block_dword (block[3 ],3 ); -write_octeon_64bit_block_dword (block[4 ],4 ); -write_octeon_64bit_block_dword (block[5 ],5 ); -write_octeon_64bit_block_dword (block[6 ],6 ); -octeon_sha1_start (block[7 ]); -} +write_octeon_64bit_block_dword (block[0 ],0 ); +write_octeon_64bit_block_dword (block[1 ],1 ); +write_octeon_64bit_block_dword (block[2 ],2 ); +write_octeon_64bit_block_dword (block[3 ],3 ); +write_octeon_64bit_block_dword (block[4 ],4 ); +write_octeon_64bit_block_dword (block[5 ],5 ); +write_octeon_64bit_block_dword (block[6 ],6 ); +octeon_sha1_start (block[7 ]); -static void __octeon_sha1_update (struct sha1_state *sctx,const u8 *data, -unsigned int len) -{ -unsigned int partial; -unsigned int done; -const u8 *src; - - partial = sctx->count %SHA1_BLOCK_SIZE ; - sctx->count += len; - done =0 ; - src = data; - -if ((partial + len) >=SHA1_BLOCK_SIZE ) { -if (partial) { - done = -partial; -memcpy (sctx->buffer + partial, data, - done +SHA1_BLOCK_SIZE ); - src = sctx->buffer; - } - -do { -octeon_sha1_transform (src); - done +=SHA1_BLOCK_SIZE ; - src = data + done; - }while (done +SHA1_BLOCK_SIZE <= len); - - partial =0 ; - } -memcpy (sctx->buffer + partial, src, len - done); + src +=SHA1_BLOCK_SIZE ; + }while (--blocks); } static int octeon_sha1_update(struct shash_desc *desc, const u8 *data,
@@ -109,95 +81,47 @@ static int octeon_sha1_update(struct shash_desc *desc, const u8 *data,struct sha1_state *sctx =shash_desc_ctx (desc);struct octeon_cop2_state state;unsigned long flags; - -/* -* Small updates never reach the crypto engine, so the generic sha1 is -* faster because of the heavyweight octeon_crypto_enable() / -* octeon_crypto_disable(). -*/ -if ((sctx->count %SHA1_BLOCK_SIZE ) + len <SHA1_BLOCK_SIZE ) -return crypto_sha1_update (desc, data, len); +int remain; flags =octeon_crypto_enable (&state);octeon_sha1_store_hash (sctx); -__octeon_sha1_update (sctx, data, len); + remain =sha1_base_do_update_blocks (desc, data, len, + octeon_sha1_transform);octeon_sha1_read_hash (sctx);octeon_crypto_disable (&state, flags); - -return 0 ; +return remain; } -static int octeon_sha1_final (struct shash_desc *desc,u8 *out) +static int octeon_sha1_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) {struct sha1_state *sctx =shash_desc_ctx (desc); -static const u8 padding[64 ] = {0x80 , };struct octeon_cop2_state state; -__be32 *dst = (__be32 *)out; -unsigned int pad_len;unsigned long flags; -unsigned int index; -__be64 bits; -int i; - -/* Save number of bits. */ - bits =cpu_to_be64 (sctx->count <<3 ); - -/* Pad out to 56 mod 64. */ - index = sctx->count &0x3f ; - pad_len = (index <56 ) ? (56 - index) : ((64 +56 ) - index); flags =octeon_crypto_enable (&state);octeon_sha1_store_hash (sctx); -__octeon_sha1_update (sctx, padding, pad_len); - -/* Append length (before padding). */ -__octeon_sha1_update (sctx, (const u8 *)&bits,sizeof (bits)); +sha1_base_do_finup (desc, src, len, octeon_sha1_transform);octeon_sha1_read_hash (sctx);octeon_crypto_disable (&state, flags); - -/* Store state in digest */ -for (i =0 ; i <5 ; i++) - dst[i] =cpu_to_be32 (sctx->state[i]); - -/* Zeroize sensitive information. */ -memset (sctx,0 ,sizeof (*sctx)); - -return 0 ; -} - -static int octeon_sha1_export (struct shash_desc *desc,void *out) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); -return 0 ; -} - -static int octeon_sha1_import (struct shash_desc *desc,const void *in) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); -return 0 ; +return sha1_base_finish (desc, out); }static struct shash_alg octeon_sha1_alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = octeon_sha1_update, - .final = octeon_sha1_final, - .export = octeon_sha1_export, - .import = octeon_sha1_import, - .descsize =sizeof (struct sha1_state ), - .statesize =sizeof (struct sha1_state ), + .finup = octeon_sha1_finup, + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name="octeon-sha1" , .cra_priority =OCTEON_CR_OPCODE_PRIORITY , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 18/67 crypto: sha1-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/sha1_generic.c | 33 ++++++++++++--------------------- include/crypto/sha1.h | 8 -------- 2 files changed, 12 insertions(+), 29 deletions(-)
crypto/sha1_generic.c +12 -21
@@ -12,13 +12,11 @@* Copyright (c) Jean-Francois Dive <[email protected]> */ #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> -#include <asm/byteorder.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE ] = {0xda ,0x39 ,0xa3 ,0xee ,0x5e ,0x6b ,0x4b ,0x0d ,
@@ -39,38 +37,31 @@ static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,memzero_explicit (temp,sizeof (temp)); } -int crypto_sha1_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static int crypto_sha1_update (struct shash_desc *desc,const u8 *data, +unsigned int len) { -return sha1_base_do_update (desc, data, len, sha1_generic_block_fn); +return sha1_base_do_update_blocks (desc, data, len, + sha1_generic_block_fn); } -EXPORT_SYMBOL (crypto_sha1_update); -static int sha1_final (struct shash_desc *desc,u8 *out) +static int crypto_sha1_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out) { -sha1_base_do_finalize (desc, sha1_generic_block_fn); +sha1_base_do_finup (desc, data, len, sha1_generic_block_fn);return sha1_base_finish (desc, out); } -int crypto_sha1_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out) -{ -sha1_base_do_update (desc, data, len, sha1_generic_block_fn); -return sha1_final (desc, out); -} -EXPORT_SYMBOL (crypto_sha1_finup); -static struct shash_alg alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = crypto_sha1_update, - .final = sha1_final, .finup = crypto_sha1_finup, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name="sha1-generic" , .cra_priority =100 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
include/crypto/sha1.h +0 -8
@@ -26,14 +26,6 @@ struct sha1_state {u8 buffer[SHA1_BLOCK_SIZE ]; }; -struct shash_desc ; - -extern int crypto_sha1_update (struct shash_desc *desc,const u8 *data, -unsigned int len); - -extern int crypto_sha1_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *hash); -/* * An implementation of SHA-1's compression function. Don't use in new code! * You shouldn't be using SHA-1, and even if you *have* to use SHA-1, this isn't
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 19/67 crypto: arm/sha1-ce - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm/crypto/sha1-ce-glue.c | 36 ++++++++-------------------------- 1 file changed, 8 insertions(+), 28 deletions(-)
arch/arm/crypto/sha1-ce-glue.c +8 -28
@@ -5,20 +5,14 @@* Copyright (C) 2015 Linaro Ltd <[email protected]> */ +#include <asm/neon.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> -#include <asm/hwcap.h> -#include <asm/neon.h> -#include <asm/simd.h> - -#include "sha1.h" -MODULE_DESCRIPTION ("SHA1 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_LICENSE ("GPL v2" );
@@ -29,50 +23,36 @@ asmlinkage void sha1_ce_transform(struct sha1_state *sst, u8 const *src,static int sha1_ce_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha1_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count %SHA1_BLOCK_SIZE ) + len <SHA1_BLOCK_SIZE ) -return sha1_update_arm (desc, data, len); +int remain;kernel_neon_begin (); -sha1_base_do_update (desc, data, len, sha1_ce_transform); + remain =sha1_base_do_update_blocks (desc, data, len, sha1_ce_transform);kernel_neon_end (); -return 0 ; +return remain; }static int sha1_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) -return sha1_finup_arm (desc, data, len, out); -kernel_neon_begin (); -if (len) -sha1_base_do_update (desc, data, len, sha1_ce_transform); -sha1_base_do_finalize (desc, sha1_ce_transform); +sha1_base_do_finup (desc, data, len, sha1_ce_transform);kernel_neon_end ();return sha1_base_finish (desc, out); } -static int sha1_ce_final (struct shash_desc *desc,u8 *out) -{ -return sha1_ce_finup (desc,NULL ,0 , out); -} -static struct shash_alg alg = { .init = sha1_base_init, .update = sha1_ce_update, - .final = sha1_ce_final, .finup = sha1_ce_finup, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .digestsize =SHA1_DIGEST_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-ce" , .cra_priority =200 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 20/67 crypto: arm/sha1-neon - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm/crypto/sha1_neon_glue.c | 39 ++++++++------------------------ 1 file changed, 10 insertions(+), 29 deletions(-)
arch/arm/crypto/sha1_neon_glue.c +10 -29
@@ -13,18 +13,12 @@* Copyright (c) Chandramouli Narayanan <[email protected]> */ +#include <asm/neon.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> -#include <asm/neon.h> -#include <asm/simd.h> - -#include "sha1.h" +#include <linux/kernel.h> +#include <linux/module.h> asmlinkage void sha1_transform_neon(struct sha1_state *state_h,const u8 *data,int rounds);
@@ -32,50 +26,37 @@ asmlinkage void sha1_transform_neon(struct sha1_state *state_h,static int sha1_neon_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha1_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count %SHA1_BLOCK_SIZE ) + len <SHA1_BLOCK_SIZE ) -return sha1_update_arm (desc, data, len); +int remain;kernel_neon_begin (); -sha1_base_do_update (desc, data, len, sha1_transform_neon); + remain =sha1_base_do_update_blocks (desc, data, len, + sha1_transform_neon);kernel_neon_end (); -return 0 ; +return remain; }static int sha1_neon_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) -return sha1_finup_arm (desc, data, len, out); -kernel_neon_begin (); -if (len) -sha1_base_do_update (desc, data, len, sha1_transform_neon); -sha1_base_do_finalize (desc, sha1_transform_neon); +sha1_base_do_finup (desc, data, len, sha1_transform_neon);kernel_neon_end ();return sha1_base_finish (desc, out); } -static int sha1_neon_final (struct shash_desc *desc,u8 *out) -{ -return sha1_neon_finup (desc,NULL ,0 , out); -} -static struct shash_alg alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = sha1_neon_update, - .final = sha1_neon_final, .finup = sha1_neon_finup, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-neon" , .cra_priority =250 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 21/67 crypto: arm/sha1-asm - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/arm/crypto/sha1.h | 14 -------------- arch/arm/crypto/sha1_glue.c | 33 +++++++++++---------------------- 2 files changed, 11 insertions(+), 36 deletions(-) delete mode 100644 arch/arm/crypto/sha1.h
arch/arm/crypto/sha1.h deleted +0 -14
@@ -1,14 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef ASM_ARM_CRYPTO_SHA1_H -#define ASM_ARM_CRYPTO_SHA1_H - -#include <linux/crypto.h> -#include <crypto/sha1.h> - -extern int sha1_update_arm (struct shash_desc *desc,const u8 *data, -unsigned int len); - -extern int sha1_finup_arm (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out); - -#endif
arch/arm/crypto/sha1_glue.c +11 -22
@@ -12,53 +12,42 @@*/ #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/types.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> -#include <asm/byteorder.h> - -#include "sha1.h" +#include <linux/kernel.h> +#include <linux/module.h> asmlinkage void sha1_block_data_order(struct sha1_state *digest,const u8 *data,int rounds); -int sha1_update_arm (struct shash_desc *desc,const u8 *data, -unsigned int len) +static int sha1_update_arm (struct shash_desc *desc,const u8 *data, +unsigned int len) {/* make sure signature matches sha1_block_fn() */ BUILD_BUG_ON (offsetof(struct sha1_state , state) !=0 ); -return sha1_base_do_update (desc, data, len, sha1_block_data_order); +return sha1_base_do_update_blocks (desc, data, len, + sha1_block_data_order); } -EXPORT_SYMBOL_GPL (sha1_update_arm); -static int sha1_final (struct shash_desc *desc,u8 *out) +static int sha1_finup_arm (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out) { -sha1_base_do_finalize (desc, sha1_block_data_order); +sha1_base_do_finup (desc, data, len, sha1_block_data_order);return sha1_base_finish (desc, out); } -int sha1_finup_arm (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out) -{ -sha1_base_do_update (desc, data, len, sha1_block_data_order); -return sha1_final (desc, out); -} -EXPORT_SYMBOL_GPL (sha1_finup_arm); -static struct shash_alg alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = sha1_update_arm, - .final = sha1_final, .finup = sha1_finup_arm, - .descsize =sizeof (struct sha1_state ), + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name="sha1-asm" , .cra_priority =150 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 22/67 crypto: powerpc/sha1 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/powerpc/crypto/sha1.c | 101 ++++++++----------------------------- 1 file changed, 20 insertions(+), 81 deletions(-)
arch/powerpc/crypto/sha1.c +20 -81
@@ -13,107 +13,46 @@* Copyright (c) Jean-Francois Dive <[email protected]> */ #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> -#include <asm/byteorder.h> +#include <linux/kernel.h> +#include <linux/module.h> -void powerpc_sha_transform (u32 *state,const u8 *src); +asmlinkage void powerpc_sha_transform(u32 *state, const u8 *src); + +static void powerpc_sha_block (struct sha1_state *sctx,const u8 *data, +int blocks) +{ +do { +powerpc_sha_transform (sctx->state, data); + data +=64 ; + }while (--blocks); +}static int powerpc_sha1_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha1_state *sctx =shash_desc_ctx (desc); -unsigned int partial, done; -const u8 *src; - - partial = sctx->count &0x3f ; - sctx->count += len; - done =0 ; - src = data; - -if ((partial + len) >63 ) { - -if (partial) { - done = -partial; -memcpy (sctx->buffer + partial, data, done +64 ); - src = sctx->buffer; - } - -do { -powerpc_sha_transform (sctx->state, src); - done +=64 ; - src = data + done; - }while (done +63 < len); - - partial =0 ; - } -memcpy (sctx->buffer + partial, src, len - done); - -return 0 ; +return sha1_base_do_update_blocks (desc, data, len, powerpc_sha_block); } -/* Add padding and return the message digest. */ -static int powerpc_sha1_final (struct shash_desc *desc,u8 *out) +static int powerpc_sha1_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha1_state *sctx =shash_desc_ctx (desc); -__be32 *dst = (__be32 *)out; -u32 i, index, padlen; -__be64 bits; -static const u8 padding[64 ] = {0x80 , }; - - bits =cpu_to_be64 (sctx->count <<3 ); - -/* Pad out to 56 mod 64 */ - index = sctx->count &0x3f ; - padlen = (index <56 ) ? (56 - index) : ((64 +56 ) - index); -powerpc_sha1_update (desc, padding, padlen); - -/* Append length */ -powerpc_sha1_update (desc, (const u8 *)&bits,sizeof (bits)); - -/* Store state in digest */ -for (i =0 ; i <5 ; i++) - dst[i] =cpu_to_be32 (sctx->state[i]); - -/* Wipe context */ -memset (sctx,0 ,sizeof *sctx); - -return 0 ; -} - -static int powerpc_sha1_export (struct shash_desc *desc,void *out) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); -return 0 ; -} - -static int powerpc_sha1_import (struct shash_desc *desc,const void *in) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); -return 0 ; +sha1_base_do_finup (desc, src, len, powerpc_sha_block); +return sha1_base_finish (desc, out); }static struct shash_alg alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = powerpc_sha1_update, - .final = powerpc_sha1_final, - .export = powerpc_sha1_export, - .import = powerpc_sha1_import, - .descsize =sizeof (struct sha1_state ), - .statesize =sizeof (struct sha1_state ), + .finup = powerpc_sha1_finup, + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name="sha1-powerpc" , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 23/67 crypto: powerpc/sha1-spe - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/powerpc/crypto/sha1-spe-glue.c | 132 +++++----------------------- 1 file changed, 24 insertions(+), 108 deletions(-)
arch/powerpc/crypto/sha1-spe-glue.c +24 -108
@@ -7,16 +7,13 @@* Copyright (c) 2015 Markus Stockhausen <[email protected]> */ +#include <asm/switch_to.h> #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> -#include <asm/byteorder.h> -#include <asm/switch_to.h> -#include <linux/hardirq.h> +#include <linux/kernel.h> +#include <linux/preempt.h> +#include <linux/module.h> /* * MAX_BYTES defines the number of bytes that are allowed to be processed
@@ -30,7 +27,7 @@*/ #define MAX_BYTES 2048 -extern void ppc_spe_sha1_transform (u32 *state,const u8 *src,u32 blocks); +asmlinkage void ppc_spe_sha1_transform(u32 *state, const u8 *src, u32 blocks);static void spe_begin (void ) {
@@ -46,126 +43,45 @@ static void spe_end(void)preempt_enable (); } -static inline void ppc_sha1_clear_context (struct sha1_state *sctx) +static void ppc_spe_sha1_block (struct sha1_state *sctx,const u8 *src, +int blocks) { -int count =sizeof (struct sha1_state ) >>2 ; -u32 *ptr = (u32 *)sctx; +do { +int unit =min (blocks,MAX_BYTES /SHA1_BLOCK_SIZE ); -/* make sure we can clear the fast way */ -BUILD_BUG_ON (sizeof (struct sha1_state ) %4 ); -do { *ptr++ =0 ; }while (--count); +spe_begin (); +ppc_spe_sha1_transform (sctx->state, src, unit); +spe_end (); + + src += unit *SHA1_BLOCK_SIZE ; + blocks -= unit; + }while (blocks); }static int ppc_spe_sha1_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha1_state *sctx =shash_desc_ctx (desc); -const unsigned int offset = sctx->count &0x3f ; -const unsigned int avail =64 - offset; -unsigned int bytes; -const u8 *src = data; - -if (avail > len) { - sctx->count += len; -memcpy ((char *)sctx->buffer + offset, src, len); -return 0 ; - } - - sctx->count += len; - -if (offset) { -memcpy ((char *)sctx->buffer + offset, src, avail); - -spe_begin (); -ppc_spe_sha1_transform (sctx->state, (const u8 *)sctx->buffer,1 ); -spe_end (); - - len -= avail; - src += avail; - } - -while (len >63 ) { - bytes = (len >MAX_BYTES ) ?MAX_BYTES : len; - bytes = bytes & ~0x3f ; - -spe_begin (); -ppc_spe_sha1_transform (sctx->state, src, bytes >>6 ); -spe_end (); - - src += bytes; - len -= bytes; - } - -memcpy ((char *)sctx->buffer, src, len); -return 0 ; +return sha1_base_do_update_blocks (desc, data, len, ppc_spe_sha1_block); } -static int ppc_spe_sha1_final (struct shash_desc *desc,u8 *out) +static int ppc_spe_sha1_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha1_state *sctx =shash_desc_ctx (desc); -const unsigned int offset = sctx->count &0x3f ; -char *p = (char *)sctx->buffer + offset; -int padlen; -__be64 *pbits = (__be64 *)(((char *)&sctx->buffer) +56 ); -__be32 *dst = (__be32 *)out; - - padlen =55 - offset; - *p++ =0x80 ; - -spe_begin (); - -if (padlen <0 ) { -memset (p,0x00 , padlen +sizeof (u64)); -ppc_spe_sha1_transform (sctx->state, sctx->buffer,1 ); - p = (char *)sctx->buffer; - padlen =56 ; - } - -memset (p,0 , padlen); - *pbits =cpu_to_be64 (sctx->count <<3 ); -ppc_spe_sha1_transform (sctx->state, sctx->buffer,1 ); - -spe_end (); - - dst[0 ] =cpu_to_be32 (sctx->state[0 ]); - dst[1 ] =cpu_to_be32 (sctx->state[1 ]); - dst[2 ] =cpu_to_be32 (sctx->state[2 ]); - dst[3 ] =cpu_to_be32 (sctx->state[3 ]); - dst[4 ] =cpu_to_be32 (sctx->state[4 ]); - -ppc_sha1_clear_context (sctx); -return 0 ; -} - -static int ppc_spe_sha1_export (struct shash_desc *desc,void *out) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); -return 0 ; -} - -static int ppc_spe_sha1_import (struct shash_desc *desc,const void *in) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); -return 0 ; +sha1_base_do_finup (desc, src, len, ppc_spe_sha1_block); +return sha1_base_finish (desc, out); }static struct shash_alg alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = ppc_spe_sha1_update, - .final = ppc_spe_sha1_final, - .export = ppc_spe_sha1_export, - .import = ppc_spe_sha1_import, - .descsize =sizeof (struct sha1_state ), - .statesize =sizeof (struct sha1_state ), + .finup = ppc_spe_sha1_finup, + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name="sha1-ppc-spe" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 24/67 crypto: s390/sha1 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/s390/crypto/sha.h | 13 +++--- arch/s390/crypto/sha1_s390.c | 22 +++++------ arch/s390/crypto/sha_common.c | 74 +++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 16 deletions(-)
arch/s390/crypto/sha.h +8 -5
@@ -10,27 +10,30 @@#ifndef _CRYPTO_ARCH_S390_SHA_H#define _CRYPTO_ARCH_S390_SHA_H -#include <linux/crypto.h> -#include <crypto/sha1.h> -#include <crypto/sha2.h> #include <crypto/sha3.h> +#include <linux/types.h> /* must be big enough for the largest SHA variant */ #define SHA3_STATE_SIZE 200#define CPACF_MAX_PARMBLOCK_SIZE SHA3_STATE_SIZE#define SHA_MAX_BLOCK_SIZE SHA3_224_BLOCK_SIZE +#define S390_SHA_CTX_SIZE offsetof(struct s390_sha_ctx, buf)struct s390_sha_ctx {u64 count;/* message length in bytes */ u32 state[CPACF_MAX_PARMBLOCK_SIZE /sizeof (u32)]; -u8 buf[SHA_MAX_BLOCK_SIZE ];int func;/* KIMD function to use */ -int first_message_part; +bool first_message_part; +u8 buf[SHA_MAX_BLOCK_SIZE ]; };struct shash_desc ;int s390_sha_update (struct shash_desc *desc,const u8 *data,unsigned int len); +int s390_sha_update_blocks (struct shash_desc *desc,const u8 *data, +unsigned int len);int s390_sha_final (struct shash_desc *desc,u8 *out); +int s390_sha_finup (struct shash_desc *desc,const u8 *src,unsigned int len, +u8 *out);#endif
arch/s390/crypto/sha1_s390.c +11 -11
@@ -18,12 +18,12 @@* Copyright (c) Andrew McDonald <[email protected]> * Copyright (c) Jean-Francois Dive <[email protected]> */ -#include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/cpufeature.h> -#include <crypto/sha1.h> #include <asm/cpacf.h> +#include <crypto/internal/hash.h> +#include <crypto/sha1.h> +#include <linux/cpufeature.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "sha.h"
@@ -49,7 +49,6 @@ static int s390_sha1_export(struct shash_desc *desc, void *out) octx->count = sctx->count;memcpy (octx->state, sctx->state,sizeof (octx->state)); -memcpy (octx->buffer, sctx->buf,sizeof (octx->buffer));return 0 ; }
@@ -60,7 +59,6 @@ static int s390_sha1_import(struct shash_desc *desc, const void *in) sctx->count = ictx->count;memcpy (sctx->state, ictx->state,sizeof (ictx->state)); -memcpy (sctx->buf, ictx->buffer,sizeof (ictx->buffer)); sctx->func =CPACF_KIMD_SHA_1 ;return 0 ; }
@@ -68,16 +66,18 @@ static int s390_sha1_import(struct shash_desc *desc, const void *in)static struct shash_alg alg = { .digestsize =SHA1_DIGEST_SIZE , .init = s390_sha1_init, - .update = s390_sha_update, - .final = s390_sha_final, + .update = s390_sha_update_blocks, + .finup = s390_sha_finup, .export = s390_sha1_export, .import = s390_sha1_import, - .descsize =sizeof (struct s390_sha_ctx ), - .statesize =sizeof (struct sha1_state ), + .descsize =S390_SHA_CTX_SIZE , + .statesize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name="sha1-s390" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
arch/s390/crypto/sha_common.c +74 -0
85 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 25/67 crypto: sparc/sha1 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/sparc/crypto/sha1_glue.c | 109 +++++----------------------------- 1 file changed, 15 insertions(+), 94 deletions(-)
arch/sparc/crypto/sha1_glue.c +15 -94
@@ -11,124 +11,45 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <asm/elf.h> +#include <asm/pstate.h> #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha1.h> #include <crypto/sha1_base.h> - -#include <asm/pstate.h> -#include <asm/elf.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "opcodes.h" -asmlinkage void sha1_sparc64_transform(u32 *digest, const char *data, -unsigned int rounds); - -static void __sha1_sparc64_update (struct sha1_state *sctx,const u8 *data, -unsigned int len,unsigned int partial) -{ -unsigned int done =0 ; - - sctx->count += len; -if (partial) { - done =SHA1_BLOCK_SIZE - partial; -memcpy (sctx->buffer + partial, data, done); -sha1_sparc64_transform (sctx->state, sctx->buffer,1 ); - } -if (len - done >=SHA1_BLOCK_SIZE ) { -const unsigned int rounds = (len - done) /SHA1_BLOCK_SIZE ; - -sha1_sparc64_transform (sctx->state, data + done, rounds); - done += rounds *SHA1_BLOCK_SIZE ; - } - -memcpy (sctx->buffer, data + done, len - done); -} +asmlinkage void sha1_sparc64_transform(struct sha1_state *digest, +const u8 *data,int rounds);static int sha1_sparc64_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha1_state *sctx =shash_desc_ctx (desc); -unsigned int partial = sctx->count %SHA1_BLOCK_SIZE ; - -/* Handle the fast case right here */ -if (partial + len <SHA1_BLOCK_SIZE ) { - sctx->count += len; -memcpy (sctx->buffer + partial, data, len); - }else -__sha1_sparc64_update (sctx, data, len, partial); - -return 0 ; +return sha1_base_do_update_blocks (desc, data, len, + sha1_sparc64_transform); }/* Add padding and return the message digest. */ -static int sha1_sparc64_final (struct shash_desc *desc,u8 *out) +static int sha1_sparc64_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha1_state *sctx =shash_desc_ctx (desc); -unsigned int i, index, padlen; -__be32 *dst = (__be32 *)out; -__be64 bits; -static const u8 padding[SHA1_BLOCK_SIZE ] = {0x80 , }; - - bits =cpu_to_be64 (sctx->count <<3 ); - -/* Pad out to 56 mod 64 and append length */ - index = sctx->count %SHA1_BLOCK_SIZE ; - padlen = (index <56 ) ? (56 - index) : ((SHA1_BLOCK_SIZE +56 ) - index); - -/* We need to fill a whole block for __sha1_sparc64_update() */ -if (padlen <=56 ) { - sctx->count += padlen; -memcpy (sctx->buffer + index, padding, padlen); - }else { -__sha1_sparc64_update (sctx, padding, padlen, index); - } -__sha1_sparc64_update (sctx, (const u8 *)&bits,sizeof (bits),56 ); - -/* Store state in digest */ -for (i =0 ; i <5 ; i++) - dst[i] =cpu_to_be32 (sctx->state[i]); - -/* Wipe context */ -memset (sctx,0 ,sizeof (*sctx)); - -return 0 ; -} - -static int sha1_sparc64_export (struct shash_desc *desc,void *out) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); - -return 0 ; -} - -static int sha1_sparc64_import (struct shash_desc *desc,const void *in) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); - -return 0 ; +sha1_base_do_finup (desc, src, len, sha1_sparc64_transform); +return sha1_base_finish (desc, out); }static struct shash_alg alg = { .digestsize =SHA1_DIGEST_SIZE , .init = sha1_base_init, .update = sha1_sparc64_update, - .final = sha1_sparc64_final, - .export = sha1_sparc64_export, - .import = sha1_sparc64_import, - .descsize =sizeof (struct sha1_state ), - .statesize =sizeof (struct sha1_state ), + .finup = sha1_sparc64_finup, + .descsize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name="sha1-sparc64" , .cra_priority =SPARC_CR_OPCODE_PRIORITY , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 26/67 crypto: sha1_base - Remove partial block helpers
In reply to Herbert XuNow that all sha1_base users have been converted to use the API partial block handling, remove the partial block helpers.
--- include/crypto/sha1_base.h | 61 -------------------------------------- 1 file changed, 61 deletions(-)
include/crypto/sha1_base.h +0 -61
@@ -31,44 +31,6 @@ static inline int sha1_base_init(struct shash_desc *desc)return 0 ; } -static inline int sha1_base_do_update (struct shash_desc *desc, -const u8 *data, -unsigned int len, -sha1_block_fn *block_fn) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); -unsigned int partial = sctx->count %SHA1_BLOCK_SIZE ; - - sctx->count += len; - -if (unlikely ((partial + len) >=SHA1_BLOCK_SIZE )) { -int blocks; - -if (partial) { -int p =SHA1_BLOCK_SIZE - partial; - -memcpy (sctx->buffer + partial, data, p); - data += p; - len -= p; - -block_fn (sctx, sctx->buffer,1 ); - } - - blocks = len /SHA1_BLOCK_SIZE ; - len %=SHA1_BLOCK_SIZE ; - -if (blocks) { -block_fn (sctx, data, blocks); - data += blocks *SHA1_BLOCK_SIZE ; - } - partial =0 ; - } -if (len) -memcpy (sctx->buffer + partial, data, len); - -return 0 ; -} - static inline int sha1_base_do_update_blocks(struct shash_desc *desc, const u8 *data, unsigned int len,
@@ -82,29 +44,6 @@ static inline int sha1_base_do_update_blocks(struct shash_desc *desc,return remain; } -static inline int sha1_base_do_finalize (struct shash_desc *desc, -sha1_block_fn *block_fn) -{ -const int bit_offset =SHA1_BLOCK_SIZE -sizeof (__be64); -struct sha1_state *sctx =shash_desc_ctx (desc); -__be64 *bits = (__be64 *)(sctx->buffer + bit_offset); -unsigned int partial = sctx->count %SHA1_BLOCK_SIZE ; - - sctx->buffer[partial++] =0x80 ; -if (partial > bit_offset) { -memset (sctx->buffer + partial,0x0 ,SHA1_BLOCK_SIZE - partial); - partial =0 ; - -block_fn (sctx, sctx->buffer,1 ); - } - -memset (sctx->buffer + partial,0x0 , bit_offset - partial); - *bits =cpu_to_be64 (sctx->count <<3 ); -block_fn (sctx, sctx->buffer,1 ); - -return 0 ; -} -static inline int sha1_base_do_finup (struct shash_desc *desc,const u8 *src,unsigned int len,sha1_block_fn *block_fn)
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 27/67 crypto: x86/sha256 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/x86/crypto/sha256_ssse3_glue.c | 106 +++++++++++----------------- include/crypto/sha2.h | 5 ++ include/crypto/sha256_base.h | 80 +++++++++++++++++++-- 3 files changed, 118 insertions(+), 73 deletions(-)
arch/x86/crypto/sha256_ssse3_glue.c +40 -66
@@ -29,19 +29,14 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <asm/cpu_device_id.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> -#include <linux/string.h> -#include <asm/cpu_device_id.h> -#include <asm/simd.h> +#include <linux/kernel.h> +#include <linux/module.h> -asmlinkage void sha256_transform_ssse3(struct sha256_state *state, +asmlinkage void sha256_transform_ssse3(struct crypto_sha256_state *state,const u8 *data,int blocks);static const struct x86_cpu_id module_cpu_ids[] = {
@@ -54,37 +49,29 @@ static const struct x86_cpu_id module_cpu_ids[] = {MODULE_DEVICE_TABLE (x86cpu, module_cpu_ids);static int _sha256_update (struct shash_desc *desc,const u8 *data, -unsigned int len,sha256_block_fn *sha256_xform) +unsigned int len, +crypto_sha256_block_fn *sha256_xform) { -struct sha256_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count %SHA256_BLOCK_SIZE ) + len <SHA256_BLOCK_SIZE ) -return crypto_sha256_update (desc, data, len); +int remain;/* -* Make sure struct sha256_state begins directly with the SHA256 +* Make sure struct crypto_sha256_state begins directly with the SHA256 * 256-bit internal state, as this is what the asm functions expect. */ -BUILD_BUG_ON (offsetof(struct sha256_state , state) !=0 ); +BUILD_BUG_ON (offsetof(struct crypto_sha256_state , state) !=0 );kernel_fpu_begin (); -sha256_base_do_update (desc, data, len, sha256_xform); + remain =sha256_base_do_update_blocks (desc, data, len, sha256_xform);kernel_fpu_end (); -return 0 ; +return remain; }static int sha256_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out,sha256_block_fn *sha256_xform) +unsigned int len,u8 *out,crypto_sha256_block_fn *sha256_xform) { -if (!crypto_simd_usable ()) -return crypto_sha256_finup (desc, data, len, out); -kernel_fpu_begin (); -if (len) -sha256_base_do_update (desc, data, len, sha256_xform); -sha256_base_do_finalize (desc, sha256_xform); +sha256_base_do_finup (desc, data, len, sha256_xform);kernel_fpu_end ();return sha256_base_finish (desc, out);
@@ -102,12 +89,6 @@ static int sha256_ssse3_finup(struct shash_desc *desc, const u8 *data,return sha256_finup (desc, data, len, out, sha256_transform_ssse3); } -/* Add padding and return the message digest. */ -static int sha256_ssse3_final (struct shash_desc *desc,u8 *out) -{ -return sha256_ssse3_finup (desc,NULL ,0 , out); -} -static int sha256_ssse3_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {
@@ -119,14 +100,15 @@ static struct shash_alg sha256_ssse3_algs[] = { { .digestsize = SHA256_DIGEST_SIZE, .init = sha256_base_init, .update = sha256_ssse3_update, - .final = sha256_ssse3_final, .finup = sha256_ssse3_finup, .digest = sha256_ssse3_digest, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha256", .cra_driver_name = "sha256-ssse3", .cra_priority = 150, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA256_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -134,13 +116,14 @@ static struct shash_alg sha256_ssse3_algs[] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = sha256_ssse3_update, - .final = sha256_ssse3_final, .finup = sha256_ssse3_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name = "sha224-ssse3", .cra_priority = 150, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -161,7 +144,7 @@ static void unregister_sha256_ssse3(void) ARRAY_SIZE(sha256_ssse3_algs)); } -asmlinkage void sha256_transform_avx(struct sha256_state *state, +asmlinkage void sha256_transform_avx(struct crypto_sha256_state *state,const u8 *data,int blocks); static int sha256_avx_update(struct shash_desc *desc, const u8 *data,
@@ -176,11 +159,6 @@ static int sha256_avx_finup(struct shash_desc *desc, const u8 *data,return sha256_finup (desc, data, len, out, sha256_transform_avx); } -static int sha256_avx_final (struct shash_desc *desc,u8 *out) -{ -return sha256_avx_finup (desc,NULL ,0 , out); -} -static int sha256_avx_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {
@@ -192,14 +170,15 @@ static struct shash_alg sha256_avx_algs[] = { { .digestsize = SHA256_DIGEST_SIZE, .init = sha256_base_init, .update = sha256_avx_update, - .final = sha256_avx_final, .finup = sha256_avx_finup, .digest = sha256_avx_digest, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha256", .cra_driver_name = "sha256-avx", .cra_priority = 160, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA256_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -207,13 +186,14 @@ static struct shash_alg sha256_avx_algs[] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = sha256_avx_update, - .final = sha256_avx_final, .finup = sha256_avx_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name = "sha224-avx", .cra_priority = 160, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -245,7 +225,7 @@ static void unregister_sha256_avx(void) ARRAY_SIZE(sha256_avx_algs)); } -asmlinkage void sha256_transform_rorx(struct sha256_state *state, +asmlinkage void sha256_transform_rorx(struct crypto_sha256_state *state,const u8 *data,int blocks); static int sha256_avx2_update(struct shash_desc *desc, const u8 *data,
@@ -260,11 +240,6 @@ static int sha256_avx2_finup(struct shash_desc *desc, const u8 *data,return sha256_finup (desc, data, len, out, sha256_transform_rorx); } -static int sha256_avx2_final (struct shash_desc *desc,u8 *out) -{ -return sha256_avx2_finup (desc,NULL ,0 , out); -} -static int sha256_avx2_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {
@@ -276,14 +251,15 @@ static struct shash_alg sha256_avx2_algs[] = { { .digestsize = SHA256_DIGEST_SIZE, .init = sha256_base_init, .update = sha256_avx2_update, - .final = sha256_avx2_final, .finup = sha256_avx2_finup, .digest = sha256_avx2_digest, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha256", .cra_driver_name = "sha256-avx2", .cra_priority = 170, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA256_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -291,13 +267,14 @@ static struct shash_alg sha256_avx2_algs[] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = sha256_avx2_update, - .final = sha256_avx2_final, .finup = sha256_avx2_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name = "sha224-avx2", .cra_priority = 170, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -327,7 +304,7 @@ static void unregister_sha256_avx2(void) ARRAY_SIZE(sha256_avx2_algs)); } -asmlinkage void sha256_ni_transform(struct sha256_state *digest, +asmlinkage void sha256_ni_transform(struct crypto_sha256_state *digest,const u8 *data,int rounds); static int sha256_ni_update(struct shash_desc *desc, const u8 *data,
@@ -342,11 +319,6 @@ static int sha256_ni_finup(struct shash_desc *desc, const u8 *data,return sha256_finup (desc, data, len, out, sha256_ni_transform); } -static int sha256_ni_final (struct shash_desc *desc,u8 *out) -{ -return sha256_ni_finup (desc,NULL ,0 , out); -} -static int sha256_ni_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {
@@ -358,14 +330,15 @@ static struct shash_alg sha256_ni_algs[] = { { .digestsize = SHA256_DIGEST_SIZE, .init = sha256_base_init, .update = sha256_ni_update, - .final = sha256_ni_final, .finup = sha256_ni_finup, .digest = sha256_ni_digest, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha256", .cra_driver_name = "sha256-ni", .cra_priority = 250, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA256_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -373,13 +346,14 @@ static struct shash_alg sha256_ni_algs[] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = sha256_ni_update, - .final = sha256_ni_final, .finup = sha256_ni_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name = "sha224-ni", .cra_priority = 250, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
include/crypto/sha2.h +5 -0
@@ -64,6 +64,11 @@ extern const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE];extern const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE ]; +struct crypto_sha256_state { +u32 state[SHA256_DIGEST_SIZE /4 ]; +u64 count; +}; +struct sha256_state {u32 state[SHA256_DIGEST_SIZE /4 ];u64 count;
include/crypto/sha256_base.h +73 -7
118 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 28/67 crypto: mips/octeon-sha256 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- .../mips/cavium-octeon/crypto/octeon-sha256.c | 161 ++++-------------- 1 file changed, 37 insertions(+), 124 deletions(-)
arch/mips/cavium-octeon/crypto/octeon-sha256.c +37 -124
@@ -14,15 +14,12 @@* SHA224 Support Copyright 2007 Intel Corporation <[email protected]> */ -#include <linux/mm.h> -#include <crypto/sha2.h> -#include <crypto/sha256_base.h> -#include <linux/init.h> -#include <linux/types.h> -#include <linux/module.h> -#include <asm/byteorder.h> #include <asm/octeon/octeon.h> #include <crypto/internal/hash.h> +#include <crypto/sha2.h> +#include <crypto/sha256_base.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "octeon-crypto.h"
@@ -30,7 +27,7 @@* We pass everything as 64-bit. OCTEON can handle misaligned data. */ -static void octeon_sha256_store_hash (struct sha256_state *sctx) +static void octeon_sha256_store_hash (struct crypto_sha256_state *sctx) {u64 *hash = (u64 *)sctx->state;
@@ -40,7 +37,7 @@ static void octeon_sha256_store_hash(struct sha256_state *sctx)write_octeon_64bit_hash_dword (hash[3 ],3 ); } -static void octeon_sha256_read_hash (struct sha256_state *sctx) +static void octeon_sha256_read_hash (struct crypto_sha256_state *sctx) {u64 *hash = (u64 *)sctx->state;
@@ -50,158 +47,72 @@ static void octeon_sha256_read_hash(struct sha256_state *sctx) hash[3 ] =read_octeon_64bit_hash_dword (3 ); } -static void octeon_sha256_transform (const void *_block) +static void octeon_sha256_transform (struct crypto_sha256_state *sctx, +const u8 *src,int blocks) { -const u64 *block = _block; +do { +const u64 *block = (const u64 *)src; -write_octeon_64bit_block_dword (block[0 ],0 ); -write_octeon_64bit_block_dword (block[1 ],1 ); -write_octeon_64bit_block_dword (block[2 ],2 ); -write_octeon_64bit_block_dword (block[3 ],3 ); -write_octeon_64bit_block_dword (block[4 ],4 ); -write_octeon_64bit_block_dword (block[5 ],5 ); -write_octeon_64bit_block_dword (block[6 ],6 ); -octeon_sha256_start (block[7 ]); -} +write_octeon_64bit_block_dword (block[0 ],0 ); +write_octeon_64bit_block_dword (block[1 ],1 ); +write_octeon_64bit_block_dword (block[2 ],2 ); +write_octeon_64bit_block_dword (block[3 ],3 ); +write_octeon_64bit_block_dword (block[4 ],4 ); +write_octeon_64bit_block_dword (block[5 ],5 ); +write_octeon_64bit_block_dword (block[6 ],6 ); +octeon_sha256_start (block[7 ]); -static void __octeon_sha256_update (struct sha256_state *sctx,const u8 *data, -unsigned int len) -{ -unsigned int partial; -unsigned int done; -const u8 *src; - - partial = sctx->count %SHA256_BLOCK_SIZE ; - sctx->count += len; - done =0 ; - src = data; - -if ((partial + len) >=SHA256_BLOCK_SIZE ) { -if (partial) { - done = -partial; -memcpy (sctx->buf + partial, data, - done +SHA256_BLOCK_SIZE ); - src = sctx->buf; - } - -do { -octeon_sha256_transform (src); - done +=SHA256_BLOCK_SIZE ; - src = data + done; - }while (done +SHA256_BLOCK_SIZE <= len); - - partial =0 ; - } -memcpy (sctx->buf + partial, src, len - done); + src +=SHA256_BLOCK_SIZE ; + }while (--blocks); }static int octeon_sha256_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha256_state *sctx =shash_desc_ctx (desc); +struct crypto_sha256_state *sctx =shash_desc_ctx (desc);struct octeon_cop2_state state;unsigned long flags; - -/* -* Small updates never reach the crypto engine, so the generic sha256 is -* faster because of the heavyweight octeon_crypto_enable() / -* octeon_crypto_disable(). -*/ -if ((sctx->count %SHA256_BLOCK_SIZE ) + len <SHA256_BLOCK_SIZE ) -return crypto_sha256_update (desc, data, len); +int remain; flags =octeon_crypto_enable (&state);octeon_sha256_store_hash (sctx); -__octeon_sha256_update (sctx, data, len); + remain =sha256_base_do_update_blocks (desc, data, len, + octeon_sha256_transform);octeon_sha256_read_hash (sctx);octeon_crypto_disable (&state, flags); - -return 0 ; +return remain; } -static int octeon_sha256_final (struct shash_desc *desc,u8 *out) +static int octeon_sha256_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha256_state *sctx =shash_desc_ctx (desc); -static const u8 padding[64 ] = {0x80 , }; +struct crypto_sha256_state *sctx =shash_desc_ctx (desc);struct octeon_cop2_state state; -__be32 *dst = (__be32 *)out; -unsigned int pad_len;unsigned long flags; -unsigned int index; -__be64 bits; -int i; - -/* Save number of bits. */ - bits =cpu_to_be64 (sctx->count <<3 ); - -/* Pad out to 56 mod 64. */ - index = sctx->count &0x3f ; - pad_len = (index <56 ) ? (56 - index) : ((64 +56 ) - index); flags =octeon_crypto_enable (&state);octeon_sha256_store_hash (sctx); -__octeon_sha256_update (sctx, padding, pad_len); - -/* Append length (before padding). */ -__octeon_sha256_update (sctx, (const u8 *)&bits,sizeof (bits)); +sha256_base_do_finup (desc, src, len, octeon_sha256_transform);octeon_sha256_read_hash (sctx);octeon_crypto_disable (&state, flags); - -/* Store state in digest */ -for (i =0 ; i <8 ; i++) - dst[i] =cpu_to_be32 (sctx->state[i]); - -/* Zeroize sensitive information. */ -memset (sctx,0 ,sizeof (*sctx)); - -return 0 ; -} - -static int octeon_sha224_final (struct shash_desc *desc,u8 *hash) -{ -u8 D [SHA256_DIGEST_SIZE ]; - -octeon_sha256_final (desc,D ); - -memcpy (hash,D ,SHA224_DIGEST_SIZE ); -memzero_explicit (D ,SHA256_DIGEST_SIZE ); - -return 0 ; -} - -static int octeon_sha256_export (struct shash_desc *desc,void *out) -{ -struct sha256_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); -return 0 ; -} - -static int octeon_sha256_import (struct shash_desc *desc,const void *in) -{ -struct sha256_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); -return 0 ; +return sha256_base_finish (desc, out); }static struct shash_alg octeon_sha256_algs[2 ] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = octeon_sha256_update, - .final = octeon_sha256_final, - .export = octeon_sha256_export, - .import = octeon_sha256_import, - .descsize =sizeof (struct sha256_state ), - .statesize =sizeof (struct sha256_state ), + .finup = octeon_sha256_finup, + .descsize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name="octeon-sha256" , .cra_priority =OCTEON_CR_OPCODE_PRIORITY , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -209,11 +120,13 @@ static struct shash_alg octeon_sha256_algs[2] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = octeon_sha256_update, - .final = octeon_sha224_final, - .descsize = sizeof(struct sha256_state), + .finup = octeon_sha256_finup, + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name= "octeon-sha224", + .cra_priority = OCTEON_CR_OPCODE_PRIORITY, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 29/67 crypto: riscv/sha256 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/riscv/crypto/sha256-riscv64-glue.c | 68 ++++++++++--------------- include/crypto/sha256_base.h | 3 ++ lib/crypto/sha256.c | 18 +++++-- 3 files changed, 44 insertions(+), 45 deletions(-)
arch/riscv/crypto/sha256-riscv64-glue.c +28 -40
@@ -14,7 +14,7 @@#include <crypto/internal/hash.h> #include <crypto/internal/simd.h> #include <crypto/sha256_base.h> -#include <linux/linkage.h> +#include <linux/kernel.h> #include <linux/module.h> /*
@@ -22,50 +22,36 @@* It is assumed to be the first field. */ asmlinkage void sha256_transform_zvknha_or_zvknhb_zvkb( -struct sha256_state *state,const u8 *data,int num_blocks); +struct crypto_sha256_state *state,const u8 *data,int num_blocks); + +static void sha256_block (struct crypto_sha256_state *state,const u8 *data, +int num_blocks) +{ +/* +* Ensure struct crypto_sha256_state begins directly with the SHA-256 +* 256-bit internal state, as this is what the asm function expects. +*/ +BUILD_BUG_ON (offsetof(struct crypto_sha256_state , state) !=0 ); + +if (crypto_simd_usable ()) { +kernel_vector_begin (); +sha256_transform_zvknha_or_zvknhb_zvkb (state, data, num_blocks); +kernel_vector_end (); + }else +sha256_transform_blocks (state, data, num_blocks); +}static int riscv64_sha256_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -/* -* Ensure struct sha256_state begins directly with the SHA-256 -* 256-bit internal state, as this is what the asm function expects. -*/ -BUILD_BUG_ON (offsetof(struct sha256_state , state) !=0 ); - -if (crypto_simd_usable ()) { -kernel_vector_begin (); -sha256_base_do_update (desc, data, len, - sha256_transform_zvknha_or_zvknhb_zvkb); -kernel_vector_end (); - }else { -crypto_sha256_update (desc, data, len); - } -return 0 ; +return sha256_base_do_update_blocks (desc, data, len, sha256_block); }static int riscv64_sha256_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (crypto_simd_usable ()) { -kernel_vector_begin (); -if (len) -sha256_base_do_update ( - desc, data, len, - sha256_transform_zvknha_or_zvknhb_zvkb); -sha256_base_do_finalize ( - desc, sha256_transform_zvknha_or_zvknhb_zvkb); -kernel_vector_end (); - -return sha256_base_finish (desc, out); - } - -return crypto_sha256_finup (desc, data, len, out); -} - -static int riscv64_sha256_final (struct shash_desc *desc,u8 *out) -{ -return riscv64_sha256_finup (desc,NULL ,0 , out); +sha256_base_do_finup (desc, data, len, sha256_block); +return sha256_base_finish (desc, out); } static int riscv64_sha256_digest(struct shash_desc *desc, const u8 *data,
@@ -79,13 +65,14 @@ static struct shash_alg riscv64_sha256_algs[] = { { .init = sha256_base_init, .update = riscv64_sha256_update, - .final = riscv64_sha256_final, .finup = riscv64_sha256_finup, .digest = riscv64_sha256_digest, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .digestsize = SHA256_DIGEST_SIZE, .base = { .cra_blocksize = SHA256_BLOCK_SIZE, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_priority = 300, .cra_name = "sha256", .cra_driver_name = "sha256-riscv64-zvknha_or_zvknhb-zvkb",
@@ -94,12 +81,13 @@ static struct shash_alg riscv64_sha256_algs[] = { }, { .init = sha224_base_init, .update = riscv64_sha256_update, - .final = riscv64_sha256_final, .finup = riscv64_sha256_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .digestsize = SHA224_DIGEST_SIZE, .base = { .cra_blocksize = SHA224_BLOCK_SIZE, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_priority = 300, .cra_name = "sha224", .cra_driver_name = "sha224-riscv64-zvknha_or_zvknhb-zvkb",
include/crypto/sha256_base.h +3 -0
@@ -198,4 +198,7 @@ static inline int sha256_base_finish(struct shash_desc *desc, u8 *out) return __sha256_base_finish(sctx->state, out, digest_size); } +void sha256_transform_blocks (struct crypto_sha256_state *sst, +const u8 *input,int blocks); +#endif /* _CRYPTO_SHA256_BASE_H */
lib/crypto/sha256.c +13 -5
@@ -118,28 +118,36 @@ static void sha256_transform(u32 *state, const u8 *input, u32 *W) state[4 ] += e; state[5 ] += f; state[6 ] += g; state[7 ] += h; } -static void sha256_transform_blocks (struct sha256_state *sctx, -const u8 *input,int blocks) +void sha256_transform_blocks (struct crypto_sha256_state *sst, +const u8 *input,int blocks) {u32 W [64 ];do { -sha256_transform (sctx->state, input,W ); +sha256_transform (sst->state, input,W ); input +=SHA256_BLOCK_SIZE ; }while (--blocks);memzero_explicit (W ,sizeof (W )); } +EXPORT_SYMBOL_GPL (sha256_transform_blocks); + +static void lib_sha256_transform_blocks (struct sha256_state *sctx, +const u8 *input,int blocks) +{ +sha256_transform_blocks ((struct crypto_sha256_state *)sctx, input, + blocks); +}void sha256_update (struct sha256_state *sctx,const u8 *data,unsigned int len) { -lib_sha256_base_do_update (sctx, data, len, sha256_transform_blocks); +lib_sha256_base_do_update (sctx, data, len, lib_sha256_transform_blocks); }EXPORT_SYMBOL (sha256_update);static void __sha256_final (struct sha256_state *sctx,u8 *out,int digest_size) { -lib_sha256_base_do_finalize (sctx, sha256_transform_blocks); +lib_sha256_base_do_finalize (sctx, lib_sha256_transform_blocks);lib_sha256_base_finish (sctx, out, digest_size); }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 30/67 crypto: sha256-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/sha256_generic.c | 50 +++++++++++++++++------------------------ include/crypto/sha2.h | 6 ----- 2 files changed, 21 insertions(+), 35 deletions(-)
crypto/sha256_generic.c +21 -29
@@ -8,14 +8,10 @@* SHA224 Support Copyright 2007 Intel Corporation <[email protected]> */ #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> -#include <asm/byteorder.h> -#include <linux/unaligned.h> +#include <linux/kernel.h> +#include <linux/module.h> const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE ] = {0xd1 ,0x4a ,0x02 ,0x8c ,0x2a ,0x3a ,0x2b ,0xc9 ,0x47 ,
@@ -33,42 +29,37 @@ const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = { };EXPORT_SYMBOL_GPL (sha256_zero_message_hash); -int crypto_sha256_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static void sha256_block (struct crypto_sha256_state *sctx,const u8 *input, +int blocks) { -sha256_update (shash_desc_ctx (desc), data, len); -return 0 ; -} -EXPORT_SYMBOL (crypto_sha256_update); - -static int crypto_sha256_final (struct shash_desc *desc,u8 *out) -{ -if (crypto_shash_digestsize (desc->tfm) ==SHA224_DIGEST_SIZE ) -sha224_final (shash_desc_ctx (desc), out); -else -sha256_final (shash_desc_ctx (desc), out); -return 0 ; +sha256_transform_blocks (sctx, input, blocks); } -int crypto_sha256_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *hash) +static int crypto_sha256_update (struct shash_desc *desc,const u8 *data, +unsigned int len) { -sha256_update (shash_desc_ctx (desc), data, len); -return crypto_sha256_final (desc, hash); +return sha256_base_do_update_blocks (desc, data, len, sha256_block); +} + +static int crypto_sha256_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *hash) +{ +sha256_base_do_finup (desc, data, len, sha256_block); +return sha256_base_finish (desc, hash); } -EXPORT_SYMBOL (crypto_sha256_finup);static struct shash_alg sha256_algs[2 ] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = crypto_sha256_update, - .final = crypto_sha256_final, .finup = crypto_sha256_finup, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name="sha256-generic" , .cra_priority =100 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -76,13 +67,14 @@ static struct shash_alg sha256_algs[2] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = crypto_sha256_update, - .final = crypto_sha256_final, .finup = crypto_sha256_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name= "sha224-generic", .cra_priority = 100, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
include/crypto/sha2.h +0 -6
@@ -83,12 +83,6 @@ struct sha512_state {struct shash_desc ; -extern int crypto_sha256_update (struct shash_desc *desc,const u8 *data, -unsigned int len); - -extern int crypto_sha256_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *hash); -extern int crypto_sha512_update (struct shash_desc *desc,const u8 *data,unsigned int len);
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 31/67 crypto: arm/sha256-ce - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm/crypto/sha2-ce-glue.c | 52 ++++++++++------------------------ 1 file changed, 15 insertions(+), 37 deletions(-)
arch/arm/crypto/sha2-ce-glue.c +15 -37
@@ -5,91 +5,69 @@* Copyright (C) 2015 Linaro Ltd <[email protected]> */ +#include <asm/neon.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> -#include <asm/hwcap.h> -#include <asm/simd.h> -#include <asm/neon.h> -#include <linux/unaligned.h> - -#include "sha256_glue.h" -MODULE_DESCRIPTION ("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_LICENSE ("GPL v2" ); -asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src, -int blocks); +asmlinkage void sha2_ce_transform(struct crypto_sha256_state *sst, +u8 const *src,int blocks);static int sha2_ce_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha256_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count %SHA256_BLOCK_SIZE ) + len <SHA256_BLOCK_SIZE ) -return crypto_sha256_arm_update (desc, data, len); +int remain;kernel_neon_begin (); -sha256_base_do_update (desc, data, len, - (sha256_block_fn *)sha2_ce_transform); + remain =sha256_base_do_update_blocks (desc, data, len, + sha2_ce_transform);kernel_neon_end (); - -return 0 ; +return remain; }static int sha2_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) -return crypto_sha256_arm_finup (desc, data, len, out); -kernel_neon_begin (); -if (len) -sha256_base_do_update (desc, data, len, - (sha256_block_fn *)sha2_ce_transform); -sha256_base_do_finalize (desc, (sha256_block_fn *)sha2_ce_transform); +sha256_base_do_finup (desc, data, len, sha2_ce_transform);kernel_neon_end (); -return sha256_base_finish (desc, out); } -static int sha2_ce_final (struct shash_desc *desc,u8 *out) -{ -return sha2_ce_finup (desc,NULL ,0 , out); -} -static struct shash_alg algs[] = { { .init = sha224_base_init, .update = sha2_ce_update, - .final = sha2_ce_final, .finup = sha2_ce_finup, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .digestsize =SHA224_DIGEST_SIZE , .base = { .cra_name ="sha224" , .cra_driver_name ="sha224-ce" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , } }, { .init = sha256_base_init, .update = sha2_ce_update, - .final = sha2_ce_final, .finup = sha2_ce_finup, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .digestsize =SHA256_DIGEST_SIZE , .base = { .cra_name ="sha256" , .cra_driver_name ="sha256-ce" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 32/67 crypto: arm/sha256-neon - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm/crypto/sha256_neon_glue.c | 49 ++++++++++-------------------- 1 file changed, 16 insertions(+), 33 deletions(-)
arch/arm/crypto/sha256_neon_glue.c +16 -33
@@ -9,69 +9,51 @@* Copyright © 2014 Jussi Kivilinna <[email protected]> */ +#include <asm/neon.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> -#include <linux/types.h> -#include <linux/string.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> -#include <asm/byteorder.h> -#include <asm/simd.h> -#include <asm/neon.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "sha256_glue.h" -asmlinkage void sha256_block_data_order_neon(struct sha256_state *digest, -const u8 *data,int num_blks); +asmlinkage void sha256_block_data_order_neon( +struct crypto_sha256_state *digest,const u8 *data,int num_blks);static int crypto_sha256_neon_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha256_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count %SHA256_BLOCK_SIZE ) + len <SHA256_BLOCK_SIZE ) -return crypto_sha256_arm_update (desc, data, len); +int remain;kernel_neon_begin (); -sha256_base_do_update (desc, data, len, sha256_block_data_order_neon); + remain =sha256_base_do_update_blocks (desc, data, len, + sha256_block_data_order_neon);kernel_neon_end (); - -return 0 ; +return remain; }static int crypto_sha256_neon_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) -return crypto_sha256_arm_finup (desc, data, len, out); -kernel_neon_begin (); -if (len) -sha256_base_do_update (desc, data, len, - sha256_block_data_order_neon); -sha256_base_do_finalize (desc, sha256_block_data_order_neon); +sha256_base_do_finup (desc, data, len, sha256_block_data_order_neon);kernel_neon_end (); -return sha256_base_finish (desc, out); } -static int crypto_sha256_neon_final (struct shash_desc *desc,u8 *out) -{ -return crypto_sha256_neon_finup (desc,NULL ,0 , out); -} -struct shash_alg sha256_neon_algs[] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = crypto_sha256_neon_update, - .final = crypto_sha256_neon_final, .finup = crypto_sha256_neon_finup, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name ="sha256-neon" , .cra_priority =250 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -79,13 +61,14 @@ struct shash_alg sha256_neon_algs[] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = crypto_sha256_neon_update, - .final = crypto_sha256_neon_final, .finup = crypto_sha256_neon_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name = "sha224-neon", .cra_priority = 250, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 33/67 crypto: arm/sha256-asm - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/arm/crypto/sha256_glue.c | 46 ++++++++++++++--------------------- arch/arm/crypto/sha256_glue.h | 8 +----- 2 files changed, 19 insertions(+), 35 deletions(-)
arch/arm/crypto/sha256_glue.c +18 -28
@@ -10,58 +10,47 @@* Author: Tim Chen <[email protected]> */ +#include <asm/neon.h> #include <crypto/internal/hash.h> -#include <linux/crypto.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> -#include <linux/string.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> -#include <asm/simd.h> -#include <asm/neon.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "sha256_glue.h" -asmlinkage void sha256_block_data_order(struct sha256_state *state, +asmlinkage void sha256_block_data_order(struct crypto_sha256_state *state,const u8 *data,int num_blks); -int crypto_sha256_arm_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static int crypto_sha256_arm_update (struct shash_desc *desc,const u8 *data, +unsigned int len) {/* make sure casting to sha256_block_fn() is safe */ -BUILD_BUG_ON (offsetof(struct sha256_state , state) !=0 ); +BUILD_BUG_ON (offsetof(struct crypto_sha256_state , state) !=0 ); -return sha256_base_do_update (desc, data, len, sha256_block_data_order); +return sha256_base_do_update_blocks (desc, data, len, + sha256_block_data_order); } -EXPORT_SYMBOL (crypto_sha256_arm_update); -static int crypto_sha256_arm_final (struct shash_desc *desc,u8 *out) +static int crypto_sha256_arm_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out) { -sha256_base_do_finalize (desc, sha256_block_data_order); +sha256_base_do_finup (desc, data, len, sha256_block_data_order);return sha256_base_finish (desc, out); } -int crypto_sha256_arm_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out) -{ -sha256_base_do_update (desc, data, len, sha256_block_data_order); -return crypto_sha256_arm_final (desc, out); -} -EXPORT_SYMBOL (crypto_sha256_arm_finup); -static struct shash_alg algs[] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = crypto_sha256_arm_update, - .final = crypto_sha256_arm_final, .finup = crypto_sha256_arm_finup, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name ="sha256-asm" , .cra_priority =150 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -69,13 +58,14 @@ static struct shash_alg algs[] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = crypto_sha256_arm_update, - .final = crypto_sha256_arm_final, .finup = crypto_sha256_arm_finup, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name = "sha224-asm", .cra_priority = 150, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
arch/arm/crypto/sha256_glue.h +1 -7
@@ -2,14 +2,8 @@#ifndef _CRYPTO_SHA256_GLUE_H#define _CRYPTO_SHA256_GLUE_H -#include <linux/crypto.h> +#include <crypto/hash.h> extern struct shash_alg sha256_neon_algs[2 ]; -int crypto_sha256_arm_update (struct shash_desc *desc,const u8 *data, -unsigned int len); - -int crypto_sha256_arm_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *hash); -#endif /* _CRYPTO_SHA256_GLUE_H */
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 34/67 crypto: arm64/sha256-ce - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/sha2-ce-glue.c | 90 +++++++------------------------- 1 file changed, 18 insertions(+), 72 deletions(-)
arch/arm64/crypto/sha2-ce-glue.c +18 -72
@@ -6,15 +6,13 @@*/ #include <asm/neon.h> -#include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> +#include <linux/string.h> MODULE_DESCRIPTION ("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );
@@ -23,7 +21,7 @@ MODULE_ALIAS_CRYPTO("sha224");MODULE_ALIAS_CRYPTO ("sha256" );struct sha256_ce_state { -struct sha256_state sst; +struct crypto_sha256_state sst;u32 finalize; };
@@ -33,7 +31,7 @@ extern const u32 sha256_ce_offsetof_finalize; asmlinkage int __sha256_ce_transform(struct sha256_ce_state *sst, u8 const *src,int blocks); -static void sha256_ce_transform (struct sha256_state *sst,u8 const *src, +static void sha256_ce_transform (struct crypto_sha256_state *sst,u8 const *src,int blocks) {while (blocks) {
@@ -54,42 +52,21 @@ const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state , finalize); -asmlinkage void sha256_block_data_order(u32 *digest, u8 const *src, int blocks); - -static void sha256_arm64_transform (struct sha256_state *sst,u8 const *src, -int blocks) -{ -sha256_block_data_order (sst->state, src, blocks); -} -static int sha256_ce_update (struct shash_desc *desc,const u8 *data,unsigned int len) {struct sha256_ce_state *sctx =shash_desc_ctx (desc); -if (!crypto_simd_usable ()) -return sha256_base_do_update (desc, data, len, - sha256_arm64_transform); - sctx->finalize =0 ; -sha256_base_do_update (desc, data, len, sha256_ce_transform); - -return 0 ; +return sha256_base_do_update_blocks (desc, data, len, + sha256_ce_transform); }static int sha256_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {struct sha256_ce_state *sctx =shash_desc_ctx (desc); -bool finalize = !sctx->sst.count && !(len %SHA256_BLOCK_SIZE ) && len; - -if (!crypto_simd_usable ()) { -if (len) -sha256_base_do_update (desc, data, len, - sha256_arm64_transform); -sha256_base_do_finalize (desc, sha256_arm64_transform); -return sha256_base_finish (desc, out); - } +bool finalize = !(len %SHA256_BLOCK_SIZE ) && len;/* * Allow the asm code to perform the finalization if there is no
@@ -97,23 +74,11 @@ static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,*/ sctx->finalize = finalize; -sha256_base_do_update (desc, data, len, sha256_ce_transform); -if (!finalize) -sha256_base_do_finalize (desc, sha256_ce_transform); -return sha256_base_finish (desc, out); -} - -static int sha256_ce_final (struct shash_desc *desc,u8 *out) -{ -struct sha256_ce_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable ()) { -sha256_base_do_finalize (desc, sha256_arm64_transform); -return sha256_base_finish (desc, out); - } - - sctx->finalize =0 ; -sha256_base_do_finalize (desc, sha256_ce_transform); +if (finalize) +sha256_base_do_update_blocks (desc, data, len, + sha256_ce_transform); +else +sha256_base_do_finup (desc, data, len, sha256_ce_transform);return sha256_base_finish (desc, out); }
@@ -124,55 +89,36 @@ static int sha256_ce_digest(struct shash_desc *desc, const u8 *data,return sha256_ce_finup (desc, data, len, out); } -static int sha256_ce_export (struct shash_desc *desc,void *out) -{ -struct sha256_ce_state *sctx =shash_desc_ctx (desc); - -memcpy (out, &sctx->sst,sizeof (struct sha256_state )); -return 0 ; -} - -static int sha256_ce_import (struct shash_desc *desc,const void *in) -{ -struct sha256_ce_state *sctx =shash_desc_ctx (desc); - -memcpy (&sctx->sst, in,sizeof (struct sha256_state )); - sctx->finalize =0 ; -return 0 ; -} -static struct shash_alg algs[] = { { .init = sha224_base_init, .update = sha256_ce_update, - .final = sha256_ce_final, .finup = sha256_ce_finup, - .export = sha256_ce_export, - .import = sha256_ce_import, .descsize =sizeof (struct sha256_ce_state ), - .statesize =sizeof (struct sha256_state ), + .statesize =sizeof (struct crypto_sha256_state ), .digestsize =SHA224_DIGEST_SIZE , .base = { .cra_name ="sha224" , .cra_driver_name ="sha224-ce" , .cra_priority =200 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , } }, { .init = sha256_base_init, .update = sha256_ce_update, - .final = sha256_ce_final, .finup = sha256_ce_finup, .digest = sha256_ce_digest, - .export = sha256_ce_export, - .import = sha256_ce_import, .descsize =sizeof (struct sha256_ce_state ), - .statesize =sizeof (struct sha256_state ), + .statesize =sizeof (struct crypto_sha256_state ), .digestsize =SHA256_DIGEST_SIZE , .base = { .cra_name ="sha256" , .cra_driver_name ="sha256-ce" , .cra_priority =200 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 35/67 crypto: arm64/sha256 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/sha256-glue.c | 97 +++++++++++++-------------------- 1 file changed, 37 insertions(+), 60 deletions(-)
arch/arm64/crypto/sha256-glue.c +37 -60
@@ -5,16 +5,13 @@* Copyright (c) 2016 Linaro Ltd. <[email protected]> */ -#include <asm/hwcap.h> #include <asm/neon.h> -#include <asm/simd.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> +#include <linux/cpufeature.h> +#include <linux/kernel.h> #include <linux/module.h> -#include <linux/string.h> -#include <linux/types.h> MODULE_DESCRIPTION ("SHA-224/SHA-256 secure hash for arm64" );MODULE_AUTHOR ("Andy Polyakov <[email protected]>" );
@@ -27,8 +24,8 @@ asmlinkage void sha256_block_data_order(u32 *digest, const void *data, unsigned int num_blks);EXPORT_SYMBOL (sha256_block_data_order); -static void sha256_arm64_transform (struct sha256_state *sst,u8 const *src, -int blocks) +static void sha256_arm64_transform (struct crypto_sha256_state *sst, +u8 const *src,int blocks) {sha256_block_data_order (sst->state, src, blocks); }
@@ -36,55 +33,52 @@ static void sha256_arm64_transform(struct sha256_state *sst, u8 const *src, asmlinkage void sha256_block_neon(u32 *digest, const void *data,unsigned int num_blks); -static void sha256_neon_transform (struct sha256_state *sst,u8 const *src, -int blocks) +static void sha256_neon_transform (struct crypto_sha256_state *sst, +u8 const *src,int blocks) { +kernel_neon_begin ();sha256_block_neon (sst->state, src, blocks); +kernel_neon_end (); }static int crypto_sha256_arm64_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -return sha256_base_do_update (desc, data, len, sha256_arm64_transform); +return sha256_base_do_update_blocks (desc, data, len, + sha256_arm64_transform); }static int crypto_sha256_arm64_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (len) -sha256_base_do_update (desc, data, len, sha256_arm64_transform); -sha256_base_do_finalize (desc, sha256_arm64_transform); - +sha256_base_do_finup (desc, data, len, sha256_arm64_transform);return sha256_base_finish (desc, out); } -static int crypto_sha256_arm64_final (struct shash_desc *desc,u8 *out) -{ -return crypto_sha256_arm64_finup (desc,NULL ,0 , out); -} -static struct shash_alg algs[] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = crypto_sha256_arm64_update, - .final = crypto_sha256_arm64_final, .finup = crypto_sha256_arm64_finup, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .base.cra_name ="sha256" , .base.cra_driver_name ="sha256-arm64" , .base.cra_priority =125 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA256_BLOCK_SIZE , .base.cra_module =THIS_MODULE , }, { .digestsize =SHA224_DIGEST_SIZE , .init = sha224_base_init, .update = crypto_sha256_arm64_update, - .final = crypto_sha256_arm64_final, .finup = crypto_sha256_arm64_finup, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .base.cra_name ="sha224" , .base.cra_driver_name ="sha224-arm64" , .base.cra_priority =125 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA224_BLOCK_SIZE , .base.cra_module =THIS_MODULE , } };
@@ -92,13 +86,7 @@ static struct shash_alg algs[] = { {static int sha256_update_neon (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha256_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable ()) -return sha256_base_do_update (desc, data, len, - sha256_arm64_transform); - -while (len >0 ) { + do { unsigned int chunk = len; /*
@@ -106,65 +94,54 @@ static int sha256_update_neon(struct shash_desc *desc, const u8 *data,* input when running on a preemptible kernel, but process the * data block by block instead. */ -if (IS_ENABLED (CONFIG_PREEMPTION ) && - chunk + sctx->count %SHA256_BLOCK_SIZE >SHA256_BLOCK_SIZE ) - chunk =SHA256_BLOCK_SIZE - - sctx->count %SHA256_BLOCK_SIZE ; +if (IS_ENABLED (CONFIG_PREEMPTION )) + chunk =SHA256_BLOCK_SIZE ; -kernel_neon_begin (); -sha256_base_do_update (desc, data, chunk, sha256_neon_transform); -kernel_neon_end (); + chunk -=sha256_base_do_update_blocks (desc, data, chunk, + sha256_neon_transform); data += chunk; len -= chunk; - } -return 0 ; + }while (len >=SHA256_BLOCK_SIZE ); +return len; }static int sha256_finup_neon (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) { -if (len) -sha256_base_do_update (desc, data, len, - sha256_arm64_transform); -sha256_base_do_finalize (desc, sha256_arm64_transform); - }else { -if (len) -sha256_update_neon (desc, data, len); -kernel_neon_begin (); -sha256_base_do_finalize (desc, sha256_neon_transform); -kernel_neon_end (); - } -return sha256_base_finish (desc, out); -} +if (len >=SHA256_BLOCK_SIZE ) { +int remain =sha256_update_neon (desc, data, len); -static int sha256_final_neon (struct shash_desc *desc,u8 *out) -{ -return sha256_finup_neon (desc,NULL ,0 , out); + data += len - remain; + len = remain; + } +sha256_base_do_finup (desc, data, len, sha256_neon_transform); +return sha256_base_finish (desc, out); }static struct shash_alg neon_algs[] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = sha256_update_neon, - .final = sha256_final_neon, .finup = sha256_finup_neon, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .base.cra_name ="sha256" , .base.cra_driver_name ="sha256-arm64-neon" , .base.cra_priority =150 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA256_BLOCK_SIZE , .base.cra_module =THIS_MODULE , }, { .digestsize =SHA224_DIGEST_SIZE , .init = sha224_base_init, .update = sha256_update_neon, - .final = sha256_final_neon, .finup = sha256_finup_neon, - .descsize =sizeof (struct sha256_state ), + .descsize =sizeof (struct crypto_sha256_state ), .base.cra_name ="sha224" , .base.cra_driver_name ="sha224-arm64-neon" , .base.cra_priority =150 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA224_BLOCK_SIZE , .base.cra_module =THIS_MODULE , } };
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 36/67 crypto: powerpc/sha256-spe - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/powerpc/crypto/sha256-spe-glue.c | 167 +++++--------------------- 1 file changed, 30 insertions(+), 137 deletions(-)
arch/powerpc/crypto/sha256-spe-glue.c +30 -137
@@ -8,16 +8,13 @@* Copyright (c) 2015 Markus Stockhausen <[email protected]> */ +#include <asm/switch_to.h> #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> -#include <asm/byteorder.h> -#include <asm/switch_to.h> -#include <linux/hardirq.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/preempt.h> /* * MAX_BYTES defines the number of bytes that are allowed to be processed
@@ -47,151 +44,48 @@ static void spe_end(void)preempt_enable (); } -static inline void ppc_sha256_clear_context (struct sha256_state *sctx) +static void ppc_spe_sha256_block (struct crypto_sha256_state *sctx, +const u8 *src,int blocks) { -int count =sizeof (struct sha256_state ) >>2 ; -u32 *ptr = (u32 *)sctx; +do { +/* cut input data into smaller blocks */ +int unit =min (blocks,MAX_BYTES /SHA256_BLOCK_SIZE ); -/* make sure we can clear the fast way */ -BUILD_BUG_ON (sizeof (struct sha256_state ) %4 ); -do { *ptr++ =0 ; }while (--count); +spe_begin (); +ppc_spe_sha256_transform (sctx->state, src, unit); +spe_end (); + + src += unit *SHA256_BLOCK_SIZE ; + blocks -= unit; + }while (blocks); }static int ppc_spe_sha256_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha256_state *sctx =shash_desc_ctx (desc); -const unsigned int offset = sctx->count &0x3f ; -const unsigned int avail =64 - offset; -unsigned int bytes; -const u8 *src = data; - -if (avail > len) { - sctx->count += len; -memcpy ((char *)sctx->buf + offset, src, len); -return 0 ; - } - - sctx->count += len; - -if (offset) { -memcpy ((char *)sctx->buf + offset, src, avail); - -spe_begin (); -ppc_spe_sha256_transform (sctx->state, (const u8 *)sctx->buf,1 ); -spe_end (); - - len -= avail; - src += avail; - } - -while (len >63 ) { -/* cut input data into smaller blocks */ - bytes = (len >MAX_BYTES ) ?MAX_BYTES : len; - bytes = bytes & ~0x3f ; - -spe_begin (); -ppc_spe_sha256_transform (sctx->state, src, bytes >>6 ); -spe_end (); - - src += bytes; - len -= bytes; - } - -memcpy ((char *)sctx->buf, src, len); -return 0 ; +return sha256_base_do_update_blocks (desc, data, len, + ppc_spe_sha256_block); } -static int ppc_spe_sha256_final (struct shash_desc *desc,u8 *out) +static int ppc_spe_sha256_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha256_state *sctx =shash_desc_ctx (desc); -const unsigned int offset = sctx->count &0x3f ; -char *p = (char *)sctx->buf + offset; -int padlen; -__be64 *pbits = (__be64 *)(((char *)&sctx->buf) +56 ); -__be32 *dst = (__be32 *)out; - - padlen =55 - offset; - *p++ =0x80 ; - -spe_begin (); - -if (padlen <0 ) { -memset (p,0x00 , padlen +sizeof (u64)); -ppc_spe_sha256_transform (sctx->state, sctx->buf,1 ); - p = (char *)sctx->buf; - padlen =56 ; - } - -memset (p,0 , padlen); - *pbits =cpu_to_be64 (sctx->count <<3 ); -ppc_spe_sha256_transform (sctx->state, sctx->buf,1 ); - -spe_end (); - - dst[0 ] =cpu_to_be32 (sctx->state[0 ]); - dst[1 ] =cpu_to_be32 (sctx->state[1 ]); - dst[2 ] =cpu_to_be32 (sctx->state[2 ]); - dst[3 ] =cpu_to_be32 (sctx->state[3 ]); - dst[4 ] =cpu_to_be32 (sctx->state[4 ]); - dst[5 ] =cpu_to_be32 (sctx->state[5 ]); - dst[6 ] =cpu_to_be32 (sctx->state[6 ]); - dst[7 ] =cpu_to_be32 (sctx->state[7 ]); - -ppc_sha256_clear_context (sctx); -return 0 ; -} - -static int ppc_spe_sha224_final (struct shash_desc *desc,u8 *out) -{ -__be32 D [SHA256_DIGEST_SIZE >>2 ]; -__be32 *dst = (__be32 *)out; - -ppc_spe_sha256_final (desc, (u8 *)D ); - -/* avoid bytewise memcpy */ - dst[0 ] =D [0 ]; - dst[1 ] =D [1 ]; - dst[2 ] =D [2 ]; - dst[3 ] =D [3 ]; - dst[4 ] =D [4 ]; - dst[5 ] =D [5 ]; - dst[6 ] =D [6 ]; - -/* clear sensitive data */ -memzero_explicit (D ,SHA256_DIGEST_SIZE ); -return 0 ; -} - -static int ppc_spe_sha256_export (struct shash_desc *desc,void *out) -{ -struct sha256_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); -return 0 ; -} - -static int ppc_spe_sha256_import (struct shash_desc *desc,const void *in) -{ -struct sha256_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); -return 0 ; +sha256_base_do_finup (desc, src, len, ppc_spe_sha256_block); +return sha256_base_finish (desc, out); }static struct shash_alg algs[2 ] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = ppc_spe_sha256_update, - .final = ppc_spe_sha256_final, - .export = ppc_spe_sha256_export, - .import = ppc_spe_sha256_import, - .descsize =sizeof (struct sha256_state ), - .statesize =sizeof (struct sha256_state ), + .finup = ppc_spe_sha256_finup, + .descsize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name="sha256-ppc-spe" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -199,15 +93,14 @@ static struct shash_alg algs[2] = { { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = ppc_spe_sha256_update, - .final = ppc_spe_sha224_final, - .export = ppc_spe_sha256_export, - .import = ppc_spe_sha256_import, - .descsize = sizeof(struct sha256_state), - .statesize = sizeof(struct sha256_state), + .finup = ppc_spe_sha256_finup, + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name= "sha224-ppc-spe", .cra_priority = 300, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 37/67 crypto: s390/sha256 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/s390/crypto/sha256_s390.c | 35 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
arch/s390/crypto/sha256_s390.c +18 -17
@@ -8,12 +8,13 @@* Copyright IBM Corp. 2005, 2011 * Author(s): Jan Glauber ([email protected]) */ -#include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/cpufeature.h> -#include <crypto/sha2.h> #include <asm/cpacf.h> +#include <crypto/internal/hash.h> +#include <crypto/sha2.h> +#include <linux/cpufeature.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> #include "sha.h"
@@ -38,22 +39,20 @@ static int s390_sha256_init(struct shash_desc *desc)static int sha256_export (struct shash_desc *desc,void *out) {struct s390_sha_ctx *sctx =shash_desc_ctx (desc); -struct sha256_state *octx = out; +struct crypto_sha256_state *octx = out; octx->count = sctx->count;memcpy (octx->state, sctx->state,sizeof (octx->state)); -memcpy (octx->buf, sctx->buf,sizeof (octx->buf));return 0 ; }static int sha256_import (struct shash_desc *desc,const void *in) {struct s390_sha_ctx *sctx =shash_desc_ctx (desc); -const struct sha256_state *ictx = in; +const struct crypto_sha256_state *ictx = in; sctx->count = ictx->count;memcpy (sctx->state, ictx->state,sizeof (ictx->state)); -memcpy (sctx->buf, ictx->buf,sizeof (ictx->buf)); sctx->func =CPACF_KIMD_SHA_256 ;return 0 ; }
@@ -61,16 +60,17 @@ static int sha256_import(struct shash_desc *desc, const void *in)static struct shash_alg sha256_alg = { .digestsize =SHA256_DIGEST_SIZE , .init = s390_sha256_init, - .update = s390_sha_update, - .final = s390_sha_final, + .update = s390_sha_update_blocks, + .finup = s390_sha_finup, .export = sha256_export, .import = sha256_import, - .descsize =sizeof (struct s390_sha_ctx ), - .statesize =sizeof (struct sha256_state ), + .descsize =S390_SHA_CTX_SIZE , + .statesize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name="sha256-s390" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -97,16 +97,17 @@ static int s390_sha224_init(struct shash_desc *desc)static struct shash_alg sha224_alg = { .digestsize =SHA224_DIGEST_SIZE , .init = s390_sha224_init, - .update = s390_sha_update, - .final = s390_sha_final, + .update = s390_sha_update_blocks, + .finup = s390_sha_finup, .export = sha256_export, .import = sha256_import, - .descsize =sizeof (struct s390_sha_ctx ), - .statesize =sizeof (struct sha256_state ), + .descsize =S390_SHA_CTX_SIZE , + .statesize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha224" , .cra_driver_name="sha224-s390" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA224_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 38/67 crypto: sparc/sha256 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/sparc/crypto/sha256_glue.c | 121 ++++++-------------------------- 1 file changed, 20 insertions(+), 101 deletions(-)
arch/sparc/crypto/sha256_glue.c +20 -101
@@ -11,133 +11,50 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <asm/elf.h> +#include <asm/pstate.h> #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha2.h> #include <crypto/sha256_base.h> - -#include <asm/pstate.h> -#include <asm/elf.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "opcodes.h" asmlinkage void sha256_sparc64_transform(u32 *digest, const char *data,unsigned int rounds); -static void __sha256_sparc64_update (struct sha256_state *sctx,const u8 *data, -unsigned int len,unsigned int partial) +static void sha256_block (struct crypto_sha256_state *sctx,const u8 *src, +int blocks) { -unsigned int done =0 ; - - sctx->count += len; -if (partial) { - done =SHA256_BLOCK_SIZE - partial; -memcpy (sctx->buf + partial, data, done); -sha256_sparc64_transform (sctx->state, sctx->buf,1 ); - } -if (len - done >=SHA256_BLOCK_SIZE ) { -const unsigned int rounds = (len - done) /SHA256_BLOCK_SIZE ; - -sha256_sparc64_transform (sctx->state, data + done, rounds); - done += rounds *SHA256_BLOCK_SIZE ; - } - -memcpy (sctx->buf, data + done, len - done); +sha256_sparc64_transform (sctx->state, src, blocks); }static int sha256_sparc64_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha256_state *sctx =shash_desc_ctx (desc); -unsigned int partial = sctx->count %SHA256_BLOCK_SIZE ; - -/* Handle the fast case right here */ -if (partial + len <SHA256_BLOCK_SIZE ) { - sctx->count += len; -memcpy (sctx->buf + partial, data, len); - }else -__sha256_sparc64_update (sctx, data, len, partial); - -return 0 ; +return sha256_base_do_update_blocks (desc, data, len, sha256_block); } -static int sha256_sparc64_final (struct shash_desc *desc,u8 *out) +static int sha256_sparc64_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha256_state *sctx =shash_desc_ctx (desc); -unsigned int i, index, padlen; -__be32 *dst = (__be32 *)out; -__be64 bits; -static const u8 padding[SHA256_BLOCK_SIZE ] = {0x80 , }; - - bits =cpu_to_be64 (sctx->count <<3 ); - -/* Pad out to 56 mod 64 and append length */ - index = sctx->count %SHA256_BLOCK_SIZE ; - padlen = (index <56 ) ? (56 - index) : ((SHA256_BLOCK_SIZE +56 ) - index); - -/* We need to fill a whole block for __sha256_sparc64_update() */ -if (padlen <=56 ) { - sctx->count += padlen; -memcpy (sctx->buf + index, padding, padlen); - }else { -__sha256_sparc64_update (sctx, padding, padlen, index); - } -__sha256_sparc64_update (sctx, (const u8 *)&bits,sizeof (bits),56 ); - -/* Store state in digest */ -for (i =0 ; i <8 ; i++) - dst[i] =cpu_to_be32 (sctx->state[i]); - -/* Wipe context */ -memset (sctx,0 ,sizeof (*sctx)); - -return 0 ; -} - -static int sha224_sparc64_final (struct shash_desc *desc,u8 *hash) -{ -u8 D [SHA256_DIGEST_SIZE ]; - -sha256_sparc64_final (desc,D ); - -memcpy (hash,D ,SHA224_DIGEST_SIZE ); -memzero_explicit (D ,SHA256_DIGEST_SIZE ); - -return 0 ; -} - -static int sha256_sparc64_export (struct shash_desc *desc,void *out) -{ -struct sha256_state *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx,sizeof (*sctx)); -return 0 ; -} - -static int sha256_sparc64_import (struct shash_desc *desc,const void *in) -{ -struct sha256_state *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in,sizeof (*sctx)); -return 0 ; +sha256_base_do_finup (desc, src, len, sha256_block); +return sha256_base_finish (desc, out); }static struct shash_alg sha256_alg = { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = sha256_sparc64_update, - .final = sha256_sparc64_final, - .export = sha256_sparc64_export, - .import = sha256_sparc64_import, - .descsize =sizeof (struct sha256_state ), - .statesize =sizeof (struct sha256_state ), + .finup = sha256_sparc64_finup, + .descsize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name="sha256-sparc64" , .cra_priority =SPARC_CR_OPCODE_PRIORITY , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -147,12 +64,14 @@ static struct shash_alg sha224_alg = { .digestsize = SHA224_DIGEST_SIZE, .init = sha224_base_init, .update = sha256_sparc64_update, - .final = sha224_sparc64_final, - .descsize = sizeof(struct sha256_state), + .finup = sha256_sparc64_finup, + .descsize = sizeof(struct crypto_sha256_state), .base = { .cra_name = "sha224", .cra_driver_name= "sha224-sparc64", .cra_priority = SPARC_CR_OPCODE_PRIORITY, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA224_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 39/67 crypto: sha256_base - Remove partial block helpers
In reply to Herbert XuNow that all sha256_base users have been converted to use the API partial block handling, remove the partial block helpers.
--- arch/x86/crypto/sha256_ssse3_glue.c | 4 +-- include/crypto/sha256_base.h | 50 ++++++++--------------------- lib/crypto/sha256.c | 11 ++----- 3 files changed, 17 insertions(+), 48 deletions(-)
arch/x86/crypto/sha256_ssse3_glue.c +2 -2
@@ -50,7 +50,7 @@ MODULE_DEVICE_TABLE(x86cpu, module_cpu_ids);static int _sha256_update (struct shash_desc *desc,const u8 *data,unsigned int len, -crypto_sha256_block_fn *sha256_xform) +sha256_block_fn *sha256_xform) {int remain;
@@ -68,7 +68,7 @@ static int _sha256_update(struct shash_desc *desc, const u8 *data, }static int sha256_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out,crypto_sha256_block_fn *sha256_xform) +unsigned int len,u8 *out,sha256_block_fn *sha256_xform) {kernel_fpu_begin ();sha256_base_do_finup (desc, data, len, sha256_xform);
include/crypto/sha256_base.h +13 -37
118 lines not on this page · raw message
lib/crypto/sha256.c +2 -9
@@ -132,22 +132,15 @@ void sha256_transform_blocks(struct crypto_sha256_state *sst, }EXPORT_SYMBOL_GPL (sha256_transform_blocks); -static void lib_sha256_transform_blocks (struct sha256_state *sctx, -const u8 *input,int blocks) -{ -sha256_transform_blocks ((struct crypto_sha256_state *)sctx, input, - blocks); -} -void sha256_update (struct sha256_state *sctx,const u8 *data,unsigned int len) { -lib_sha256_base_do_update (sctx, data, len, lib_sha256_transform_blocks); +lib_sha256_base_do_update (sctx, data, len, sha256_transform_blocks); }EXPORT_SYMBOL (sha256_update);static void __sha256_final (struct sha256_state *sctx,u8 *out,int digest_size) { -lib_sha256_base_do_finalize (sctx, lib_sha256_transform_blocks); +lib_sha256_base_do_finalize (sctx, sha256_transform_blocks);lib_sha256_base_finish (sctx, out, digest_size); }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 40/67 crypto: arm64/sha3-ce - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/sha3-ce-glue.c | 107 +++++++++++++------------------ arch/s390/crypto/sha.h | 1 - include/crypto/sha3.h | 4 +- 3 files changed, 49 insertions(+), 63 deletions(-)
arch/arm64/crypto/sha3-ce-glue.c +46 -61
@@ -12,13 +12,13 @@#include <asm/hwcap.h> #include <asm/neon.h> #include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sha3.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> +#include <linux/string.h> +#include <linux/unaligned.h> MODULE_DESCRIPTION ("SHA3 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );
@@ -35,74 +35,55 @@ static int sha3_update(struct shash_desc *desc, const u8 *data, unsigned int len) {struct sha3_state *sctx =shash_desc_ctx (desc); -unsigned int digest_size =crypto_shash_digestsize (desc->tfm); +struct crypto_shash *tfm = desc->tfm; +unsigned int bs, ds; +int blocks; -if (!crypto_simd_usable ()) -return crypto_sha3_update (desc, data, len); + ds =crypto_shash_digestsize (tfm); + bs =crypto_shash_blocksize (tfm); + blocks = len / bs; + len -= blocks * bs; +do { +int rem; -if ((sctx->partial + len) >= sctx->rsiz) { -int blocks; - -if (sctx->partial) { -int p = sctx->rsiz - sctx->partial; - -memcpy (sctx->buf + sctx->partial, data, p); -kernel_neon_begin (); -sha3_ce_transform (sctx->st, sctx->buf,1 , digest_size); -kernel_neon_end (); - - data += p; - len -= p; - sctx->partial =0 ; - } - - blocks = len / sctx->rsiz; - len %= sctx->rsiz; - -while (blocks) { -int rem; - -kernel_neon_begin (); - rem =sha3_ce_transform (sctx->st, data, blocks, - digest_size); -kernel_neon_end (); - data += (blocks - rem) * sctx->rsiz; - blocks = rem; - } - } - -if (len) { -memcpy (sctx->buf + sctx->partial, data, len); - sctx->partial += len; - } -return 0 ; +kernel_neon_begin (); + rem =sha3_ce_transform (sctx->st, data, blocks, ds); +kernel_neon_end (); + data += (blocks - rem) * bs; + blocks = rem; + }while (blocks); +return len; } -static int sha3_final (struct shash_desc *desc,u8 *out) +static int sha3_finup (struct shash_desc *desc,const u8 *src,unsigned int len, +u8 *out) {struct sha3_state *sctx =shash_desc_ctx (desc); -unsigned int digest_size =crypto_shash_digestsize (desc->tfm); +struct crypto_shash *tfm = desc->tfm;__le64 *digest = (__le64 *)out; +u8 block[SHA3_224_BLOCK_SIZE ]; +unsigned int bs, ds;int i; -if (!crypto_simd_usable ()) -return crypto_sha3_final (desc, out); + ds =crypto_shash_digestsize (tfm); + bs =crypto_shash_blocksize (tfm); +memcpy (block, src, len); - sctx->buf[sctx->partial++] =0x06 ; -memset (sctx->buf + sctx->partial,0 , sctx->rsiz - sctx->partial); - sctx->buf[sctx->rsiz -1 ] |=0x80 ; + block[len++] =0x06 ; +memset (block + len,0 , bs - len); + block[bs -1 ] |=0x80 ;kernel_neon_begin (); -sha3_ce_transform (sctx->st, sctx->buf,1 , digest_size); +sha3_ce_transform (sctx->st, block,1 , ds);kernel_neon_end (); +memzero_explicit (block ,sizeof (block)); -for (i =0 ; i < digest_size /8 ; i++) +for (i =0 ; i < ds /8 ; i++)put_unaligned_le64 (sctx->st[i], digest++); -if (digest_size &4 ) +if (ds &4 )put_unaligned_le32 (sctx->st[i], (__le32 *)digest); -memzero_explicit (sctx,sizeof (*sctx));return 0 ; }
@@ -110,10 +91,11 @@ static struct shash_alg algs[] = { { .digestsize = SHA3_224_DIGEST_SIZE, .init = crypto_sha3_init, .update = sha3_update, - .final = sha3_final, - .descsize = sizeof(struct sha3_state), + .finup = sha3_finup, + .descsize = SHA3_STATE_SIZE, .base.cra_name = "sha3-224", .base.cra_driver_name = "sha3-224-ce", + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .base.cra_blocksize = SHA3_224_BLOCK_SIZE, .base.cra_module = THIS_MODULE, .base.cra_priority = 200,
@@ -121,10 +103,11 @@ static struct shash_alg algs[] = { { .digestsize = SHA3_256_DIGEST_SIZE, .init = crypto_sha3_init, .update = sha3_update, - .final = sha3_final, - .descsize = sizeof(struct sha3_state), + .finup = sha3_finup, + .descsize = SHA3_STATE_SIZE, .base.cra_name = "sha3-256", .base.cra_driver_name = "sha3-256-ce", + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .base.cra_blocksize = SHA3_256_BLOCK_SIZE, .base.cra_module = THIS_MODULE, .base.cra_priority = 200,
@@ -132,10 +115,11 @@ static struct shash_alg algs[] = { { .digestsize = SHA3_384_DIGEST_SIZE, .init = crypto_sha3_init, .update = sha3_update, - .final = sha3_final, - .descsize = sizeof(struct sha3_state), + .finup = sha3_finup, + .descsize = SHA3_STATE_SIZE, .base.cra_name = "sha3-384", .base.cra_driver_name = "sha3-384-ce", + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .base.cra_blocksize = SHA3_384_BLOCK_SIZE, .base.cra_module = THIS_MODULE, .base.cra_priority = 200,
@@ -143,10 +127,11 @@ static struct shash_alg algs[] = { { .digestsize = SHA3_512_DIGEST_SIZE, .init = crypto_sha3_init, .update = sha3_update, - .final = sha3_final, - .descsize = sizeof(struct sha3_state), + .finup = sha3_finup, + .descsize = SHA3_STATE_SIZE, .base.cra_name = "sha3-512", .base.cra_driver_name = "sha3-512-ce", + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .base.cra_blocksize = SHA3_512_BLOCK_SIZE, .base.cra_module = THIS_MODULE, .base.cra_priority = 200,
arch/s390/crypto/sha.h +0 -1
@@ -14,7 +14,6 @@#include <linux/types.h> /* must be big enough for the largest SHA variant */ -#define SHA3_STATE_SIZE 200#define CPACF_MAX_PARMBLOCK_SIZE SHA3_STATE_SIZE#define SHA_MAX_BLOCK_SIZE SHA3_224_BLOCK_SIZE#define S390_SHA_CTX_SIZE offsetof(struct s390_sha_ctx, buf)
include/crypto/sha3.h +3 -1
@@ -17,8 +17,10 @@#define SHA3_512_DIGEST_SIZE (512 / 8)#define SHA3_512_BLOCK_SIZE (200 - 2 * SHA3_512_DIGEST_SIZE) +#define SHA3_STATE_SIZE 200 +struct sha3_state { -u64 st[25 ]; +u64 st[SHA3_STATE_SIZE /8 ];unsigned int rsiz;unsigned int rsizw;
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 41/67 crypto: s390/sha3 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/s390/crypto/sha3_256_s390.c | 60 +++++++++++++--------------- arch/s390/crypto/sha3_512_s390.c | 67 +++++++++++++------------------- arch/s390/crypto/sha_common.c | 4 +- 3 files changed, 56 insertions(+), 75 deletions(-)
arch/s390/crypto/sha3_256_s390.c +27 -33
@@ -8,12 +8,14 @@* Copyright IBM Corp. 2019 * Author(s): Joerg Schmidbauer ([email protected]) */ -#include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/cpufeature.h> -#include <crypto/sha3.h> #include <asm/cpacf.h> +#include <crypto/internal/hash.h> +#include <crypto/sha3.h> +#include <linux/cpufeature.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> #include "sha.h"
@@ -21,11 +23,11 @@ static int sha3_256_init(struct shash_desc *desc) {struct s390_sha_ctx *sctx =shash_desc_ctx (desc); -if (!test_facility (86 ))/* msa 12 */ + sctx->first_message_part =test_facility (86 ); +if (!sctx->first_message_part)memset (sctx->state,0 ,sizeof (sctx->state)); sctx->count =0 ; sctx->func =CPACF_KIMD_SHA3_256 ; - sctx->first_message_part =1 ;return 0 ; }
@@ -35,11 +37,11 @@ static int sha3_256_export(struct shash_desc *desc, void *out)struct s390_sha_ctx *sctx =shash_desc_ctx (desc);struct sha3_state *octx = out; - octx->rsiz = sctx->count; +if (sctx->first_message_part) { +memset (sctx->state,0 ,sizeof (sctx->state)); + sctx->first_message_part =0 ; + }memcpy (octx->st, sctx->state,sizeof (octx->st)); -memcpy (octx->buf, sctx->buf,sizeof (octx->buf)); - octx->partial = sctx->first_message_part; -return 0 ; }
@@ -48,10 +50,9 @@ static int sha3_256_import(struct shash_desc *desc, const void *in)struct s390_sha_ctx *sctx =shash_desc_ctx (desc);const struct sha3_state *ictx = in; - sctx->count = ictx->rsiz; + sctx->count =0 ;memcpy (sctx->state, ictx->st,sizeof (ictx->st)); -memcpy (sctx->buf, ictx->buf,sizeof (ictx->buf)); - sctx->first_message_part = ictx->partial; + sctx->first_message_part =0 ; sctx->func =CPACF_KIMD_SHA3_256 ;return 0 ;
@@ -60,30 +61,26 @@ static int sha3_256_import(struct shash_desc *desc, const void *in)static int sha3_224_import (struct shash_desc *desc,const void *in) {struct s390_sha_ctx *sctx =shash_desc_ctx (desc); -const struct sha3_state *ictx = in; - sctx->count = ictx->rsiz; -memcpy (sctx->state, ictx->st,sizeof (ictx->st)); -memcpy (sctx->buf, ictx->buf,sizeof (ictx->buf)); - sctx->first_message_part = ictx->partial; +sha3_256_import (desc, in); sctx->func =CPACF_KIMD_SHA3_224 ; -return 0 ; }static struct shash_alg sha3_256_alg = { .digestsize =SHA3_256_DIGEST_SIZE ,/* = 32 */ .init = sha3_256_init, - .update = s390_sha_update, - .final = s390_sha_final, + .update = s390_sha_update_blocks, + .finup = s390_sha_finup, .export = sha3_256_export, .import = sha3_256_import, - .descsize =sizeof (struct s390_sha_ctx ), - .statesize =sizeof (struct sha3_state ), + .descsize =S390_SHA_CTX_SIZE , + .statesize =SHA3_STATE_SIZE , .base = { .cra_name ="sha3-256" , .cra_driver_name ="sha3-256-s390" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA3_256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -93,28 +90,25 @@ static int sha3_224_init(struct shash_desc *desc) {struct s390_sha_ctx *sctx =shash_desc_ctx (desc); -if (!test_facility (86 ))/* msa 12 */ -memset (sctx->state,0 ,sizeof (sctx->state)); - sctx->count =0 ; +sha3_256_init (desc); sctx->func =CPACF_KIMD_SHA3_224 ; - sctx->first_message_part =1 ; -return 0 ; }static struct shash_alg sha3_224_alg = { .digestsize =SHA3_224_DIGEST_SIZE , .init = sha3_224_init, - .update = s390_sha_update, - .final = s390_sha_final, + .update = s390_sha_update_blocks, + .finup = s390_sha_finup, .export = sha3_256_export,/* same as for 256 */ .import = sha3_224_import,/* function code different! */ - .descsize =sizeof (struct s390_sha_ctx ), - .statesize =sizeof (struct sha3_state ), + .descsize =S390_SHA_CTX_SIZE , + .statesize =SHA3_STATE_SIZE , .base = { .cra_name ="sha3-224" , .cra_driver_name ="sha3-224-s390" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .cra_blocksize =SHA3_224_BLOCK_SIZE , .cra_module =THIS_MODULE , }
arch/s390/crypto/sha3_512_s390.c +27 -40
137 lines not on this page · raw message
arch/s390/crypto/sha_common.c +2 -2
@@ -29,7 +29,7 @@ int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len) fc = ctx->func;if (ctx->first_message_part) - fc |=test_facility (86 ) ?CPACF_KIMD_NIP :0 ; + fc |=CPACF_KIMD_NIP ;/* process one stored block */ if (index) {
@@ -68,7 +68,7 @@ int s390_sha_update_blocks(struct shash_desc *desc, const u8 *data, fc = ctx->func;if (ctx->first_message_part) - fc |=test_facility (86 ) ?CPACF_KIMD_NIP :0 ; + fc |=CPACF_KIMD_NIP ;/* process as many blocks as possible */ n = (len / bsize) * bsize;
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 42/67 crypto: sha3-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/sha3_generic.c | 101 ++++++++++++++++++------------------------ include/crypto/sha3.h | 7 +-- 2 files changed, 47 insertions(+), 61 deletions(-)
crypto/sha3_generic.c +43 -58
@@ -9,10 +9,10 @@* Ard Biesheuvel <[email protected]> */ #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/types.h> #include <crypto/sha3.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> #include <linux/unaligned.h> /*
@@ -161,68 +161,51 @@ static void keccakf(u64 st[25])int crypto_sha3_init (struct shash_desc *desc) {struct sha3_state *sctx =shash_desc_ctx (desc); -unsigned int digest_size =crypto_shash_digestsize (desc->tfm); - - sctx->rsiz =200 -2 * digest_size; - sctx->rsizw = sctx->rsiz /8 ; - sctx->partial =0 ;memset (sctx->st,0 ,sizeof (sctx->st));return 0 ; }EXPORT_SYMBOL (crypto_sha3_init); -int crypto_sha3_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static int crypto_sha3_update (struct shash_desc *desc,const u8 *data, +unsigned int len) { +unsigned int rsiz =crypto_shash_blocksize (desc->tfm);struct sha3_state *sctx =shash_desc_ctx (desc); -unsigned int done; -const u8 *src; +unsigned int rsizw = rsiz /8 ; - done =0 ; - src = data; +do { +int i; -if ((sctx->partial + len) > (sctx->rsiz -1 )) { -if (sctx->partial) { - done = -sctx->partial; -memcpy (sctx->buf + sctx->partial, data, - done + sctx->rsiz); - src = sctx->buf; - } +for (i =0 ; i < rsizw; i++) + sctx->st[i] ^=get_unaligned_le64 (data +8 * i); +keccakf (sctx->st); -do { -unsigned int i; - -for (i =0 ; i < sctx->rsizw; i++) - sctx->st[i] ^=get_unaligned_le64 (src +8 * i); -keccakf (sctx->st); - - done += sctx->rsiz; - src = data + done; - }while (done + (sctx->rsiz -1 ) < len); - - sctx->partial =0 ; - } -memcpy (sctx->buf + sctx->partial, src, len - done); - sctx->partial += (len - done); - -return 0 ; + data += rsiz; + len -= rsiz; + }while (len >= rsiz); +return len; } -EXPORT_SYMBOL (crypto_sha3_update); -int crypto_sha3_final (struct shash_desc *desc,u8 *out) +static int crypto_sha3_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha3_state *sctx =shash_desc_ctx (desc); -unsigned int i, inlen = sctx->partial;unsigned int digest_size =crypto_shash_digestsize (desc->tfm); +unsigned int rsiz =crypto_shash_blocksize (desc->tfm); +struct sha3_state *sctx =shash_desc_ctx (desc); +__le64 block[SHA3_224_BLOCK_SIZE /8 ] = {};__le64 *digest = (__le64 *)out; +unsigned int rsizw = rsiz /8 ; +u8 *p; +int i; - sctx->buf[inlen++] =0x06 ; -memset (sctx->buf + inlen,0 , sctx->rsiz - inlen); - sctx->buf[sctx->rsiz -1 ] |=0x80 ; + p =memcpy (block, src, len); + p[len++] =0x06 ; + p[rsiz -1 ] |=0x80 ; -for (i =0 ; i < sctx->rsizw; i++) - sctx->st[i] ^=get_unaligned_le64 (sctx->buf +8 * i); +for (i =0 ; i < rsizw; i++) + sctx->st[i] ^=le64_to_cpu (block[i]); +memzero_explicit (block,sizeof (block));keccakf (sctx->st);
@@ -232,49 +215,51 @@ int crypto_sha3_final(struct shash_desc *desc, u8 *out)if (digest_size &4 )put_unaligned_le32 (sctx->st[i], (__le32 *)digest); -memset (sctx,0 ,sizeof (*sctx));return 0 ; } -EXPORT_SYMBOL (crypto_sha3_final);static struct shash_alg algs[] = { { .digestsize =SHA3_224_DIGEST_SIZE , .init = crypto_sha3_init, .update = crypto_sha3_update, - .final = crypto_sha3_final, - .descsize =sizeof (struct sha3_state ), + .finup = crypto_sha3_finup, + .descsize =SHA3_STATE_SIZE , .base.cra_name ="sha3-224" , .base.cra_driver_name ="sha3-224-generic" , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .base.cra_blocksize =SHA3_224_BLOCK_SIZE , .base.cra_module =THIS_MODULE , }, { .digestsize =SHA3_256_DIGEST_SIZE , .init = crypto_sha3_init, .update = crypto_sha3_update, - .final = crypto_sha3_final, - .descsize =sizeof (struct sha3_state ), + .finup = crypto_sha3_finup, + .descsize =SHA3_STATE_SIZE , .base.cra_name ="sha3-256" , .base.cra_driver_name ="sha3-256-generic" , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .base.cra_blocksize =SHA3_256_BLOCK_SIZE , .base.cra_module =THIS_MODULE , }, { .digestsize =SHA3_384_DIGEST_SIZE , .init = crypto_sha3_init, .update = crypto_sha3_update, - .final = crypto_sha3_final, - .descsize =sizeof (struct sha3_state ), + .finup = crypto_sha3_finup, + .descsize =SHA3_STATE_SIZE , .base.cra_name ="sha3-384" , .base.cra_driver_name ="sha3-384-generic" , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .base.cra_blocksize =SHA3_384_BLOCK_SIZE , .base.cra_module =THIS_MODULE , }, { .digestsize =SHA3_512_DIGEST_SIZE , .init = crypto_sha3_init, .update = crypto_sha3_update, - .final = crypto_sha3_final, - .descsize =sizeof (struct sha3_state ), + .finup = crypto_sha3_finup, + .descsize =SHA3_STATE_SIZE , .base.cra_name ="sha3-512" , .base.cra_driver_name ="sha3-512-generic" , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY , .base.cra_blocksize =SHA3_512_BLOCK_SIZE , .base.cra_module =THIS_MODULE , } };
@@ -289,7 +274,7 @@ static void __exit sha3_generic_mod_fini(void)crypto_unregister_shashes (algs,ARRAY_SIZE (algs)); } -subsys_initcall (sha3_generic_mod_init); +module_init (sha3_generic_mod_init);module_exit (sha3_generic_mod_fini);MODULE_LICENSE ("GPL" );
include/crypto/sha3.h +4 -3
@@ -5,6 +5,8 @@#ifndef __CRYPTO_SHA3_H__#define __CRYPTO_SHA3_H__ +#include <linux/types.h> +#define SHA3_224_DIGEST_SIZE (224 / 8)#define SHA3_224_BLOCK_SIZE (200 - 2 * SHA3_224_DIGEST_SIZE)
@@ -19,6 +21,8 @@#define SHA3_STATE_SIZE 200 +struct shash_desc ; +struct sha3_state {u64 st[SHA3_STATE_SIZE /8 ];unsigned int rsiz;
@@ -29,8 +33,5 @@ struct sha3_state { };int crypto_sha3_init (struct shash_desc *desc); -int crypto_sha3_update (struct shash_desc *desc,const u8 *data, -unsigned int len); -int crypto_sha3_final (struct shash_desc *desc,u8 *out);#endif
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 43/67 crypto: zynqmp-sha - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
As this was the last user of the extra fields in struct sha3_state, remove them.
--- drivers/crypto/xilinx/zynqmp-sha.c | 71 +++++++++--------------------- include/crypto/sha3.h | 5 --- 2 files changed, 22 insertions(+), 54 deletions(-)
drivers/crypto/xilinx/zynqmp-sha.c +22 -49
@@ -3,7 +3,6 @@* Xilinx ZynqMP SHA Driver. * Copyright (c) 2022 Xilinx Inc. */ -#include <crypto/hash.h> #include <crypto/internal/hash.h> #include <crypto/sha3.h> #include <linux/cacheflush.h>
@@ -37,10 +36,6 @@ struct zynqmp_sha_tfm_ctx {struct crypto_shash *fbk_tfm; }; -struct zynqmp_sha_desc_ctx { -struct shash_desc fbk_req; -}; -static dma_addr_t update_dma_addr, final_dma_addr;static char *ubuf, *fbuf;
@@ -64,7 +59,6 @@ static int zynqmp_sha_init_tfm(struct crypto_shash *hash)return PTR_ERR (fallback_tfm);if (crypto_shash_descsize (hash) < -sizeof (struct zynqmp_sha_desc_ctx ) +crypto_shash_descsize (tfm_ctx->fbk_tfm)) {crypto_free_shash (fallback_tfm);return -EINVAL ;
@@ -79,58 +73,41 @@ static void zynqmp_sha_exit_tfm(struct crypto_shash *hash) {struct zynqmp_sha_tfm_ctx *tfm_ctx =crypto_shash_ctx (hash); -if (tfm_ctx->fbk_tfm) { -crypto_free_shash (tfm_ctx->fbk_tfm); - tfm_ctx->fbk_tfm =NULL ; - } - -memzero_explicit (tfm_ctx,sizeof (struct zynqmp_sha_tfm_ctx )); +crypto_free_shash (tfm_ctx->fbk_tfm); }static int zynqmp_sha_init (struct shash_desc *desc) { -struct zynqmp_sha_desc_ctx *dctx =shash_desc_ctx (desc);struct zynqmp_sha_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm); +struct crypto_shash *fbtfm = tctx->fbk_tfm; +SHASH_DESC_ON_STACK (fbdesc, fbtfm); - dctx->fbk_req.tfm = tctx->fbk_tfm; -return crypto_shash_init (&dctx->fbk_req); + fbdesc->tfm = fbtfm; +return crypto_shash_init (fbdesc) ?: +crypto_shash_export_core (fbdesc,shash_desc_ctx (desc)); }static int zynqmp_sha_update (struct shash_desc *desc,const u8 *data,unsigned int length) { -struct zynqmp_sha_desc_ctx *dctx =shash_desc_ctx (desc); +struct zynqmp_sha_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm); +struct crypto_shash *fbtfm = tctx->fbk_tfm; +SHASH_DESC_ON_STACK (fbdesc, fbtfm); -return crypto_shash_update (&dctx->fbk_req, data, length); -} - -static int zynqmp_sha_final (struct shash_desc *desc,u8 *out) -{ -struct zynqmp_sha_desc_ctx *dctx =shash_desc_ctx (desc); - -return crypto_shash_final (&dctx->fbk_req, out); + fbdesc->tfm = fbtfm; +return crypto_shash_import_core (fbdesc,shash_desc_ctx (desc)) ?: +crypto_shash_update (fbdesc, data, length) ?: +crypto_shash_export_core (fbdesc,shash_desc_ctx (desc)); }static int zynqmp_sha_finup (struct shash_desc *desc,const u8 *data,unsigned int length,u8 *out) { -struct zynqmp_sha_desc_ctx *dctx =shash_desc_ctx (desc); - -return crypto_shash_finup (&dctx->fbk_req, data, length, out); -} - -static int zynqmp_sha_import (struct shash_desc *desc,const void *in) -{ -struct zynqmp_sha_desc_ctx *dctx =shash_desc_ctx (desc);struct zynqmp_sha_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm); +struct crypto_shash *fbtfm = tctx->fbk_tfm; +SHASH_DESC_ON_STACK (fbdesc, fbtfm); - dctx->fbk_req.tfm = tctx->fbk_tfm; -return crypto_shash_import (&dctx->fbk_req, in); -} - -static int zynqmp_sha_export (struct shash_desc *desc,void *out) -{ -struct zynqmp_sha_desc_ctx *dctx =shash_desc_ctx (desc); - -return crypto_shash_export (&dctx->fbk_req, out); + fbdesc->tfm = fbtfm; +return crypto_shash_import_core (fbdesc,shash_desc_ctx (desc)) ?: +crypto_shash_finup (fbdesc, data, length, out); } static int __zynqmp_sha_digest(struct shash_desc *desc, const u8 *data,
@@ -179,24 +156,20 @@ static struct zynqmp_sha_drv_ctx sha3_drv_ctx = { .sha3_384 = { .init = zynqmp_sha_init, .update = zynqmp_sha_update, - .final = zynqmp_sha_final, .finup = zynqmp_sha_finup, .digest = zynqmp_sha_digest, - .export = zynqmp_sha_export, - .import = zynqmp_sha_import, .init_tfm = zynqmp_sha_init_tfm, .exit_tfm = zynqmp_sha_exit_tfm, - .descsize =sizeof (struct zynqmp_sha_desc_ctx ) + - sizeof(struct sha3_state), - .statesize = sizeof(struct sha3_state), + .descsize = sizeof(struct sha3_state), .digestsize = SHA3_384_DIGEST_SIZE, .base = { .cra_name = "sha3-384", .cra_driver_name = "zynqmp-sha3-384", .cra_priority = 300, .cra_flags =CRYPTO_ALG_KERN_DRIVER_ONLY | -CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_NEED_FALLBACK, +CRYPTO_ALG_NEED_FALLBACK | +CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA3_384_BLOCK_SIZE, .cra_ctxsize = sizeof(struct zynqmp_sha_tfm_ctx), .cra_module = THIS_MODULE,
include/crypto/sha3.h +0 -5
@@ -25,11 +25,6 @@ struct shash_desc;struct sha3_state {u64 st[SHA3_STATE_SIZE /8 ]; -unsigned int rsiz; -unsigned int rsizw; - -unsigned int partial; -u8 buf[SHA3_224_BLOCK_SIZE ]; };int crypto_sha3_init (struct shash_desc *desc);
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 44/67 crypto: x86/sha512 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/x86/crypto/sha512_ssse3_glue.c | 79 ++++++++++------------------- include/crypto/sha2.h | 1 + include/crypto/sha512_base.h | 54 ++++++++++++++++++-- 3 files changed, 77 insertions(+), 57 deletions(-)
arch/x86/crypto/sha512_ssse3_glue.c +27 -52
@@ -27,17 +27,13 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt -#include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/string.h> -#include <linux/types.h> -#include <crypto/sha2.h> -#include <crypto/sha512_base.h> #include <asm/cpu_device_id.h> #include <asm/simd.h> +#include <crypto/internal/hash.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <crypto/sha2.h> +#include <crypto/sha512_base.h> asmlinkage void sha512_transform_ssse3(struct sha512_state *state,const u8 *data,int blocks);
@@ -45,11 +41,7 @@ asmlinkage void sha512_transform_ssse3(struct sha512_state *state,static int sha512_update (struct shash_desc *desc,const u8 *data,unsigned int len,sha512_block_fn *sha512_xform) { -struct sha512_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count[0 ] %SHA512_BLOCK_SIZE ) + len <SHA512_BLOCK_SIZE ) -return crypto_sha512_update (desc, data, len); +int remain;/* * Make sure struct sha512_state begins directly with the SHA512
@@ -58,22 +50,17 @@ static int sha512_update(struct shash_desc *desc, const u8 *data,BUILD_BUG_ON (offsetof(struct sha512_state , state) !=0 );kernel_fpu_begin (); -sha512_base_do_update (desc, data, len, sha512_xform); + remain =sha512_base_do_update_blocks (desc, data, len, sha512_xform);kernel_fpu_end (); -return 0 ; +return remain; }static int sha512_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out,sha512_block_fn *sha512_xform) { -if (!crypto_simd_usable ()) -return crypto_sha512_finup (desc, data, len, out); -kernel_fpu_begin (); -if (len) -sha512_base_do_update (desc, data, len, sha512_xform); -sha512_base_do_finalize (desc, sha512_xform); +sha512_base_do_finup (desc, data, len, sha512_xform);kernel_fpu_end ();return sha512_base_finish (desc, out);
@@ -91,23 +78,18 @@ static int sha512_ssse3_finup(struct shash_desc *desc, const u8 *data,return sha512_finup (desc, data, len, out, sha512_transform_ssse3); } -/* Add padding and return the message digest. */ -static int sha512_ssse3_final (struct shash_desc *desc,u8 *out) -{ -return sha512_ssse3_finup (desc,NULL ,0 , out); -} -static struct shash_alg sha512_ssse3_algs[] = { { .digestsize =SHA512_DIGEST_SIZE , .init = sha512_base_init, .update = sha512_ssse3_update, - .final = sha512_ssse3_final, .finup = sha512_ssse3_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .base = { .cra_name ="sha512" , .cra_driver_name ="sha512-ssse3" , .cra_priority =150 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA512_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -115,13 +97,14 @@ static struct shash_alg sha512_ssse3_algs[] = { { .digestsize = SHA384_DIGEST_SIZE, .init = sha384_base_init, .update = sha512_ssse3_update, - .final = sha512_ssse3_final, .finup = sha512_ssse3_finup, - .descsize = sizeof(struct sha512_state), + .descsize = SHA512_STATE_SIZE, .base = { .cra_name = "sha384", .cra_driver_name = "sha384-ssse3", .cra_priority = 150, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA384_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -167,23 +150,18 @@ static int sha512_avx_finup(struct shash_desc *desc, const u8 *data,return sha512_finup (desc, data, len, out, sha512_transform_avx); } -/* Add padding and return the message digest. */ -static int sha512_avx_final (struct shash_desc *desc,u8 *out) -{ -return sha512_avx_finup (desc,NULL ,0 , out); -} -static struct shash_alg sha512_avx_algs[] = { { .digestsize =SHA512_DIGEST_SIZE , .init = sha512_base_init, .update = sha512_avx_update, - .final = sha512_avx_final, .finup = sha512_avx_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .base = { .cra_name ="sha512" , .cra_driver_name ="sha512-avx" , .cra_priority =160 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA512_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -191,13 +169,14 @@ static struct shash_alg sha512_avx_algs[] = { { .digestsize = SHA384_DIGEST_SIZE, .init = sha384_base_init, .update = sha512_avx_update, - .final = sha512_avx_final, .finup = sha512_avx_finup, - .descsize = sizeof(struct sha512_state), + .descsize = SHA512_STATE_SIZE, .base = { .cra_name = "sha384", .cra_driver_name = "sha384-avx", .cra_priority = 160, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA384_BLOCK_SIZE, .cra_module = THIS_MODULE, }
@@ -233,23 +212,18 @@ static int sha512_avx2_finup(struct shash_desc *desc, const u8 *data,return sha512_finup (desc, data, len, out, sha512_transform_rorx); } -/* Add padding and return the message digest. */ -static int sha512_avx2_final (struct shash_desc *desc,u8 *out) -{ -return sha512_avx2_finup (desc,NULL ,0 , out); -} -static struct shash_alg sha512_avx2_algs[] = { { .digestsize =SHA512_DIGEST_SIZE , .init = sha512_base_init, .update = sha512_avx2_update, - .final = sha512_avx2_final, .finup = sha512_avx2_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .base = { .cra_name ="sha512" , .cra_driver_name ="sha512-avx2" , .cra_priority =170 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA512_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -257,13 +231,14 @@ static struct shash_alg sha512_avx2_algs[] = { { .digestsize = SHA384_DIGEST_SIZE, .init = sha384_base_init, .update = sha512_avx2_update, - .final = sha512_avx2_final, .finup = sha512_avx2_finup, - .descsize = sizeof(struct sha512_state), + .descsize = SHA512_STATE_SIZE, .base = { .cra_name = "sha384", .cra_driver_name = "sha384-avx2", .cra_priority = 170, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA384_BLOCK_SIZE, .cra_module = THIS_MODULE, }
include/crypto/sha2.h +1 -0
@@ -19,6 +19,7 @@#define SHA512_DIGEST_SIZE 64#define SHA512_BLOCK_SIZE 128 +#define SHA512_STATE_SIZE 80#define SHA224_H0 0xc1059ed8UL#define SHA224_H1 0x367cd507UL
include/crypto/sha512_base.h +49 -5
73 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 45/67 crypto: mips/octeon-sha512 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- .../mips/cavium-octeon/crypto/octeon-sha512.c | 155 +++++------------- 1 file changed, 42 insertions(+), 113 deletions(-)
arch/mips/cavium-octeon/crypto/octeon-sha512.c +42 -113
@@ -13,15 +13,12 @@* Copyright (c) 2003 Kyle McMartin <[email protected]> */ -#include <linux/mm.h> -#include <crypto/sha2.h> -#include <crypto/sha512_base.h> -#include <linux/init.h> -#include <linux/types.h> -#include <linux/module.h> -#include <asm/byteorder.h> #include <asm/octeon/octeon.h> #include <crypto/internal/hash.h> +#include <crypto/sha2.h> +#include <crypto/sha512_base.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "octeon-crypto.h"
@@ -53,60 +50,31 @@ static void octeon_sha512_read_hash(struct sha512_state *sctx) sctx->state[7 ] =read_octeon_64bit_hash_sha512 (7 ); } -static void octeon_sha512_transform (const void *_block) +static void octeon_sha512_transform (struct sha512_state *sctx, +const u8 *src,int blocks) { -const u64 *block = _block; +do { +const u64 *block = (const u64 *)src; -write_octeon_64bit_block_sha512 (block[0 ],0 ); -write_octeon_64bit_block_sha512 (block[1 ],1 ); -write_octeon_64bit_block_sha512 (block[2 ],2 ); -write_octeon_64bit_block_sha512 (block[3 ],3 ); -write_octeon_64bit_block_sha512 (block[4 ],4 ); -write_octeon_64bit_block_sha512 (block[5 ],5 ); -write_octeon_64bit_block_sha512 (block[6 ],6 ); -write_octeon_64bit_block_sha512 (block[7 ],7 ); -write_octeon_64bit_block_sha512 (block[8 ],8 ); -write_octeon_64bit_block_sha512 (block[9 ],9 ); -write_octeon_64bit_block_sha512 (block[10 ],10 ); -write_octeon_64bit_block_sha512 (block[11 ],11 ); -write_octeon_64bit_block_sha512 (block[12 ],12 ); -write_octeon_64bit_block_sha512 (block[13 ],13 ); -write_octeon_64bit_block_sha512 (block[14 ],14 ); -octeon_sha512_start (block[15 ]); -} +write_octeon_64bit_block_sha512 (block[0 ],0 ); +write_octeon_64bit_block_sha512 (block[1 ],1 ); +write_octeon_64bit_block_sha512 (block[2 ],2 ); +write_octeon_64bit_block_sha512 (block[3 ],3 ); +write_octeon_64bit_block_sha512 (block[4 ],4 ); +write_octeon_64bit_block_sha512 (block[5 ],5 ); +write_octeon_64bit_block_sha512 (block[6 ],6 ); +write_octeon_64bit_block_sha512 (block[7 ],7 ); +write_octeon_64bit_block_sha512 (block[8 ],8 ); +write_octeon_64bit_block_sha512 (block[9 ],9 ); +write_octeon_64bit_block_sha512 (block[10 ],10 ); +write_octeon_64bit_block_sha512 (block[11 ],11 ); +write_octeon_64bit_block_sha512 (block[12 ],12 ); +write_octeon_64bit_block_sha512 (block[13 ],13 ); +write_octeon_64bit_block_sha512 (block[14 ],14 ); +octeon_sha512_start (block[15 ]); -static void __octeon_sha512_update (struct sha512_state *sctx,const u8 *data, -unsigned int len) -{ -unsigned int part_len; -unsigned int index; -unsigned int i; - -/* Compute number of bytes mod 128. */ - index = sctx->count[0 ] %SHA512_BLOCK_SIZE ; - -/* Update number of bytes. */ -if ((sctx->count[0 ] += len) < len) - sctx->count[1 ]++; - - part_len =SHA512_BLOCK_SIZE - index; - -/* Transform as many times as possible. */ -if (len >= part_len) { -memcpy (&sctx->buf[index], data, part_len); -octeon_sha512_transform (sctx->buf); - -for (i = part_len; i +SHA512_BLOCK_SIZE <= len; - i +=SHA512_BLOCK_SIZE ) -octeon_sha512_transform (&data[i]); - - index =0 ; - }else { - i =0 ; - } - -/* Buffer remaining input. */ -memcpy (&sctx->buf[index], &data[i], len - i); + src +=SHA512_BLOCK_SIZE ; + }while (--blocks); } static int octeon_sha512_update(struct shash_desc *desc, const u8 *data,
@@ -115,89 +83,48 @@ static int octeon_sha512_update(struct shash_desc *desc, const u8 *data,struct sha512_state *sctx =shash_desc_ctx (desc);struct octeon_cop2_state state;unsigned long flags; - -/* -* Small updates never reach the crypto engine, so the generic sha512 is -* faster because of the heavyweight octeon_crypto_enable() / -* octeon_crypto_disable(). -*/ -if ((sctx->count[0 ] %SHA512_BLOCK_SIZE ) + len <SHA512_BLOCK_SIZE ) -return crypto_sha512_update (desc, data, len); +int remain; flags =octeon_crypto_enable (&state);octeon_sha512_store_hash (sctx); -__octeon_sha512_update (sctx, data, len); + remain =sha512_base_do_update_blocks (desc, data, len, + octeon_sha512_transform);octeon_sha512_read_hash (sctx);octeon_crypto_disable (&state, flags); - -return 0 ; +return remain; } -static int octeon_sha512_final (struct shash_desc *desc,u8 *hash) +static int octeon_sha512_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *hash) {struct sha512_state *sctx =shash_desc_ctx (desc); -static u8 padding[128 ] = {0x80 , };struct octeon_cop2_state state; -__be64 *dst = (__be64 *)hash; -unsigned int pad_len;unsigned long flags; -unsigned int index; -__be64 bits[2 ]; -int i; - -/* Save number of bits. */ - bits[1 ] =cpu_to_be64 (sctx->count[0 ] <<3 ); - bits[0 ] =cpu_to_be64 (sctx->count[1 ] <<3 | sctx->count[0 ] >>61 ); - -/* Pad out to 112 mod 128. */ - index = sctx->count[0 ] &0x7f ; - pad_len = (index <112 ) ? (112 - index) : ((128 +112 ) - index); flags =octeon_crypto_enable (&state);octeon_sha512_store_hash (sctx); -__octeon_sha512_update (sctx, padding, pad_len); - -/* Append length (before padding). */ -__octeon_sha512_update (sctx, (const u8 *)bits,sizeof (bits)); +sha512_base_do_finup (desc, src, len, octeon_sha512_transform);octeon_sha512_read_hash (sctx);octeon_crypto_disable (&state, flags); - -/* Store state in digest. */ -for (i =0 ; i <8 ; i++) - dst[i] =cpu_to_be64 (sctx->state[i]); - -/* Zeroize sensitive information. */ -memset (sctx,0 ,sizeof (struct sha512_state )); - -return 0 ; -} - -static int octeon_sha384_final (struct shash_desc *desc,u8 *hash) -{ -u8 D [64 ]; - -octeon_sha512_final (desc,D ); - -memcpy (hash,D ,48 ); -memzero_explicit (D ,64 ); - -return 0 ; +return sha512_base_finish (desc, hash); }static struct shash_alg octeon_sha512_algs[2 ] = { { .digestsize =SHA512_DIGEST_SIZE , .init = sha512_base_init, .update = octeon_sha512_update, - .final = octeon_sha512_final, - .descsize =sizeof (struct sha512_state ), + .finup = octeon_sha512_finup, + .descsize =SHA512_STATE_SIZE , .base = { .cra_name ="sha512" , .cra_driver_name="octeon-sha512" , .cra_priority =OCTEON_CR_OPCODE_PRIORITY , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA512_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -205,12 +132,14 @@ static struct shash_alg octeon_sha512_algs[2] = { { .digestsize = SHA384_DIGEST_SIZE, .init = sha384_base_init, .update = octeon_sha512_update, - .final = octeon_sha384_final, - .descsize = sizeof(struct sha512_state), + .finup = octeon_sha512_finup, + .descsize = SHA512_STATE_SIZE, .base = { .cra_name = "sha384", .cra_driver_name= "octeon-sha384", .cra_priority = OCTEON_CR_OPCODE_PRIORITY, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA384_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 46/67 crypto: riscv/sha512 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/riscv/crypto/sha512-riscv64-glue.c | 47 ++++++++++--------------- crypto/sha512_generic.c | 5 +-- include/crypto/sha512_base.h | 6 ++++ 3 files changed, 28 insertions(+), 30 deletions(-)
arch/riscv/crypto/sha512-riscv64-glue.c +19 -28
@@ -14,7 +14,7 @@#include <crypto/internal/hash.h> #include <crypto/internal/simd.h> #include <crypto/sha512_base.h> -#include <linux/linkage.h> +#include <linux/kernel.h> #include <linux/module.h> /*
@@ -24,8 +24,8 @@ asmlinkage void sha512_transform_zvknhb_zvkb(struct sha512_state *state,const u8 *data,int num_blocks); -static int riscv64_sha512_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static void sha512_block (struct sha512_state *state,const u8 *data, +int num_blocks) {/* * Ensure struct sha512_state begins directly with the SHA-512
@@ -35,35 +35,24 @@ static int riscv64_sha512_update(struct shash_desc *desc, const u8 *data,if (crypto_simd_usable ()) {kernel_vector_begin (); -sha512_base_do_update (desc, data, len, - sha512_transform_zvknhb_zvkb); +sha512_transform_zvknhb_zvkb (state, data, num_blocks);kernel_vector_end (); }else { -crypto_sha512_update (desc, data, len); +sha512_generic_block_fn (state, data, num_blocks); } -return 0 ; +} + +static int riscv64_sha512_update (struct shash_desc *desc,const u8 *data, +unsigned int len) +{ +return sha512_base_do_update_blocks (desc, data, len, sha512_block); }static int riscv64_sha512_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (crypto_simd_usable ()) { -kernel_vector_begin (); -if (len) -sha512_base_do_update (desc, data, len, - sha512_transform_zvknhb_zvkb); -sha512_base_do_finalize (desc, sha512_transform_zvknhb_zvkb); -kernel_vector_end (); - -return sha512_base_finish (desc, out); - } - -return crypto_sha512_finup (desc, data, len, out); -} - -static int riscv64_sha512_final (struct shash_desc *desc,u8 *out) -{ -return riscv64_sha512_finup (desc,NULL ,0 , out); +sha512_base_do_finup (desc, data, len, sha512_block); +return sha512_base_finish (desc, out); } static int riscv64_sha512_digest(struct shash_desc *desc, const u8 *data,
@@ -77,14 +66,15 @@ static struct shash_alg riscv64_sha512_algs[] = { { .init = sha512_base_init, .update = riscv64_sha512_update, - .final = riscv64_sha512_final, .finup = riscv64_sha512_finup, .digest = riscv64_sha512_digest, - .descsize = sizeof(struct sha512_state), + .descsize = SHA512_STATE_SIZE, .digestsize = SHA512_DIGEST_SIZE, .base = { .cra_blocksize = SHA512_BLOCK_SIZE, .cra_priority = 300, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_name = "sha512", .cra_driver_name = "sha512-riscv64-zvknhb-zvkb", .cra_module = THIS_MODULE,
@@ -92,13 +82,14 @@ static struct shash_alg riscv64_sha512_algs[] = { }, { .init = sha384_base_init, .update = riscv64_sha512_update, - .final = riscv64_sha512_final, .finup = riscv64_sha512_finup, - .descsize = sizeof(struct sha512_state), + .descsize = SHA512_STATE_SIZE, .digestsize = SHA384_DIGEST_SIZE, .base = { .cra_blocksize = SHA384_BLOCK_SIZE, .cra_priority = 300, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_name = "sha384", .cra_driver_name = "sha384-riscv64-zvknhb-zvkb", .cra_module = THIS_MODULE,
crypto/sha512_generic.c +3 -2
@@ -145,14 +145,15 @@ sha512_transform(u64 *state, const u8 *input) state[4 ] += e; state[5 ] += f; state[6 ] += g; state[7 ] += h; } -static void sha512_generic_block_fn (struct sha512_state *sst,u8 const *src, -int blocks) +void sha512_generic_block_fn (struct sha512_state *sst,u8 const *src, +int blocks) {while (blocks--) {sha512_transform (sst->state, src); src +=SHA512_BLOCK_SIZE ; } } +EXPORT_SYMBOL_GPL (sha512_generic_block_fn);int crypto_sha512_update (struct shash_desc *desc,const u8 *data,unsigned int len)
include/crypto/sha512_base.h +6 -0
@@ -10,7 +10,10 @@#include <crypto/internal/hash.h> #include <crypto/sha2.h> +#include <linux/compiler.h> +#include <linux/math.h> #include <linux/string.h> +#include <linux/types.h> #include <linux/unaligned.h> typedef void (sha512_block_fn)(struct sha512_state *sst, u8 const *src,
@@ -175,4 +178,7 @@ static inline int sha512_base_finish(struct shash_desc *desc, u8 *out) return 0; } +void sha512_generic_block_fn (struct sha512_state *sst,u8 const *src, +int blocks); +#endif /* _CRYPTO_SHA512_BASE_H */
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 47/67 crypto: sha512-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/sha512_generic.c | 45 ++++++++++++++++------------------------- include/crypto/sha2.h | 8 -------- 2 files changed, 17 insertions(+), 36 deletions(-)
crypto/sha512_generic.c +17 -28
@@ -6,16 +6,10 @@* Copyright (c) 2003 Kyle McMartin <[email protected]> */ #include <crypto/internal/hash.h> -#include <linux/kernel.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/init.h> -#include <linux/crypto.h> -#include <linux/types.h> #include <crypto/sha2.h> #include <crypto/sha512_base.h> -#include <linux/percpu.h> -#include <asm/byteorder.h> +#include <linux/kernel.h> +#include <linux/module.h> #include <linux/unaligned.h> const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE ] = {
@@ -148,45 +142,39 @@ sha512_transform(u64 *state, const u8 *input)void sha512_generic_block_fn (struct sha512_state *sst,u8 const *src,int blocks) { -while (blocks--) { +do {sha512_transform (sst->state, src); src +=SHA512_BLOCK_SIZE ; - } + }while (--blocks); }EXPORT_SYMBOL_GPL (sha512_generic_block_fn); -int crypto_sha512_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static int crypto_sha512_update (struct shash_desc *desc,const u8 *data, +unsigned int len) { -return sha512_base_do_update (desc, data, len, sha512_generic_block_fn); +return sha512_base_do_update_blocks (desc, data, len, + sha512_generic_block_fn); } -EXPORT_SYMBOL (crypto_sha512_update); -static int sha512_final (struct shash_desc *desc,u8 *hash) +static int crypto_sha512_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *hash) { -sha512_base_do_finalize (desc, sha512_generic_block_fn); +sha512_base_do_finup (desc, data, len, sha512_generic_block_fn);return sha512_base_finish (desc, hash); } -int crypto_sha512_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *hash) -{ -sha512_base_do_update (desc, data, len, sha512_generic_block_fn); -return sha512_final (desc, hash); -} -EXPORT_SYMBOL (crypto_sha512_finup); -static struct shash_alg sha512_algs[2 ] = { { .digestsize =SHA512_DIGEST_SIZE , .init = sha512_base_init, .update = crypto_sha512_update, - .final = sha512_final, .finup = crypto_sha512_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .base = { .cra_name ="sha512" , .cra_driver_name ="sha512-generic" , .cra_priority =100 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA512_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -194,13 +182,14 @@ static struct shash_alg sha512_algs[2] = { { .digestsize = SHA384_DIGEST_SIZE, .init = sha384_base_init, .update = crypto_sha512_update, - .final = sha512_final, .finup = crypto_sha512_finup, - .descsize = sizeof(struct sha512_state), + .descsize = SHA512_STATE_SIZE, .base = { .cra_name = "sha384", .cra_driver_name = "sha384-generic", .cra_priority = 100, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA384_BLOCK_SIZE, .cra_module = THIS_MODULE, }
include/crypto/sha2.h +0 -8
@@ -82,14 +82,6 @@ struct sha512_state {u8 buf[SHA512_BLOCK_SIZE ]; }; -struct shash_desc ; - -extern int crypto_sha512_update (struct shash_desc *desc,const u8 *data, -unsigned int len); - -extern int crypto_sha512_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *hash); -/* * Stand-alone implementation of the SHA256 algorithm. It is designed to * have as little dependencies as possible so it can be used in the
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 48/67 crypto: arm/sha512-neon - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm/crypto/sha512-neon-glue.c | 43 +++++++++--------------------- 1 file changed, 13 insertions(+), 30 deletions(-)
arch/arm/crypto/sha512-neon-glue.c +13 -30
@@ -5,16 +5,13 @@* Copyright (C) 2015 Linaro Ltd <[email protected]> */ +#include <asm/neon.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sha2.h> #include <crypto/sha512_base.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> -#include <asm/simd.h> -#include <asm/neon.h> -#include "sha512.h" MODULE_ALIAS_CRYPTO ("sha384-neon" );
@@ -26,51 +23,36 @@ asmlinkage void sha512_block_data_order_neon(struct sha512_state *state,static int sha512_neon_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha512_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count[0 ] %SHA512_BLOCK_SIZE ) + len <SHA512_BLOCK_SIZE ) -return sha512_arm_update (desc, data, len); +int remain;kernel_neon_begin (); -sha512_base_do_update (desc, data, len, sha512_block_data_order_neon); + remain =sha512_base_do_update_blocks (desc, data, len, + sha512_block_data_order_neon);kernel_neon_end (); - -return 0 ; +return remain; }static int sha512_neon_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) -return sha512_arm_finup (desc, data, len, out); -kernel_neon_begin (); -if (len) -sha512_base_do_update (desc, data, len, - sha512_block_data_order_neon); -sha512_base_do_finalize (desc, sha512_block_data_order_neon); +sha512_base_do_finup (desc, data, len, sha512_block_data_order_neon);kernel_neon_end (); -return sha512_base_finish (desc, out); } -static int sha512_neon_final (struct shash_desc *desc,u8 *out) -{ -return sha512_neon_finup (desc,NULL ,0 , out); -} -struct shash_alg sha512_neon_algs[] = { { .init = sha384_base_init, .update = sha512_neon_update, - .final = sha512_neon_final, .finup = sha512_neon_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .digestsize =SHA384_DIGEST_SIZE , .base = { .cra_name ="sha384" , .cra_driver_name ="sha384-neon" , .cra_priority =300 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA384_BLOCK_SIZE , .cra_module =THIS_MODULE ,
@@ -78,14 +60,15 @@ struct shash_alg sha512_neon_algs[] = { { }, { .init = sha512_base_init, .update = sha512_neon_update, - .final = sha512_neon_final, .finup = sha512_neon_finup, - .descsize = sizeof(struct sha512_state), + .descsize = SHA512_STATE_SIZE, .digestsize = SHA512_DIGEST_SIZE, .base = { .cra_name = "sha512", .cra_driver_name = "sha512-neon", .cra_priority = 300, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA512_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 49/67 crypto: arm/sha512-asm - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/arm/crypto/sha512-glue.c | 36 ++++++++++++++++------------------- arch/arm/crypto/sha512.h | 6 ------ 2 files changed, 16 insertions(+), 26 deletions(-)
arch/arm/crypto/sha512-glue.c +16 -20
@@ -5,15 +5,14 @@* Copyright (C) 2015 Linaro Ltd <[email protected]> */ +#include <asm/hwcap.h> +#include <asm/neon.h> #include <crypto/internal/hash.h> #include <crypto/sha2.h> #include <crypto/sha512_base.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> -#include <asm/hwcap.h> -#include <asm/neon.h> -#include "sha512.h" MODULE_DESCRIPTION ("Accelerated SHA-384/SHA-512 secure hash for ARM" );
@@ -28,50 +27,47 @@ MODULE_ALIAS_CRYPTO("sha512-arm"); asmlinkage void sha512_block_data_order(struct sha512_state *state,u8 const *src,int blocks); -int sha512_arm_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static int sha512_arm_update (struct shash_desc *desc,const u8 *data, +unsigned int len) { -return sha512_base_do_update (desc, data, len, sha512_block_data_order); +return sha512_base_do_update_blocks (desc, data, len, + sha512_block_data_order); } -static int sha512_arm_final (struct shash_desc *desc,u8 *out) +static int sha512_arm_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out) { -sha512_base_do_finalize (desc, sha512_block_data_order); +sha512_base_do_finup (desc, data, len, sha512_block_data_order);return sha512_base_finish (desc, out); } -int sha512_arm_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out) -{ -sha512_base_do_update (desc, data, len, sha512_block_data_order); -return sha512_arm_final (desc, out); -} -static struct shash_alg sha512_arm_algs[] = { { .init = sha384_base_init, .update = sha512_arm_update, - .final = sha512_arm_final, .finup = sha512_arm_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .digestsize =SHA384_DIGEST_SIZE , .base = { .cra_name ="sha384" , .cra_driver_name ="sha384-arm" , .cra_priority =250 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA512_BLOCK_SIZE , .cra_module =THIS_MODULE , } }, { .init = sha512_base_init, .update = sha512_arm_update, - .final = sha512_arm_final, .finup = sha512_arm_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .digestsize =SHA512_DIGEST_SIZE , .base = { .cra_name ="sha512" , .cra_driver_name ="sha512-arm" , .cra_priority =250 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA512_BLOCK_SIZE , .cra_module =THIS_MODULE , }
arch/arm/crypto/sha512.h +0 -6
@@ -1,9 +1,3 @@/* SPDX-License-Identifier: GPL-2.0 */ -int sha512_arm_update (struct shash_desc *desc,const u8 *data, -unsigned int len); - -int sha512_arm_finup (struct shash_desc *desc,const u8 *data, -unsigned int len,u8 *out); -extern struct shash_alg sha512_neon_algs[2 ];
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 50/67 crypto: arm64/sha512-ce - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/sha512-ce-glue.c | 49 ++++++++---------------------- 1 file changed, 12 insertions(+), 37 deletions(-)
arch/arm64/crypto/sha512-ce-glue.c +12 -37
@@ -10,14 +10,11 @@*/ #include <asm/neon.h> -#include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sha2.h> #include <crypto/sha512_base.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> MODULE_DESCRIPTION ("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions" );
@@ -29,12 +26,10 @@ MODULE_ALIAS_CRYPTO("sha512"); asmlinkage int __sha512_ce_transform(struct sha512_state *sst, u8 const *src,int blocks); -asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks); -static void sha512_ce_transform (struct sha512_state *sst,u8 const *src,int blocks) { -while (blocks) { + do { int rem; kernel_neon_begin();
@@ -42,67 +37,47 @@ static void sha512_ce_transform(struct sha512_state *sst, u8 const *src,kernel_neon_end (); src += (blocks - rem) *SHA512_BLOCK_SIZE ; blocks = rem; - } -} - -static void sha512_arm64_transform (struct sha512_state *sst,u8 const *src, -int blocks) -{ -sha512_block_data_order (sst->state, src, blocks); + }while (blocks); }static int sha512_ce_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -sha512_block_fn *fn =crypto_simd_usable () ? sha512_ce_transform - : sha512_arm64_transform; - -sha512_base_do_update (desc, data, len, fn); -return 0 ; +return sha512_base_do_update_blocks (desc, data, len, + sha512_ce_transform); }static int sha512_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -sha512_block_fn *fn =crypto_simd_usable () ? sha512_ce_transform - : sha512_arm64_transform; - -sha512_base_do_update (desc, data, len, fn); -sha512_base_do_finalize (desc, fn); -return sha512_base_finish (desc, out); -} - -static int sha512_ce_final (struct shash_desc *desc,u8 *out) -{ -sha512_block_fn *fn =crypto_simd_usable () ? sha512_ce_transform - : sha512_arm64_transform; - -sha512_base_do_finalize (desc, fn); +sha512_base_do_finup (desc, data, len, sha512_ce_transform);return sha512_base_finish (desc, out); }static struct shash_alg algs[] = { { .init = sha384_base_init, .update = sha512_ce_update, - .final = sha512_ce_final, .finup = sha512_ce_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .digestsize =SHA384_DIGEST_SIZE , .base.cra_name ="sha384" , .base.cra_driver_name ="sha384-ce" , .base.cra_priority =200 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA512_BLOCK_SIZE , .base.cra_module =THIS_MODULE , }, { .init = sha512_base_init, .update = sha512_ce_update, - .final = sha512_ce_final, .finup = sha512_ce_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .digestsize =SHA512_DIGEST_SIZE , .base.cra_name ="sha512" , .base.cra_driver_name ="sha512-ce" , .base.cra_priority =200 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA512_BLOCK_SIZE , .base.cra_module =THIS_MODULE , } };
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 51/67 crypto: arm/sha512 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/arm64/crypto/sha512-glue.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-)
arch/arm64/crypto/sha512-glue.c +11 -17
@@ -6,10 +6,10 @@*/ #include <crypto/internal/hash.h> -#include <linux/kernel.h> -#include <linux/module.h> #include <crypto/sha2.h> #include <crypto/sha512_base.h> +#include <linux/kernel.h> +#include <linux/module.h> MODULE_DESCRIPTION ("SHA-384/SHA-512 secure hash for arm64" );MODULE_AUTHOR ("Andy Polyakov <[email protected]>" );
@@ -20,7 +20,6 @@ MODULE_ALIAS_CRYPTO("sha512"); asmlinkage void sha512_block_data_order(u64 *digest, const void *data,unsigned int num_blks); -EXPORT_SYMBOL (sha512_block_data_order);static void sha512_arm64_transform (struct sha512_state *sst,u8 const *src,int blocks)
@@ -31,46 +30,41 @@ static void sha512_arm64_transform(struct sha512_state *sst, u8 const *src,static int sha512_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -return sha512_base_do_update (desc, data, len, sha512_arm64_transform); +return sha512_base_do_update_blocks (desc, data, len, + sha512_arm64_transform); }static int sha512_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (len) -sha512_base_do_update (desc, data, len, sha512_arm64_transform); -sha512_base_do_finalize (desc, sha512_arm64_transform); - +sha512_base_do_finup (desc, data, len, sha512_arm64_transform);return sha512_base_finish (desc, out); } -static int sha512_final (struct shash_desc *desc,u8 *out) -{ -return sha512_finup (desc,NULL ,0 , out); -} -static struct shash_alg algs[] = { { .digestsize =SHA512_DIGEST_SIZE , .init = sha512_base_init, .update = sha512_update, - .final = sha512_final, .finup = sha512_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .base.cra_name ="sha512" , .base.cra_driver_name ="sha512-arm64" , .base.cra_priority =150 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA512_BLOCK_SIZE , .base.cra_module =THIS_MODULE , }, { .digestsize =SHA384_DIGEST_SIZE , .init = sha384_base_init, .update = sha512_update, - .final = sha512_final, .finup = sha512_finup, - .descsize =sizeof (struct sha512_state ), + .descsize =SHA512_STATE_SIZE , .base.cra_name ="sha384" , .base.cra_driver_name ="sha384-arm64" , .base.cra_priority =150 , + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .base.cra_blocksize =SHA384_BLOCK_SIZE , .base.cra_module =THIS_MODULE , } };
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 52/67 crypto: s390/sha512 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/s390/crypto/sha.h | 14 ++-- arch/s390/crypto/sha512_s390.c | 45 ++++++------ arch/s390/crypto/sha_common.c | 124 ++++----------------------------- 3 files changed, 47 insertions(+), 136 deletions(-)
arch/s390/crypto/sha.h +9 -5
@@ -10,28 +10,32 @@#ifndef _CRYPTO_ARCH_S390_SHA_H#define _CRYPTO_ARCH_S390_SHA_H +#include <crypto/sha2.h> #include <crypto/sha3.h> #include <linux/types.h> /* must be big enough for the largest SHA variant */ #define CPACF_MAX_PARMBLOCK_SIZE SHA3_STATE_SIZE#define SHA_MAX_BLOCK_SIZE SHA3_224_BLOCK_SIZE -#define S390_SHA_CTX_SIZE offsetof(struct s390_sha_ctx, buf) +#define S390_SHA_CTX_SIZE sizeof(struct s390_sha_ctx)struct s390_sha_ctx {u64 count;/* message length in bytes */ -u32 state[CPACF_MAX_PARMBLOCK_SIZE /sizeof (u32)]; +union { +u32 state[CPACF_MAX_PARMBLOCK_SIZE /sizeof (u32)]; +struct { +u64 state[SHA512_DIGEST_SIZE ]; +u64 count_hi; + } sha512; + };int func;/* KIMD function to use */ bool first_message_part; -u8 buf[SHA_MAX_BLOCK_SIZE ]; };struct shash_desc ; -int s390_sha_update (struct shash_desc *desc,const u8 *data,unsigned int len);int s390_sha_update_blocks (struct shash_desc *desc,const u8 *data,unsigned int len); -int s390_sha_final (struct shash_desc *desc,u8 *out);int s390_sha_finup (struct shash_desc *desc,const u8 *src,unsigned int len,u8 *out);
arch/s390/crypto/sha512_s390.c +23 -22
106 lines not on this page · raw message
arch/s390/crypto/sha_common.c +15 -109
152 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 53/67 crypto: sparc/sha512 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/sparc/crypto/sha512_glue.c | 102 +++++--------------------------- 1 file changed, 15 insertions(+), 87 deletions(-)
arch/sparc/crypto/sha512_glue.c +15 -87
@@ -10,115 +10,43 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <asm/elf.h> +#include <asm/pstate.h> #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sha2.h> #include <crypto/sha512_base.h> - -#include <asm/pstate.h> -#include <asm/elf.h> +#include <linux/kernel.h> +#include <linux/module.h> #include "opcodes.h" asmlinkage void sha512_sparc64_transform(u64 *digest, const char *data,unsigned int rounds); -static void __sha512_sparc64_update (struct sha512_state *sctx,const u8 *data, -unsigned int len,unsigned int partial) +static void sha512_block (struct sha512_state *sctx,const u8 *src,int blocks) { -unsigned int done =0 ; - -if ((sctx->count[0 ] += len) < len) - sctx->count[1 ]++; -if (partial) { - done =SHA512_BLOCK_SIZE - partial; -memcpy (sctx->buf + partial, data, done); -sha512_sparc64_transform (sctx->state, sctx->buf,1 ); - } -if (len - done >=SHA512_BLOCK_SIZE ) { -const unsigned int rounds = (len - done) /SHA512_BLOCK_SIZE ; - -sha512_sparc64_transform (sctx->state, data + done, rounds); - done += rounds *SHA512_BLOCK_SIZE ; - } - -memcpy (sctx->buf, data + done, len - done); +sha512_sparc64_transform (sctx->state, src, blocks); }static int sha512_sparc64_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sha512_state *sctx =shash_desc_ctx (desc); -unsigned int partial = sctx->count[0 ] %SHA512_BLOCK_SIZE ; - -/* Handle the fast case right here */ -if (partial + len <SHA512_BLOCK_SIZE ) { -if ((sctx->count[0 ] += len) < len) - sctx->count[1 ]++; -memcpy (sctx->buf + partial, data, len); - }else -__sha512_sparc64_update (sctx, data, len, partial); - -return 0 ; +return sha512_base_do_update_blocks (desc, data, len, sha512_block); } -static int sha512_sparc64_final (struct shash_desc *desc,u8 *out) +static int sha512_sparc64_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) { -struct sha512_state *sctx =shash_desc_ctx (desc); -unsigned int i, index, padlen; -__be64 *dst = (__be64 *)out; -__be64 bits[2 ]; -static const u8 padding[SHA512_BLOCK_SIZE ] = {0x80 , }; - -/* Save number of bits */ - bits[1 ] =cpu_to_be64 (sctx->count[0 ] <<3 ); - bits[0 ] =cpu_to_be64 (sctx->count[1 ] <<3 | sctx->count[0 ] >>61 ); - -/* Pad out to 112 mod 128 and append length */ - index = sctx->count[0 ] %SHA512_BLOCK_SIZE ; - padlen = (index <112 ) ? (112 - index) : ((SHA512_BLOCK_SIZE +112 ) - index); - -/* We need to fill a whole block for __sha512_sparc64_update() */ -if (padlen <=112 ) { -if ((sctx->count[0 ] += padlen) < padlen) - sctx->count[1 ]++; -memcpy (sctx->buf + index, padding, padlen); - }else { -__sha512_sparc64_update (sctx, padding, padlen, index); - } -__sha512_sparc64_update (sctx, (const u8 *)&bits,sizeof (bits),112 ); - -/* Store state in digest */ -for (i =0 ; i <8 ; i++) - dst[i] =cpu_to_be64 (sctx->state[i]); - -/* Wipe context */ -memset (sctx,0 ,sizeof (*sctx)); - -return 0 ; -} - -static int sha384_sparc64_final (struct shash_desc *desc,u8 *hash) -{ -u8 D [64 ]; - -sha512_sparc64_final (desc,D ); - -memcpy (hash,D ,48 ); -memzero_explicit (D ,64 ); - -return 0 ; +sha512_base_do_finup (desc, src, len, sha512_block); +return sha512_base_finish (desc, out); }static struct shash_alg sha512 = { .digestsize =SHA512_DIGEST_SIZE , .init = sha512_base_init, .update = sha512_sparc64_update, - .final = sha512_sparc64_final, - .descsize =sizeof (struct sha512_state ), + .finup = sha512_sparc64_finup, + .descsize =SHA512_STATE_SIZE , .base = { .cra_name ="sha512" , .cra_driver_name="sha512-sparc64" ,
@@ -132,8 +60,8 @@ static struct shash_alg sha384 = { .digestsize = SHA384_DIGEST_SIZE, .init = sha384_base_init, .update = sha512_sparc64_update, - .final = sha384_sparc64_final, - .descsize = sizeof(struct sha512_state), + .finup = sha512_sparc64_finup, + .descsize = SHA512_STATE_SIZE, .base = { .cra_name = "sha384", .cra_driver_name= "sha384-sparc64",
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 54/67 crypto: sha512_base - Remove partial block helpers
In reply to Herbert XuNow that all sha256_base users have been converted to use the API partial block handling, remove the partial block helpers.
--- include/crypto/sha512_base.h | 64 ------------------------------------ 1 file changed, 64 deletions(-)
include/crypto/sha512_base.h +0 -64
@@ -53,46 +53,6 @@ static inline int sha512_base_init(struct shash_desc *desc)return 0 ; } -static inline int sha512_base_do_update (struct shash_desc *desc, -const u8 *data, -unsigned int len, -sha512_block_fn *block_fn) -{ -struct sha512_state *sctx =shash_desc_ctx (desc); -unsigned int partial = sctx->count[0 ] %SHA512_BLOCK_SIZE ; - - sctx->count[0 ] += len; -if (sctx->count[0 ] < len) - sctx->count[1 ]++; - -if (unlikely ((partial + len) >=SHA512_BLOCK_SIZE )) { -int blocks; - -if (partial) { -int p =SHA512_BLOCK_SIZE - partial; - -memcpy (sctx->buf + partial, data, p); - data += p; - len -= p; - -block_fn (sctx, sctx->buf,1 ); - } - - blocks = len /SHA512_BLOCK_SIZE ; - len %=SHA512_BLOCK_SIZE ; - -if (blocks) { -block_fn (sctx, data, blocks); - data += blocks *SHA512_BLOCK_SIZE ; - } - partial =0 ; - } -if (len) -memcpy (sctx->buf + partial, data, len); - -return 0 ; -} - static inline int sha512_base_do_update_blocks(struct shash_desc *desc, const u8 *data, unsigned int len,
@@ -142,30 +102,6 @@ static inline int sha512_base_do_finup(struct shash_desc *desc, const u8 *src,return 0 ; } -static inline int sha512_base_do_finalize (struct shash_desc *desc, -sha512_block_fn *block_fn) -{ -const int bit_offset =SHA512_BLOCK_SIZE -sizeof (__be64[2 ]); -struct sha512_state *sctx =shash_desc_ctx (desc); -__be64 *bits = (__be64 *)(sctx->buf + bit_offset); -unsigned int partial = sctx->count[0 ] %SHA512_BLOCK_SIZE ; - - sctx->buf[partial++] =0x80 ; -if (partial > bit_offset) { -memset (sctx->buf + partial,0x0 ,SHA512_BLOCK_SIZE - partial); - partial =0 ; - -block_fn (sctx, sctx->buf,1 ); - } - -memset (sctx->buf + partial,0x0 , bit_offset - partial); - bits[0 ] =cpu_to_be64 (sctx->count[1 ] <<3 | sctx->count[0 ] >>61 ); - bits[1 ] =cpu_to_be64 (sctx->count[0 ] <<3 ); -block_fn (sctx, sctx->buf,1 ); - -return 0 ; -} -static inline int sha512_base_finish (struct shash_desc *desc,u8 *out) {unsigned int digest_size =crypto_shash_digestsize (desc->tfm);
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 55/67 crypto: sm3-generic - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/sm3_generic.c | 31 +++++++-------------------- include/crypto/sm3.h | 1 + include/crypto/sm3_base.h | 45 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 25 deletions(-)
crypto/sm3_generic.c +8 -23
@@ -9,15 +9,10 @@*/ #include <crypto/internal/hash.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/mm.h> -#include <linux/types.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h> -#include <linux/bitops.h> -#include <asm/byteorder.h> -#include <linux/unaligned.h> +#include <linux/kernel.h> +#include <linux/module.h> const u8 sm3_zero_message_hash[SM3_DIGEST_SIZE ] = {0x1A ,0xB2 ,0x1D ,0x83 ,0x55 ,0xCF ,0xA1 ,0x7F ,
@@ -30,38 +25,28 @@ EXPORT_SYMBOL_GPL(sm3_zero_message_hash);static int crypto_sm3_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -sm3_update (shash_desc_ctx (desc), data, len); -return 0 ; -} - -static int crypto_sm3_final (struct shash_desc *desc,u8 *out) -{ -sm3_final (shash_desc_ctx (desc), out); -return 0 ; +return sm3_base_do_update_blocks (desc, data, len, sm3_block_generic); }static int crypto_sm3_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *hash) { -struct sm3_state *sctx =shash_desc_ctx (desc); - -if (len) -sm3_update (sctx, data, len); -sm3_final (sctx, hash); -return 0 ; +sm3_base_do_finup (desc, data, len, sm3_block_generic); +return sm3_base_finish (desc, hash); }static struct shash_alg sm3_alg = { .digestsize =SM3_DIGEST_SIZE , .init = sm3_base_init, .update = crypto_sm3_update, - .final = crypto_sm3_final, .finup = crypto_sm3_finup, - .descsize =sizeof (struct sm3_state ), + .descsize =SM3_STATE_SIZE , .base = { .cra_name ="sm3" , .cra_driver_name ="sm3-generic" , .cra_priority =100 , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SM3_BLOCK_SIZE , .cra_module =THIS_MODULE , }
include/crypto/sm3.h +1 -0
@@ -14,6 +14,7 @@#define SM3_DIGEST_SIZE 32#define SM3_BLOCK_SIZE 64 +#define SM3_STATE_SIZE 40#define SM3_T1 0x79CC4519#define SM3_T2 0x7A879D8A
include/crypto/sm3_base.h +43 -2
57 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 56/67 crypto: arm64/sm3-ce - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/sm3-ce-glue.c | 48 ++++++--------------------------- 1 file changed, 8 insertions(+), 40 deletions(-)
arch/arm64/crypto/sm3-ce-glue.c +8 -40
@@ -6,14 +6,11 @@*/ #include <asm/neon.h> -#include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h> MODULE_DESCRIPTION ("SM3 secure hash using ARMv8 Crypto Extensions" );
@@ -26,50 +23,20 @@ asmlinkage void sm3_ce_transform(struct sm3_state *sst, u8 const *src,static int sm3_ce_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -if (!crypto_simd_usable ()) { -sm3_update (shash_desc_ctx (desc), data, len); -return 0 ; - } +int remain;kernel_neon_begin (); -sm3_base_do_update (desc, data, len, sm3_ce_transform); + remain =sm3_base_do_update_blocks (desc, data, len, sm3_ce_transform);kernel_neon_end (); - -return 0 ; -} - -static int sm3_ce_final (struct shash_desc *desc,u8 *out) -{ -if (!crypto_simd_usable ()) { -sm3_final (shash_desc_ctx (desc), out); -return 0 ; - } - -kernel_neon_begin (); -sm3_base_do_finalize (desc, sm3_ce_transform); -kernel_neon_end (); - -return sm3_base_finish (desc, out); +return remain; }static int sm3_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) { -struct sm3_state *sctx =shash_desc_ctx (desc); - -if (len) -sm3_update (sctx, data, len); -sm3_final (sctx, out); -return 0 ; - } -kernel_neon_begin (); -if (len) -sm3_base_do_update (desc, data, len, sm3_ce_transform); -sm3_base_do_finalize (desc, sm3_ce_transform); +sm3_base_do_finup (desc, data, len, sm3_ce_transform);kernel_neon_end (); -return sm3_base_finish (desc, out); }
@@ -77,11 +44,12 @@ static struct shash_alg sm3_alg = { .digestsize = SM3_DIGEST_SIZE, .init = sm3_base_init, .update = sm3_ce_update, - .final = sm3_ce_final, .finup = sm3_ce_finup, - .descsize = sizeof(struct sm3_state), + .descsize = SM3_STATE_SIZE, .base.cra_name = "sm3", .base.cra_driver_name = "sm3-ce", + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .base.cra_blocksize = SM3_BLOCK_SIZE, .base.cra_module = THIS_MODULE, .base.cra_priority = 400,
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 57/67 crypto: arm64/sm3-neon - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/sm3-neon-glue.c | 48 ++++++------------------------- 1 file changed, 8 insertions(+), 40 deletions(-)
arch/arm64/crypto/sm3-neon-glue.c +8 -40
@@ -6,14 +6,11 @@*/ #include <asm/neon.h> -#include <asm/simd.h> -#include <linux/unaligned.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h> #include <linux/cpufeature.h> -#include <linux/crypto.h> +#include <linux/kernel.h> #include <linux/module.h>
@@ -23,50 +20,20 @@ asmlinkage void sm3_neon_transform(struct sm3_state *sst, u8 const *src,static int sm3_neon_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -if (!crypto_simd_usable ()) { -sm3_update (shash_desc_ctx (desc), data, len); -return 0 ; - } +int remain;kernel_neon_begin (); -sm3_base_do_update (desc, data, len, sm3_neon_transform); + remain =sm3_base_do_update_blocks (desc, data, len, sm3_neon_transform);kernel_neon_end (); - -return 0 ; -} - -static int sm3_neon_final (struct shash_desc *desc,u8 *out) -{ -if (!crypto_simd_usable ()) { -sm3_final (shash_desc_ctx (desc), out); -return 0 ; - } - -kernel_neon_begin (); -sm3_base_do_finalize (desc, sm3_neon_transform); -kernel_neon_end (); - -return sm3_base_finish (desc, out); +return remain; }static int sm3_neon_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) { -struct sm3_state *sctx =shash_desc_ctx (desc); - -if (len) -sm3_update (sctx, data, len); -sm3_final (sctx, out); -return 0 ; - } -kernel_neon_begin (); -if (len) -sm3_base_do_update (desc, data, len, sm3_neon_transform); -sm3_base_do_finalize (desc, sm3_neon_transform); +sm3_base_do_finup (desc, data, len, sm3_neon_transform);kernel_neon_end (); -return sm3_base_finish (desc, out); }
@@ -74,11 +41,12 @@ static struct shash_alg sm3_alg = { .digestsize = SM3_DIGEST_SIZE, .init = sm3_base_init, .update = sm3_neon_update, - .final = sm3_neon_final, .finup = sm3_neon_finup, - .descsize = sizeof(struct sm3_state), + .descsize = SM3_STATE_SIZE, .base.cra_name = "sm3", .base.cra_driver_name = "sm3-neon", + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .base.cra_blocksize = SM3_BLOCK_SIZE, .base.cra_module = THIS_MODULE, .base.cra_priority = 200,
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 58/67 crypto: riscv/sm3 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/riscv/crypto/sm3-riscv64-glue.c | 49 ++++++++++------------------ 1 file changed, 17 insertions(+), 32 deletions(-)
arch/riscv/crypto/sm3-riscv64-glue.c +17 -32
@@ -13,8 +13,9 @@#include <asm/vector.h> #include <crypto/internal/hash.h> #include <crypto/internal/simd.h> +#include <crypto/sm3.h> #include <crypto/sm3_base.h> -#include <linux/linkage.h> +#include <linux/kernel.h> #include <linux/module.h> /*
@@ -24,8 +25,8 @@ asmlinkage void sm3_transform_zvksh_zvkb(struct sm3_state *state,const u8 *data,int num_blocks); -static int riscv64_sm3_update (struct shash_desc *desc,const u8 *data, -unsigned int len) +static void sm3_block (struct sm3_state *state,const u8 *data, +int num_blocks) {/* * Ensure struct sm3_state begins directly with the SM3
@@ -35,52 +36,36 @@ static int riscv64_sm3_update(struct shash_desc *desc, const u8 *data,if (crypto_simd_usable ()) {kernel_vector_begin (); -sm3_base_do_update (desc, data, len, sm3_transform_zvksh_zvkb); +sm3_transform_zvksh_zvkb (state, data, num_blocks);kernel_vector_end (); }else { -sm3_update (shash_desc_ctx (desc), data, len); +sm3_block_generic (state, data, num_blocks); } -return 0 ; +} + +static int riscv64_sm3_update (struct shash_desc *desc,const u8 *data, +unsigned int len) +{ +return sm3_base_do_update_blocks (desc, data, len, sm3_block); }static int riscv64_sm3_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -struct sm3_state *ctx; - -if (crypto_simd_usable ()) { -kernel_vector_begin (); -if (len) -sm3_base_do_update (desc, data, len, - sm3_transform_zvksh_zvkb); -sm3_base_do_finalize (desc, sm3_transform_zvksh_zvkb); -kernel_vector_end (); - -return sm3_base_finish (desc, out); - } - - ctx =shash_desc_ctx (desc); -if (len) -sm3_update (ctx, data, len); -sm3_final (ctx, out); - -return 0 ; -} - -static int riscv64_sm3_final (struct shash_desc *desc,u8 *out) -{ -return riscv64_sm3_finup (desc,NULL ,0 , out); +sm3_base_do_finup (desc, data, len, sm3_block); +return sm3_base_finish (desc, out); }static struct shash_alg riscv64_sm3_alg = { .init = sm3_base_init, .update = riscv64_sm3_update, - .final = riscv64_sm3_final, .finup = riscv64_sm3_finup, - .descsize =sizeof (struct sm3_state ), + .descsize =SM3_STATE_SIZE , .digestsize =SM3_DIGEST_SIZE , .base = { .cra_blocksize =SM3_BLOCK_SIZE , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_priority =300 , .cra_name ="sm3" , .cra_driver_name ="sm3-riscv64-zvksh-zvkb" ,
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 59/67 crypto: x86/sm3 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/x86/crypto/sm3_avx_glue.c | 54 +++++++--------------------------- 1 file changed, 10 insertions(+), 44 deletions(-)
arch/x86/crypto/sm3_avx_glue.c +10 -44
@@ -10,12 +10,11 @@#include <crypto/internal/hash.h> #include <crypto/internal/simd.h> -#include <linux/init.h> -#include <linux/module.h> -#include <linux/types.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h> -#include <asm/simd.h> +#include <linux/cpufeature.h> +#include <linux/kernel.h> +#include <linux/module.h> asmlinkage void sm3_transform_avx(struct sm3_state *state,const u8 *data,int nblocks);
@@ -23,13 +22,7 @@ asmlinkage void sm3_transform_avx(struct sm3_state *state,static int sm3_avx_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -struct sm3_state *sctx =shash_desc_ctx (desc); - -if (!crypto_simd_usable () || - (sctx->count %SM3_BLOCK_SIZE ) + len <SM3_BLOCK_SIZE ) { -sm3_update (sctx, data, len); -return 0 ; - } +int remain;/* * Make sure struct sm3_state begins directly with the SM3
@@ -38,45 +31,17 @@ static int sm3_avx_update(struct shash_desc *desc, const u8 *data,BUILD_BUG_ON (offsetof(struct sm3_state , state) !=0 );kernel_fpu_begin (); -sm3_base_do_update (desc, data, len, sm3_transform_avx); + remain =sm3_base_do_update_blocks (desc, data, len, sm3_transform_avx);kernel_fpu_end (); - -return 0 ; +return remain; }static int sm3_avx_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -if (!crypto_simd_usable ()) { -struct sm3_state *sctx =shash_desc_ctx (desc); - -if (len) -sm3_update (sctx, data, len); - -sm3_final (sctx, out); -return 0 ; - } -kernel_fpu_begin (); -if (len) -sm3_base_do_update (desc, data, len, sm3_transform_avx); -sm3_base_do_finalize (desc, sm3_transform_avx); +sm3_base_do_finup (desc, data, len, sm3_transform_avx);kernel_fpu_end (); - -return sm3_base_finish (desc, out); -} - -static int sm3_avx_final (struct shash_desc *desc,u8 *out) -{ -if (!crypto_simd_usable ()) { -sm3_final (shash_desc_ctx (desc), out); -return 0 ; - } - -kernel_fpu_begin (); -sm3_base_do_finalize (desc, sm3_transform_avx); -kernel_fpu_end (); -return sm3_base_finish (desc, out); }
@@ -84,13 +49,14 @@ static struct shash_alg sm3_avx_alg = { .digestsize = SM3_DIGEST_SIZE, .init = sm3_base_init, .update = sm3_avx_update, - .final = sm3_avx_final, .finup = sm3_avx_finup, - .descsize = sizeof(struct sm3_state), + .descsize = SM3_STATE_SIZE, .base = { .cra_name = "sm3", .cra_driver_name = "sm3-avx", .cra_priority = 300, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SM3_BLOCK_SIZE, .cra_module = THIS_MODULE, }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 60/67 crypto: lib/sm3 - Remove partial block helpers
In reply to Herbert XuNow that all sm3_base users have been converted to use the API partial block handling, remove the partial block helpers as well as the lib/crypto functions.
--- include/crypto/sm3.h | 2 -- include/crypto/sm3_base.h | 64 ++---------------------------------- lib/crypto/sm3.c | 68 +++------------------------------------ 3 files changed, 6 insertions(+), 128 deletions(-)
include/crypto/sm3.h +0 -2
@@ -60,7 +60,5 @@ static inline void sm3_init(struct sm3_state *sctx) }void sm3_block_generic (struct sm3_state *sctx,u8 const *data,int blocks); -void sm3_update (struct sm3_state *sctx,const u8 *data,unsigned int len); -void sm3_final (struct sm3_state *sctx,u8 *out);#endif
include/crypto/sm3_base.h +2 -62
84 lines not on this page · raw message
lib/crypto/sm3.c +4 -64
80 lines not on this page · raw message
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 61/67 crypto: cbcmac - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/ccm.c | 59 +++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-)
crypto/ccm.c +24 -35
@@ -10,11 +10,12 @@#include <crypto/internal/hash.h> #include <crypto/internal/skcipher.h> #include <crypto/scatterwalk.h> +#include <crypto/utils.h> #include <linux/err.h> -#include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> +#include <linux/string.h> struct ccm_instance_ctx {struct crypto_skcipher_spawn ctr;
@@ -54,11 +55,6 @@ struct cbcmac_tfm_ctx {struct crypto_cipher *child; }; -struct cbcmac_desc_ctx { -unsigned int len; -u8 dg[]; -}; -static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx (struct aead_request *req) {
@@ -783,12 +779,10 @@ static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,static int crypto_cbcmac_digest_init (struct shash_desc *pdesc) { -struct cbcmac_desc_ctx *ctx =shash_desc_ctx (pdesc);int bs =crypto_shash_digestsize (pdesc->tfm); +u8 *dg =shash_desc_ctx (pdesc); - ctx->len =0 ; -memset (ctx->dg,0 , bs); - +memset (dg,0 , bs);return 0 ; }
@@ -797,39 +791,34 @@ static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p, {struct crypto_shash *parent = pdesc->tfm;struct cbcmac_tfm_ctx *tctx =crypto_shash_ctx (parent); -struct cbcmac_desc_ctx *ctx =shash_desc_ctx (pdesc);struct crypto_cipher *tfm = tctx->child;int bs =crypto_shash_digestsize (parent); +u8 *dg =shash_desc_ctx (pdesc); -while (len >0 ) { -unsigned int l =min (len, bs - ctx->len); - -crypto_xor (&ctx->dg[ctx->len], p, l); - ctx->len +=l; - len -= l; - p += l; - -if (ctx->len == bs) { -crypto_cipher_encrypt_one (tfm, ctx->dg, ctx->dg); - ctx->len =0 ; - } - } - -return 0 ; +do { +crypto_xor (dg, p, bs); +crypto_cipher_encrypt_one (tfm, dg, dg); + p += bs; + len -= bs; + }while (len >= bs); +return len; } -static int crypto_cbcmac_digest_final (struct shash_desc *pdesc,u8 *out) +static int crypto_cbcmac_digest_finup (struct shash_desc *pdesc,const u8 *src, +unsigned int len,u8 *out) {struct crypto_shash *parent = pdesc->tfm;struct cbcmac_tfm_ctx *tctx =crypto_shash_ctx (parent); -struct cbcmac_desc_ctx *ctx =shash_desc_ctx (pdesc);struct crypto_cipher *tfm = tctx->child;int bs =crypto_shash_digestsize (parent); +u8 *dg =shash_desc_ctx (pdesc); -if (ctx->len) -crypto_cipher_encrypt_one (tfm, ctx->dg, ctx->dg); - -memcpy (out, ctx->dg, bs); +if (len) { +crypto_xor (dg, src, len); +crypto_cipher_encrypt_one (tfm, out, dg); +return 0 ; + } +memcpy (out, dg, bs);return 0 ; }
@@ -886,16 +875,16 @@ static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb) inst->alg.base.cra_blocksize = alg->cra_blocksize; inst->alg.digestsize = alg->cra_blocksize; - inst->alg.descsize =sizeof (struct cbcmac_desc_ctx ) + - alg->cra_blocksize; + inst->alg.descsize = alg->cra_blocksize; + inst->alg.base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY ; inst->alg.base.cra_ctxsize =sizeof (struct cbcmac_tfm_ctx ); inst->alg.base.cra_init = cbcmac_init_tfm; inst->alg.base.cra_exit = cbcmac_exit_tfm; inst->alg.init = crypto_cbcmac_digest_init; inst->alg.update = crypto_cbcmac_digest_update; - inst->alg.final = crypto_cbcmac_digest_final; + inst->alg.finup = crypto_cbcmac_digest_finup; inst->alg.setkey = crypto_cbcmac_digest_setkey; inst->free = shash_free_singlespawn_instance;
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 62/67 crypto: cmac - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/cmac.c | 92 ++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 74 deletions(-)
crypto/cmac.c +18 -74
@@ -13,9 +13,12 @@#include <crypto/internal/cipher.h> #include <crypto/internal/hash.h> +#include <crypto/utils.h> #include <linux/err.h> #include <linux/kernel.h> #include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> /* * +------------------------
@@ -31,22 +34,6 @@ struct cmac_tfm_ctx {__be64 consts[]; }; -/* -* +------------------------ -* | <shash desc> -* +------------------------ -* | cmac_desc_ctx -* +------------------------ -* | odds (block size) -* +------------------------ -* | prev (block size) -* +------------------------ -*/ -struct cmac_desc_ctx { -unsigned int len; -u8 odds[]; -}; -static int crypto_cmac_digest_setkey (struct crypto_shash *parent,const u8 *inkey,unsigned int keylen) {
@@ -102,13 +89,10 @@ static int crypto_cmac_digest_setkey(struct crypto_shash *parent,static int crypto_cmac_digest_init (struct shash_desc *pdesc) { -struct cmac_desc_ctx *ctx =shash_desc_ctx (pdesc);int bs =crypto_shash_blocksize (pdesc->tfm); -u8 *prev = &ctx->odds[bs]; +u8 *prev =shash_desc_ctx (pdesc); - ctx->len =0 ;memset (prev,0 , bs); -return 0 ; }
@@ -117,77 +101,36 @@ static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p, {struct crypto_shash *parent = pdesc->tfm;struct cmac_tfm_ctx *tctx =crypto_shash_ctx (parent); -struct cmac_desc_ctx *ctx =shash_desc_ctx (pdesc);struct crypto_cipher *tfm = tctx->child;int bs =crypto_shash_blocksize (parent); -u8 *odds = ctx->odds; -u8 *prev = odds + bs; +u8 *prev =shash_desc_ctx (pdesc); -/* checking the data can fill the block */ -if ((ctx->len + len) <= bs) { -memcpy (odds + ctx->len, p, len); - ctx->len += len; -return 0 ; - } - -/* filling odds with new data and encrypting it */ -memcpy (odds + ctx->len, p, bs - ctx->len); - len -= bs - ctx->len; - p += bs - ctx->len; - -crypto_xor (prev, odds, bs); -crypto_cipher_encrypt_one (tfm, prev, prev); - -/* clearing the length */ - ctx->len =0 ; - -/* encrypting the rest of data */ -while (len > bs) { +do {crypto_xor (prev, p, bs);crypto_cipher_encrypt_one (tfm, prev, prev); p += bs; len -= bs; - } - -/* keeping the surplus of blocksize */ -if (len) { -memcpy (odds, p, len); - ctx->len = len; - } - -return 0 ; + }while (len >= bs); +return len; } -static int crypto_cmac_digest_final (struct shash_desc *pdesc,u8 *out) +static int crypto_cmac_digest_finup (struct shash_desc *pdesc,const u8 *src, +unsigned int len,u8 *out) {struct crypto_shash *parent = pdesc->tfm;struct cmac_tfm_ctx *tctx =crypto_shash_ctx (parent); -struct cmac_desc_ctx *ctx =shash_desc_ctx (pdesc);struct crypto_cipher *tfm = tctx->child;int bs =crypto_shash_blocksize (parent); -u8 *odds = ctx->odds; -u8 *prev = odds + bs; +u8 *prev =shash_desc_ctx (pdesc);unsigned int offset =0 ; -if (ctx->len != bs) { -unsigned int rlen; -u8 *p = odds + ctx->len; - - *p =0x80 ; - p++; - - rlen = bs - ctx->len -1 ; -if (rlen) -memset (p,0 , rlen); - +crypto_xor (prev, src, len); +if (len != bs) { + prev[len] ^=0x80 ; offset += bs; } - -crypto_xor (prev, odds, bs);crypto_xor (prev, (const u8 *)tctx->consts + offset, bs); -crypto_cipher_encrypt_one (tfm, out, prev); -return 0 ; }
@@ -269,13 +212,14 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb) inst->alg.base.cra_blocksize = alg->cra_blocksize; inst->alg.base.cra_ctxsize =sizeof (struct cmac_tfm_ctx ) + alg->cra_blocksize *2 ; + inst->alg.base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINAL_NONZERO ; inst->alg.digestsize = alg->cra_blocksize; - inst->alg.descsize =sizeof (struct cmac_desc_ctx ) + - alg->cra_blocksize *2 ; + inst->alg.descsize = alg->cra_blocksize; inst->alg.init = crypto_cmac_digest_init; inst->alg.update = crypto_cmac_digest_update; - inst->alg.final = crypto_cmac_digest_final; + inst->alg.finup = crypto_cmac_digest_finup; inst->alg.setkey = crypto_cmac_digest_setkey; inst->alg.init_tfm = cmac_init_tfm; inst->alg.clone_tfm = cmac_clone_tfm;
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 63/67 crypto: xcbc - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- crypto/xcbc.c | 92 ++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 74 deletions(-)
crypto/xcbc.c +18 -74
@@ -8,9 +8,12 @@#include <crypto/internal/cipher.h> #include <crypto/internal/hash.h> +#include <crypto/utils.h> #include <linux/err.h> #include <linux/kernel.h> #include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> static u_int32_t ks[12 ] = {0x01010101 ,0x01010101 ,0x01010101 ,0x01010101 ,0x02020202 ,0x02020202 ,0x02020202 ,0x02020202 ,
@@ -30,22 +33,6 @@ struct xcbc_tfm_ctx {u8 consts[]; }; -/* -* +------------------------ -* | <shash desc> -* +------------------------ -* | xcbc_desc_ctx -* +------------------------ -* | odds (block size) -* +------------------------ -* | prev (block size) -* +------------------------ -*/ -struct xcbc_desc_ctx { -unsigned int len; -u8 odds[]; -}; -#define XCBC_BLOCKSIZE 16 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
@@ -70,13 +57,10 @@ static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,static int crypto_xcbc_digest_init (struct shash_desc *pdesc) { -struct xcbc_desc_ctx *ctx =shash_desc_ctx (pdesc);int bs =crypto_shash_blocksize (pdesc->tfm); -u8 *prev = &ctx->odds[bs]; +u8 *prev =shash_desc_ctx (pdesc); - ctx->len =0 ;memset (prev,0 , bs); -return 0 ; }
@@ -85,77 +69,36 @@ static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p, {struct crypto_shash *parent = pdesc->tfm;struct xcbc_tfm_ctx *tctx =crypto_shash_ctx (parent); -struct xcbc_desc_ctx *ctx =shash_desc_ctx (pdesc);struct crypto_cipher *tfm = tctx->child;int bs =crypto_shash_blocksize (parent); -u8 *odds = ctx->odds; -u8 *prev = odds + bs; +u8 *prev =shash_desc_ctx (pdesc); -/* checking the data can fill the block */ -if ((ctx->len + len) <= bs) { -memcpy (odds + ctx->len, p, len); - ctx->len += len; -return 0 ; - } - -/* filling odds with new data and encrypting it */ -memcpy (odds + ctx->len, p, bs - ctx->len); - len -= bs - ctx->len; - p += bs - ctx->len; - -crypto_xor (prev, odds, bs); -crypto_cipher_encrypt_one (tfm, prev, prev); - -/* clearing the length */ - ctx->len =0 ; - -/* encrypting the rest of data */ -while (len > bs) { +do {crypto_xor (prev, p, bs);crypto_cipher_encrypt_one (tfm, prev, prev); p += bs; len -= bs; - } - -/* keeping the surplus of blocksize */ -if (len) { -memcpy (odds, p, len); - ctx->len = len; - } - -return 0 ; + }while (len >= bs); +return len; } -static int crypto_xcbc_digest_final (struct shash_desc *pdesc,u8 *out) +static int crypto_xcbc_digest_finup (struct shash_desc *pdesc,const u8 *src, +unsigned int len,u8 *out) {struct crypto_shash *parent = pdesc->tfm;struct xcbc_tfm_ctx *tctx =crypto_shash_ctx (parent); -struct xcbc_desc_ctx *ctx =shash_desc_ctx (pdesc);struct crypto_cipher *tfm = tctx->child;int bs =crypto_shash_blocksize (parent); -u8 *odds = ctx->odds; -u8 *prev = odds + bs; +u8 *prev =shash_desc_ctx (pdesc);unsigned int offset =0 ; -if (ctx->len != bs) { -unsigned int rlen; -u8 *p = odds + ctx->len; - - *p =0x80 ; - p++; - - rlen = bs - ctx->len -1 ; -if (rlen) -memset (p,0 , rlen); - +crypto_xor (prev, src, len); +if (len != bs) { + prev[len] ^=0x80 ; offset += bs; } - -crypto_xor (prev, odds, bs);crypto_xor (prev, &tctx->consts[offset], bs); -crypto_cipher_encrypt_one (tfm, out, prev); -return 0 ; }
@@ -216,17 +159,18 @@ static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) inst->alg.base.cra_blocksize = alg->cra_blocksize; inst->alg.base.cra_ctxsize =sizeof (struct xcbc_tfm_ctx ) + alg->cra_blocksize *2 ; + inst->alg.base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINAL_NONZERO ; inst->alg.digestsize = alg->cra_blocksize; - inst->alg.descsize =sizeof (struct xcbc_desc_ctx ) + - alg->cra_blocksize *2 ; + inst->alg.descsize = alg->cra_blocksize; inst->alg.base.cra_init = xcbc_init_tfm; inst->alg.base.cra_exit = xcbc_exit_tfm; inst->alg.init = crypto_xcbc_digest_init; inst->alg.update = crypto_xcbc_digest_update; - inst->alg.final = crypto_xcbc_digest_final; + inst->alg.finup = crypto_xcbc_digest_finup; inst->alg.setkey = crypto_xcbc_digest_setkey; inst->free = shash_free_singlespawn_instance;
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 64/67 crypto: arm64/aes - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also remove the unnecessary SIMD fallback path.
--- arch/arm64/crypto/aes-glue.c | 122 ++++++++++++----------------------- 1 file changed, 41 insertions(+), 81 deletions(-)
arch/arm64/crypto/aes-glue.c +41 -81
@@ -5,19 +5,20 @@* Copyright (C) 2013 - 2017 Linaro Ltd <[email protected]> */ -#include <asm/neon.h> #include <asm/hwcap.h> -#include <asm/simd.h> +#include <asm/neon.h> #include <crypto/aes.h> #include <crypto/ctr.h> -#include <crypto/sha2.h> #include <crypto/internal/hash.h> -#include <crypto/internal/simd.h> #include <crypto/internal/skcipher.h> #include <crypto/scatterwalk.h> -#include <linux/module.h> -#include <linux/cpufeature.h> +#include <crypto/sha2.h> +#include <crypto/utils.h> #include <crypto/xts.h> +#include <linux/cpufeature.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> #include "aes-ce-setkey.h"
@@ -130,7 +131,6 @@ struct mac_tfm_ctx { };struct mac_desc_ctx { -unsigned int len;u8 dg[AES_BLOCK_SIZE ]; };
@@ -869,109 +869,64 @@ static int mac_init(struct shash_desc *desc)struct mac_desc_ctx *ctx =shash_desc_ctx (desc);memset (ctx->dg,0 ,AES_BLOCK_SIZE ); - ctx->len =0 ; -return 0 ; }static void mac_do_update (struct crypto_aes_ctx *ctx,u8 const in[],int blocks, -u8 dg[],int enc_before,int enc_after) +u8 dg[],int enc_before) {int rounds =6 + ctx->key_length /4 ; +int rem; -if (crypto_simd_usable ()) { -int rem; - -do { -kernel_neon_begin (); - rem =aes_mac_update (in, ctx->key_enc, rounds, blocks, - dg, enc_before, enc_after); -kernel_neon_end (); - in += (blocks - rem) *AES_BLOCK_SIZE ; - blocks = rem; - enc_before =0 ; - }while (blocks); - }else { -if (enc_before) -aes_encrypt (ctx, dg, dg); - -while (blocks--) { -crypto_xor (dg, in,AES_BLOCK_SIZE ); - in +=AES_BLOCK_SIZE ; - -if (blocks || enc_after) -aes_encrypt (ctx, dg, dg); - } - } +do { +kernel_neon_begin (); + rem =aes_mac_update (in, ctx->key_enc, rounds, blocks, + dg, enc_before, !enc_before); +kernel_neon_end (); + in += (blocks - rem) *AES_BLOCK_SIZE ; + blocks = rem; + }while (blocks); }static int mac_update (struct shash_desc *desc,const u8 *p,unsigned int len) {struct mac_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct mac_desc_ctx *ctx =shash_desc_ctx (desc); +int blocks = len /AES_BLOCK_SIZE ; -while (len >0 ) { -unsigned int l; - -if ((ctx->len %AES_BLOCK_SIZE ) ==0 && - (ctx->len + len) >AES_BLOCK_SIZE ) { - -int blocks = len /AES_BLOCK_SIZE ; - - len %=AES_BLOCK_SIZE ; - -mac_do_update (&tctx->key, p, blocks, ctx->dg, - (ctx->len !=0 ), (len !=0 )); - - p += blocks *AES_BLOCK_SIZE ; - -if (!len) { - ctx->len =AES_BLOCK_SIZE ; -break ; - } - ctx->len =0 ; - } - - l =min (len,AES_BLOCK_SIZE - ctx->len); - -if (l <=AES_BLOCK_SIZE ) { -crypto_xor (ctx->dg + ctx->len, p, l); - ctx->len += l; - len -= l; - p += l; - } - } - -return 0 ; + len %=AES_BLOCK_SIZE ; +mac_do_update (&tctx->key, p, blocks, ctx->dg,0 ); +return len; } -static int cbcmac_final (struct shash_desc *desc,u8 *out) +static int cbcmac_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) {struct mac_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct mac_desc_ctx *ctx =shash_desc_ctx (desc); -mac_do_update (&tctx->key,NULL ,0 , ctx->dg, (ctx->len !=0 ),0 ); - +if (len) { +crypto_xor (ctx->dg, src, len); +mac_do_update (&tctx->key,NULL ,0 , ctx->dg,1 ); + }memcpy (out, ctx->dg,AES_BLOCK_SIZE ); -return 0 ; } -static int cmac_final (struct shash_desc *desc,u8 *out) +static int cmac_finup (struct shash_desc *desc,const u8 *src,unsigned int len, +u8 *out) {struct mac_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct mac_desc_ctx *ctx =shash_desc_ctx (desc);u8 *consts = tctx->consts; -if (ctx->len !=AES_BLOCK_SIZE ) { - ctx->dg[ctx->len] ^=0x80 ; +crypto_xor (ctx->dg, src, len); +if (len !=AES_BLOCK_SIZE ) { + ctx->dg[len] ^=0x80 ; consts +=AES_BLOCK_SIZE ; } - -mac_do_update (&tctx->key, consts,1 , ctx->dg,0 ,1 ); - +mac_do_update (&tctx->key, consts,1 , ctx->dg,0 );memcpy (out, ctx->dg,AES_BLOCK_SIZE ); -return 0 ; }
@@ -979,6 +934,8 @@ static struct shash_alg mac_algs[] = { { .base.cra_name = "cmac(aes)", .base.cra_driver_name = "cmac-aes-" MODE, .base.cra_priority = PRIO, + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINAL_NONZERO, .base.cra_blocksize = AES_BLOCK_SIZE, .base.cra_ctxsize =sizeof (struct mac_tfm_ctx ) + 2 * AES_BLOCK_SIZE,
@@ -987,13 +944,15 @@ static struct shash_alg mac_algs[] = { { .digestsize = AES_BLOCK_SIZE, .init = mac_init, .update = mac_update, - .final = cmac_final, + .finup = cmac_finup, .setkey = cmac_setkey, .descsize = sizeof(struct mac_desc_ctx), }, { .base.cra_name = "xcbc(aes)", .base.cra_driver_name = "xcbc-aes-" MODE, .base.cra_priority = PRIO, + .base.cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINAL_NONZERO, .base.cra_blocksize = AES_BLOCK_SIZE, .base.cra_ctxsize =sizeof (struct mac_tfm_ctx ) + 2 * AES_BLOCK_SIZE,
@@ -1002,13 +961,14 @@ static struct shash_alg mac_algs[] = { { .digestsize = AES_BLOCK_SIZE, .init = mac_init, .update = mac_update, - .final = cmac_final, + .finup = cmac_finup, .setkey = xcbc_setkey, .descsize = sizeof(struct mac_desc_ctx), }, { .base.cra_name = "cbcmac(aes)", .base.cra_driver_name = "cbcmac-aes-" MODE, .base.cra_priority = PRIO, + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .base.cra_blocksize = AES_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct mac_tfm_ctx), .base.cra_module = THIS_MODULE,
@@ -1016,7 +976,7 @@ static struct shash_alg mac_algs[] = { { .digestsize = AES_BLOCK_SIZE, .init = mac_init, .update = mac_update, - .final = cbcmac_final, + .finup = cbcmac_finup, .setkey = cbcmac_setkey, .descsize = sizeof(struct mac_desc_ctx), } };
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 65/67 crypto: arm64/sm4 - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- arch/arm64/crypto/sm4-ce-glue.c | 98 +++++++++++---------------------- 1 file changed, 31 insertions(+), 67 deletions(-)
arch/arm64/crypto/sm4-ce-glue.c +31 -67
@@ -8,19 +8,18 @@* Copyright (C) 2022 Tianjia Zhang <[email protected]> */ -#include <linux/module.h> -#include <linux/crypto.h> -#include <linux/kernel.h> -#include <linux/cpufeature.h> #include <asm/neon.h> -#include <asm/simd.h> #include <crypto/b128ops.h> -#include <crypto/internal/simd.h> -#include <crypto/internal/skcipher.h> #include <crypto/internal/hash.h> +#include <crypto/internal/skcipher.h> #include <crypto/scatterwalk.h> -#include <crypto/xts.h> #include <crypto/sm4.h> +#include <crypto/utils.h> +#include <crypto/xts.h> +#include <linux/cpufeature.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> #define BYTES2BLKS (nbytes) ((nbytes) >> 4)
@@ -64,7 +63,6 @@ struct sm4_mac_tfm_ctx { };struct sm4_mac_desc_ctx { -unsigned int len;u8 digest[SM4_BLOCK_SIZE ]; };
@@ -591,8 +589,6 @@ static int sm4_mac_init(struct shash_desc *desc)struct sm4_mac_desc_ctx *ctx =shash_desc_ctx (desc);memset (ctx->digest,0 ,SM4_BLOCK_SIZE ); - ctx->len =0 ; -return 0 ; }
@@ -601,87 +597,50 @@ static int sm4_mac_update(struct shash_desc *desc, const u8 *p, {struct sm4_mac_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct sm4_mac_desc_ctx *ctx =shash_desc_ctx (desc); -unsigned int l, nblocks; +unsigned int nblocks = len /SM4_BLOCK_SIZE ; -if (len ==0 ) -return 0 ; - -if (ctx->len || ctx->len + len <SM4_BLOCK_SIZE ) { - l =min (len,SM4_BLOCK_SIZE - ctx->len); - -crypto_xor (ctx->digest + ctx->len, p, l); - ctx->len += l; - len -= l; - p += l; - } - -if (len && (ctx->len %SM4_BLOCK_SIZE ) ==0 ) { -kernel_neon_begin (); - -if (len <SM4_BLOCK_SIZE && ctx->len ==SM4_BLOCK_SIZE ) { -sm4_ce_crypt_block (tctx->key.rkey_enc, - ctx->digest, ctx->digest); - ctx->len =0 ; - }else { - nblocks = len /SM4_BLOCK_SIZE ; - len %=SM4_BLOCK_SIZE ; - -sm4_ce_mac_update (tctx->key.rkey_enc, ctx->digest, p, - nblocks, (ctx->len ==SM4_BLOCK_SIZE ), - (len !=0 )); - - p += nblocks *SM4_BLOCK_SIZE ; - -if (len ==0 ) - ctx->len =SM4_BLOCK_SIZE ; - } - -kernel_neon_end (); - -if (len) { -crypto_xor (ctx->digest, p, len); - ctx->len = len; - } - } - -return 0 ; + len %=SM4_BLOCK_SIZE ; +kernel_neon_begin (); +sm4_ce_mac_update (tctx->key.rkey_enc, ctx->digest, p, + nblocks, false, true); +kernel_neon_end (); +return len; } -static int sm4_cmac_final (struct shash_desc *desc,u8 *out) +static int sm4_cmac_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) {struct sm4_mac_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct sm4_mac_desc_ctx *ctx =shash_desc_ctx (desc);const u8 *consts = tctx->consts; -if (ctx->len !=SM4_BLOCK_SIZE ) { - ctx->digest[ctx->len] ^=0x80 ; +crypto_xor (ctx->digest, src, len); +if (len !=SM4_BLOCK_SIZE ) { + ctx->digest[len] ^=0x80 ; consts +=SM4_BLOCK_SIZE ; } -kernel_neon_begin ();sm4_ce_mac_update (tctx->key.rkey_enc, ctx->digest, consts,1 , false, true);kernel_neon_end (); -memcpy (out, ctx->digest,SM4_BLOCK_SIZE ); -return 0 ; } -static int sm4_cbcmac_final (struct shash_desc *desc,u8 *out) +static int sm4_cbcmac_finup (struct shash_desc *desc,const u8 *src, +unsigned int len,u8 *out) {struct sm4_mac_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct sm4_mac_desc_ctx *ctx =shash_desc_ctx (desc); -if (ctx->len) { +if (len) { +crypto_xor (ctx->digest, src, len);kernel_neon_begin ();sm4_ce_crypt_block (tctx->key.rkey_enc, ctx->digest, ctx->digest);kernel_neon_end (); } -memcpy (out, ctx->digest,SM4_BLOCK_SIZE ); -return 0 ; }
@@ -691,6 +650,8 @@ static struct shash_alg sm4_mac_algs[] = { .cra_name = "cmac(sm4)", .cra_driver_name = "cmac-sm4-ce", .cra_priority = 400, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINAL_NONZERO, .cra_blocksize = SM4_BLOCK_SIZE, .cra_ctxsize =sizeof (struct sm4_mac_tfm_ctx ) + SM4_BLOCK_SIZE * 2,
@@ -699,7 +660,7 @@ static struct shash_alg sm4_mac_algs[] = { .digestsize = SM4_BLOCK_SIZE, .init = sm4_mac_init, .update = sm4_mac_update, - .final = sm4_cmac_final, + .finup = sm4_cmac_finup, .setkey = sm4_cmac_setkey, .descsize = sizeof(struct sm4_mac_desc_ctx), }, {
@@ -707,6 +668,8 @@ static struct shash_alg sm4_mac_algs[] = { .cra_name = "xcbc(sm4)", .cra_driver_name = "xcbc-sm4-ce", .cra_priority = 400, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINAL_NONZERO, .cra_blocksize = SM4_BLOCK_SIZE, .cra_ctxsize =sizeof (struct sm4_mac_tfm_ctx ) + SM4_BLOCK_SIZE * 2,
@@ -715,7 +678,7 @@ static struct shash_alg sm4_mac_algs[] = { .digestsize = SM4_BLOCK_SIZE, .init = sm4_mac_init, .update = sm4_mac_update, - .final = sm4_cmac_final, + .finup = sm4_cmac_finup, .setkey = sm4_xcbc_setkey, .descsize = sizeof(struct sm4_mac_desc_ctx), }, {
@@ -723,6 +686,7 @@ static struct shash_alg sm4_mac_algs[] = { .cra_name = "cbcmac(sm4)", .cra_driver_name = "cbcmac-sm4-ce", .cra_priority = 400, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, .cra_blocksize = SM4_BLOCK_SIZE, .cra_ctxsize = sizeof(struct sm4_mac_tfm_ctx), .cra_module = THIS_MODULE,
@@ -730,7 +694,7 @@ static struct shash_alg sm4_mac_algs[] = { .digestsize = SM4_BLOCK_SIZE, .init = sm4_mac_init, .update = sm4_mac_update, - .final = sm4_cbcmac_final, + .finup = sm4_cbcmac_finup, .setkey = sm4_cbcmac_setkey, .descsize = sizeof(struct sm4_mac_desc_ctx), }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 66/67 crypto: nx - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
Also switch to the generic export format.
--- drivers/crypto/nx/nx-aes-xcbc.c | 128 +++++++++------------------- drivers/crypto/nx/nx-sha256.c | 130 ++++++++++++----------------- drivers/crypto/nx/nx-sha512.c | 143 +++++++++++++------------------- drivers/crypto/nx/nx.c | 15 ++-- drivers/crypto/nx/nx.h | 6 +- 5 files changed, 161 insertions(+), 261 deletions(-)
drivers/crypto/nx/nx-aes-xcbc.c +40 -88
@@ -7,13 +7,14 @@* Author: Kent Yoder <[email protected]> */ -#include <crypto/internal/hash.h> #include <crypto/aes.h> -#include <crypto/algapi.h> +#include <crypto/internal/hash.h> +#include <linux/atomic.h> +#include <linux/errno.h> +#include <linux/kernel.h> #include <linux/module.h> -#include <linux/types.h> -#include <linux/crypto.h> -#include <asm/vio.h> +#include <linux/spinlock.h> +#include <linux/string.h> #include "nx_csbcpb.h" #include "nx.h"
@@ -21,8 +22,6 @@struct xcbc_state {u8 state[AES_BLOCK_SIZE ]; -unsigned int count; -u8 buffer[AES_BLOCK_SIZE ]; }; static int nx_xcbc_set_key(struct crypto_shash *desc,
@@ -58,7 +57,7 @@ static int nx_xcbc_set_key(struct crypto_shash *desc,*/ static int nx_xcbc_empty (struct shash_desc *desc,u8 *out) { -struct nx_crypto_ctx *nx_ctx =crypto_tfm_ctx (&desc->tfm->base); +struct nx_crypto_ctx *nx_ctx =crypto_shash_ctx (desc->tfm);struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;struct nx_sg *in_sg, *out_sg;u8 keys[2 ][AES_BLOCK_SIZE ];
@@ -135,9 +134,9 @@ static int nx_xcbc_empty(struct shash_desc *desc, u8 *out)return rc; } -static int nx_crypto_ctx_aes_xcbc_init2 (struct crypto_tfm *tfm) +static int nx_crypto_ctx_aes_xcbc_init2 (struct crypto_shash *tfm) { -struct nx_crypto_ctx *nx_ctx =crypto_tfm_ctx (tfm); +struct nx_crypto_ctx *nx_ctx =crypto_shash_ctx (tfm);struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;int err;
@@ -166,31 +165,24 @@ static int nx_xcbc_update(struct shash_desc *desc,const u8 *data, unsigned int len) { + struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc->tfm);struct xcbc_state *sctx =shash_desc_ctx (desc); -struct nx_crypto_ctx *nx_ctx =crypto_tfm_ctx (&desc->tfm->base);struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;struct nx_sg *in_sg;struct nx_sg *out_sg; -u32 to_process =0 , leftover, total;unsigned int max_sg_len;unsigned long irq_flags; +u32 to_process, total;int rc =0 ;int data_len;spin_lock_irqsave (&nx_ctx->lock, irq_flags); +memcpy (csbcpb->cpb.aes_xcbc.out_cv_mac, sctx->state,AES_BLOCK_SIZE ); +NX_CPB_FDM (csbcpb) |=NX_FDM_INTERMEDIATE ; +NX_CPB_FDM (csbcpb) |=NX_FDM_CONTINUATION ; - total = sctx->count + len; - -/* 2 cases for total data len: -* 1: <= AES_BLOCK_SIZE: copy into state, return 0 -* 2: > AES_BLOCK_SIZE: process X blocks, copy in leftover -*/ -if (total <=AES_BLOCK_SIZE ) { -memcpy (sctx->buffer + sctx->count, data, len); - sctx->count += len; - goto out; - } + total = len; in_sg = nx_ctx->in_sg; max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
@@ -200,7 +192,7 @@ static int nx_xcbc_update(struct shash_desc *desc, data_len =AES_BLOCK_SIZE ; out_sg =nx_build_sg_list (nx_ctx->out_sg, (u8 *)sctx->state, - &len, nx_ctx->ap->sglen); + &data_len, nx_ctx->ap->sglen);if (data_len !=AES_BLOCK_SIZE ) { rc = -EINVAL ;
@@ -210,56 +202,21 @@ static int nx_xcbc_update(struct shash_desc *desc, nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) *sizeof (struct nx_sg ); do { - to_process = total - to_process; - to_process = to_process & ~(AES_BLOCK_SIZE - 1); + to_process = total & ~(AES_BLOCK_SIZE - 1); - leftover = total - to_process; - - /* the hardware will not accept a 0 byte operation for this - * algorithm and the operation MUST be finalized to be correct. - * So if we happen to get an update that falls on a block sized - * boundary, we must save off the last block to finalize with - * later. */ - if (!leftover) { - to_process -= AES_BLOCK_SIZE; - leftover = AES_BLOCK_SIZE; - } - - if (sctx->count) { - data_len = sctx->count; - in_sg = nx_build_sg_list(nx_ctx->in_sg, - (u8 *) sctx->buffer, - &data_len, - max_sg_len); - if (data_len != sctx->count) { - rc = -EINVAL; - goto out; - } - } - - data_len = to_process - sctx->count; in_sg = nx_build_sg_list(in_sg, (u8 *) data, - &data_len, + &to_process, max_sg_len); - if (data_len != to_process - sctx->count) { - rc = -EINVAL; - goto out; - } - nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); /* we've hit the nx chip previously and we're updating again, * so copy over the partial digest */ - if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) { - memcpy(csbcpb->cpb.aes_xcbc.cv, - csbcpb->cpb.aes_xcbc.out_cv_mac, - AES_BLOCK_SIZE); - } + memcpy(csbcpb->cpb.aes_xcbc.cv, + csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE); - NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) { rc = -EINVAL; goto out;
@@ -271,28 +228,24 @@ static int nx_xcbc_update(struct shash_desc *desc,atomic_inc (&(nx_ctx->stats->aes_ops)); -/* everything after the first update is continuation */ -NX_CPB_FDM (csbcpb) |=NX_FDM_CONTINUATION ; - total -= to_process; - data += to_process - sctx->count; - sctx->count =0 ; + data += to_process; in_sg = nx_ctx->in_sg; - }while (leftover >AES_BLOCK_SIZE ); + }while (total >=AES_BLOCK_SIZE ); -/* copy the leftover back into the state struct */ -memcpy (sctx->buffer, data, leftover); - sctx->count = leftover; + rc = total; +memcpy (sctx->state, csbcpb->cpb.aes_xcbc.out_cv_mac,AES_BLOCK_SIZE ); out:spin_unlock_irqrestore (&nx_ctx->lock, irq_flags);return rc; } -static int nx_xcbc_final (struct shash_desc *desc,u8 *out) +static int nx_xcbc_finup (struct shash_desc *desc,const u8 *src, +unsigned int nbytes,u8 *out) { +struct nx_crypto_ctx *nx_ctx =crypto_shash_ctx (desc->tfm);struct xcbc_state *sctx =shash_desc_ctx (desc); -struct nx_crypto_ctx *nx_ctx =crypto_tfm_ctx (&desc->tfm->base);struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;struct nx_sg *in_sg, *out_sg;unsigned long irq_flags;
@@ -301,12 +254,10 @@ static int nx_xcbc_final(struct shash_desc *desc, u8 *out)spin_lock_irqsave (&nx_ctx->lock, irq_flags); -if (NX_CPB_FDM (csbcpb) &NX_FDM_CONTINUATION ) { -/* we've hit the nx chip previously, now we're finalizing, -* so copy over the partial digest */ -memcpy (csbcpb->cpb.aes_xcbc.cv, - csbcpb->cpb.aes_xcbc.out_cv_mac,AES_BLOCK_SIZE ); - }else if (sctx->count ==0 ) { +if (nbytes) { +/* non-zero final, so copy over the partial digest */ +memcpy (csbcpb->cpb.aes_xcbc.cv, sctx->state,AES_BLOCK_SIZE ); + }else {/* * we've never seen an update, so this is a 0 byte op. The * hardware cannot handle a 0 byte op, so just ECB to
@@ -320,11 +271,11 @@ static int nx_xcbc_final(struct shash_desc *desc, u8 *out)* this is not an intermediate operation */ NX_CPB_FDM (csbcpb) &= ~NX_FDM_INTERMEDIATE ; - len = sctx->count; - in_sg =nx_build_sg_list (nx_ctx->in_sg, (u8 *)sctx->buffer, - &len, nx_ctx->ap->sglen); + len = nbytes; + in_sg =nx_build_sg_list (nx_ctx->in_sg, (u8 *)src, &len, + nx_ctx->ap->sglen); -if (len != sctx->count) { +if (len != nbytes) { rc = -EINVAL ; goto out; }
@@ -362,18 +313,19 @@ struct shash_alg nx_shash_aes_xcbc_alg = { .digestsize = AES_BLOCK_SIZE, .init = nx_xcbc_init, .update = nx_xcbc_update, - .final = nx_xcbc_final, + .finup = nx_xcbc_finup, .setkey = nx_xcbc_set_key, .descsize = sizeof(struct xcbc_state), - .statesize = sizeof(struct xcbc_state), + .init_tfm = nx_crypto_ctx_aes_xcbc_init2, + .exit_tfm = nx_crypto_ctx_shash_exit, .base = { .cra_name = "xcbc(aes)", .cra_driver_name = "xcbc-aes-nx", .cra_priority = 300, + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINAL_NONZERO, .cra_blocksize = AES_BLOCK_SIZE, .cra_module = THIS_MODULE, .cra_ctxsize = sizeof(struct nx_crypto_ctx), - .cra_init = nx_crypto_ctx_aes_xcbc_init2, - .cra_exit = nx_crypto_ctx_exit, } };
drivers/crypto/nx/nx-sha256.c +51 -79
242 lines not on this page · raw message
drivers/crypto/nx/nx-sha512.c +57 -86
270 lines not on this page · raw message
drivers/crypto/nx/nx.c +9 -6
@@ -124,8 +124,6 @@ struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head, }if ((sg - sg_head) == sgmax) { -pr_err ("nx: scatter/gather list overflow, pid: %d\n" , - current->pid); sg++;break ; }
@@ -702,14 +700,14 @@ int nx_crypto_ctx_aes_ecb_init(struct crypto_skcipher *tfm) NX_MODE_AES_ECB); } -int nx_crypto_ctx_sha_init (struct crypto_tfm *tfm) +int nx_crypto_ctx_sha_init (struct crypto_shash *tfm) { -return nx_crypto_ctx_init (crypto_tfm_ctx (tfm),NX_FC_SHA ,NX_MODE_SHA ); +return nx_crypto_ctx_init (crypto_shash_ctx (tfm),NX_FC_SHA ,NX_MODE_SHA ); } -int nx_crypto_ctx_aes_xcbc_init (struct crypto_tfm *tfm) +int nx_crypto_ctx_aes_xcbc_init (struct crypto_shash *tfm) { -return nx_crypto_ctx_init (crypto_tfm_ctx (tfm),NX_FC_AES , +return nx_crypto_ctx_init (crypto_shash_ctx (tfm),NX_FC_AES ,NX_MODE_AES_XCBC_MAC ); }
@@ -744,6 +742,11 @@ void nx_crypto_ctx_aead_exit(struct crypto_aead *tfm)kfree_sensitive (nx_ctx->kmem); } +void nx_crypto_ctx_shash_exit (struct crypto_shash *tfm) +{ +nx_crypto_ctx_exit (crypto_shash_ctx (tfm)); +} +static int nx_probe (struct vio_dev *viodev,const struct vio_device_id *id) { dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
drivers/crypto/nx/nx.h +4 -2
@@ -3,6 +3,7 @@#ifndef __NX_H__#define __NX_H__ +#include <asm/vio.h> #include <crypto/ctr.h> #include <crypto/internal/aead.h> #include <crypto/internal/hash.h>
@@ -147,14 +148,15 @@ struct scatterlist;/* prototypes */ int nx_crypto_ctx_aes_ccm_init (struct crypto_aead *tfm);int nx_crypto_ctx_aes_gcm_init (struct crypto_aead *tfm); -int nx_crypto_ctx_aes_xcbc_init (struct crypto_tfm *tfm); +int nx_crypto_ctx_aes_xcbc_init (struct crypto_shash *tfm);int nx_crypto_ctx_aes_ctr_init (struct crypto_skcipher *tfm);int nx_crypto_ctx_aes_cbc_init (struct crypto_skcipher *tfm);int nx_crypto_ctx_aes_ecb_init (struct crypto_skcipher *tfm); -int nx_crypto_ctx_sha_init (struct crypto_tfm *tfm); +int nx_crypto_ctx_sha_init (struct crypto_shash *tfm);void nx_crypto_ctx_exit (struct crypto_tfm *tfm);void nx_crypto_ctx_skcipher_exit (struct crypto_skcipher *tfm);void nx_crypto_ctx_aead_exit (struct crypto_aead *tfm); +void nx_crypto_ctx_shash_exit (struct crypto_shash *tfm);void nx_ctx_init (struct nx_crypto_ctx *nx_ctx,unsigned int function);int nx_hcall_sync (struct nx_crypto_ctx *ctx,struct vio_pfo_op *op,u32 may_sleep);
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 67/67 crypto: padlock-sha - Use API partial block handling
In reply to Herbert XuUse the Crypto API partial block handling.
--- drivers/crypto/padlock-sha.c | 464 +++++++++++------------------------ 1 file changed, 138 insertions(+), 326 deletions(-)
drivers/crypto/padlock-sha.c +138 -326
@@ -7,59 +7,83 @@* Copyright (c) 2006 Michal Ludvig <[email protected]> */ +#include <asm/cpu_device_id.h> #include <crypto/internal/hash.h> #include <crypto/padlock.h> #include <crypto/sha1.h> #include <crypto/sha2.h> +#include <linux/cpufeature.h> #include <linux/err.h> -#include <linux/module.h> -#include <linux/init.h> -#include <linux/errno.h> -#include <linux/interrupt.h> #include <linux/kernel.h> -#include <linux/scatterlist.h> -#include <asm/cpu_device_id.h> -#include <asm/fpu/api.h> +#include <linux/module.h> -struct padlock_sha_desc { -struct shash_desc fallback; -}; +#define PADLOCK_SHA_DESCSIZE (128 + ((PADLOCK_ALIGNMENT - 1) & \ + ~(CRYPTO_MINALIGN - 1)))struct padlock_sha_ctx { -struct crypto_shash *fallback; +struct crypto_ahash *fallback; }; -static int padlock_sha_init (struct shash_desc *desc) +static inline void *padlock_shash_desc_ctx (struct shash_desc *desc) { -struct padlock_sha_desc *dctx =shash_desc_ctx (desc); -struct padlock_sha_ctx *ctx =crypto_shash_ctx (desc->tfm); +return PTR_ALIGN (shash_desc_ctx (desc),PADLOCK_ALIGNMENT ); +} - dctx->fallback.tfm = ctx->fallback; -return crypto_shash_init (&dctx->fallback); +static int padlock_sha1_init (struct shash_desc *desc) +{ +struct sha1_state *sctx =padlock_shash_desc_ctx (desc); + + *sctx = (struct sha1_state ){ + .state = {SHA1_H0 ,SHA1_H1 ,SHA1_H2 ,SHA1_H3 ,SHA1_H4 }, + }; + +return 0 ; +} + +static int padlock_sha256_init (struct shash_desc *desc) +{ +struct sha256_state *sctx =padlock_shash_desc_ctx (desc); + +sha256_init (sctx); +return 0 ; }static int padlock_sha_update (struct shash_desc *desc,const u8 *data,unsigned int length) { -struct padlock_sha_desc *dctx =shash_desc_ctx (desc); +struct padlock_sha_ctx *ctx =crypto_shash_ctx (desc->tfm); +u8 *state =padlock_shash_desc_ctx (desc); +HASH_REQUEST_ON_STACK (req, ctx->fallback); +int remain; -return crypto_shash_update (&dctx->fallback, data, length); +ahash_request_set_callback (req,0 ,NULL ,NULL ); +ahash_request_set_virt (req, data,NULL , length); + remain =crypto_ahash_import (req, state) ?: +crypto_ahash_update (req); +if (remain <0 ) +return remain; +return crypto_ahash_export (req, state) ?: remain; }static int padlock_sha_export (struct shash_desc *desc,void *out) { -struct padlock_sha_desc *dctx =shash_desc_ctx (desc); - -return crypto_shash_export (&dctx->fallback, out); +memcpy (out,padlock_shash_desc_ctx (desc), +crypto_shash_coresize (desc->tfm)); +return 0 ; }static int padlock_sha_import (struct shash_desc *desc,const void *in) { -struct padlock_sha_desc *dctx =shash_desc_ctx (desc); -struct padlock_sha_ctx *ctx =crypto_shash_ctx (desc->tfm); +unsigned int bs =crypto_shash_blocksize (desc->tfm); +unsigned int ss =crypto_shash_coresize (desc->tfm); +u64 *state =padlock_shash_desc_ctx (desc); - dctx->fallback.tfm = ctx->fallback; -return crypto_shash_import (&dctx->fallback, in); +memcpy (state, in, ss); + +/* Stop evil imports from generating a fault. */ + state[ss /8 -1 ] &= ~(bs -1 ); + +return 0 ; } static inline void padlock_output_block(uint32_t *src,
@@ -69,65 +93,38 @@ static inline void padlock_output_block(uint32_t *src, *dst++ =swab32 (*src++); } +static int padlock_sha_finup (struct shash_desc *desc,const u8 *in, +unsigned int count,u8 *out) +{ +struct padlock_sha_ctx *ctx =crypto_shash_ctx (desc->tfm); +HASH_REQUEST_ON_STACK (req, ctx->fallback); + +ahash_request_set_callback (req,0 ,NULL ,NULL ); +ahash_request_set_virt (req, in, out, count); +return crypto_ahash_import (req,padlock_shash_desc_ctx (desc)) ?: +crypto_ahash_finup (req); +} +static int padlock_sha1_finup (struct shash_desc *desc,const u8 *in,unsigned int count,u8 *out) {/* We can't store directly to *out as it may be unaligned. */ /* BTW Don't reduce the buffer size below 128 Bytes! * PadLock microcode needs it that big. */ - char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__ - ((aligned(STACK_ALIGN))); -char *result =PTR_ALIGN (&buf[0 ],PADLOCK_ALIGNMENT ); -struct padlock_sha_desc *dctx =shash_desc_ctx (desc); -struct sha1_state state; -unsigned int space; -unsigned int leftover; -int err; +struct sha1_state *state =padlock_shash_desc_ctx (desc); +u64 start = state->count; - err =crypto_shash_export (&dctx->fallback, &state); -if (err) - goto out; - -if (state.count + count >ULONG_MAX ) -return crypto_shash_finup (&dctx->fallback, in, count, out); - - leftover = ((state.count -1 ) & (SHA1_BLOCK_SIZE -1 )) +1 ; - space =SHA1_BLOCK_SIZE - leftover; -if (space) { -if (count > space) { - err =crypto_shash_update (&dctx->fallback, in, space) ?: -crypto_shash_export (&dctx->fallback, &state); -if (err) - goto out; - count -= space; - in += space; - }else { -memcpy (state.buffer + leftover, in, count); - in = state.buffer; - count += leftover; - state.count &= ~(SHA1_BLOCK_SIZE -1 ); - } - } - -memcpy (result, &state.state,SHA1_DIGEST_SIZE ); +if (start + count >ULONG_MAX ) +return padlock_sha_finup (desc, in, count, out); asmvolatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */ : \ - :"c" ((unsigned long )state.count + count), \ -"a" ((unsigned long )state.count), \ -"S" (in),"D" (result)); + :"c" ((unsigned long )start + count), \ +"a" ((unsigned long )start), \ +"S" (in),"D" (state)); -padlock_output_block ((uint32_t *)result, (uint32_t *)out,5 ); - -out: -return err; -} - -static int padlock_sha1_final (struct shash_desc *desc,u8 *out) -{ -const u8 *buf = (void *)desc; - -return padlock_sha1_finup (desc, buf,0 , out); +padlock_output_block (state->state, (uint32_t *)out,5 ); +return 0 ; } static int padlock_sha256_finup(struct shash_desc *desc, const u8 *in,
@@ -136,79 +133,41 @@ static int padlock_sha256_finup(struct shash_desc *desc, const u8 *in,/* We can't store directly to *out as it may be unaligned. */ /* BTW Don't reduce the buffer size below 128 Bytes! * PadLock microcode needs it that big. */ - char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__ - ((aligned(STACK_ALIGN))); -char *result =PTR_ALIGN (&buf[0 ],PADLOCK_ALIGNMENT ); -struct padlock_sha_desc *dctx =shash_desc_ctx (desc); -struct sha256_state state; -unsigned int space; -unsigned int leftover; -int err; +struct sha256_state *state =padlock_shash_desc_ctx (desc); +u64 start = state->count; - err =crypto_shash_export (&dctx->fallback, &state); -if (err) - goto out; - -if (state.count + count >ULONG_MAX ) -return crypto_shash_finup (&dctx->fallback, in, count, out); - - leftover = ((state.count -1 ) & (SHA256_BLOCK_SIZE -1 )) +1 ; - space =SHA256_BLOCK_SIZE - leftover; -if (space) { -if (count > space) { - err =crypto_shash_update (&dctx->fallback, in, space) ?: -crypto_shash_export (&dctx->fallback, &state); -if (err) - goto out; - count -= space; - in += space; - }else { -memcpy (state.buf + leftover, in, count); - in = state.buf; - count += leftover; - state.count &= ~(SHA1_BLOCK_SIZE -1 ); - } - } - -memcpy (result, &state.state,SHA256_DIGEST_SIZE ); +if (start + count >ULONG_MAX ) +return padlock_sha_finup (desc, in, count, out); asmvolatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */ : \ - :"c" ((unsigned long )state.count + count), \ -"a" ((unsigned long )state.count), \ -"S" (in),"D" (result)); + :"c" ((unsigned long )start + count), \ +"a" ((unsigned long )start), \ +"S" (in),"D" (state)); -padlock_output_block ((uint32_t *)result, (uint32_t *)out,8 ); - -out: -return err; -} - -static int padlock_sha256_final (struct shash_desc *desc,u8 *out) -{ -const u8 *buf = (void *)desc; - -return padlock_sha256_finup (desc, buf,0 , out); +padlock_output_block (state->state, (uint32_t *)out,8 ); +return 0 ; }static int padlock_init_tfm (struct crypto_shash *hash) {const char *fallback_driver_name =crypto_shash_alg_name (hash);struct padlock_sha_ctx *ctx =crypto_shash_ctx (hash); -struct crypto_shash *fallback_tfm; +struct crypto_ahash *fallback_tfm;/* Allocate a fallback and abort if it failed. */ - fallback_tfm =crypto_alloc_shash (fallback_driver_name,0 , -CRYPTO_ALG_NEED_FALLBACK ); + fallback_tfm =crypto_alloc_ahash (fallback_driver_name,0 , +CRYPTO_ALG_NEED_FALLBACK | +CRYPTO_ALG_ASYNC );if (IS_ERR (fallback_tfm)) { printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n", fallback_driver_name);return PTR_ERR (fallback_tfm); } -if (crypto_shash_descsize (hash) <sizeof (struct padlock_sha_desc ) + -crypto_shash_descsize (fallback_tfm)) { -crypto_free_shash (fallback_tfm); +if (crypto_shash_statesize (hash) < +crypto_ahash_statesize (fallback_tfm)) { +crypto_free_ahash (fallback_tfm);return -EINVAL ; }
@@ -221,27 +180,27 @@ static void padlock_exit_tfm(struct crypto_shash *hash) {struct padlock_sha_ctx *ctx =crypto_shash_ctx (hash); -crypto_free_shash (ctx->fallback); +crypto_free_ahash (ctx->fallback); }static struct shash_alg sha1_alg = { .digestsize =SHA1_DIGEST_SIZE , - .init = padlock_sha_init, + .init = padlock_sha1_init, .update = padlock_sha_update, .finup = padlock_sha1_finup, - .final = padlock_sha1_final, .export = padlock_sha_export, .import = padlock_sha_import, .init_tfm = padlock_init_tfm, .exit_tfm = padlock_exit_tfm, - .descsize =sizeof (struct padlock_sha_desc ) + -sizeof (struct sha1_state ), - .statesize =sizeof (struct sha1_state ), + .descsize =PADLOCK_SHA_DESCSIZE , + .statesize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-padlock" , .cra_priority =PADLOCK_CRA_PRIORITY , - .cra_flags =CRYPTO_ALG_NEED_FALLBACK , + .cra_flags =CRYPTO_ALG_NEED_FALLBACK | +CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_ctxsize =sizeof (struct padlock_sha_ctx ), .cra_module =THIS_MODULE ,
@@ -250,22 +209,22 @@ static struct shash_alg sha1_alg = {static struct shash_alg sha256_alg = { .digestsize =SHA256_DIGEST_SIZE , - .init = padlock_sha_init, + .init = padlock_sha256_init, .update = padlock_sha_update, .finup = padlock_sha256_finup, - .final = padlock_sha256_final, + .init_tfm = padlock_init_tfm, .export = padlock_sha_export, .import = padlock_sha_import, - .init_tfm = padlock_init_tfm, .exit_tfm = padlock_exit_tfm, - .descsize =sizeof (struct padlock_sha_desc ) + -sizeof (struct sha256_state ), - .statesize =sizeof (struct sha256_state ), + .descsize =PADLOCK_SHA_DESCSIZE , + .statesize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name ="sha256-padlock" , .cra_priority =PADLOCK_CRA_PRIORITY , - .cra_flags =CRYPTO_ALG_NEED_FALLBACK , + .cra_flags =CRYPTO_ALG_NEED_FALLBACK | +CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_ctxsize =sizeof (struct padlock_sha_ctx ), .cra_module =THIS_MODULE ,
@@ -274,207 +233,58 @@ static struct shash_alg sha256_alg = {/* Add two shash_alg instance for hardware-implemented * * multiple-parts hash supported by VIA Nano Processor.*/ -static int padlock_sha1_init_nano (struct shash_desc *desc) -{ -struct sha1_state *sctx =shash_desc_ctx (desc); - - *sctx = (struct sha1_state ){ - .state = {SHA1_H0 ,SHA1_H1 ,SHA1_H2 ,SHA1_H3 ,SHA1_H4 }, - }; - -return 0 ; -}static int padlock_sha1_update_nano (struct shash_desc *desc, -const u8 *data,unsigned int len) +const u8 *src,unsigned int len) { -struct sha1_state *sctx =shash_desc_ctx (desc); -unsigned int partial, done; -const u8 *src;/*The PHE require the out buffer must 128 bytes and 16-bytes aligned*/ - u8 buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__ - ((aligned(STACK_ALIGN))); -u8 *dst =PTR_ALIGN (&buf[0 ],PADLOCK_ALIGNMENT ); +struct sha1_state *state =padlock_shash_desc_ctx (desc); +int blocks = len /SHA1_BLOCK_SIZE ; - partial = sctx->count &0x3f ; - sctx->count += len; - done =0 ; - src = data; -memcpy (dst, (u8 *)(sctx->state),SHA1_DIGEST_SIZE ); + len -= blocks *SHA1_BLOCK_SIZE ; + state->count += blocks *SHA1_BLOCK_SIZE ; -if ((partial + len) >=SHA1_BLOCK_SIZE ) { - -/* Append the bytes in state's buffer to a block to handle */ -if (partial) { - done = -partial; -memcpy (sctx->buffer + partial, data, - done +SHA1_BLOCK_SIZE ); - src = sctx->buffer; - asmvolatile (".byte 0xf3,0x0f,0xa6,0xc8" - :"+S" (src),"+D" (dst) \ - :"a" ((long )-1 ),"c" ((unsigned long )1 )); - done +=SHA1_BLOCK_SIZE ; - src = data + done; - } - -/* Process the left bytes from the input data */ -if (len - done >=SHA1_BLOCK_SIZE ) { - asmvolatile (".byte 0xf3,0x0f,0xa6,0xc8" - :"+S" (src),"+D" (dst) - :"a" ((long )-1 ), -"c" ((unsigned long )((len - done) /SHA1_BLOCK_SIZE ))); - done += ((len - done) - (len - done) %SHA1_BLOCK_SIZE ); - src = data + done; - } - partial =0 ; - } -memcpy ((u8 *)(sctx->state), dst,SHA1_DIGEST_SIZE ); -memcpy (sctx->buffer + partial, src, len - done); - -return 0 ; +/* Process the left bytes from the input data */ + asmvolatile (".byte 0xf3,0x0f,0xa6,0xc8" + :"+S" (src),"+D" (state) + :"a" ((long )-1 ), +"c" ((unsigned long )blocks)); +return len; } -static int padlock_sha1_final_nano (struct shash_desc *desc,u8 *out) -{ -struct sha1_state *state = (struct sha1_state *)shash_desc_ctx (desc); -unsigned int partial, padlen; -__be64 bits; -static const u8 padding[64 ] = {0x80 , }; - - bits =cpu_to_be64 (state->count <<3 ); - -/* Pad out to 56 mod 64 */ - partial = state->count &0x3f ; - padlen = (partial <56 ) ? (56 - partial) : ((64 +56 ) - partial); -padlock_sha1_update_nano (desc, padding, padlen); - -/* Append length field bytes */ -padlock_sha1_update_nano (desc, (const u8 *)&bits,sizeof (bits)); - -/* Swap to output */ -padlock_output_block ((uint32_t *)(state->state), (uint32_t *)out,5 ); - -return 0 ; -} - -static int padlock_sha256_init_nano (struct shash_desc *desc) -{ -struct sha256_state *sctx =shash_desc_ctx (desc); - - *sctx = (struct sha256_state ){ - .state = {SHA256_H0 ,SHA256_H1 ,SHA256_H2 ,SHA256_H3 , \ -SHA256_H4 ,SHA256_H5 ,SHA256_H6 ,SHA256_H7 }, - }; - -return 0 ; -} - -static int padlock_sha256_update_nano (struct shash_desc *desc,const u8 *data, +static int padlock_sha256_update_nano (struct shash_desc *desc,const u8 *src,unsigned int len) { -struct sha256_state *sctx =shash_desc_ctx (desc); -unsigned int partial, done; -const u8 *src;/*The PHE require the out buffer must 128 bytes and 16-bytes aligned*/ - u8 buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__ - ((aligned(STACK_ALIGN))); -u8 *dst =PTR_ALIGN (&buf[0 ],PADLOCK_ALIGNMENT ); +struct crypto_sha256_state *state =padlock_shash_desc_ctx (desc); +int blocks = len /SHA256_BLOCK_SIZE ; - partial = sctx->count &0x3f ; - sctx->count += len; - done =0 ; - src = data; -memcpy (dst, (u8 *)(sctx->state),SHA256_DIGEST_SIZE ); + len -= blocks *SHA256_BLOCK_SIZE ; + state->count += blocks *SHA256_BLOCK_SIZE ; -if ((partial + len) >=SHA256_BLOCK_SIZE ) { - -/* Append the bytes in state's buffer to a block to handle */ -if (partial) { - done = -partial; -memcpy (sctx->buf + partial, data, - done +SHA256_BLOCK_SIZE ); - src = sctx->buf; - asmvolatile (".byte 0xf3,0x0f,0xa6,0xd0" - :"+S" (src),"+D" (dst) - :"a" ((long )-1 ),"c" ((unsigned long )1 )); - done +=SHA256_BLOCK_SIZE ; - src = data + done; - } - -/* Process the left bytes from input data*/ -if (len - done >=SHA256_BLOCK_SIZE ) { - asmvolatile (".byte 0xf3,0x0f,0xa6,0xd0" - :"+S" (src),"+D" (dst) - :"a" ((long )-1 ), -"c" ((unsigned long )((len - done) /64 ))); - done += ((len - done) - (len - done) %64 ); - src = data + done; - } - partial =0 ; - } -memcpy ((u8 *)(sctx->state), dst,SHA256_DIGEST_SIZE ); -memcpy (sctx->buf + partial, src, len - done); - -return 0 ; -} - -static int padlock_sha256_final_nano (struct shash_desc *desc,u8 *out) -{ -struct sha256_state *state = - (struct sha256_state *)shash_desc_ctx (desc); -unsigned int partial, padlen; -__be64 bits; -static const u8 padding[64 ] = {0x80 , }; - - bits =cpu_to_be64 (state->count <<3 ); - -/* Pad out to 56 mod 64 */ - partial = state->count &0x3f ; - padlen = (partial <56 ) ? (56 - partial) : ((64 +56 ) - partial); -padlock_sha256_update_nano (desc, padding, padlen); - -/* Append length field bytes */ -padlock_sha256_update_nano (desc, (const u8 *)&bits,sizeof (bits)); - -/* Swap to output */ -padlock_output_block ((uint32_t *)(state->state), (uint32_t *)out,8 ); - -return 0 ; -} - -static int padlock_sha_export_nano (struct shash_desc *desc, -void *out) -{ -int statesize =crypto_shash_statesize (desc->tfm); -void *sctx =shash_desc_ctx (desc); - -memcpy (out, sctx, statesize); -return 0 ; -} - -static int padlock_sha_import_nano (struct shash_desc *desc, -const void *in) -{ -int statesize =crypto_shash_statesize (desc->tfm); -void *sctx =shash_desc_ctx (desc); - -memcpy (sctx, in, statesize); -return 0 ; +/* Process the left bytes from input data*/ + asmvolatile (".byte 0xf3,0x0f,0xa6,0xd0" + :"+S" (src),"+D" (state) + :"a" ((long )-1 ), +"c" ((unsigned long )blocks)); +return len; }static struct shash_alg sha1_alg_nano = { .digestsize =SHA1_DIGEST_SIZE , - .init = padlock_sha1_init_nano, + .init = padlock_sha1_init, .update = padlock_sha1_update_nano, - .final = padlock_sha1_final_nano, - .export = padlock_sha_export_nano, - .import = padlock_sha_import_nano, - .descsize =sizeof (struct sha1_state ), - .statesize =sizeof (struct sha1_state ), + .finup = padlock_sha1_finup, + .export = padlock_sha_export, + .import = padlock_sha_import, + .descsize =PADLOCK_SHA_DESCSIZE , + .statesize =SHA1_STATE_SIZE , .base = { .cra_name ="sha1" , .cra_driver_name ="sha1-padlock-nano" , .cra_priority =PADLOCK_CRA_PRIORITY , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA1_BLOCK_SIZE , .cra_module =THIS_MODULE , }
@@ -482,17 +292,19 @@ static struct shash_alg sha1_alg_nano = {static struct shash_alg sha256_alg_nano = { .digestsize =SHA256_DIGEST_SIZE , - .init = padlock_sha256_init_nano, + .init = padlock_sha256_init, .update = padlock_sha256_update_nano, - .final = padlock_sha256_final_nano, - .export = padlock_sha_export_nano, - .import = padlock_sha_import_nano, - .descsize =sizeof (struct sha256_state ), - .statesize =sizeof (struct sha256_state ), + .finup = padlock_sha256_finup, + .export = padlock_sha_export, + .import = padlock_sha_import, + .descsize =PADLOCK_SHA_DESCSIZE , + .statesize =sizeof (struct crypto_sha256_state ), .base = { .cra_name ="sha256" , .cra_driver_name ="sha256-padlock-nano" , .cra_priority =PADLOCK_CRA_PRIORITY , + .cra_flags =CRYPTO_AHASH_ALG_BLOCK_ONLY | +CRYPTO_AHASH_ALG_FINUP_MAX , .cra_blocksize =SHA256_BLOCK_SIZE , .cra_module =THIS_MODULE , }
Signature · 1 line
2.39.5
Eric Biggers
maintainerQuoted 3 lines · Herbert Xu
This series is based on
Which links to a random message in a thread.
Going to cover letter says:
This series is based on
https://lore.kernel.org/linux-crypto/[email protected]
But that one lists no base-commit.
And this series does not apply to current cryptodev/master.
So there's no way to apply this series to review it.
I think the high-level idea is still suspect, as I said before. Especially for sha256 and sha512 which I will be fixing to have proper library APIs. I don't think it's particularly helpful to be futzing around with how those are integrated into shash when I'll be fixing it properly soon.
But whatever, as usual for your submissions this will get pushed out anyway, likely without running the tests (FYI the compression tests are already failing on cryptodev/master due to your recent changes).
- Eric
Herbert Xu
maintainerPATCH 1/1 crypto: acomp - Add missing return statements in compress/decompress
Quoted 4 lines · Eric Biggers
And this series does not apply to current cryptodev/master.
So there's no way to apply this series to review it.
Yes it's conflicting with the powerpc uaccess patch. I'll repost.
Quoted 4 lines
I think the high-level idea is still suspect, as I said before. Especially for sha256 and sha512 which I will be fixing to have proper library APIs. I don't think it's particularly helpful to be futzing around with how those are integrated into shash when I'll be fixing it properly soon.
As I said it's not that big a deal for shash, but the reason I'm doing it for shash as well as ahash is to maintain a consistent export format so that ahash can fallback to shash at any time.
I did try to keep the sha256 library interface working so as not to impede your work on that.
Quoted 3 lines
But whatever, as usual for your submissions this will get pushed out anyway, likely without running the tests (FYI the compression tests are already failing on cryptodev/master due to your recent changes).
The shash algorithm has gone through all the usual tests.
Thanks for reporting the deflate breakage. I wasn't expecting that as the change in question didn't actually touch deflate.
Cheers,
---8<---
The return statements were missing which causes REQ_CHAIN algorithms to execute twice for every request.
crypto/acompress.c +2 -2
@@ -292,7 +292,7 @@ int crypto_acomp_compress(struct acomp_req *req)if (acomp_req_on_stack (req) &&acomp_is_async (tfm))return -EAGAIN ;if (crypto_acomp_req_chain (tfm) ||acomp_request_issg (req)) -crypto_acomp_reqtfm (req)->compress (req); +return crypto_acomp_reqtfm (req)->compress (req);return acomp_do_req_chain (req, true); }EXPORT_SYMBOL_GPL (crypto_acomp_compress);
@@ -304,7 +304,7 @@ int crypto_acomp_decompress(struct acomp_req *req)if (acomp_req_on_stack (req) &&acomp_is_async (tfm))return -EAGAIN ;if (crypto_acomp_req_chain (tfm) ||acomp_request_issg (req)) -crypto_acomp_reqtfm (req)->decompress (req); +return crypto_acomp_reqtfm (req)->decompress (req);return acomp_do_req_chain (req, false); }EXPORT_SYMBOL_GPL (crypto_acomp_decompress);
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt