Skip to content

Commit

Permalink
crypto: testmgr - test setkey in no-SIMD context
Browse files Browse the repository at this point in the history
Since crypto_shash_setkey(), crypto_ahash_setkey(),
crypto_skcipher_setkey(), and crypto_aead_setkey() apparently need to
work in no-SIMD context on some architectures, make the self-tests cover
this scenario.  Specifically, sometimes do the setkey while under
crypto_disable_simd_for_test(), and do this independently from disabling
SIMD for the other parts of the crypto operation since there is no
guarantee that all parts happen in the same context.  (I.e., drivers
mustn't store the key in different formats for SIMD vs. no-SIMD.)

Signed-off-by: Eric Biggers <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
ebiggers authored and herbertx committed Jun 7, 2024
1 parent aabbf21 commit fa501bf
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions crypto/testmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ struct test_sg_division {
* the @key_offset
* @finalization_type: what finalization function to use for hashes
* @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
* This applies to the parts of the operation that aren't controlled
* individually by @nosimd_setkey or @src_divs[].nosimd.
* @nosimd_setkey: set the key (if applicable) with SIMD disabled? Requires
* !CRYPTO_TFM_REQ_MAY_SLEEP.
*/
struct testvec_config {
const char *name;
Expand All @@ -306,6 +310,7 @@ struct testvec_config {
bool key_offset_relative_to_alignmask;
enum finalization_type finalization_type;
bool nosimd;
bool nosimd_setkey;
};

#define TESTVEC_CONFIG_NAMELEN 192
Expand Down Expand Up @@ -533,7 +538,8 @@ static bool valid_testvec_config(const struct testvec_config *cfg)
cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
return false;

if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
if ((cfg->nosimd || cfg->nosimd_setkey ||
(flags & SGDIVS_HAVE_NOSIMD)) &&
(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
return false;

Expand Down Expand Up @@ -841,7 +847,10 @@ static int prepare_keybuf(const u8 *key, unsigned int ksize,
return 0;
}

/* Like setkey_f(tfm, key, ksize), but sometimes misalign the key */
/*
* Like setkey_f(tfm, key, ksize), but sometimes misalign the key.
* In addition, run the setkey function in no-SIMD context if requested.
*/
#define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask) \
({ \
const u8 *keybuf, *keyptr; \
Expand All @@ -850,7 +859,11 @@ static int prepare_keybuf(const u8 *key, unsigned int ksize,
err = prepare_keybuf((key), (ksize), (cfg), (alignmask), \
&keybuf, &keyptr); \
if (err == 0) { \
if ((cfg)->nosimd_setkey) \
crypto_disable_simd_for_test(); \
err = setkey_f((tfm), keyptr, (ksize)); \
if ((cfg)->nosimd_setkey) \
crypto_reenable_simd_for_test(); \
kfree(keybuf); \
} \
err; \
Expand Down Expand Up @@ -1118,9 +1131,15 @@ static void generate_random_testvec_config(struct rnd_state *rng,
break;
}

if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) && prandom_bool(rng)) {
cfg->nosimd = true;
p += scnprintf(p, end - p, " nosimd");
if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP)) {
if (prandom_bool(rng)) {
cfg->nosimd = true;
p += scnprintf(p, end - p, " nosimd");
}
if (prandom_bool(rng)) {
cfg->nosimd_setkey = true;
p += scnprintf(p, end - p, " nosimd_setkey");
}
}

p += scnprintf(p, end - p, " src_divs=[");
Expand Down

0 comments on commit fa501bf

Please sign in to comment.