crypto: improved skcipher, aead, and hash tests
Subsystems (5)
v2 cover crypto: improved skcipher, aead, and hash tests
Hello,
Crypto algorithms must produce the same output for the same input regardless of data layout, i.e. how the src and dst scatterlists are divided into chunks and how each chunk is aligned. Request flags such as CRYPTO_TFM_REQ_MAY_SLEEP must not affect the result either.
However, testing of this currently has many gaps. For example, individual algorithms are responsible for providing their own chunked test vectors. But many don't bother to do this or test only one or two cases, providing poor test coverage. Also, other things such as buffers spanning a page boundary, misaligned IVs, and CRYPTO_TFM_REQ_MAY_SLEEP are never tested at all.
Test code is also duplicated between the chunked and non-chunked cases, making it difficult to make other improvements.
To improve the situation, this patch series basically moves the chunk descriptions into the testmgr itself so that they are shared by all algorithms. However, it's done in an extensible way via a new struct 'testvec_config', which describes not just the scaled chunk lengths but also all other aspects of the crypto operation besides the data itself such as the buffer alignments, the request flags, whether the operation is in-place or not, the IV alignment, and for hash algorithms when to do each update() and when to use finup() vs. final() vs. digest().
Then, this patch series makes skcipher, aead, and hash algorithms be tested against a list of default testvec_configs, replacing the current test code. This improves overall test coverage, without reducing test performance too much. Note that the test vectors themselves are not changed, except for removing the chunk lists.
This series also adds randomized fuzz tests, enabled by a new kconfig option intended for developer use only, where skcipher, aead, and hash algorithms are tested against many randomly generated testvec_configs. This provides much more comprehensive test coverage.
I've run these improved tests on x86, arm32, and arm64 with all crypto algorithms enabled, and they have already found many bugs. Patches 1-7 and the patches from Ard Biesheuvel fix most of the bugs found so far. A bug was also detected in the Rockchip crypto driver which remains to be fixed. Also many AEADs incorrectly change aead_request::base.tfm, but for now I'm temporarily working around that in the tests as I plan to fix it later after the other types of bugs are addressed.
If anyone reading this has access to systems with other architectures or crypto drivers that may not have been tested yet, you can help by applying these patches on your system, enabling CONFIG_CRYPTO_MANAGER_EXTRA_TESTS, and reporting or fixing any test failures.
This patch series can also be found in git at
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git
branch "testmgr-improvements".
Changed since v1:
- Made CONFIG_CRYPTO_MANAGER_EXTRA_TESTS depend on CONFIG_DEBUG_KERNEL.
- Improved commit description of AEGIS and MORUS fixes.
- A few very minor cleanups to the test code.
Eric Biggers (15):
crypto: aegis - fix handling chunked inputs
crypto: morus - fix handling chunked inputs
crypto: x86/aegis - fix handling chunked inputs and MAY_SLEEP
crypto: x86/morus - fix handling chunked inputs and MAY_SLEEP
crypto: x86/aesni-gcm - fix crash on empty plaintext
crypto: ahash - fix another early termination in hash walk
crypto: arm64/aes-neonbs - fix returning final keystream block
crypto: testmgr - add testvec_config struct and helper functions
crypto: testmgr - introduce CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
crypto: testmgr - implement random testvec_config generation
crypto: testmgr - convert skcipher testing to use testvec_configs
crypto: testmgr - convert aead testing to use testvec_configs
crypto: testmgr - convert hash testing to use testvec_configs
crypto: testmgr - check for skcipher_request corruption
crypto: testmgr - check for aead_request corruption
arch/arm64/crypto/aes-neonbs-core.S | 8 +- arch/x86/crypto/aegis128-aesni-glue.c | 38 +- arch/x86/crypto/aegis128l-aesni-glue.c | 38 +- arch/x86/crypto/aegis256-aesni-glue.c | 38 +- arch/x86/crypto/aesni-intel_glue.c | 13 +- arch/x86/crypto/morus1280_glue.c | 40 +- arch/x86/crypto/morus640_glue.c | 39 +- crypto/Kconfig | 10 + crypto/aegis128.c | 14 +- crypto/aegis128l.c | 14 +- crypto/aegis256.c | 14 +- crypto/ahash.c | 14 +- crypto/morus1280.c | 13 +- crypto/morus640.c | 13 +- crypto/testmgr.c | 2549 +++++++++++++----------- crypto/testmgr.h | 407 +--- 16 files changed, 1556 insertions(+), 1706 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/15 crypto: aegis - fix handling chunked inputs
From: Eric Biggers <[email protected]>
The generic AEGIS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the skcipher_walk API gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end. Fix them.
--- crypto/aegis128.c | 14 +++++++------- crypto/aegis128l.c | 14 +++++++------- crypto/aegis256.c | 14 +++++++------- 3 files changed, 21 insertions(+), 21 deletions(-)
crypto/aegis128.c +7 -7
@@ -286,19 +286,19 @@ static void crypto_aegis128_process_crypt(struct aegis_state *state, const struct aegis128_ops *ops) {struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, chunksize); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }
crypto/aegis128l.c +7 -7
@@ -349,19 +349,19 @@ static void crypto_aegis128l_process_crypt(struct aegis_state *state, const struct aegis128l_ops *ops) {struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, chunksize); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }
crypto/aegis256.c +7 -7
@@ -299,19 +299,19 @@ static void crypto_aegis256_process_crypt(struct aegis_state *state, const struct aegis256_ops *ops) {struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, chunksize); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }
Signature · 1 line
2.20.1
Re: [PATCH v2 01/15] crypto: aegis - fix handling chunked inputs
Quoted 8 lines · Eric Biggers
From: Eric Biggers <[email protected]>
The generic AEGIS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the
skcipher_walkAPI gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end. Fix them.
Quoted 4 lines
Fixes f606a88e5823 ("crypto: aegis - Add generic AEGIS AEAD implementations")Cc <[email protected]> # v4.18+Cc Ondrej Mosnacek <[email protected]>Signed-off-by Eric Biggers <[email protected]>
Quoted 102 lines
--- crypto/aegis128.c | 14 +++++++------- crypto/aegis128l.c | 14 +++++++------- crypto/aegis256.c | 14 +++++++------- 3 files changed, 21 insertions(+), 21 deletions(-)crypto/aegis128.c +7
-7@@ -286,19 +286,19 @@ static void crypto_aegis128_process_crypt(struct aegis_state *state, const struct aegis128_ops *ops) {struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, chunksize); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }crypto/aegis128l.c +7
-7@@ -349,19 +349,19 @@ static void crypto_aegis128l_process_crypt(struct aegis_state *state, const struct aegis128l_ops *ops) {struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, chunksize); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }crypto/aegis256.c +7
-8@@ -299,19 +299,19 @@ static void crypto_aegis256_process_crypt(struct aegis_state *state, const struct aegis256_ops *ops) {struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, chunksize); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } } --2.20.1
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
PATCH v2 2/15 crypto: morus - fix handling chunked inputs
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The generic MORUS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the skcipher_walk API gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end. Fix them.
--- crypto/morus1280.c | 13 +++++++------ crypto/morus640.c | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-)
crypto/morus1280.c +7 -6
@@ -362,18 +362,19 @@ static void crypto_morus1280_process_crypt(struct morus1280_state *state, const struct morus1280_ops *ops) {struct skcipher_walk walk; -u8 *dst; -const u8 *src; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, walk.nbytes); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }
crypto/morus640.c +7 -6
@@ -361,18 +361,19 @@ static void crypto_morus640_process_crypt(struct morus640_state *state, const struct morus640_ops *ops) {struct skcipher_walk walk; -u8 *dst; -const u8 *src; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, walk.nbytes); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }
Signature · 1 line
2.20.1
Re: [PATCH v2 02/15] crypto: morus - fix handling chunked inputs
Quoted 8 lines · Eric Biggers
From: Eric Biggers <[email protected]>
The generic MORUS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the
skcipher_walkAPI gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end. Fix them.
Quoted 4 lines
Fixes 396be41f16fd ("crypto: morus - Add generic MORUS AEAD implementations")Cc <[email protected]> # v4.18+Cc Ondrej Mosnacek <[email protected]>Signed-off-by Eric Biggers <[email protected]>
Quoted 66 lines
--- crypto/morus1280.c | 13 +++++++------ crypto/morus640.c | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-)crypto/morus1280.c +7
-6@@ -362,18 +362,19 @@ static void crypto_morus1280_process_crypt(struct morus1280_state *state, const struct morus1280_ops *ops) {struct skcipher_walk walk; -u8 *dst; -const u8 *src; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, walk.nbytes); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } }crypto/morus640.c +7
-7@@ -361,18 +361,19 @@ static void crypto_morus640_process_crypt(struct morus640_state *state, const struct morus640_ops *ops) {struct skcipher_walk walk; -u8 *dst; -const u8 *src; ops->skcipher_walk_init (&walk, req, false);while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; +unsigned int nbytes = walk.nbytes; - ops->crypt_chunk (state, dst, src, walk.nbytes); +if (nbytes < walk.total) + nbytes =round_down (nbytes, walk.stride); -skcipher_walk_done (&walk,0 ); + ops->crypt_chunk (state, walk.dst.virt.addr, walk.src.virt.addr, + nbytes); + +skcipher_walk_done (&walk, walk.nbytes - nbytes); } } --
Quoted 2 lines
2.20.1
--
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
PATCH v2 3/15 crypto: x86/aegis - fix handling chunked inputs and MAY_SLEEP
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The x86 AEGIS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the skcipher_walk API gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end.
Also, when the CRYPTO_TFM_REQ_MAY_SLEEP flag is given, they can incorrectly sleep in the skcipher_walk_*() functions while preemption has been disabled by kernel_fpu_begin().
Fix these bugs.
--- arch/x86/crypto/aegis128-aesni-glue.c | 38 ++++++++++---------------- arch/x86/crypto/aegis128l-aesni-glue.c | 38 ++++++++++---------------- arch/x86/crypto/aegis256-aesni-glue.c | 38 ++++++++++---------------- 3 files changed, 45 insertions(+), 69 deletions(-)
arch/x86/crypto/aegis128-aesni-glue.c +15 -23
@@ -119,31 +119,20 @@ static void crypto_aegis128_aesni_process_ad( }static void crypto_aegis128_aesni_process_crypt ( -struct aegis_state *state,struct aead_request *req, +struct aegis_state *state,struct skcipher_walk *walk,const struct aegis_crypt_ops *ops) { -struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize, base; - - ops->skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops->crypt_blocks (state, chunksize, src, dst); - - base = chunksize & ~(AEGIS128_BLOCK_SIZE -1 ); - src += base; - dst += base; - chunksize &=AEGIS128_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops->crypt_tail (state, chunksize, src, dst); +while (walk->nbytes >=AEGIS128_BLOCK_SIZE ) { + ops->crypt_blocks (state, +round_down (walk->nbytes,AEGIS128_BLOCK_SIZE ), + walk->src.virt.addr, walk->dst.virt.addr); +skcipher_walk_done (walk, walk->nbytes %AEGIS128_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops->crypt_tail (state, walk->nbytes, walk->src.virt.addr, + walk->dst.virt.addr); +skcipher_walk_done (walk,0 ); } }
@@ -186,13 +175,16 @@ static void crypto_aegis128_aesni_crypt(struct aead_request *req, {struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct aegis_ctx *ctx =crypto_aegis128_aesni_ctx (tfm); +struct skcipher_walk walk;struct aegis_state state; + ops->skcipher_walk_init (&walk, req, true); +kernel_fpu_begin ();crypto_aegis128_aesni_init (&state, ctx->key.bytes, req->iv);crypto_aegis128_aesni_process_ad (&state, req->src, req->assoclen); -crypto_aegis128_aesni_process_crypt (&state, req, ops); +crypto_aegis128_aesni_process_crypt (&state, &walk, ops);crypto_aegis128_aesni_final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();
arch/x86/crypto/aegis128l-aesni-glue.c +15 -23
@@ -119,31 +119,20 @@ static void crypto_aegis128l_aesni_process_ad( }static void crypto_aegis128l_aesni_process_crypt ( -struct aegis_state *state,struct aead_request *req, +struct aegis_state *state,struct skcipher_walk *walk,const struct aegis_crypt_ops *ops) { -struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize, base; - - ops->skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops->crypt_blocks (state, chunksize, src, dst); - - base = chunksize & ~(AEGIS128L_BLOCK_SIZE -1 ); - src += base; - dst += base; - chunksize &=AEGIS128L_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops->crypt_tail (state, chunksize, src, dst); +while (walk->nbytes >=AEGIS128L_BLOCK_SIZE ) { + ops->crypt_blocks (state,round_down (walk->nbytes, +AEGIS128L_BLOCK_SIZE ), + walk->src.virt.addr, walk->dst.virt.addr); +skcipher_walk_done (walk, walk->nbytes %AEGIS128L_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops->crypt_tail (state, walk->nbytes, walk->src.virt.addr, + walk->dst.virt.addr); +skcipher_walk_done (walk,0 ); } }
@@ -186,13 +175,16 @@ static void crypto_aegis128l_aesni_crypt(struct aead_request *req, {struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct aegis_ctx *ctx =crypto_aegis128l_aesni_ctx (tfm); +struct skcipher_walk walk;struct aegis_state state; + ops->skcipher_walk_init (&walk, req, true); +kernel_fpu_begin ();crypto_aegis128l_aesni_init (&state, ctx->key.bytes, req->iv);crypto_aegis128l_aesni_process_ad (&state, req->src, req->assoclen); -crypto_aegis128l_aesni_process_crypt (&state, req, ops); +crypto_aegis128l_aesni_process_crypt (&state, &walk, ops);crypto_aegis128l_aesni_final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();
arch/x86/crypto/aegis256-aesni-glue.c +15 -23
@@ -119,31 +119,20 @@ static void crypto_aegis256_aesni_process_ad( }static void crypto_aegis256_aesni_process_crypt ( -struct aegis_state *state,struct aead_request *req, +struct aegis_state *state,struct skcipher_walk *walk,const struct aegis_crypt_ops *ops) { -struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize, base; - - ops->skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops->crypt_blocks (state, chunksize, src, dst); - - base = chunksize & ~(AEGIS256_BLOCK_SIZE -1 ); - src += base; - dst += base; - chunksize &=AEGIS256_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops->crypt_tail (state, chunksize, src, dst); +while (walk->nbytes >=AEGIS256_BLOCK_SIZE ) { + ops->crypt_blocks (state, +round_down (walk->nbytes,AEGIS256_BLOCK_SIZE ), + walk->src.virt.addr, walk->dst.virt.addr); +skcipher_walk_done (walk, walk->nbytes %AEGIS256_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops->crypt_tail (state, walk->nbytes, walk->src.virt.addr, + walk->dst.virt.addr); +skcipher_walk_done (walk,0 ); } }
@@ -186,13 +175,16 @@ static void crypto_aegis256_aesni_crypt(struct aead_request *req, {struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct aegis_ctx *ctx =crypto_aegis256_aesni_ctx (tfm); +struct skcipher_walk walk;struct aegis_state state; + ops->skcipher_walk_init (&walk, req, true); +kernel_fpu_begin ();crypto_aegis256_aesni_init (&state, ctx->key, req->iv);crypto_aegis256_aesni_process_ad (&state, req->src, req->assoclen); -crypto_aegis256_aesni_process_crypt (&state, req, ops); +crypto_aegis256_aesni_process_crypt (&state, &walk, ops);crypto_aegis256_aesni_final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();
Signature · 1 line
2.20.1
Re: [PATCH v2 03/15] crypto: x86/aegis - fix handling chunked inputs and MAY_SLEEP
Quoted 12 lines · Eric Biggers
From: Eric Biggers <[email protected]>
The x86 AEGIS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the
skcipher_walkAPI gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end.Also, when the
CRYPTO_TFM_REQ_MAY_SLEEPflag is given, they can incorrectly sleep in the skcipher_walk_*() functions while preemption has been disabled bykernel_fpu_begin().
Quoted 6 lines
Fix these bugs.
Fixes 1d373d4e8e15 ("crypto: x86 - Add optimized AEGIS implementations")Cc <[email protected]> # v4.18+Cc Ondrej Mosnacek <[email protected]>Signed-off-by Eric Biggers <[email protected]>
Quoted 204 lines
--- arch/x86/crypto/aegis128-aesni-glue.c | 38 ++++++++++---------------- arch/x86/crypto/aegis128l-aesni-glue.c | 38 ++++++++++---------------- arch/x86/crypto/aegis256-aesni-glue.c | 38 ++++++++++---------------- 3 files changed, 45 insertions(+), 69 deletions(-)arch/x86/crypto/aegis128-aesni-glue.c +15
-23@@ -119,31 +119,20 @@ static void crypto_aegis128_aesni_process_ad( }static void crypto_aegis128_aesni_process_crypt ( -struct aegis_state *state,struct aead_request *req, +struct aegis_state *state,struct skcipher_walk *walk,const struct aegis_crypt_ops *ops) { -struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize, base; - - ops->skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops->crypt_blocks (state, chunksize, src, dst); - - base = chunksize & ~(AEGIS128_BLOCK_SIZE -1 ); - src += base; - dst += base; - chunksize &=AEGIS128_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops->crypt_tail (state, chunksize, src, dst); +while (walk->nbytes >=AEGIS128_BLOCK_SIZE ) { + ops->crypt_blocks (state, +round_down (walk->nbytes,AEGIS128_BLOCK_SIZE ), + walk->src.virt.addr, walk->dst.virt.addr); +skcipher_walk_done (walk, walk->nbytes %AEGIS128_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops->crypt_tail (state, walk->nbytes, walk->src.virt.addr, + walk->dst.virt.addr); +skcipher_walk_done (walk,0 ); } }@@ -186,13 +175,16 @@ static void crypto_aegis128_aesni_crypt(struct aead_request *req, {struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct aegis_ctx *ctx =crypto_aegis128_aesni_ctx (tfm); +struct skcipher_walk walk;struct aegis_state state; + ops->skcipher_walk_init (&walk, req, true); +kernel_fpu_begin ();crypto_aegis128_aesni_init (&state, ctx->key.bytes, req->iv);crypto_aegis128_aesni_process_ad (&state, req->src, req->assoclen); -crypto_aegis128_aesni_process_crypt (&state, req, ops); +crypto_aegis128_aesni_process_crypt (&state, &walk, ops);crypto_aegis128_aesni_final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();arch/x86/crypto/aegis128l-aesni-glue.c +15
-23@@ -119,31 +119,20 @@ static void crypto_aegis128l_aesni_process_ad( }static void crypto_aegis128l_aesni_process_crypt ( -struct aegis_state *state,struct aead_request *req, +struct aegis_state *state,struct skcipher_walk *walk,const struct aegis_crypt_ops *ops) { -struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize, base; - - ops->skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops->crypt_blocks (state, chunksize, src, dst); - - base = chunksize & ~(AEGIS128L_BLOCK_SIZE -1 ); - src += base; - dst += base; - chunksize &=AEGIS128L_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops->crypt_tail (state, chunksize, src, dst); +while (walk->nbytes >=AEGIS128L_BLOCK_SIZE ) { + ops->crypt_blocks (state,round_down (walk->nbytes, +AEGIS128L_BLOCK_SIZE ), + walk->src.virt.addr, walk->dst.virt.addr); +skcipher_walk_done (walk, walk->nbytes %AEGIS128L_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops->crypt_tail (state, walk->nbytes, walk->src.virt.addr, + walk->dst.virt.addr); +skcipher_walk_done (walk,0 ); } }@@ -186,13 +175,16 @@ static void crypto_aegis128l_aesni_crypt(struct aead_request *req, {struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct aegis_ctx *ctx =crypto_aegis128l_aesni_ctx (tfm); +struct skcipher_walk walk;struct aegis_state state; + ops->skcipher_walk_init (&walk, req, true); +kernel_fpu_begin ();crypto_aegis128l_aesni_init (&state, ctx->key.bytes, req->iv);crypto_aegis128l_aesni_process_ad (&state, req->src, req->assoclen); -crypto_aegis128l_aesni_process_crypt (&state, req, ops); +crypto_aegis128l_aesni_process_crypt (&state, &walk, ops);crypto_aegis128l_aesni_final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();arch/x86/crypto/aegis256-aesni-glue.c +15
-24@@ -119,31 +119,20 @@ static void crypto_aegis256_aesni_process_ad( }static void crypto_aegis256_aesni_process_crypt ( -struct aegis_state *state,struct aead_request *req, +struct aegis_state *state,struct skcipher_walk *walk,const struct aegis_crypt_ops *ops) { -struct skcipher_walk walk; -u8 *src, *dst; -unsigned int chunksize, base; - - ops->skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - src = walk.src.virt.addr; - dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops->crypt_blocks (state, chunksize, src, dst); - - base = chunksize & ~(AEGIS256_BLOCK_SIZE -1 ); - src += base; - dst += base; - chunksize &=AEGIS256_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops->crypt_tail (state, chunksize, src, dst); +while (walk->nbytes >=AEGIS256_BLOCK_SIZE ) { + ops->crypt_blocks (state, +round_down (walk->nbytes,AEGIS256_BLOCK_SIZE ), + walk->src.virt.addr, walk->dst.virt.addr); +skcipher_walk_done (walk, walk->nbytes %AEGIS256_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops->crypt_tail (state, walk->nbytes, walk->src.virt.addr, + walk->dst.virt.addr); +skcipher_walk_done (walk,0 ); } }@@ -186,13 +175,16 @@ static void crypto_aegis256_aesni_crypt(struct aead_request *req, {struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct aegis_ctx *ctx =crypto_aegis256_aesni_ctx (tfm); +struct skcipher_walk walk;struct aegis_state state; + ops->skcipher_walk_init (&walk, req, true); +kernel_fpu_begin ();crypto_aegis256_aesni_init (&state, ctx->key, req->iv);crypto_aegis256_aesni_process_ad (&state, req->src, req->assoclen); -crypto_aegis256_aesni_process_crypt (&state, req, ops); +crypto_aegis256_aesni_process_crypt (&state, &walk, ops);crypto_aegis256_aesni_final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end (); --2.20.1
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
PATCH v2 4/15 crypto: x86/morus - fix handling chunked inputs and MAY_SLEEP
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The x86 MORUS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the skcipher_walk API gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end.
Also, when the CRYPTO_TFM_REQ_MAY_SLEEP flag is given, they can incorrectly sleep in the skcipher_walk_*() functions while preemption has been disabled by kernel_fpu_begin().
Fix these bugs.
--- arch/x86/crypto/morus1280_glue.c | 40 +++++++++++++------------------- arch/x86/crypto/morus640_glue.c | 39 ++++++++++++------------------- 2 files changed, 31 insertions(+), 48 deletions(-)
arch/x86/crypto/morus1280_glue.c +16 -24
@@ -85,31 +85,20 @@ static void crypto_morus1280_glue_process_ad(static void crypto_morus1280_glue_process_crypt (struct morus1280_state *state,struct morus1280_ops ops, -struct aead_request *req) +struct skcipher_walk *walk) { -struct skcipher_walk walk; -u8 *cursor_src, *cursor_dst; -unsigned int chunksize, base; - - ops.skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - cursor_src = walk.src.virt.addr; - cursor_dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops.crypt_blocks (state, cursor_src, cursor_dst, chunksize); - - base = chunksize & ~(MORUS1280_BLOCK_SIZE -1 ); - cursor_src += base; - cursor_dst += base; - chunksize &=MORUS1280_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops.crypt_tail (state, cursor_src, cursor_dst, - chunksize); +while (walk->nbytes >=MORUS1280_BLOCK_SIZE ) { + ops.crypt_blocks (state, walk->src.virt.addr, + walk->dst.virt.addr, +round_down (walk->nbytes, +MORUS1280_BLOCK_SIZE )); +skcipher_walk_done (walk, walk->nbytes %MORUS1280_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops.crypt_tail (state, walk->src.virt.addr, walk->dst.virt.addr, + walk->nbytes); +skcipher_walk_done (walk,0 ); } }
@@ -147,12 +136,15 @@ static void crypto_morus1280_glue_crypt(struct aead_request *req,struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct morus1280_ctx *ctx =crypto_aead_ctx (tfm);struct morus1280_state state; +struct skcipher_walk walk; + + ops.skcipher_walk_init (&walk, req, true);kernel_fpu_begin (); ctx->ops->init (&state, &ctx->key, req->iv);crypto_morus1280_glue_process_ad (&state, ctx->ops, req->src, req->assoclen); -crypto_morus1280_glue_process_crypt (&state, ops, req); +crypto_morus1280_glue_process_crypt (&state, ops, &walk); ctx->ops->final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();
arch/x86/crypto/morus640_glue.c +15 -24
@@ -85,31 +85,19 @@ static void crypto_morus640_glue_process_ad(static void crypto_morus640_glue_process_crypt (struct morus640_state *state,struct morus640_ops ops, -struct aead_request *req) +struct skcipher_walk *walk) { -struct skcipher_walk walk; -u8 *cursor_src, *cursor_dst; -unsigned int chunksize, base; - - ops.skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - cursor_src = walk.src.virt.addr; - cursor_dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops.crypt_blocks (state, cursor_src, cursor_dst, chunksize); - - base = chunksize & ~(MORUS640_BLOCK_SIZE -1 ); - cursor_src += base; - cursor_dst += base; - chunksize &=MORUS640_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops.crypt_tail (state, cursor_src, cursor_dst, - chunksize); +while (walk->nbytes >=MORUS640_BLOCK_SIZE ) { + ops.crypt_blocks (state, walk->src.virt.addr, + walk->dst.virt.addr, +round_down (walk->nbytes,MORUS640_BLOCK_SIZE )); +skcipher_walk_done (walk, walk->nbytes %MORUS640_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops.crypt_tail (state, walk->src.virt.addr, walk->dst.virt.addr, + walk->nbytes); +skcipher_walk_done (walk,0 ); } }
@@ -143,12 +131,15 @@ static void crypto_morus640_glue_crypt(struct aead_request *req,struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct morus640_ctx *ctx =crypto_aead_ctx (tfm);struct morus640_state state; +struct skcipher_walk walk; + + ops.skcipher_walk_init (&walk, req, true);kernel_fpu_begin (); ctx->ops->init (&state, &ctx->key, req->iv);crypto_morus640_glue_process_ad (&state, ctx->ops, req->src, req->assoclen); -crypto_morus640_glue_process_crypt (&state, ops, req); +crypto_morus640_glue_process_crypt (&state, ops, &walk); ctx->ops->final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();
Signature · 1 line
2.20.1
Re: [PATCH v2 04/15] crypto: x86/morus - fix handling chunked inputs and MAY_SLEEP
Quoted 12 lines · Eric Biggers
From: Eric Biggers <[email protected]>
The x86 MORUS implementations all fail the improved AEAD tests because they produce the wrong result with some data layouts. The issue is that they assume that if the
skcipher_walkAPI gives 'nbytes' not aligned to the walksize (a.k.a. walk.stride), then it is the end of the data. In fact, this can happen before the end.Also, when the
CRYPTO_TFM_REQ_MAY_SLEEPflag is given, they can incorrectly sleep in the skcipher_walk_*() functions while preemption has been disabled bykernel_fpu_begin().
Quoted 6 lines
Fix these bugs.
Fixes 56e8e57fc3a7 ("crypto: morus - Add common SIMD glue code for MORUS")Cc <[email protected]> # v4.18+Cc Ondrej Mosnacek <[email protected]>Signed-off-by Eric Biggers <[email protected]>
Quoted 137 lines
--- arch/x86/crypto/morus1280_glue.c | 40 +++++++++++++------------------- arch/x86/crypto/morus640_glue.c | 39 ++++++++++++------------------- 2 files changed, 31 insertions(+), 48 deletions(-)arch/x86/crypto/morus1280_glue.c +16
-24@@ -85,31 +85,20 @@ static void crypto_morus1280_glue_process_ad(static void crypto_morus1280_glue_process_crypt (struct morus1280_state *state,struct morus1280_ops ops, -struct aead_request *req) +struct skcipher_walk *walk) { -struct skcipher_walk walk; -u8 *cursor_src, *cursor_dst; -unsigned int chunksize, base; - - ops.skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - cursor_src = walk.src.virt.addr; - cursor_dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops.crypt_blocks (state, cursor_src, cursor_dst, chunksize); - - base = chunksize & ~(MORUS1280_BLOCK_SIZE -1 ); - cursor_src += base; - cursor_dst += base; - chunksize &=MORUS1280_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops.crypt_tail (state, cursor_src, cursor_dst, - chunksize); +while (walk->nbytes >=MORUS1280_BLOCK_SIZE ) { + ops.crypt_blocks (state, walk->src.virt.addr, + walk->dst.virt.addr, +round_down (walk->nbytes, +MORUS1280_BLOCK_SIZE )); +skcipher_walk_done (walk, walk->nbytes %MORUS1280_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops.crypt_tail (state, walk->src.virt.addr, walk->dst.virt.addr, + walk->nbytes); +skcipher_walk_done (walk,0 ); } }@@ -147,12 +136,15 @@ static void crypto_morus1280_glue_crypt(struct aead_request *req,struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct morus1280_ctx *ctx =crypto_aead_ctx (tfm);struct morus1280_state state; +struct skcipher_walk walk; + + ops.skcipher_walk_init (&walk, req, true);kernel_fpu_begin (); ctx->ops->init (&state, &ctx->key, req->iv);crypto_morus1280_glue_process_ad (&state, ctx->ops, req->src, req->assoclen); -crypto_morus1280_glue_process_crypt (&state, ops, req); +crypto_morus1280_glue_process_crypt (&state, ops, &walk); ctx->ops->final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end ();arch/x86/crypto/morus640_glue.c +15
-25@@ -85,31 +85,19 @@ static void crypto_morus640_glue_process_ad(static void crypto_morus640_glue_process_crypt (struct morus640_state *state,struct morus640_ops ops, -struct aead_request *req) +struct skcipher_walk *walk) { -struct skcipher_walk walk; -u8 *cursor_src, *cursor_dst; -unsigned int chunksize, base; - - ops.skcipher_walk_init (&walk, req, false); - -while (walk.nbytes) { - cursor_src = walk.src.virt.addr; - cursor_dst = walk.dst.virt.addr; - chunksize = walk.nbytes; - - ops.crypt_blocks (state, cursor_src, cursor_dst, chunksize); - - base = chunksize & ~(MORUS640_BLOCK_SIZE -1 ); - cursor_src += base; - cursor_dst += base; - chunksize &=MORUS640_BLOCK_SIZE -1 ; - -if (chunksize >0 ) - ops.crypt_tail (state, cursor_src, cursor_dst, - chunksize); +while (walk->nbytes >=MORUS640_BLOCK_SIZE ) { + ops.crypt_blocks (state, walk->src.virt.addr, + walk->dst.virt.addr, +round_down (walk->nbytes,MORUS640_BLOCK_SIZE )); +skcipher_walk_done (walk, walk->nbytes %MORUS640_BLOCK_SIZE ); + } -skcipher_walk_done (&walk,0 ); +if (walk->nbytes) { + ops.crypt_tail (state, walk->src.virt.addr, walk->dst.virt.addr, + walk->nbytes); +skcipher_walk_done (walk,0 ); } }@@ -143,12 +131,15 @@ static void crypto_morus640_glue_crypt(struct aead_request *req,struct crypto_aead *tfm =crypto_aead_reqtfm (req);struct morus640_ctx *ctx =crypto_aead_ctx (tfm);struct morus640_state state; +struct skcipher_walk walk; + + ops.skcipher_walk_init (&walk, req, true);kernel_fpu_begin (); ctx->ops->init (&state, &ctx->key, req->iv);crypto_morus640_glue_process_ad (&state, ctx->ops, req->src, req->assoclen); -crypto_morus640_glue_process_crypt (&state, ops, req); +crypto_morus640_glue_process_crypt (&state, ops, &walk); ctx->ops->final (&state, tag_xor, req->assoclen, cryptlen);kernel_fpu_end (); --2.20.1
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
PATCH v2 5/15 crypto: x86/aesni-gcm - fix crash on empty plaintext
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
gcmaes_crypt_by_sg() dereferences the NULL pointer returned by scatterwalk_ffwd() when encrypting an empty plaintext and the source scatterlist ends immediately after the associated data.
Fix it by only fast-forwarding to the src/dst data scatterlists if the data length is nonzero.
This bug is reproduced by the "rfc4543(gcm(aes))" test vectors when run with the new AEAD test manager.
--- arch/x86/crypto/aesni-intel_glue.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +8 -5
@@ -813,11 +813,14 @@ static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,scatterwalk_map_and_copy (assoc, req->src,0 , assoclen,0 ); } - src_sg =scatterwalk_ffwd (src_start, req->src, req->assoclen); -scatterwalk_start (&src_sg_walk, src_sg); -if (req->src != req->dst) { - dst_sg =scatterwalk_ffwd (dst_start, req->dst, req->assoclen); -scatterwalk_start (&dst_sg_walk, dst_sg); +if (left) { + src_sg =scatterwalk_ffwd (src_start, req->src, req->assoclen); +scatterwalk_start (&src_sg_walk, src_sg); +if (req->src != req->dst) { + dst_sg =scatterwalk_ffwd (dst_start, req->dst, + req->assoclen); +scatterwalk_start (&dst_sg_walk, dst_sg); + } }kernel_fpu_begin ();
Signature · 1 line
2.20.1
PATCH v2 6/15 crypto: ahash - fix another early termination in hash walk
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Hash algorithms with an alignmask set, e.g. "xcbc(aes-aesni)" and "michael_mic", fail the improved hash tests because they sometimes produce the wrong digest. The bug is that in the case where a scatterlist element crosses pages, not all the data is actually hashed because the scatterlist walk terminates too early. This happens because the 'nbytes' variable in crypto_hash_walk_done() is assigned the number of bytes remaining in the page, then later interpreted as the number of bytes remaining in the scatterlist element. Fix it.
--- crypto/ahash.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
crypto/ahash.c +7 -7
@@ -86,17 +86,17 @@ static int hash_walk_new_entry(struct crypto_hash_walk *walk)int crypto_hash_walk_done (struct crypto_hash_walk *walk,int err) {unsigned int alignmask = walk->alignmask; -unsigned int nbytes = walk->entrylen; walk->data -= walk->offset; -if (nbytes && walk->offset & alignmask && !err) { - walk->offset =ALIGN (walk->offset, alignmask +1 ); - nbytes =min (nbytes, - ((unsigned int )(PAGE_SIZE )) - walk->offset); - walk->entrylen -= nbytes; +if (walk->entrylen && (walk->offset & alignmask) && !err) { +unsigned int nbytes; + walk->offset =ALIGN (walk->offset, alignmask +1 ); + nbytes =min (walk->entrylen, + (unsigned int )(PAGE_SIZE - walk->offset));if (nbytes) { + walk->entrylen -= nbytes; walk->data += walk->offset;return nbytes; }
@@ -116,7 +116,7 @@ int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)if (err)return err; -if (nbytes) { +if (walk->entrylen) { walk->offset =0 ; walk->pg++;return hash_walk_next (walk);
Signature · 1 line
2.20.1
PATCH v2 7/15 crypto: arm64/aes-neonbs - fix returning final keystream block
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The arm64 NEON bit-sliced implementation of AES-CTR fails the improved skcipher tests because it sometimes produces the wrong ciphertext. The bug is that the final keystream block isn't returned from the assembly code when the number of non-final blocks is zero. This can happen if the input data ends a few bytes after a page boundary. In this case the last bytes get "encrypted" by XOR'ing them with uninitialized memory.
Fix the assembly code to return the final keystream block when needed.
--- arch/arm64/crypto/aes-neonbs-core.S | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/arm64/crypto/aes-neonbs-core.S +6 -2
@@ -971,18 +971,22 @@ CPU_LE( rev x8, x8 ) 8: next_ctr v0 st1 {v0.16b}, [x24] - cbz x23, 0f + cbz x23, .Lctr_done cond_yield_neon 98b b 99b -0: frame_pop +.Lctr_done: + frame_pop ret /* * If we are handling the tail of the input (x6 != NULL), return the * final keystream block back to the caller. */ +0: cbz x25, 8b + st1 {v0.16b}, [x25] + b 8b 1: cbz x25, 8b st1 {v1.16b}, [x25] b 8b
Signature · 1 line
2.20.1
PATCH v2 8/15 crypto: testmgr - add testvec_config struct and helper functions
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Crypto algorithms must produce the same output for the same input regardless of data layout, i.e. how the src and dst scatterlists are divided into chunks and how each chunk is aligned. Request flags such as CRYPTO_TFM_REQ_MAY_SLEEP must not affect the result either.
However, testing of this currently has many gaps. For example, individual algorithms are responsible for providing their own chunked test vectors. But many don't bother to do this or test only one or two cases, providing poor test coverage. Also, other things such as misaligned IVs and CRYPTO_TFM_REQ_MAY_SLEEP are never tested at all.
Test code is also duplicated between the chunked and non-chunked cases, making it difficult to make other improvements.
To improve the situation, this patch series basically moves the chunk descriptions into the testmgr itself so that they are shared by all algorithms. However, it's done in an extensible way via a new struct 'testvec_config', which describes not just the scaled chunk lengths but also all other aspects of the crypto operation besides the data itself such as the buffer alignments, the request flags, whether the operation is in-place or not, the IV alignment, and for hash algorithms when to do each update() and when to use finup() vs. final() vs. digest().
Then, this patch series makes skcipher, aead, and hash algorithms be tested against a list of default testvec_configs, replacing the current test code. This improves overall test coverage, without reducing test performance too much. Note that the test vectors themselves are not changed, except for removing the chunk lists.
This series also adds randomized fuzz tests, enabled by a new kconfig option intended for developer use only, where skcipher, aead, and hash algorithms are tested against many randomly generated testvec_configs. This provides much more comprehensive test coverage.
These improved tests have already exposed many bugs.
To start it off, this initial patch adds the testvec_config and various helper functions that will be used by the skcipher, aead, and hash test code that will be converted to use the new testvec_config framework.
--- crypto/testmgr.c | 452 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 437 insertions(+), 15 deletions(-)
crypto/testmgr.c +437 -15
@@ -5,6 +5,7 @@* Copyright (c) 2002 Jean-Francois Dive <[email protected]> * Copyright (c) 2007 Nokia Siemens Networks * Copyright (c) 2008 Herbert Xu <[email protected]> +* Copyright (c) 2019 Google LLC * * Updated RFC4106 AES-GCM testing. * Authors: Aidan O'Mahony ([email protected])
@@ -26,6 +27,7 @@#include <linux/err.h> #include <linux/fips.h> #include <linux/module.h> +#include <linux/once.h> #include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/string.h>
@@ -146,12 +148,12 @@ static void hexdump(unsigned char *buf, unsigned int len) buf, len, false); } -static int testmgr_alloc_buf (char *buf[XBUFSIZE ]) +static int __testmgr_alloc_buf (char *buf[XBUFSIZE ],int order) {int i;for (i =0 ; i <XBUFSIZE ; i++) { - buf[i] = (void *)__get_free_page (GFP_KERNEL ); + buf[i] = (char *)__get_free_pages (GFP_KERNEL , order);if (!buf[i]) goto err_free_buf; }
@@ -160,17 +162,435 @@ static int testmgr_alloc_buf(char *buf[XBUFSIZE]) err_free_buf:while (i-- >0 ) -free_page ((unsigned long )buf[i]); +free_pages ((unsigned long )buf[i], order);return -ENOMEM ; } -static void testmgr_free_buf (char *buf[XBUFSIZE ]) +static int testmgr_alloc_buf (char *buf[XBUFSIZE ]) +{ +return __testmgr_alloc_buf (buf,0 ); +} + +static void __testmgr_free_buf (char *buf[XBUFSIZE ],int order) {int i;for (i =0 ; i <XBUFSIZE ; i++) -free_page ((unsigned long )buf[i]); +free_pages ((unsigned long )buf[i], order); +} + +static void testmgr_free_buf (char *buf[XBUFSIZE ]) +{ +__testmgr_free_buf (buf,0 ); +} + +#define TESTMGR_POISON_BYTE 0xfe +#define TESTMGR_POISON_LEN 16 + +static inline void testmgr_poison (void *addr,size_t len) +{ +memset (addr,TESTMGR_POISON_BYTE , len); +} + +/* Is the memory region still fully poisoned? */ +static inline bool testmgr_is_poison (const void *addr,size_t len) +{ +return memchr_inv (addr,TESTMGR_POISON_BYTE , len) ==NULL ; +} + +/* flush type for hash algorithms */ +enum flush_type { +/* merge with update of previous buffer(s) */ +FLUSH_TYPE_NONE =0 , + +/* update with previous buffer(s) before doing this one */ +FLUSH_TYPE_FLUSH , + +/* likewise, but also export and re-import the intermediate state */ +FLUSH_TYPE_REIMPORT , +}; + +/* finalization function for hash algorithms */ +enum finalization_type { +FINALIZATION_TYPE_FINAL ,/* use final() */ +FINALIZATION_TYPE_FINUP ,/* use finup() */ +FINALIZATION_TYPE_DIGEST ,/* use digest() */ +}; + +#define TEST_SG_TOTAL 10000 + +/** +* struct test_sg_division - description of a scatterlist entry +* +* This struct describes one entry of a scatterlist being constructed to check a +* crypto test vector. +* +* @proportion_of_total: length of this chunk relative to the total length, +* given as a proportion out of TEST_SG_TOTAL so that it +* scales to fit any test vector +* @offset: byte offset into a 2-page buffer at which this chunk will start +* @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the +* @offset +* @flush_type: for hashes, whether an update() should be done now vs. +* continuing to accumulate data +*/ +struct test_sg_division { +unsigned int proportion_of_total; +unsigned int offset; +bool offset_relative_to_alignmask; +enum flush_type flush_type; +}; + +/** +* struct testvec_config - configuration for testing a crypto test vector +* +* This struct describes the data layout and other parameters with which each +* crypto test vector can be tested. +* +* @name: name of this config, logged for debugging purposes if a test fails +* @inplace: operate on the data in-place, if applicable for the algorithm type? +* @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP +* @src_divs: description of how to arrange the source scatterlist +* @dst_divs: description of how to arrange the dst scatterlist, if applicable +* for the algorithm type. Defaults to @src_divs if unset. +* @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1], +* where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary +* @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to +* the @iv_offset +* @finalization_type: what finalization function to use for hashes +*/ +struct testvec_config { +const char *name; +bool inplace; +u32 req_flags; +struct test_sg_division src_divs[XBUFSIZE ]; +struct test_sg_division dst_divs[XBUFSIZE ]; +unsigned int iv_offset; +bool iv_offset_relative_to_alignmask; +enum finalization_type finalization_type; +}; + +#define TESTVEC_CONFIG_NAMELEN 192 + +static unsigned int count_test_sg_divisions (const struct test_sg_division *divs) +{ +unsigned int remaining =TEST_SG_TOTAL ; +unsigned int ndivs =0 ; + +do { + remaining -= divs[ndivs++].proportion_of_total; + }while (remaining); + +return ndivs; +} + +static bool valid_sg_divisions (const struct test_sg_division *divs, +unsigned int count,bool *any_flushes_ret) +{ +unsigned int total =0 ; +unsigned int i; + +for (i =0 ; i < count && total !=TEST_SG_TOTAL ; i++) { +if (divs[i].proportion_of_total <=0 || + divs[i].proportion_of_total >TEST_SG_TOTAL - total) +return false; + total += divs[i].proportion_of_total; +if (divs[i].flush_type !=FLUSH_TYPE_NONE ) + *any_flushes_ret = true; + } +return total ==TEST_SG_TOTAL && +memchr_inv (&divs[i],0 , (count - i) *sizeof (divs[0 ])) ==NULL ; +} + +/* +* Check whether the given testvec_config is valid. This isn't strictly needed +* since every testvec_config should be valid, but check anyway so that people +* don't unknowingly add broken configs that don't do what they wanted. +*/ +static bool valid_testvec_config (const struct testvec_config *cfg) +{ +bool any_flushes = false; + +if (cfg->name ==NULL ) +return false; + +if (!valid_sg_divisions (cfg->src_divs,ARRAY_SIZE (cfg->src_divs), + &any_flushes)) +return false; + +if (cfg->dst_divs[0 ].proportion_of_total) { +if (!valid_sg_divisions (cfg->dst_divs, +ARRAY_SIZE (cfg->dst_divs), + &any_flushes)) +return false; + }else { +if (memchr_inv (cfg->dst_divs,0 ,sizeof (cfg->dst_divs))) +return false; +/* defaults to dst_divs=src_divs */ + } + +if (cfg->iv_offset + + (cfg->iv_offset_relative_to_alignmask ?MAX_ALGAPI_ALIGNMASK :0 ) > +MAX_ALGAPI_ALIGNMASK +1 ) +return false; + +if (any_flushes && cfg->finalization_type ==FINALIZATION_TYPE_DIGEST ) +return false; + +return true; +} + +struct test_sglist { +char *bufs[XBUFSIZE ]; +struct scatterlist sgl[XBUFSIZE ]; +struct scatterlist sgl_saved[XBUFSIZE ]; +struct scatterlist *sgl_ptr; +unsigned int nents; +}; + +static int init_test_sglist (struct test_sglist *tsgl) +{ +return __testmgr_alloc_buf (tsgl->bufs,1 /* two pages per buffer */ ); +} + +static void destroy_test_sglist (struct test_sglist *tsgl) +{ +return __testmgr_free_buf (tsgl->bufs,1 /* two pages per buffer */ ); +} + +/** +* build_test_sglist() - build a scatterlist for a crypto test +* +* @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page +* buffers which the scatterlist @tsgl->sgl[] will be made to point into. +* @divs: the layout specification on which the scatterlist will be based +* @alignmask: the algorithm's alignmask +* @total_len: the total length of the scatterlist to build in bytes +* @data: if non-NULL, the buffers will be filled with this data until it ends. +* Otherwise the buffers will be poisoned. In both cases, some bytes +* past the end of each buffer will be poisoned to help detect overruns. +* @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry +* corresponds will be returned here. This will match @divs except +* that divisions resolving to a length of 0 are omitted as they are +* not included in the scatterlist. +* +* Return: 0 or a -errno value +*/ +static int build_test_sglist (struct test_sglist *tsgl, +const struct test_sg_division *divs, +const unsigned int alignmask, +const unsigned int total_len, +struct iov_iter *data, +const struct test_sg_division *out_divs[XBUFSIZE ]) +{ +struct { +const struct test_sg_division *div; +size_t length; + } partitions[XBUFSIZE ]; +const unsigned int ndivs =count_test_sg_divisions (divs); +unsigned int len_remaining = total_len; +unsigned int i; + +BUILD_BUG_ON (ARRAY_SIZE (partitions) !=ARRAY_SIZE (tsgl->sgl)); +if (WARN_ON (ndivs >ARRAY_SIZE (partitions))) +return -EINVAL ; + +/* Calculate the (div, length) pairs */ + tsgl->nents =0 ; +for (i =0 ; i < ndivs; i++) { +unsigned int len_this_sg = +min (len_remaining, + (total_len * divs[i].proportion_of_total + +TEST_SG_TOTAL /2 ) /TEST_SG_TOTAL ); + +if (len_this_sg !=0 ) { + partitions[tsgl->nents].div = &divs[i]; + partitions[tsgl->nents].length = len_this_sg; + tsgl->nents++; + len_remaining -= len_this_sg; + } + } +if (tsgl->nents ==0 ) { + partitions[tsgl->nents].div = &divs[0 ]; + partitions[tsgl->nents].length =0 ; + tsgl->nents++; + } + partitions[tsgl->nents -1 ].length += len_remaining; + +/* Set up the sgl entries and fill the data or poison */ +sg_init_table (tsgl->sgl, tsgl->nents); +for (i =0 ; i < tsgl->nents; i++) { +unsigned int offset = partitions[i].div->offset; +void *addr; + +if (partitions[i].div->offset_relative_to_alignmask) + offset += alignmask; + +while (offset + partitions[i].length +TESTMGR_POISON_LEN > +2 *PAGE_SIZE ) { +if (WARN_ON (offset <=0 )) +return -EINVAL ; + offset /=2 ; + } + + addr = &tsgl->bufs[i][offset]; +sg_set_buf (&tsgl->sgl[i], addr, partitions[i].length); + +if (out_divs) + out_divs[i] = partitions[i].div; + +if (data) { +size_t copy_len, copied; + + copy_len =min (partitions[i].length, data->count); + copied =copy_from_iter (addr, copy_len, data); +if (WARN_ON (copied != copy_len)) +return -EINVAL ; +testmgr_poison (addr + copy_len, partitions[i].length + +TESTMGR_POISON_LEN - copy_len); + }else { +testmgr_poison (addr, partitions[i].length + +TESTMGR_POISON_LEN ); + } + } + +sg_mark_end (&tsgl->sgl[tsgl->nents -1 ]); + tsgl->sgl_ptr = tsgl->sgl; +memcpy (tsgl->sgl_saved, tsgl->sgl, tsgl->nents *sizeof (tsgl->sgl[0 ])); +return 0 ; +} + +/* +* Verify that a scatterlist crypto operation produced the correct output. +* +* @tsgl: scatterlist containing the actual output +* @expected_output: buffer containing the expected output +* @len_to_check: length of @expected_output in bytes +* @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result +* @check_poison: verify that the poison bytes after each chunk are intact? +* +* Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun. +*/ +static int verify_correct_output (const struct test_sglist *tsgl, +const char *expected_output, +unsigned int len_to_check, +unsigned int unchecked_prefix_len, +bool check_poison) +{ +unsigned int i; + +for (i =0 ; i < tsgl->nents; i++) { +struct scatterlist *sg = &tsgl->sgl_ptr[i]; +unsigned int len = sg->length; +unsigned int offset = sg->offset; +const char *actual_output; + +if (unchecked_prefix_len) { +if (unchecked_prefix_len >= len) { + unchecked_prefix_len -= len; +continue ; + } + offset += unchecked_prefix_len; + len -= unchecked_prefix_len; + unchecked_prefix_len =0 ; + } + len =min (len, len_to_check); + actual_output =page_address (sg_page (sg)) + offset; +if (memcmp (expected_output, actual_output, len) !=0 ) +return -EINVAL ; +if (check_poison && + !testmgr_is_poison (actual_output + len,TESTMGR_POISON_LEN )) +return -EOVERFLOW ; + len_to_check -= len; + expected_output += len; + } +if (WARN_ON (len_to_check !=0 )) +return -EINVAL ; +return 0 ; +} + +static bool is_test_sglist_corrupted (const struct test_sglist *tsgl) +{ +unsigned int i; + +for (i =0 ; i < tsgl->nents; i++) { +if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link) +return true; +if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset) +return true; +if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length) +return true; + } +return false; +} + +struct cipher_test_sglists { +struct test_sglist src; +struct test_sglist dst; +}; + +static struct cipher_test_sglists *alloc_cipher_test_sglists (void ) +{ +struct cipher_test_sglists *tsgls; + + tsgls =kmalloc (sizeof (*tsgls),GFP_KERNEL ); +if (!tsgls) +return NULL ; + +if (init_test_sglist (&tsgls->src) !=0 ) + goto fail_kfree; +if (init_test_sglist (&tsgls->dst) !=0 ) + goto fail_destroy_src; + +return tsgls; + +fail_destroy_src: +destroy_test_sglist (&tsgls->src); +fail_kfree: +kfree (tsgls); +return NULL ; +} + +static void free_cipher_test_sglists (struct cipher_test_sglists *tsgls) +{ +if (tsgls) { +destroy_test_sglist (&tsgls->src); +destroy_test_sglist (&tsgls->dst); +kfree (tsgls); + } +} + +/* Build the src and dst scatterlists for an skcipher or AEAD test */ +static int build_cipher_test_sglists (struct cipher_test_sglists *tsgls, +const struct testvec_config *cfg, +unsigned int alignmask, +unsigned int src_total_len, +unsigned int dst_total_len, +const struct kvec *inputs, +unsigned int nr_inputs) +{ +struct iov_iter input; +int err; + +iov_iter_kvec (&input,WRITE , inputs, nr_inputs, src_total_len); + err =build_test_sglist (&tsgls->src, cfg->src_divs, alignmask, + cfg->inplace ? +max (dst_total_len, src_total_len) : + src_total_len, + &input,NULL ); +if (err) +return err; + +if (cfg->inplace) { + tsgls->dst.sgl_ptr = tsgls->src.sgl; + tsgls->dst.nents = tsgls->src.nents; +return 0 ; + } +return build_test_sglist (&tsgls->dst, + cfg->dst_divs[0 ].proportion_of_total ? + cfg->dst_divs : cfg->src_divs, + alignmask, dst_total_len,NULL ,NULL ); }static int ahash_guard_result (char *result,char c,int size)
@@ -3657,18 +4077,10 @@ static const struct alg_test_desc alg_test_descs[] = { } }; -static bool alg_test_descs_checked; - -static void alg_test_descs_check_order (void ) +static void alg_check_test_descs_order (void ) {int i; -/* only check once */ -if (alg_test_descs_checked) -return ; - - alg_test_descs_checked = true; -for (i =1 ; i <ARRAY_SIZE (alg_test_descs); i++) {int diff =strcmp (alg_test_descs[i -1 ].alg, alg_test_descs[i].alg);
@@ -3686,6 +4098,16 @@ static void alg_test_descs_check_order(void) } } +static void alg_check_testvec_configs (void ) +{ +} + +static void testmgr_onetime_init (void ) +{ +alg_check_test_descs_order (); +alg_check_testvec_configs (); +} +static int alg_find_test (const char *alg) {int start =0 ;
@@ -3722,7 +4144,7 @@ int alg_test(const char *driver, const char *alg, u32 type, u32 mask)return 0 ; } -alg_test_descs_check_order (); +DO_ONCE (testmgr_onetime_init);if ((type &CRYPTO_ALG_TYPE_MASK ) ==CRYPTO_ALG_TYPE_CIPHER ) {char nalg[CRYPTO_MAX_ALG_NAME ];
Signature · 1 line
2.20.1
PATCH v2 9/15 crypto: testmgr - introduce CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
To achieve more comprehensive crypto test coverage, I'd like to add fuzz tests that use random data layouts and request flags.
To be most effective these tests should be part of testmgr, so they automatically run on every algorithm registered with the crypto API. However, they will take much longer to run than the current tests and therefore will only really be intended to be run by developers, whereas the current tests have a wider audience.
Therefore, add a new kconfig option CONFIG_CRYPTO_MANAGER_EXTRA_TESTS that can be set by developers to enable these extra, expensive tests.
Similar to the regular tests, also add a module parameter cryptomgr.noextratests to support disabling the tests.
Finally, another module parameter cryptomgr.fuzz_iterations is added to control how many iterations the fuzz tests do. Note: for now setting this to 0 will be equivalent to cryptomgr.noextratests=1. But I opted for separate parameters to provide more flexibility to add other types of tests under the "extra tests" category in the future.
--- crypto/Kconfig | 10 ++++++++++ crypto/testmgr.c | 14 ++++++++++++++ 2 files changed, 24 insertions(+)
crypto/Kconfig +10 -0
@@ -168,6 +168,16 @@ config CRYPTO_MANAGER_DISABLE_TESTS Disable run-time self tests that normally take place at algorithm registration. +config CRYPTO_MANAGER_EXTRA_TESTS + bool "Enable extra run-time crypto self tests" + depends on DEBUG_KERNEL && !CRYPTO_MANAGER_DISABLE_TESTS + help + Enable extra run-time self tests of registered crypto algorithms, + including randomized fuzz tests. + + This is intended for developer use only, as these tests take much + longer to run than the normal self tests. + config CRYPTO_GF128MUL tristate "GF(2^128) multiplication functions" help
crypto/testmgr.c +14 -0
@@ -43,6 +43,16 @@ static bool notests;module_param (notests, bool,0644 );MODULE_PARM_DESC (notests,"disable crypto self-tests" ); +#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS +static bool noextratests; +module_param (noextratests, bool,0644 ); +MODULE_PARM_DESC (noextratests,"disable expensive crypto self-tests" ); + +static unsigned int fuzz_iterations =100 ; +module_param (fuzz_iterations, uint,0644 ); +MODULE_PARM_DESC (fuzz_iterations,"number of fuzz test iterations" ); +#endif +#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS /* a perfect nop */
@@ -4106,6 +4116,10 @@ static void testmgr_onetime_init(void) {alg_check_test_descs_order ();alg_check_testvec_configs (); + +#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS +pr_warn ("alg: extra crypto tests enabled. This is intended for developer use only.\n" ); +#endif }static int alg_find_test (const char *alg)
Signature · 1 line
2.20.1
PATCH v2 10/15 crypto: testmgr - implement random testvec_config generation
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add functions that generate a random testvec_config, in preparation for using it for randomized fuzz tests.
--- crypto/testmgr.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+)
crypto/testmgr.c +117 -0
@@ -28,6 +28,7 @@#include <linux/fips.h> #include <linux/module.h> #include <linux/once.h> +#include <linux/random.h> #include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/string.h>
@@ -603,6 +604,122 @@ static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls, alignmask, dst_total_len, NULL, NULL); } +#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS +static char *generate_random_sgl_divisions (struct test_sg_division *divs, +size_t max_divs,char *p,char *end, +bool gen_flushes) +{ +struct test_sg_division *div = divs; +unsigned int remaining =TEST_SG_TOTAL ; + +do { +unsigned int this_len; + +if (div == &divs[max_divs -1 ] ||prandom_u32 () %2 ==0 ) + this_len = remaining; +else + this_len =1 + (prandom_u32 () % remaining); + div->proportion_of_total = this_len; + +if (prandom_u32 () %4 ==0 ) + div->offset = (PAGE_SIZE -128 ) + (prandom_u32 () %128 ); +else if (prandom_u32 () %2 ==0 ) + div->offset =prandom_u32 () %32 ; +else + div->offset =prandom_u32 () %PAGE_SIZE ; +if (prandom_u32 () %8 ==0 ) + div->offset_relative_to_alignmask = true; + + div->flush_type =FLUSH_TYPE_NONE ; +if (gen_flushes) { +switch (prandom_u32 () %4 ) { +case 0 : + div->flush_type =FLUSH_TYPE_REIMPORT ; +break ; +case 1 : + div->flush_type =FLUSH_TYPE_FLUSH ; +break ; + } + } + +BUILD_BUG_ON (TEST_SG_TOTAL !=10000 );/* for "%u.%u%%" */ + p +=scnprintf (p, end - p,"%s%u.%u%%@%s+%u%s" , + div->flush_type ==FLUSH_TYPE_NONE ?"" : + div->flush_type ==FLUSH_TYPE_FLUSH ? +"<flush> " :"<reimport> " , + this_len /100 , this_len %100 , + div->offset_relative_to_alignmask ? +"alignmask" :"" , + div->offset, this_len == remaining ?"" :", " ); + remaining -= this_len; + div++; + }while (remaining); + +return p; +} + +/* Generate a random testvec_config for fuzz testing */ +static void generate_random_testvec_config (struct testvec_config *cfg, +char *name,size_t max_namelen) +{ +char *p = name; +char *const end = name + max_namelen; + +memset (cfg,0 ,sizeof (*cfg)); + + cfg->name = name; + + p +=scnprintf (p, end - p,"random:" ); + +if (prandom_u32 () %2 ==0 ) { + cfg->inplace = true; + p +=scnprintf (p, end - p," inplace" ); + } + +if (prandom_u32 () %2 ==0 ) { + cfg->req_flags |=CRYPTO_TFM_REQ_MAY_SLEEP ; + p +=scnprintf (p, end - p," may_sleep" ); + } + +switch (prandom_u32 () %4 ) { +case 0 : + cfg->finalization_type =FINALIZATION_TYPE_FINAL ; + p +=scnprintf (p, end - p," use_final" ); +break ; +case 1 : + cfg->finalization_type =FINALIZATION_TYPE_FINUP ; + p +=scnprintf (p, end - p," use_finup" ); +break ; +default : + cfg->finalization_type =FINALIZATION_TYPE_DIGEST ; + p +=scnprintf (p, end - p," use_digest" ); +break ; + } + + p +=scnprintf (p, end - p," src_divs=[" ); + p =generate_random_sgl_divisions (cfg->src_divs, +ARRAY_SIZE (cfg->src_divs), p, end, + (cfg->finalization_type != +FINALIZATION_TYPE_DIGEST )); + p +=scnprintf (p, end - p,"]" ); + +if (!cfg->inplace &&prandom_u32 () %2 ==0 ) { + p +=scnprintf (p, end - p," dst_divs=[" ); + p =generate_random_sgl_divisions (cfg->dst_divs, +ARRAY_SIZE (cfg->dst_divs), + p, end, false); + p +=scnprintf (p, end - p,"]" ); + } + +if (prandom_u32 () %2 ==0 ) { + cfg->iv_offset =1 + (prandom_u32 () %MAX_ALGAPI_ALIGNMASK ); + p +=scnprintf (p, end - p," iv_offset=%u" , cfg->iv_offset); + } + +WARN_ON_ONCE (!valid_testvec_config (cfg)); +} +#endif /* CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */ +static int ahash_guard_result (char *result,char c,int size) {int i;
Signature · 1 line
2.20.1
PATCH v2 11/15 crypto: testmgr - convert skcipher testing to use testvec_configs
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Convert alg_test_skcipher() to use the new test framework, adding a list of testvec_configs to test by default. When the extra self-tests are enabled, randomly generated testvec_configs are tested as well.
This improves skcipher test coverage mainly because now all algorithms have a variety of data layouts tested, whereas before each algorithm was responsible for declaring its own chunked test cases which were often missing or provided poor test coverage. The new code also tests both the MAY_SLEEP and !MAY_SLEEP cases, different IV alignments, and buffers that cross pages.
This has already found a bug in the arm64 ctr-aes-neonbs algorithm. It would have easily found many past bugs.
I removed the skcipher chunked test vectors that were the same as non-chunked ones, but left the ones that were unique.
--- crypto/testmgr.c | 515 ++++++++++++++++++++++------------------------- crypto/testmgr.h | 253 ----------------------- 2 files changed, 245 insertions(+), 523 deletions(-)
crypto/testmgr.c +245 -270
@@ -284,6 +284,68 @@ struct testvec_config {#define TESTVEC_CONFIG_NAMELEN 192 +/* +* The following are the lists of testvec_configs to test for each algorithm +* type when the basic crypto self-tests are enabled, i.e. when +* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test +* coverage, while keeping the test time much shorter than the full fuzz tests +* so that the basic tests can be enabled in a wider range of circumstances. +*/ + +/* Configs for skciphers and aeads */ +static const struct testvec_config default_cipher_testvec_configs[] = { + { + .name ="in-place" , + .inplace = true, + .src_divs = { { .proportion_of_total =10000 } }, + }, { + .name ="out-of-place" , + .src_divs = { { .proportion_of_total =10000 } }, + }, { + .name ="unaligned buffer, offset=1" , + .src_divs = { { .proportion_of_total =10000 , .offset =1 } }, + .iv_offset =1 , + }, { + .name ="buffer aligned only to alignmask" , + .src_divs = { + { + .proportion_of_total =10000 , + .offset =1 , + .offset_relative_to_alignmask = true, + }, + }, + .iv_offset =1 , + .iv_offset_relative_to_alignmask = true, + }, { + .name ="two even aligned splits" , + .src_divs = { + { .proportion_of_total =5000 }, + { .proportion_of_total =5000 }, + }, + }, { + .name ="uneven misaligned splits, may sleep" , + .req_flags =CRYPTO_TFM_REQ_MAY_SLEEP , + .src_divs = { + { .proportion_of_total =1900 , .offset =33 }, + { .proportion_of_total =3300 , .offset =7 }, + { .proportion_of_total =4800 , .offset =18 }, + }, + .iv_offset =3 , + }, { + .name ="misaligned splits crossing pages, inplace" , + .inplace = true, + .src_divs = { + { + .proportion_of_total =7500 , + .offset =PAGE_SIZE -32 + }, { + .proportion_of_total =2500 , + .offset =PAGE_SIZE -7 + }, + }, + } +}; +static unsigned int count_test_sg_divisions (const struct test_sg_division *divs) {unsigned int remaining =TEST_SG_TOTAL ;
@@ -1608,8 +1670,6 @@ static int test_cipher(struct crypto_cipher *tfm, int enc, j =0 ;for (i =0 ; i < tcount; i++) { -if (template[i].np) -continue ;if (fips_enabled && template[i].fips_skip)continue ;
@@ -1667,282 +1727,214 @@ static int test_cipher(struct crypto_cipher *tfm, int enc,return ret; } -static int __test_skcipher (struct crypto_skcipher *tfm,int enc, -const struct cipher_testvec *template, -unsigned int tcount, -const bool diff_dst,const int align_offset) +static int test_skcipher_vec_cfg (const char *driver,int enc, +const struct cipher_testvec *vec, +unsigned int vec_num, +const struct testvec_config *cfg, +struct skcipher_request *req, +struct cipher_test_sglists *tsgls) { -const char *algo = -crypto_tfm_alg_driver_name (crypto_skcipher_tfm (tfm)); -unsigned int i, j, k, n, temp; -char *q; -struct skcipher_request *req; -struct scatterlist sg[8 ]; -struct scatterlist sgout[8 ]; -const char *e, *d; -struct crypto_wait wait; -const char *input, *result; -void *data; -char iv[MAX_IVLEN ]; -char *xbuf[XBUFSIZE ]; -char *xoutbuf[XBUFSIZE ]; -int ret = -ENOMEM ; -unsigned int ivsize =crypto_skcipher_ivsize (tfm); - -if (testmgr_alloc_buf (xbuf)) - goto out_nobuf; - -if (diff_dst &&testmgr_alloc_buf (xoutbuf)) - goto out_nooutbuf; - -if (diff_dst) - d ="-ddst" ; -else - d ="" ; +struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +const unsigned int alignmask =crypto_skcipher_alignmask (tfm); +const unsigned int ivsize =crypto_skcipher_ivsize (tfm); +const u32 req_flags =CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; +const char *op = enc ?"encryption" :"decryption" ; +DECLARE_CRYPTO_WAIT (wait); +u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK +1 ) +MAX_IVLEN ]; +u8 *iv =PTR_ALIGN (&_iv[0 ],2 * (MAX_ALGAPI_ALIGNMASK +1 )) + + cfg->iv_offset + + (cfg->iv_offset_relative_to_alignmask ? alignmask :0 ); +struct kvec input; +int err; -if (enc ==ENCRYPT ) - e ="encryption" ; +/* Set the key */ +if (vec->wk) +crypto_skcipher_set_flags (tfm,CRYPTO_TFM_REQ_FORBID_WEAK_KEYS );else - e ="decryption" ; - -crypto_init_wait (&wait); - - req =skcipher_request_alloc (tfm,GFP_KERNEL ); -if (!req) { -pr_err ("alg: skcipher%s: Failed to allocate request for %s\n" , - d, algo); - goto out; +crypto_skcipher_clear_flags (tfm, +CRYPTO_TFM_REQ_FORBID_WEAK_KEYS ); + err =crypto_skcipher_setkey (tfm, vec->key, vec->klen); +if (err) { +if (vec->fail)/* expectedly failed to set key? */ +return 0 ; +pr_err ("alg: skcipher: %s setkey failed with err %d on test vector %u; flags=%#x\n" , + driver, err, vec_num,crypto_skcipher_get_flags (tfm)); +return err; + } +if (vec->fail) { +pr_err ("alg: skcipher: %s setkey unexpectedly succeeded on test vector %u\n" , + driver, vec_num); +return -EINVAL ; } -skcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , - crypto_req_done, &wait); - - j =0 ; -for (i =0 ; i < tcount; i++) { -if (template[i].np && !template[i].also_non_np) -continue ; - -if (fips_enabled && template[i].fips_skip) -continue ; - -if (template[i].iv && !(template[i].generates_iv && enc)) -memcpy (iv, template[i].iv, ivsize); +/* The IV must be copied to a buffer, as the algorithm may modify it */ +if (ivsize) { +if (WARN_ON (ivsize >MAX_IVLEN )) +return -EINVAL ; +if (vec->iv && !(vec->generates_iv && enc)) +memcpy (iv, vec->iv, ivsize);else -memset (iv,0 ,MAX_IVLEN ); - - input = enc ? template[i].ptext : template[i].ctext; - result = enc ? template[i].ctext : template[i].ptext; - j++; - ret = -EINVAL ; -if (WARN_ON (align_offset + template[i].len >PAGE_SIZE )) - goto out; - - data = xbuf[0 ]; - data += align_offset; -memcpy (data, input, template[i].len); - -crypto_skcipher_clear_flags (tfm, ~0 ); -if (template[i].wk) -crypto_skcipher_set_flags (tfm,CRYPTO_TFM_REQ_FORBID_WEAK_KEYS ); - - ret =crypto_skcipher_setkey (tfm, template[i].key, - template[i].klen); -if (template[i].fail == !ret) { -pr_err ("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n" , - d, j, algo,crypto_skcipher_get_flags (tfm)); - goto out; - }else if (ret) -continue ; - -sg_init_one (&sg[0 ], data, template[i].len); -if (diff_dst) { - data = xoutbuf[0 ]; - data += align_offset; -sg_init_one (&sgout[0 ], data, template[i].len); - } - -skcipher_request_set_crypt (req, sg, (diff_dst) ? sgout : sg, - template[i].len, iv); - ret =crypto_wait_req (enc ?crypto_skcipher_encrypt (req) : -crypto_skcipher_decrypt (req), &wait); - -if (ret) { -pr_err ("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n" , - d, e, j, algo, -ret); - goto out; - } - - q = data; -if (memcmp (q, result, template[i].len)) { -pr_err ("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n" , - d, j, e, algo); -hexdump (q, template[i].len); - ret = -EINVAL ; - goto out; - } - -if (template[i].generates_iv && enc && -memcmp (iv, template[i].iv,crypto_skcipher_ivsize (tfm))) { -pr_err ("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n" , - d, j, e, algo); -hexdump (iv,crypto_skcipher_ivsize (tfm)); - ret = -EINVAL ; - goto out; +memset (iv,0 , ivsize); + }else { +if (vec->generates_iv) { +pr_err ("alg: skcipher: %s has ivsize=0 but test vector %u generates IV!\n" , + driver, vec_num); +return -EINVAL ; } + iv =NULL ; } - j =0 ; -for (i =0 ; i < tcount; i++) { -/* alignment tests are only done with continuous buffers */ -if (align_offset !=0 ) -break ; - -if (!template[i].np) -continue ; - -if (fips_enabled && template[i].fips_skip) -continue ; - -if (template[i].iv && !(template[i].generates_iv && enc)) -memcpy (iv, template[i].iv, ivsize); -else -memset (iv,0 ,MAX_IVLEN ); - - input = enc ? template[i].ptext : template[i].ctext; - result = enc ? template[i].ctext : template[i].ptext; - j++; -crypto_skcipher_clear_flags (tfm, ~0 ); -if (template[i].wk) -crypto_skcipher_set_flags (tfm,CRYPTO_TFM_REQ_FORBID_WEAK_KEYS ); - - ret =crypto_skcipher_setkey (tfm, template[i].key, - template[i].klen); -if (template[i].fail == !ret) { -pr_err ("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n" , - d, j, algo,crypto_skcipher_get_flags (tfm)); - goto out; - }else if (ret) -continue ; - - temp =0 ; - ret = -EINVAL ; -sg_init_table (sg, template[i].np); -if (diff_dst) -sg_init_table (sgout, template[i].np); -for (k =0 ; k < template[i].np; k++) { -if (WARN_ON (offset_in_page (IDX [k]) + - template[i].tap[k] >PAGE_SIZE )) - goto out; - - q = xbuf[IDX [k] >>PAGE_SHIFT ] +offset_in_page (IDX [k]); - -memcpy (q, input + temp, template[i].tap[k]); - -if (offset_in_page (q) + template[i].tap[k] <PAGE_SIZE ) - q[template[i].tap[k]] =0 ; - -sg_set_buf (&sg[k], q, template[i].tap[k]); -if (diff_dst) { - q = xoutbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]); - -sg_set_buf (&sgout[k], q, template[i].tap[k]); +/* Build the src/dst scatterlists */ + input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext; + input.iov_len = vec->len; + err =build_cipher_test_sglists (tsgls, cfg, alignmask, + vec->len, vec->len, &input,1 ); +if (err) { +pr_err ("alg: skcipher: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return err; + } -memset (q,0 , template[i].tap[k]); -if (offset_in_page (q) + - template[i].tap[k] <PAGE_SIZE ) - q[template[i].tap[k]] =0 ; - } +/* Do the actual encryption or decryption */ +testmgr_poison (req->__ctx,crypto_skcipher_reqsize (tfm)); +skcipher_request_set_callback (req, req_flags, crypto_req_done, &wait); +skcipher_request_set_crypt (req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr, + vec->len, iv); + err =crypto_wait_req (enc ?crypto_skcipher_encrypt (req) : +crypto_skcipher_decrypt (req), &wait); +if (err) { +pr_err ("alg: skcipher: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n" , + driver, op, err, vec_num, cfg->name); +return err; + } - temp += template[i].tap[k]; - } +/* Check for the correct output (ciphertext or plaintext) */ + err =verify_correct_output (&tsgls->dst, enc ? vec->ctext : vec->ptext, + vec->len,0 , true); +if (err == -EOVERFLOW ) { +pr_err ("alg: skcipher: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return err; + } +if (err) { +pr_err ("alg: skcipher: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return err; + } -skcipher_request_set_crypt (req, sg, (diff_dst) ? sgout : sg, - template[i].len, iv); +/* If applicable, check that the algorithm generated the correct IV */ +if (vec->generates_iv && enc &&memcmp (iv, vec->iv, ivsize) !=0 ) { +pr_err ("alg: skcipher: %s %s test failed (wrong output IV) on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +hexdump (iv, ivsize); +return -EINVAL ; + } - ret =crypto_wait_req (enc ?crypto_skcipher_encrypt (req) : -crypto_skcipher_decrypt (req), &wait); +return 0 ; +} -if (ret) { -pr_err ("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n" , - d, e, j, algo, -ret); - goto out; - } +static int test_skcipher_vec (const char *driver,int enc, +const struct cipher_testvec *vec, +unsigned int vec_num, +struct skcipher_request *req, +struct cipher_test_sglists *tsgls) +{ +unsigned int i; +int err; - temp =0 ; - ret = -EINVAL ; -for (k =0 ; k < template[i].np; k++) { -if (diff_dst) - q = xoutbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]); -else - q = xbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]); +if (fips_enabled && vec->fips_skip) +return 0 ; -if (memcmp (q, result + temp, template[i].tap[k])) { -pr_err ("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n" , - d, j, e, k, algo); -hexdump (q, template[i].tap[k]); - goto out; - } +for (i =0 ; i <ARRAY_SIZE (default_cipher_testvec_configs); i++) { + err =test_skcipher_vec_cfg (driver, enc, vec, vec_num, + &default_cipher_testvec_configs[i], + req, tsgls); +if (err) +return err; + } - q += template[i].tap[k]; -for (n =0 ;offset_in_page (q + n) && q[n]; n++) - ; -if (n) { -pr_err ("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n" , - d, j, e, k, algo, n); -hexdump (q, n); - goto out; - } - temp += template[i].tap[k]; +#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS +if (!noextratests) { +struct testvec_config cfg; +char cfgname[TESTVEC_CONFIG_NAMELEN ]; + +for (i =0 ; i < fuzz_iterations; i++) { +generate_random_testvec_config (&cfg, cfgname, +sizeof (cfgname)); + err =test_skcipher_vec_cfg (driver, enc, vec, vec_num, + &cfg, req, tsgls); +if (err) +return err; } } +#endif +return 0 ; +} - ret =0 ; +static int test_skcipher (const char *driver,int enc, +const struct cipher_test_suite *suite, +struct skcipher_request *req, +struct cipher_test_sglists *tsgls) +{ +unsigned int i; +int err; -out: -skcipher_request_free (req); -if (diff_dst) -testmgr_free_buf (xoutbuf); -out_nooutbuf: -testmgr_free_buf (xbuf); -out_nobuf: -return ret; +for (i =0 ; i < suite->count; i++) { + err =test_skcipher_vec (driver, enc, &suite->vecs[i], i, req, + tsgls); +if (err) +return err; + } +return 0 ; } -static int test_skcipher (struct crypto_skcipher *tfm,int enc, -const struct cipher_testvec *template, -unsigned int tcount) +static int alg_test_skcipher (const struct alg_test_desc *desc, +const char *driver,u32 type,u32 mask) { -unsigned int alignmask; -int ret; +const struct cipher_test_suite *suite = &desc->suite.cipher; +struct crypto_skcipher *tfm; +struct skcipher_request *req =NULL ; +struct cipher_test_sglists *tsgls =NULL ; +int err; -/* test 'dst == src' case */ - ret =__test_skcipher (tfm, enc, template, tcount, false,0 ); -if (ret) -return ret; +if (suite->count <=0 ) { +pr_err ("alg: skcipher: empty test suite for %s\n" , driver); +return -EINVAL ; + } -/* test 'dst != src' case */ - ret =__test_skcipher (tfm, enc, template, tcount, true,0 ); -if (ret) -return ret; + tfm =crypto_alloc_skcipher (driver, type, mask); +if (IS_ERR (tfm)) { +pr_err ("alg: skcipher: failed to allocate transform for %s: %ld\n" , + driver,PTR_ERR (tfm)); +return PTR_ERR (tfm); + } -/* test unaligned buffers, check with one byte offset */ - ret =__test_skcipher (tfm, enc, template, tcount, true,1 ); -if (ret) -return ret; + req =skcipher_request_alloc (tfm,GFP_KERNEL ); +if (!req) { +pr_err ("alg: skcipher: failed to allocate request for %s\n" , + driver); + err = -ENOMEM ; + goto out; + } - alignmask =crypto_tfm_alg_alignmask (&tfm->base); -if (alignmask) { -/* Check if alignment mask for tfm is correctly set. */ - ret =__test_skcipher (tfm, enc, template, tcount, true, - alignmask +1 ); -if (ret) -return ret; + tsgls =alloc_cipher_test_sglists (); +if (!tsgls) { +pr_err ("alg: skcipher: failed to allocate test buffers for %s\n" , + driver); + err = -ENOMEM ; + goto out; } -return 0 ; + err =test_skcipher (driver,ENCRYPT , suite, req, tsgls); +if (err) + goto out; + + err =test_skcipher (driver,DECRYPT , suite, req, tsgls); +out: +free_cipher_test_sglists (tsgls); +skcipher_request_free (req); +crypto_free_skcipher (tfm); +return err; } static int test_comp(struct crypto_comp *tfm,
@@ -2326,28 +2318,6 @@ static int alg_test_cipher(const struct alg_test_desc *desc,return err; } -static int alg_test_skcipher (const struct alg_test_desc *desc, -const char *driver,u32 type,u32 mask) -{ -const struct cipher_test_suite *suite = &desc->suite.cipher; -struct crypto_skcipher *tfm; -int err; - - tfm =crypto_alloc_skcipher (driver, type, mask); -if (IS_ERR (tfm)) { -printk (KERN_ERR "alg: skcipher: Failed to load transform for " -"%s: %ld\n" , driver,PTR_ERR (tfm)); -return PTR_ERR (tfm); - } - - err =test_skcipher (tfm,ENCRYPT , suite->vecs, suite->count); -if (!err) - err =test_skcipher (tfm,DECRYPT , suite->vecs, suite->count); - -crypto_free_skcipher (tfm); -return err; -} -static int alg_test_comp (const struct alg_test_desc *desc,const char *driver,u32 type,u32 mask) {
@@ -4227,6 +4197,11 @@ static void alg_check_test_descs_order(void)static void alg_check_testvec_configs (void ) { +int i; + +for (i =0 ; i <ARRAY_SIZE (default_cipher_testvec_configs); i++) +WARN_ON (!valid_testvec_config ( + &default_cipher_testvec_configs[i])); }static void testmgr_onetime_init (void )
crypto/testmgr.h +0 -253
637 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 12/15 crypto: testmgr - convert aead testing to use testvec_configs
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Convert alg_test_aead() to use the new test framework, using the same list of testvec_configs that skcipher testing uses.
This significantly improves AEAD test coverage mainly because previously there was only very limited test coverage of the possible data layouts. Now the data layouts to test are listed in one place for all algorithms and optionally are also randomly generated. In fact, only one AEAD algorithm (AES-GCM) even had a chunked test case before.
This already found bugs in all the AEGIS and MORUS implementations, the x86 AES-GCM implementation, and the arm64 AES-CCM implementation.
I removed the AEAD chunked test vectors that were the same as non-chunked ones, but left the ones that were unique.
Note: the rewritten test code allocates an aead_request just once per algorithm rather than once per encryption/decryption, but some AEAD algorithms incorrectly change the tfm pointer in the request. It's nontrivial to fix these, so to move forward I'm temporarily working around it by resetting the tfm pointer. But they'll need to be fixed.
--- crypto/testmgr.c | 613 ++++++++++++++--------------------------------- crypto/testmgr.h | 47 ---- 2 files changed, 185 insertions(+), 475 deletions(-)
crypto/testmgr.c +185 -428
@@ -1208,443 +1208,222 @@ static int test_hash(struct crypto_ahash *tfm,return 0 ; } -static int __test_aead (struct crypto_aead *tfm,int enc, -const struct aead_testvec *template,unsigned int tcount, -const bool diff_dst,const int align_offset) +static int test_aead_vec_cfg (const char *driver,int enc, +const struct aead_testvec *vec, +unsigned int vec_num, +const struct testvec_config *cfg, +struct aead_request *req, +struct cipher_test_sglists *tsgls) { -const char *algo =crypto_tfm_alg_driver_name (crypto_aead_tfm (tfm)); -unsigned int i, j, k, n, temp; -int ret = -ENOMEM ; -char *q; -char *key; -struct aead_request *req; -struct scatterlist *sg; -struct scatterlist *sgout; -const char *e, *d; -struct crypto_wait wait; -unsigned int authsize, iv_len; -char *iv; -char *xbuf[XBUFSIZE ]; -char *xoutbuf[XBUFSIZE ]; -char *axbuf[XBUFSIZE ]; - - iv =kzalloc (MAX_IVLEN ,GFP_KERNEL ); -if (!iv) -return ret; - key =kmalloc (MAX_KEYLEN ,GFP_KERNEL ); -if (!key) - goto out_noxbuf; -if (testmgr_alloc_buf (xbuf)) - goto out_noxbuf; -if (testmgr_alloc_buf (axbuf)) - goto out_noaxbuf; -if (diff_dst &&testmgr_alloc_buf (xoutbuf)) - goto out_nooutbuf; - -/* avoid "the frame size is larger than 1024 bytes" compiler warning */ - sg =kmalloc (array3_size (sizeof (*sg),8 , (diff_dst ?4 :2 )), -GFP_KERNEL ); -if (!sg) - goto out_nosg; - sgout = &sg[16 ]; - -if (diff_dst) - d ="-ddst" ; -else - d ="" ; +struct crypto_aead *tfm =crypto_aead_reqtfm (req); +const unsigned int alignmask =crypto_aead_alignmask (tfm); +const unsigned int ivsize =crypto_aead_ivsize (tfm); +const unsigned int authsize = vec->clen - vec->plen; +const u32 req_flags =CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; +const char *op = enc ?"encryption" :"decryption" ; +DECLARE_CRYPTO_WAIT (wait); +u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK +1 ) +MAX_IVLEN ]; +u8 *iv =PTR_ALIGN (&_iv[0 ],2 * (MAX_ALGAPI_ALIGNMASK +1 )) + + cfg->iv_offset + + (cfg->iv_offset_relative_to_alignmask ? alignmask :0 ); +struct kvec input[2 ]; +int err; -if (enc ==ENCRYPT ) - e ="encryption" ; +/* Set the key */ +if (vec->wk) +crypto_aead_set_flags (tfm,CRYPTO_TFM_REQ_FORBID_WEAK_KEYS );else - e ="decryption" ; - -crypto_init_wait (&wait); - - req =aead_request_alloc (tfm,GFP_KERNEL ); -if (!req) { -pr_err ("alg: aead%s: Failed to allocate request for %s\n" , - d, algo); - goto out; +crypto_aead_clear_flags (tfm,CRYPTO_TFM_REQ_FORBID_WEAK_KEYS ); + err =crypto_aead_setkey (tfm, vec->key, vec->klen); +if (err) { +if (vec->fail)/* expectedly failed to set key? */ +return 0 ; +pr_err ("alg: aead: %s setkey failed with err %d on test vector %u; flags=%#x\n" , + driver, err, vec_num,crypto_aead_get_flags (tfm)); +return err; } - -aead_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , - crypto_req_done, &wait); - - iv_len =crypto_aead_ivsize (tfm); - -for (i =0 , j =0 ; i < tcount; i++) { -const char *input, *expected_output; -unsigned int inlen, outlen; -char *inbuf, *outbuf, *assocbuf; - -if (template[i].np) -continue ; -if (enc) { -if (template[i].novrfy) -continue ; - input = template[i].ptext; - inlen = template[i].plen; - expected_output = template[i].ctext; - outlen = template[i].clen; - }else { - input = template[i].ctext; - inlen = template[i].clen; - expected_output = template[i].ptext; - outlen = template[i].plen; - } - - j++; - -/* some templates have no input data but they will -* touch input -*/ - inbuf = xbuf[0 ] + align_offset; - assocbuf = axbuf[0 ]; - - ret = -EINVAL ; -if (WARN_ON (align_offset + template[i].clen >PAGE_SIZE || - template[i].alen >PAGE_SIZE )) - goto out; - -memcpy (inbuf, input, inlen); -memcpy (assocbuf, template[i].assoc, template[i].alen); -if (template[i].iv) -memcpy (iv, template[i].iv, iv_len); -else -memset (iv,0 , iv_len); - -crypto_aead_clear_flags (tfm, ~0 ); -if (template[i].wk) -crypto_aead_set_flags (tfm, -CRYPTO_TFM_REQ_FORBID_WEAK_KEYS ); - -if (template[i].klen >MAX_KEYLEN ) { -pr_err ("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n" , - d, j, algo, template[i].klen, -MAX_KEYLEN ); - ret = -EINVAL ; - goto out; - } -memcpy (key, template[i].key, template[i].klen); - - ret =crypto_aead_setkey (tfm, key, template[i].klen); -if (template[i].fail == !ret) { -pr_err ("alg: aead%s: setkey failed on test %d for %s: flags=%x\n" , - d, j, algo,crypto_aead_get_flags (tfm)); - goto out; - }else if (ret) -continue ; - - authsize = template[i].clen - template[i].plen; - ret =crypto_aead_setauthsize (tfm, authsize); -if (ret) { -pr_err ("alg: aead%s: Failed to set authsize to %u on test %d for %s\n" , - d, authsize, j, algo); - goto out; - } - - k = !!template[i].alen; -sg_init_table (sg, k +1 ); -sg_set_buf (&sg[0 ], assocbuf, template[i].alen); -sg_set_buf (&sg[k], inbuf, template[i].clen); - outbuf = inbuf; - -if (diff_dst) { -sg_init_table (sgout, k +1 ); -sg_set_buf (&sgout[0 ], assocbuf, template[i].alen); - - outbuf = xoutbuf[0 ] + align_offset; -sg_set_buf (&sgout[k], outbuf, template[i].clen); - } - -aead_request_set_crypt (req, sg, (diff_dst) ? sgout : sg, inlen, - iv); - -aead_request_set_ad (req, template[i].alen); - - ret =crypto_wait_req (enc ?crypto_aead_encrypt (req) - :crypto_aead_decrypt (req), &wait); - -switch (ret) { -case 0 : -if (template[i].novrfy) { -/* verification was supposed to fail */ -pr_err ("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n" , - d, e, j, algo); -/* so really, we got a bad message */ - ret = -EBADMSG ; - goto out; - } -break ; -case -EBADMSG : -if (template[i].novrfy) -/* verification failure was expected */ -continue ; -/* fall through */ -default : -pr_err ("alg: aead%s: %s failed on test %d for %s: ret=%d\n" , - d, e, j, algo, -ret); - goto out; - } - -if (memcmp (outbuf, expected_output, outlen)) { -pr_err ("alg: aead%s: Test %d failed on %s for %s\n" , - d, j, e, algo); -hexdump (outbuf, outlen); - ret = -EINVAL ; - goto out; - } +if (vec->fail) { +pr_err ("alg: aead: %s setkey unexpectedly succeeded on test vector %u\n" , + driver, vec_num); +return -EINVAL ; } -for (i =0 , j =0 ; i < tcount; i++) { -const char *input, *expected_output; -unsigned int inlen, outlen; - -/* alignment tests are only done with continuous buffers */ -if (align_offset !=0 ) -break ; - -if (!template[i].np) -continue ; - -if (enc) { -if (template[i].novrfy) -continue ; - input = template[i].ptext; - inlen = template[i].plen; - expected_output = template[i].ctext; - outlen = template[i].clen; - }else { - input = template[i].ctext; - inlen = template[i].clen; - expected_output = template[i].ptext; - outlen = template[i].plen; - } - - j++; - -if (template[i].iv) -memcpy (iv, template[i].iv, iv_len); -else -memset (iv,0 ,MAX_IVLEN ); - -crypto_aead_clear_flags (tfm, ~0 ); -if (template[i].wk) -crypto_aead_set_flags (tfm, -CRYPTO_TFM_REQ_FORBID_WEAK_KEYS ); -if (template[i].klen >MAX_KEYLEN ) { -pr_err ("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n" , - d, j, algo, template[i].klen,MAX_KEYLEN ); - ret = -EINVAL ; - goto out; - } -memcpy (key, template[i].key, template[i].klen); - - ret =crypto_aead_setkey (tfm, key, template[i].klen); -if (template[i].fail == !ret) { -pr_err ("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n" , - d, j, algo,crypto_aead_get_flags (tfm)); - goto out; - }else if (ret) -continue ; - - authsize = template[i].clen - template[i].plen; - - ret = -EINVAL ; -sg_init_table (sg, template[i].anp + template[i].np); -if (diff_dst) -sg_init_table (sgout, template[i].anp + template[i].np); - - ret = -EINVAL ; -for (k =0 , temp =0 ; k < template[i].anp; k++) { -if (WARN_ON (offset_in_page (IDX [k]) + - template[i].atap[k] >PAGE_SIZE )) - goto out; -sg_set_buf (&sg[k], -memcpy (axbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]), - template[i].assoc + temp, - template[i].atap[k]), - template[i].atap[k]); -if (diff_dst) -sg_set_buf (&sgout[k], - axbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]), - template[i].atap[k]); - temp += template[i].atap[k]; - } - -for (k =0 , temp =0 ; k < template[i].np; k++) { - n = template[i].tap[k]; -if (k == template[i].np -1 && !enc) - n += authsize; - -if (WARN_ON (offset_in_page (IDX [k]) + n >PAGE_SIZE )) - goto out; - - q = xbuf[IDX [k] >>PAGE_SHIFT ] +offset_in_page (IDX [k]); -memcpy (q, input + temp, n); -sg_set_buf (&sg[template[i].anp + k], q, n); - -if (diff_dst) { - q = xoutbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]); - -memset (q,0 , n); - -sg_set_buf (&sgout[template[i].anp + k], q, n); - } - -if (k == template[i].np -1 && enc) - n += authsize; -if (offset_in_page (q) + n <PAGE_SIZE ) - q[n] =0 ; - - temp += n; - } +/* Set the authentication tag size */ + err =crypto_aead_setauthsize (tfm, authsize); +if (err) { +pr_err ("alg: aead: %s setauthsize failed with err %d on test vector %u\n" , + driver, err, vec_num); +return err; + } - ret =crypto_aead_setauthsize (tfm, authsize); -if (ret) { -pr_err ("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n" , - d, authsize, j, algo); - goto out; - } +/* The IV must be copied to a buffer, as the algorithm may modify it */ +if (WARN_ON (ivsize >MAX_IVLEN )) +return -EINVAL ; +if (vec->iv) +memcpy (iv, vec->iv, ivsize); +else +memset (iv,0 , ivsize); -if (enc) { -if (WARN_ON (sg[template[i].anp + k -1 ].offset + - sg[template[i].anp + k -1 ].length + - authsize >PAGE_SIZE )) { - ret = -EINVAL ; - goto out; - } +/* Build the src/dst scatterlists */ + input[0 ].iov_base = (void *)vec->assoc; + input[0 ].iov_len = vec->alen; + input[1 ].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext; + input[1 ].iov_len = enc ? vec->plen : vec->clen; + err =build_cipher_test_sglists (tsgls, cfg, alignmask, + vec->alen + (enc ? vec->plen : + vec->clen), + vec->alen + (enc ? vec->clen : + vec->plen), + input,2 ); +if (err) { +pr_err ("alg: aead: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return err; + } -if (diff_dst) - sgout[template[i].anp + k -1 ].length += - authsize; - sg[template[i].anp + k -1 ].length += authsize; - } +/* Do the actual encryption or decryption */ +testmgr_poison (req->__ctx,crypto_aead_reqsize (tfm)); +aead_request_set_callback (req, req_flags, crypto_req_done, &wait); +aead_request_set_crypt (req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr, + enc ? vec->plen : vec->clen, iv); +aead_request_set_ad (req, vec->alen); + err =crypto_wait_req (enc ?crypto_aead_encrypt (req) : +crypto_aead_decrypt (req), &wait); -aead_request_set_crypt (req, sg, (diff_dst) ? sgout : sg, - inlen, iv); +aead_request_set_tfm (req, tfm);/* TODO: get rid of this */ -aead_request_set_ad (req, template[i].alen); +if (err) { +if (err == -EBADMSG && vec->novrfy) +return 0 ; +pr_err ("alg: aead: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n" , + driver, op, err, vec_num, cfg->name); +return err; + } +if (vec->novrfy) { +pr_err ("alg: aead: %s %s unexpectedly succeeded on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return -EINVAL ; + } - ret =crypto_wait_req (enc ?crypto_aead_encrypt (req) - :crypto_aead_decrypt (req), &wait); +/* Check for the correct output (ciphertext or plaintext) */ + err =verify_correct_output (&tsgls->dst, enc ? vec->ctext : vec->ptext, + enc ? vec->clen : vec->plen, + vec->alen, enc || !cfg->inplace); +if (err == -EOVERFLOW ) { +pr_err ("alg: aead: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return err; + } +if (err) { +pr_err ("alg: aead: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return err; + } -switch (ret) { -case 0 : -if (template[i].novrfy) { -/* verification was supposed to fail */ -pr_err ("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n" , - d, e, j, algo); -/* so really, we got a bad message */ - ret = -EBADMSG ; - goto out; - } -break ; -case -EBADMSG : -if (template[i].novrfy) -/* verification failure was expected */ -continue ; -/* fall through */ -default : -pr_err ("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n" , - d, e, j, algo, -ret); - goto out; - } +return 0 ; +} - ret = -EINVAL ; -for (k =0 , temp =0 ; k < template[i].np; k++) { -if (diff_dst) - q = xoutbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]); -else - q = xbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]); +static int test_aead_vec (const char *driver,int enc, +const struct aead_testvec *vec,unsigned int vec_num, +struct aead_request *req, +struct cipher_test_sglists *tsgls) +{ +unsigned int i; +int err; - n = template[i].tap[k]; -if (k == template[i].np -1 && enc) - n += authsize; +if (enc && vec->novrfy) +return 0 ; -if (memcmp (q, expected_output + temp, n)) { -pr_err ("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n" , - d, j, e, k, algo); -hexdump (q, n); - goto out; - } +for (i =0 ; i <ARRAY_SIZE (default_cipher_testvec_configs); i++) { + err =test_aead_vec_cfg (driver, enc, vec, vec_num, + &default_cipher_testvec_configs[i], + req, tsgls); +if (err) +return err; + } - q += n; -if (k == template[i].np -1 && !enc) { -if (!diff_dst &&memcmp (q, input + temp + n, - authsize)) - n = authsize; -else - n =0 ; - }else { -for (n =0 ;offset_in_page (q + n) && q[n]; n++) - ; - } -if (n) { -pr_err ("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n" , - d, j, e, k, algo, n); -hexdump (q, n); - goto out; - } +#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS +if (!noextratests) { +struct testvec_config cfg; +char cfgname[TESTVEC_CONFIG_NAMELEN ]; - temp += template[i].tap[k]; +for (i =0 ; i < fuzz_iterations; i++) { +generate_random_testvec_config (&cfg, cfgname, +sizeof (cfgname)); + err =test_aead_vec_cfg (driver, enc, vec, vec_num, + &cfg, req, tsgls); +if (err) +return err; } } +#endif +return 0 ; +} - ret =0 ; +static int test_aead (const char *driver,int enc, +const struct aead_test_suite *suite, +struct aead_request *req, +struct cipher_test_sglists *tsgls) +{ +unsigned int i; +int err; -out: -aead_request_free (req); -kfree (sg); -out_nosg: -if (diff_dst) -testmgr_free_buf (xoutbuf); -out_nooutbuf: -testmgr_free_buf (axbuf); -out_noaxbuf: -testmgr_free_buf (xbuf); -out_noxbuf: -kfree (key); -kfree (iv); -return ret; +for (i =0 ; i < suite->count; i++) { + err =test_aead_vec (driver, enc, &suite->vecs[i], i, req, + tsgls); +if (err) +return err; + } +return 0 ; } -static int test_aead (struct crypto_aead *tfm,int enc, -const struct aead_testvec *template,unsigned int tcount) +static int alg_test_aead (const struct alg_test_desc *desc,const char *driver, +u32 type,u32 mask) { -unsigned int alignmask; -int ret; +const struct aead_test_suite *suite = &desc->suite.aead; +struct crypto_aead *tfm; +struct aead_request *req =NULL ; +struct cipher_test_sglists *tsgls =NULL ; +int err; -/* test 'dst == src' case */ - ret =__test_aead (tfm, enc, template, tcount, false,0 ); -if (ret) -return ret; +if (suite->count <=0 ) { +pr_err ("alg: aead: empty test suite for %s\n" , driver); +return -EINVAL ; + } -/* test 'dst != src' case */ - ret =__test_aead (tfm, enc, template, tcount, true,0 ); -if (ret) -return ret; + tfm =crypto_alloc_aead (driver, type, mask); +if (IS_ERR (tfm)) { +pr_err ("alg: aead: failed to allocate transform for %s: %ld\n" , + driver,PTR_ERR (tfm)); +return PTR_ERR (tfm); + } -/* test unaligned buffers, check with one byte offset */ - ret =__test_aead (tfm, enc, template, tcount, true,1 ); -if (ret) -return ret; + req =aead_request_alloc (tfm,GFP_KERNEL ); +if (!req) { +pr_err ("alg: aead: failed to allocate request for %s\n" , + driver); + err = -ENOMEM ; + goto out; + } - alignmask =crypto_tfm_alg_alignmask (&tfm->base); -if (alignmask) { -/* Check if alignment mask for tfm is correctly set. */ - ret =__test_aead (tfm, enc, template, tcount, true, - alignmask +1 ); -if (ret) -return ret; + tsgls =alloc_cipher_test_sglists (); +if (!tsgls) { +pr_err ("alg: aead: failed to allocate test buffers for %s\n" , + driver); + err = -ENOMEM ; + goto out; } -return 0 ; + err =test_aead (driver,ENCRYPT , suite, req, tsgls); +if (err) + goto out; + + err =test_aead (driver,DECRYPT , suite, req, tsgls); +out: +free_cipher_test_sglists (tsgls); +aead_request_free (req); +crypto_free_aead (tfm); +return err; } static int test_cipher(struct crypto_cipher *tfm, int enc,
@@ -2274,28 +2053,6 @@ static int test_cprng(struct crypto_rng *tfm,return err; } -static int alg_test_aead (const struct alg_test_desc *desc,const char *driver, -u32 type,u32 mask) -{ -const struct aead_test_suite *suite = &desc->suite.aead; -struct crypto_aead *tfm; -int err; - - tfm =crypto_alloc_aead (driver, type, mask); -if (IS_ERR (tfm)) { -printk (KERN_ERR "alg: aead: Failed to load transform for %s: " -"%ld\n" , driver,PTR_ERR (tfm)); -return PTR_ERR (tfm); - } - - err =test_aead (tfm,ENCRYPT , suite->vecs, suite->count); -if (!err) - err =test_aead (tfm,DECRYPT , suite->vecs, suite->count); - -crypto_free_aead (tfm); -return err; -} -static int alg_test_cipher (const struct alg_test_desc *desc,const char *driver,u32 type,u32 mask) {
crypto/testmgr.h +0 -47
71 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 13/15 crypto: testmgr - convert hash testing to use testvec_configs
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Convert alg_test_hash() to use the new test framework, adding a list of testvec_configs to test by default. When the extra self-tests are enabled, randomly generated testvec_configs are tested as well.
This improves hash test coverage mainly because now all algorithms have a variety of data layouts tested, whereas before each algorithm was responsible for declaring its own chunked test cases which were often missing or provided poor test coverage. The new code also tests both the MAY_SLEEP and !MAY_SLEEP cases and buffers that cross pages.
This already found bugs in the hash walk code and in the arm32 and arm64 implementations of crct10dif.
I removed the hash chunked test vectors that were the same as non-chunked ones, but left the ones that were unique.
--- crypto/testmgr.c | 795 ++++++++++++++++++++--------------------------- crypto/testmgr.h | 107 +------ 2 files changed, 352 insertions(+), 550 deletions(-)
crypto/testmgr.c +343 -452
@@ -71,18 +71,6 @@ int alg_test(const char *driver, const char *alg, u32 type, u32 mask)*/ #define XBUFSIZE 8 -/* -* Indexes into the xbuf to simulate cross-page access. -*/ -#define IDX1 32 -#define IDX2 32400 -#define IDX3 1511 -#define IDX4 8193 -#define IDX5 22222 -#define IDX6 17101 -#define IDX7 27333 -#define IDX8 3000 -/* * Used by test_cipher() */
@@ -149,9 +137,6 @@ struct alg_test_desc { } suite; }; -static const unsigned int IDX [8 ] = { -IDX1 ,IDX2 ,IDX3 ,IDX4 ,IDX5 ,IDX6 ,IDX7 ,IDX8 }; -static void hexdump (unsigned char *buf,unsigned int len) { print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
@@ -346,6 +331,79 @@ static const struct testvec_config default_cipher_testvec_configs[] = { } }; +static const struct testvec_config default_hash_testvec_configs[] = { + { + .name ="init+update+final aligned buffer" , + .src_divs = { { .proportion_of_total =10000 } }, + .finalization_type =FINALIZATION_TYPE_FINAL , + }, { + .name ="init+finup aligned buffer" , + .src_divs = { { .proportion_of_total =10000 } }, + .finalization_type =FINALIZATION_TYPE_FINUP , + }, { + .name ="digest aligned buffer" , + .src_divs = { { .proportion_of_total =10000 } }, + .finalization_type =FINALIZATION_TYPE_DIGEST , + }, { + .name ="init+update+final misaligned buffer" , + .src_divs = { { .proportion_of_total =10000 , .offset =1 } }, + .finalization_type =FINALIZATION_TYPE_FINAL , + }, { + .name ="digest buffer aligned only to alignmask" , + .src_divs = { + { + .proportion_of_total =10000 , + .offset =1 , + .offset_relative_to_alignmask = true, + }, + }, + .finalization_type =FINALIZATION_TYPE_DIGEST , + }, { + .name ="init+update+update+final two even splits" , + .src_divs = { + { .proportion_of_total =5000 }, + { + .proportion_of_total =5000 , + .flush_type =FLUSH_TYPE_FLUSH , + }, + }, + .finalization_type =FINALIZATION_TYPE_FINAL , + }, { + .name ="digest uneven misaligned splits, may sleep" , + .req_flags =CRYPTO_TFM_REQ_MAY_SLEEP , + .src_divs = { + { .proportion_of_total =1900 , .offset =33 }, + { .proportion_of_total =3300 , .offset =7 }, + { .proportion_of_total =4800 , .offset =18 }, + }, + .finalization_type =FINALIZATION_TYPE_DIGEST , + }, { + .name ="digest misaligned splits crossing pages" , + .src_divs = { + { + .proportion_of_total =7500 , + .offset =PAGE_SIZE -32 , + }, { + .proportion_of_total =2500 , + .offset =PAGE_SIZE -7 , + }, + }, + .finalization_type =FINALIZATION_TYPE_DIGEST , + }, { + .name ="import/export" , + .src_divs = { + { + .proportion_of_total =6500 , + .flush_type =FLUSH_TYPE_REIMPORT , + }, { + .proportion_of_total =3500 , + .flush_type =FLUSH_TYPE_REIMPORT , + }, + }, + .finalization_type =FINALIZATION_TYPE_FINAL , + } +}; +static unsigned int count_test_sg_divisions (const struct test_sg_division *divs) {unsigned int remaining =TEST_SG_TOTAL ;
@@ -782,430 +840,320 @@ static void generate_random_testvec_config(struct testvec_config *cfg, }#endif /* CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */ -static int ahash_guard_result (char *result,char c,int size) +static int check_nonfinal_hash_op (const char *op,int err, +u8 *result,unsigned int digestsize, +const char *driver,unsigned int vec_num, +const struct testvec_config *cfg) { -int i; - -for (i =0 ; i < size; i++) { -if (result[i] != c) -return -EINVAL ; - } - -return 0 ; -} - -static int ahash_partial_update (struct ahash_request **preq, -struct crypto_ahash *tfm,const struct hash_testvec *template, -void *hash_buff,int k,int temp,struct scatterlist *sg, -const char *algo,char *result,struct crypto_wait *wait) -{ -char *state; -struct ahash_request *req; -int statesize, ret = -EINVAL ; -static const unsigned char guard[] = {0x00 ,0xba ,0xad ,0x00 }; -int digestsize =crypto_ahash_digestsize (tfm); - - req = *preq; - statesize =crypto_ahash_statesize ( -crypto_ahash_reqtfm (req)); - state =kmalloc (statesize +sizeof (guard),GFP_KERNEL ); -if (!state) { -pr_err ("alg: hash: Failed to alloc state for %s\n" , algo); - goto out_nostate; - } -memcpy (state + statesize, guard,sizeof (guard)); -memset (result,1 , digestsize); - ret =crypto_ahash_export (req, state); -WARN_ON (memcmp (state + statesize, guard,sizeof (guard))); -if (ret) { -pr_err ("alg: hash: Failed to export() for %s\n" , algo); - goto out; - } - ret =ahash_guard_result (result,1 , digestsize); -if (ret) { -pr_err ("alg: hash: Failed, export used req->result for %s\n" , - algo); - goto out; - } -ahash_request_free (req); - req =ahash_request_alloc (tfm,GFP_KERNEL ); -if (!req) { -pr_err ("alg: hash: Failed to alloc request for %s\n" , algo); - goto out_noreq; - } -ahash_request_set_callback (req, -CRYPTO_TFM_REQ_MAY_BACKLOG , - crypto_req_done, wait); - -memcpy (hash_buff, template->plaintext + temp, - template->tap[k]); -sg_init_one (&sg[0 ], hash_buff, template->tap[k]); -ahash_request_set_crypt (req, sg, result, template->tap[k]); - ret =crypto_ahash_import (req, state); -if (ret) { -pr_err ("alg: hash: Failed to import() for %s\n" , algo); - goto out; +if (err) { +pr_err ("alg: hash: %s %s() failed with err %d on test vector %u, cfg=\"%s\"\n" , + driver, op, err, vec_num, cfg->name); +return err; } - ret =ahash_guard_result (result,1 , digestsize); -if (ret) { -pr_err ("alg: hash: Failed, import used req->result for %s\n" , - algo); - goto out; +if (!testmgr_is_poison (result, digestsize)) { +pr_err ("alg: hash: %s %s() used result buffer on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return -EINVAL ; } - ret =crypto_wait_req (crypto_ahash_update (req), wait); -if (ret) - goto out; - *preq = req; - ret =0 ; - goto out_noreq; -out: -ahash_request_free (req); -out_noreq: -kfree (state); -out_nostate: -return ret; +return 0 ; } -enum hash_test { -HASH_TEST_DIGEST , -HASH_TEST_FINAL , -HASH_TEST_FINUP -}; - -static int __test_hash (struct crypto_ahash *tfm, -const struct hash_testvec *template,unsigned int tcount, -enum hash_test test_type,const int align_offset) +static int test_hash_vec_cfg (const char *driver, +const struct hash_testvec *vec, +unsigned int vec_num, +const struct testvec_config *cfg, +struct ahash_request *req, +struct test_sglist *tsgl, +u8 *hashstate) { -const char *algo =crypto_tfm_alg_driver_name (crypto_ahash_tfm (tfm)); -size_t digest_size =crypto_ahash_digestsize (tfm); -unsigned int i, j, k, temp; -struct scatterlist sg[8 ]; -char *result; -char *key; -struct ahash_request *req; -struct crypto_wait wait; -void *hash_buff; -char *xbuf[XBUFSIZE ]; -int ret = -ENOMEM ; - - result =kmalloc (digest_size,GFP_KERNEL ); -if (!result) -return ret; - key =kmalloc (MAX_KEYLEN ,GFP_KERNEL ); -if (!key) - goto out_nobuf; -if (testmgr_alloc_buf (xbuf)) - goto out_nobuf; +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +const unsigned int alignmask =crypto_ahash_alignmask (tfm); +const unsigned int digestsize =crypto_ahash_digestsize (tfm); +const unsigned int statesize =crypto_ahash_statesize (tfm); +const u32 req_flags =CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; +const struct test_sg_division *divs[XBUFSIZE ]; +DECLARE_CRYPTO_WAIT (wait); +struct kvec _input; +struct iov_iter input; +unsigned int i; +struct scatterlist *pending_sgl; +unsigned int pending_len; +u8 result[HASH_MAX_DIGESTSIZE +TESTMGR_POISON_LEN ]; +int err; -crypto_init_wait (&wait); +/* Set the key, if specified */ +if (vec->ksize) { + err =crypto_ahash_setkey (tfm, vec->key, vec->ksize); +if (err) { +pr_err ("alg: hash: %s setkey failed with err %d on test vector %u; flags=%#x\n" , + driver, err, vec_num, +crypto_ahash_get_flags (tfm)); +return err; + } + } - req =ahash_request_alloc (tfm,GFP_KERNEL ); -if (!req) { -printk (KERN_ERR "alg: hash: Failed to allocate request for " -"%s\n" , algo); - goto out_noreq; +/* Build the scatterlist for the source data */ + _input.iov_base = (void *)vec->plaintext; + _input.iov_len = vec->psize; +iov_iter_kvec (&input,WRITE , &_input,1 , vec->psize); + err =build_test_sglist (tsgl, cfg->src_divs, alignmask, vec->psize, + &input, divs); +if (err) { +pr_err ("alg: hash: %s: error preparing scatterlist for test vector %u, cfg=\"%s\"\n" , + driver, vec_num, cfg->name); +return err; } -ahash_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , - crypto_req_done, &wait); - j =0 ; -for (i =0 ; i < tcount; i++) { -if (template[i].np) -continue ; +/* Do the actual hashing */ - ret = -EINVAL ; -if (WARN_ON (align_offset + template[i].psize >PAGE_SIZE )) - goto out; +testmgr_poison (req->__ctx,crypto_ahash_reqsize (tfm)); +testmgr_poison (result, digestsize +TESTMGR_POISON_LEN ); - j++; -memset (result,0 , digest_size); +if (cfg->finalization_type ==FINALIZATION_TYPE_DIGEST ) { +/* Just using digest() */ +ahash_request_set_callback (req, req_flags, crypto_req_done, + &wait); +ahash_request_set_crypt (req, tsgl->sgl, result, vec->psize); + err =crypto_wait_req (crypto_ahash_digest (req), &wait); +if (err) { +pr_err ("alg: hash: %s digest() failed with err %d on test vector %u, cfg=\"%s\"\n" , + driver, err, vec_num, cfg->name); +return err; + } + goto result_ready; + } - hash_buff = xbuf[0 ]; - hash_buff += align_offset; +/* Using init(), zero or more update(), then final() or finup() */ -memcpy (hash_buff, template[i].plaintext, template[i].psize); -sg_init_one (&sg[0 ], hash_buff, template[i].psize); +ahash_request_set_callback (req, req_flags, crypto_req_done, &wait); +ahash_request_set_crypt (req,NULL , result,0 ); + err =crypto_wait_req (crypto_ahash_init (req), &wait); + err =check_nonfinal_hash_op ("init" , err, result, digestsize, + driver, vec_num, cfg); +if (err) +return err; -if (template[i].ksize) { -crypto_ahash_clear_flags (tfm, ~0 ); -if (template[i].ksize >MAX_KEYLEN ) { -pr_err ("alg: hash: setkey failed on test %d for %s: key size %d > %d\n" , - j, algo, template[i].ksize,MAX_KEYLEN ); - ret = -EINVAL ; - goto out; - } -memcpy (key, template[i].key, template[i].ksize); - ret =crypto_ahash_setkey (tfm, key, template[i].ksize); -if (ret) { -printk (KERN_ERR "alg: hash: setkey failed on " -"test %d for %s: ret=%d\n" , j, algo, - -ret); - goto out; - } + pending_sgl =NULL ; + pending_len =0 ; +for (i =0 ; i < tsgl->nents; i++) { +if (divs[i]->flush_type !=FLUSH_TYPE_NONE && + pending_sgl !=NULL ) { +/* update() with the pending data */ +ahash_request_set_callback (req, req_flags, + crypto_req_done, &wait); +ahash_request_set_crypt (req, pending_sgl, result, + pending_len); + err =crypto_wait_req (crypto_ahash_update (req), &wait); + err =check_nonfinal_hash_op ("update" , err, + result, digestsize, + driver, vec_num, cfg); +if (err) +return err; + pending_sgl =NULL ; + pending_len =0 ; } - -ahash_request_set_crypt (req, sg, result, template[i].psize); -switch (test_type) { -case HASH_TEST_DIGEST : - ret =crypto_wait_req (crypto_ahash_digest (req), &wait); -if (ret) { -pr_err ("alg: hash: digest failed on test %d " -"for %s: ret=%d\n" , j, algo, -ret); - goto out; - } -break ; - -case HASH_TEST_FINAL : -memset (result,1 , digest_size); - ret =crypto_wait_req (crypto_ahash_init (req), &wait); -if (ret) { -pr_err ("alg: hash: init failed on test %d " -"for %s: ret=%d\n" , j, algo, -ret); - goto out; - } - ret =ahash_guard_result (result,1 , digest_size); -if (ret) { -pr_err ("alg: hash: init failed on test %d " -"for %s: used req->result\n" , j, algo); - goto out; - } - ret =crypto_wait_req (crypto_ahash_update (req), &wait); -if (ret) { -pr_err ("alg: hash: update failed on test %d " -"for %s: ret=%d\n" , j, algo, -ret); - goto out; - } - ret =ahash_guard_result (result,1 , digest_size); -if (ret) { -pr_err ("alg: hash: update failed on test %d " -"for %s: used req->result\n" , j, algo); - goto out; - } - ret =crypto_wait_req (crypto_ahash_final (req), &wait); -if (ret) { -pr_err ("alg: hash: final failed on test %d " -"for %s: ret=%d\n" , j, algo, -ret); - goto out; +if (divs[i]->flush_type ==FLUSH_TYPE_REIMPORT ) { +/* Test ->export() and ->import() */ +testmgr_poison (hashstate + statesize, +TESTMGR_POISON_LEN ); + err =crypto_ahash_export (req, hashstate); + err =check_nonfinal_hash_op ("export" , err, + result, digestsize, + driver, vec_num, cfg); +if (err) +return err; +if (!testmgr_is_poison (hashstate + statesize, +TESTMGR_POISON_LEN )) { +pr_err ("alg: hash: %s export() overran state buffer on test vector %u, cfg=\"%s\"\n" , + driver, vec_num, cfg->name); +return -EOVERFLOW ; } -break ; -case HASH_TEST_FINUP : -memset (result,1 , digest_size); - ret =crypto_wait_req (crypto_ahash_init (req), &wait); -if (ret) { -pr_err ("alg: hash: init failed on test %d " -"for %s: ret=%d\n" , j, algo, -ret); - goto out; - } - ret =ahash_guard_result (result,1 , digest_size); -if (ret) { -pr_err ("alg: hash: init failed on test %d " -"for %s: used req->result\n" , j, algo); - goto out; - } - ret =crypto_wait_req (crypto_ahash_finup (req), &wait); -if (ret) { -pr_err ("alg: hash: final failed on test %d " -"for %s: ret=%d\n" , j, algo, -ret); - goto out; - } -break ; +testmgr_poison (req->__ctx,crypto_ahash_reqsize (tfm)); + err =crypto_ahash_import (req, hashstate); + err =check_nonfinal_hash_op ("import" , err, + result, digestsize, + driver, vec_num, cfg); +if (err) +return err; } +if (pending_sgl ==NULL ) + pending_sgl = &tsgl->sgl[i]; + pending_len += tsgl->sgl[i].length; + } -if (memcmp (result, template[i].digest, -crypto_ahash_digestsize (tfm))) { -printk (KERN_ERR "alg: hash: Test %d failed for %s\n" , - j, algo); -hexdump (result,crypto_ahash_digestsize (tfm)); - ret = -EINVAL ; - goto out; +ahash_request_set_callback (req, req_flags, crypto_req_done, &wait); +ahash_request_set_crypt (req, pending_sgl, result, pending_len); +if (cfg->finalization_type ==FINALIZATION_TYPE_FINAL ) { +/* finish with update() and final() */ + err =crypto_wait_req (crypto_ahash_update (req), &wait); + err =check_nonfinal_hash_op ("update" , err, result, digestsize, + driver, vec_num, cfg); +if (err) +return err; + err =crypto_wait_req (crypto_ahash_final (req), &wait); +if (err) { +pr_err ("alg: hash: %s final() failed with err %d on test vector %u, cfg=\"%s\"\n" , + driver, err, vec_num, cfg->name); +return err; + } + }else { +/* finish with finup() */ + err =crypto_wait_req (crypto_ahash_finup (req), &wait); +if (err) { +pr_err ("alg: hash: %s finup() failed with err %d on test vector %u, cfg=\"%s\"\n" , + driver, err, vec_num, cfg->name); +return err; } } -if (test_type) - goto out; - - j =0 ; -for (i =0 ; i < tcount; i++) { -/* alignment tests are only done with continuous buffers */ -if (align_offset !=0 ) -break ; +result_ready: +/* Check that the algorithm produced the correct digest */ +if (memcmp (result, vec->digest, digestsize) !=0 ) { +pr_err ("alg: hash: %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n" , + driver, vec_num, cfg->name); +return -EINVAL ; + } +if (!testmgr_is_poison (&result[digestsize],TESTMGR_POISON_LEN )) { +pr_err ("alg: hash: %s overran result buffer on test vector %u, cfg=\"%s\"\n" , + driver, vec_num, cfg->name); +return -EOVERFLOW ; + } -if (!template[i].np) -continue ; +return 0 ; +} - j++; -memset (result,0 , digest_size); +static int test_hash_vec (const char *driver,const struct hash_testvec *vec, +unsigned int vec_num,struct ahash_request *req, +struct test_sglist *tsgl,u8 *hashstate) +{ +unsigned int i; +int err; - temp =0 ; -sg_init_table (sg, template[i].np); - ret = -EINVAL ; -for (k =0 ; k < template[i].np; k++) { -if (WARN_ON (offset_in_page (IDX [k]) + - template[i].tap[k] >PAGE_SIZE )) - goto out; -sg_set_buf (&sg[k], -memcpy (xbuf[IDX [k] >>PAGE_SHIFT ] + -offset_in_page (IDX [k]), - template[i].plaintext + temp, - template[i].tap[k]), - template[i].tap[k]); - temp += template[i].tap[k]; - } - -if (template[i].ksize) { -if (template[i].ksize >MAX_KEYLEN ) { -pr_err ("alg: hash: setkey failed on test %d for %s: key size %d > %d\n" , - j, algo, template[i].ksize,MAX_KEYLEN ); - ret = -EINVAL ; - goto out; - } -crypto_ahash_clear_flags (tfm, ~0 ); -memcpy (key, template[i].key, template[i].ksize); - ret =crypto_ahash_setkey (tfm, key, template[i].ksize); - -if (ret) { -printk (KERN_ERR "alg: hash: setkey " -"failed on chunking test %d " -"for %s: ret=%d\n" , j, algo, -ret); - goto out; - } - } +for (i =0 ; i <ARRAY_SIZE (default_hash_testvec_configs); i++) { + err =test_hash_vec_cfg (driver, vec, vec_num, + &default_hash_testvec_configs[i], + req, tsgl, hashstate); +if (err) +return err; + } -ahash_request_set_crypt (req, sg, result, template[i].psize); - ret =crypto_wait_req (crypto_ahash_digest (req), &wait); -if (ret) { -pr_err ("alg: hash: digest failed on chunking test %d for %s: ret=%d\n" , - j, algo, -ret); - goto out; - } +#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS +if (!noextratests) { +struct testvec_config cfg; +char cfgname[TESTVEC_CONFIG_NAMELEN ]; -if (memcmp (result, template[i].digest, -crypto_ahash_digestsize (tfm))) { -printk (KERN_ERR "alg: hash: Chunking test %d " -"failed for %s\n" , j, algo); -hexdump (result,crypto_ahash_digestsize (tfm)); - ret = -EINVAL ; - goto out; +for (i =0 ; i < fuzz_iterations; i++) { +generate_random_testvec_config (&cfg, cfgname, +sizeof (cfgname)); + err =test_hash_vec_cfg (driver, vec, vec_num, &cfg, + req, tsgl, hashstate); +if (err) +return err; } } +#endif +return 0 ; +} -/* partial update exercise */ - j =0 ; -for (i =0 ; i < tcount; i++) { -/* alignment tests are only done with continuous buffers */ -if (align_offset !=0 ) -break ; +static int __alg_test_hash (const struct hash_testvec *vecs, +unsigned int num_vecs,const char *driver, +u32 type,u32 mask) +{ +struct crypto_ahash *tfm; +struct ahash_request *req =NULL ; +struct test_sglist *tsgl =NULL ; +u8 *hashstate =NULL ; +unsigned int i; +int err; -if (template[i].np <2 ) -continue ; + tfm =crypto_alloc_ahash (driver, type, mask); +if (IS_ERR (tfm)) { +pr_err ("alg: hash: failed to allocate transform for %s: %ld\n" , + driver,PTR_ERR (tfm)); +return PTR_ERR (tfm); + } - j++; -memset (result,0 , digest_size); + req =ahash_request_alloc (tfm,GFP_KERNEL ); +if (!req) { +pr_err ("alg: hash: failed to allocate request for %s\n" , + driver); + err = -ENOMEM ; + goto out; + } - ret = -EINVAL ; - hash_buff = xbuf[0 ]; -memcpy (hash_buff, template[i].plaintext, - template[i].tap[0 ]); -sg_init_one (&sg[0 ], hash_buff, template[i].tap[0 ]); - -if (template[i].ksize) { -crypto_ahash_clear_flags (tfm, ~0 ); -if (template[i].ksize >MAX_KEYLEN ) { -pr_err ("alg: hash: setkey failed on test %d for %s: key size %d > %d\n" , - j, algo, template[i].ksize,MAX_KEYLEN ); - ret = -EINVAL ; - goto out; - } -memcpy (key, template[i].key, template[i].ksize); - ret =crypto_ahash_setkey (tfm, key, template[i].ksize); -if (ret) { -pr_err ("alg: hash: setkey failed on test %d for %s: ret=%d\n" , - j, algo, -ret); - goto out; - } - } + tsgl =kmalloc (sizeof (*tsgl),GFP_KERNEL ); +if (!tsgl ||init_test_sglist (tsgl) !=0 ) { +pr_err ("alg: hash: failed to allocate test buffers for %s\n" , + driver); +kfree (tsgl); + tsgl =NULL ; + err = -ENOMEM ; + goto out; + } -ahash_request_set_crypt (req, sg, result, template[i].tap[0 ]); - ret =crypto_wait_req (crypto_ahash_init (req), &wait); -if (ret) { -pr_err ("alg: hash: init failed on test %d for %s: ret=%d\n" , - j, algo, -ret); - goto out; - } - ret =crypto_wait_req (crypto_ahash_update (req), &wait); -if (ret) { -pr_err ("alg: hash: update failed on test %d for %s: ret=%d\n" , - j, algo, -ret); - goto out; - } + hashstate =kmalloc (crypto_ahash_statesize (tfm) +TESTMGR_POISON_LEN , +GFP_KERNEL ); +if (!hashstate) { +pr_err ("alg: hash: failed to allocate hash state buffer for %s\n" , + driver); + err = -ENOMEM ; + goto out; + } - temp = template[i].tap[0 ]; -for (k =1 ; k < template[i].np; k++) { - ret =ahash_partial_update (&req, tfm, &template[i], - hash_buff, k, temp, &sg[0 ], algo, result, - &wait); -if (ret) { -pr_err ("alg: hash: partial update failed on test %d for %s: ret=%d\n" , - j, algo, -ret); - goto out_noreq; - } - temp += template[i].tap[k]; - } - ret =crypto_wait_req (crypto_ahash_final (req), &wait); -if (ret) { -pr_err ("alg: hash: final failed on test %d for %s: ret=%d\n" , - j, algo, -ret); - goto out; - } -if (memcmp (result, template[i].digest, -crypto_ahash_digestsize (tfm))) { -pr_err ("alg: hash: Partial Test %d failed for %s\n" , - j, algo); -hexdump (result,crypto_ahash_digestsize (tfm)); - ret = -EINVAL ; +for (i =0 ; i < num_vecs; i++) { + err =test_hash_vec (driver, &vecs[i], i, req, tsgl, hashstate); +if (err) goto out; - } } - - ret =0 ; - + err =0 ; out: +kfree (hashstate); +if (tsgl) { +destroy_test_sglist (tsgl); +kfree (tsgl); + }ahash_request_free (req); -out_noreq: -testmgr_free_buf (xbuf); -out_nobuf: -kfree (key); -kfree (result); -return ret; +crypto_free_ahash (tfm); +return err; } -static int test_hash (struct crypto_ahash *tfm, -const struct hash_testvec *template, -unsigned int tcount,enum hash_test test_type) +static int alg_test_hash (const struct alg_test_desc *desc,const char *driver, +u32 type,u32 mask) { -unsigned int alignmask; -int ret; +const struct hash_testvec *template = desc->suite.hash.vecs; +unsigned int tcount = desc->suite.hash.count; +unsigned int nr_unkeyed, nr_keyed; +int err; - ret =__test_hash (tfm, template, tcount, test_type,0 ); -if (ret) -return ret; +/* +* For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests +* first, before setting a key on the tfm. To make this easier, we +* require that the unkeyed test vectors (if any) are listed first. +*/ -/* test unaligned buffers, check with one byte offset */ - ret =__test_hash (tfm, template, tcount, test_type,1 ); -if (ret) -return ret; +for (nr_unkeyed =0 ; nr_unkeyed < tcount; nr_unkeyed++) { +if (template[nr_unkeyed].ksize) +break ; + } +for (nr_keyed =0 ; nr_unkeyed + nr_keyed < tcount; nr_keyed++) { +if (!template[nr_unkeyed + nr_keyed].ksize) { +pr_err ("alg: hash: test vectors for %s out of order, " +"unkeyed ones must come first\n" , desc->alg); +return -EINVAL ; + } + } - alignmask =crypto_tfm_alg_alignmask (&tfm->base); -if (alignmask) { -/* Check if alignment mask for tfm is correctly set. */ - ret =__test_hash (tfm, template, tcount, test_type, - alignmask +1 ); -if (ret) -return ret; + err =0 ; +if (nr_unkeyed) { + err =__alg_test_hash (template, nr_unkeyed, driver, type, mask); + template += nr_unkeyed; } -return 0 ; +if (!err && nr_keyed) + err =__alg_test_hash (template, nr_keyed, driver, type, mask); + +return err; } static int test_aead_vec_cfg(const char *driver, int enc,
@@ -2113,67 +2061,6 @@ static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,return err; } -static int __alg_test_hash (const struct hash_testvec *template, -unsigned int tcount,const char *driver, -u32 type,u32 mask) -{ -struct crypto_ahash *tfm; -int err; - - tfm =crypto_alloc_ahash (driver, type, mask); -if (IS_ERR (tfm)) { -printk (KERN_ERR "alg: hash: Failed to load transform for %s: " -"%ld\n" , driver,PTR_ERR (tfm)); -return PTR_ERR (tfm); - } - - err =test_hash (tfm, template, tcount,HASH_TEST_DIGEST ); -if (!err) - err =test_hash (tfm, template, tcount,HASH_TEST_FINAL ); -if (!err) - err =test_hash (tfm, template, tcount,HASH_TEST_FINUP ); -crypto_free_ahash (tfm); -return err; -} - -static int alg_test_hash (const struct alg_test_desc *desc,const char *driver, -u32 type,u32 mask) -{ -const struct hash_testvec *template = desc->suite.hash.vecs; -unsigned int tcount = desc->suite.hash.count; -unsigned int nr_unkeyed, nr_keyed; -int err; - -/* -* For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests -* first, before setting a key on the tfm. To make this easier, we -* require that the unkeyed test vectors (if any) are listed first. -*/ - -for (nr_unkeyed =0 ; nr_unkeyed < tcount; nr_unkeyed++) { -if (template[nr_unkeyed].ksize) -break ; - } -for (nr_keyed =0 ; nr_unkeyed + nr_keyed < tcount; nr_keyed++) { -if (!template[nr_unkeyed + nr_keyed].ksize) { -pr_err ("alg: hash: test vectors for %s out of order, " -"unkeyed ones must come first\n" , desc->alg); -return -EINVAL ; - } - } - - err =0 ; -if (nr_unkeyed) { - err =__alg_test_hash (template, nr_unkeyed, driver, type, mask); - template += nr_unkeyed; - } - -if (!err && nr_keyed) - err =__alg_test_hash (template, nr_keyed, driver, type, mask); - -return err; -} -static int alg_test_crc32c (const struct alg_test_desc *desc,const char *driver,u32 type,u32 mask) {
@@ -3959,6 +3846,10 @@ static void alg_check_testvec_configs(void)for (i =0 ; i <ARRAY_SIZE (default_cipher_testvec_configs); i++)WARN_ON (!valid_testvec_config ( &default_cipher_testvec_configs[i])); + +for (i =0 ; i <ARRAY_SIZE (default_hash_testvec_configs); i++) +WARN_ON (!valid_testvec_config ( + &default_hash_testvec_configs[i])); }static void testmgr_onetime_init (void )
crypto/testmgr.h +9 -98
366 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v2 13/15] crypto: testmgr - convert hash testing to use testvec_configs
Hi Eric,
Le 01/02/2019 à 08:51, Eric Biggers a écrit :
Quoted 28 lines
From: Eric Biggers <[email protected]>
Convert
alg_test_hash()to use the new test framework, adding a list oftestvec_configsto test by default. When the extra self-tests are enabled, randomly generatedtestvec_configsare tested as well.This improves hash test coverage mainly because now all algorithms have a variety of data layouts tested, whereas before each algorithm was responsible for declaring its own chunked test cases which were often missing or provided poor test coverage. The new code also tests both the
MAY_SLEEPand !MAY_SLEEP cases and buffers that cross pages.This already found bugs in the hash walk code and in the arm32 and arm64 implementations of crct10dif.
I removed the hash chunked test vectors that were the same as non-chunked ones, but left the ones that were unique.
Signed-off-by Eric Biggers <[email protected]>--- crypto/testmgr.c | 795 ++++++++++++++++++++--------------------------- crypto/testmgr.h | 107 +------ 2 files changed, 352 insertions(+), 550 deletions(-)crypto/testmgr.c +0
-0
[...]
Quoted 34 lines
crypto/testmgr.c +10
-23-static int __test_hash (struct crypto_ahash *tfm, -const struct hash_testvec *template,unsigned int tcount, -enum hash_test test_type,const int align_offset) +static int test_hash_vec_cfg (const char *driver, +const struct hash_testvec *vec, +unsigned int vec_num, +const struct testvec_config *cfg, +struct ahash_request *req, +struct test_sglist *tsgl, +u8 *hashstate) { -const char *algo =crypto_tfm_alg_driver_name (crypto_ahash_tfm (tfm)); -size_t digest_size =crypto_ahash_digestsize (tfm); -unsigned int i, j, k, temp; -struct scatterlist sg[8 ]; -char *result; -char *key; -struct ahash_request *req; -struct crypto_wait wait; -void *hash_buff; -char *xbuf[XBUFSIZE ]; -int ret = -ENOMEM ; - - result =kmalloc (digest_size,GFP_KERNEL ); -if (!result) -return ret; - key =kmalloc (MAX_KEYLEN ,GFP_KERNEL ); -if (!key) - goto out_nobuf; -if (testmgr_alloc_buf (xbuf)) - goto out_nobuf; +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +const unsigned int alignmask =crypto_ahash_alignmask (tfm); +const unsigned int digestsize =crypto_ahash_digestsize (tfm);
crypto/testmgr.c static void generate_random_testvec_config(struct testvec_config *cfg, · line 881
Quoted 10 lines
crypto/testmgr.c +10
-0+const unsigned int statesize =crypto_ahash_statesize (tfm); +const u32 req_flags =CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; +const struct test_sg_division *divs[XBUFSIZE ]; +DECLARE_CRYPTO_WAIT (wait); +struct kvec _input; +struct iov_iter input; +unsigned int i; +struct scatterlist *pending_sgl; +unsigned int pending_len; +u8 result[HASH_MAX_DIGESTSIZE +TESTMGR_POISON_LEN ];
Before this patch, result was allocated with kmalloc(). Now, result is in the stack. Is there a reason for this change ?
Due to this change, the talitos driver fails when using CONFIG_VMAP_STACK, because result is not dma-able anymore.
CONFIG_DEBUG_VIRTUAL warns on:
[ 4.276401] WARNING: CPU: 0 PID: 72 at ./arch/powerpc/include/asm/io.h:804 dma_direct_map_page+0x50/0x178
[ 4.285725] CPU: 0 PID: 72 Comm: cryptomgr_test Tainted: G W
5.3.0-rc6-s3k-dev-00897-g03e8e9014403-dirty #2182
[snip registers]
[ 4.353542] NIP [c0066eac] dma_direct_map_page+0x50/0x178
[ 4.358872] LR [c0066eac] dma_direct_map_page+0x50/0x178
[ 4.364074] Call Trace:
[ 4.366533] [c9d0fc18] [c0066eac] dma_direct_map_page+0x50/0x178 (unreliable)
[ 4.373587] [c9d0fc38] [c033ac38] __map_single_talitos_ptr+0x54/0x9c
[ 4.379869] [c9d0fc48] [c033e878] ahash_process_req+0x318/0x7a8
[ 4.385739] [c9d0fca8] [c0210b68] do_ahash_op.isra.0+0x24/0x70
[ 4.391494] [c9d0fcb8] [c02130e8] test_ahash_vec_cfg+0x478/0x5a8
[ 4.397432] [c9d0fda8] [c0213b40] __alg_test_hash.isra.13+0x16c/0x334
[ 4.403797] [c9d0fe08] [c0213d84] alg_test_hash+0x7c/0x164
[ 4.409218] [c9d0fe28] [c0213f88] alg_test+0xc0/0x384
[ 4.414209] [c9d0fef8] [c020f0ec] cryptomgr_test+0x48/0x50
[ 4.419623] [c9d0ff08] [c003a818] kthread+0xe0/0x10c
[ 4.424539] [c9d0ff38] [c000e1d0] ret_from_kernel_thread+0x14/0x1cHow should this be fixed ?
Christophe
Re: [PATCH v2 13/15] crypto: testmgr - convert hash testing to use testvec_configs
Quoted 84 lines · Christophe Leroy
Hi Eric,
Le 01/02/2019 à 08:51, Eric Biggers a écrit :
Quoted 28 lines
From: Eric Biggers <[email protected]>
Convert
alg_test_hash()to use the new test framework, adding a list oftestvec_configsto test by default. When the extra self-tests are enabled, randomly generatedtestvec_configsare tested as well.This improves hash test coverage mainly because now all algorithms have a variety of data layouts tested, whereas before each algorithm was responsible for declaring its own chunked test cases which were often missing or provided poor test coverage. The new code also tests both the
MAY_SLEEPand !MAY_SLEEP cases and buffers that cross pages.This already found bugs in the hash walk code and in the arm32 and arm64 implementations of crct10dif.
I removed the hash chunked test vectors that were the same as non-chunked ones, but left the ones that were unique.
Signed-off-by Eric Biggers <[email protected]>--- crypto/testmgr.c | 795 ++++++++++++++++++++--------------------------- crypto/testmgr.h | 107 +------ 2 files changed, 352 insertions(+), 550 deletions(-)crypto/testmgr.c +0
-0[...]
Quoted 34 lines
crypto/testmgr.c +10
-23-static int __test_hash (struct crypto_ahash *tfm, -const struct hash_testvec *template,unsigned int tcount, -enum hash_test test_type,const int align_offset) +static int test_hash_vec_cfg (const char *driver, +const struct hash_testvec *vec, +unsigned int vec_num, +const struct testvec_config *cfg, +struct ahash_request *req, +struct test_sglist *tsgl, +u8 *hashstate) { -const char *algo =crypto_tfm_alg_driver_name (crypto_ahash_tfm (tfm)); -size_t digest_size =crypto_ahash_digestsize (tfm); -unsigned int i, j, k, temp; -struct scatterlist sg[8 ]; -char *result; -char *key; -struct ahash_request *req; -struct crypto_wait wait; -void *hash_buff; -char *xbuf[XBUFSIZE ]; -int ret = -ENOMEM ; - - result =kmalloc (digest_size,GFP_KERNEL ); -if (!result) -return ret; - key =kmalloc (MAX_KEYLEN ,GFP_KERNEL ); -if (!key) - goto out_nobuf; -if (testmgr_alloc_buf (xbuf)) - goto out_nobuf; +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +const unsigned int alignmask =crypto_ahash_alignmask (tfm); +const unsigned int digestsize =crypto_ahash_digestsize (tfm);crypto/testmgr.c static void generate_random_testvec_config(struct testvec_config *cfg, · line 881
Quoted 10 lines
crypto/testmgr.c +10
-0+const unsigned int statesize =crypto_ahash_statesize (tfm); +const u32 req_flags =CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; +const struct test_sg_division *divs[XBUFSIZE ]; +DECLARE_CRYPTO_WAIT (wait); +struct kvec _input; +struct iov_iter input; +unsigned int i; +struct scatterlist *pending_sgl; +unsigned int pending_len; +u8 result[HASH_MAX_DIGESTSIZE +TESTMGR_POISON_LEN ];Before this patch, result was allocated with
kmalloc(). Now, result is in the stack. Is there a reason for this change ?
Quoted 3 lines
Due to this change, the talitos driver fails when using
CONFIG_VMAP_STACK, because result is not dma-able anymore.
Yes, moving 'result' to the stack simplified the code slightly and also helps detect buggy drivers that assume that 'result' is DMA-able. Note that some "real" API users also put 'result' on the stack, as the API allows it. This is not new; the only new thing is that the tests now test it.
Quoted 3 lines
How should this be fixed ?
Take a look at how other drivers have fixed this issue in the past, e.g. commit c19650d6ea9 ("crypto: caam - fix DMA mapping of stack memory"). Seems that you need to DMA into a temporary buffer.
- Eric
PATCH v2 14/15 crypto: testmgr - check for skcipher_request corruption
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Check that algorithms do not change the skcipher_request structure, as users may rely on submitting the request again (e.g. after copying new data into the same source buffer) without reinitializing everything.
--- crypto/testmgr.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
crypto/testmgr.c +41 -0
@@ -1535,6 +1535,47 @@ static int test_skcipher_vec_cfg(const char *driver, int enc,return err; } +/* Check that the algorithm didn't overwrite things it shouldn't have */ +if (req->cryptlen != vec->len || + req->iv != iv || + req->src != tsgls->src.sgl_ptr || + req->dst != tsgls->dst.sgl_ptr || +crypto_skcipher_reqtfm (req) != tfm || + req->base.complete != crypto_req_done || + req->base.flags != req_flags || + req->base.data != &wait) { +pr_err ("alg: skcipher: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +if (req->cryptlen != vec->len) +pr_err ("alg: skcipher: changed 'req->cryptlen'\n" ); +if (req->iv != iv) +pr_err ("alg: skcipher: changed 'req->iv'\n" ); +if (req->src != tsgls->src.sgl_ptr) +pr_err ("alg: skcipher: changed 'req->src'\n" ); +if (req->dst != tsgls->dst.sgl_ptr) +pr_err ("alg: skcipher: changed 'req->dst'\n" ); +if (crypto_skcipher_reqtfm (req) != tfm) +pr_err ("alg: skcipher: changed 'req->base.tfm'\n" ); +if (req->base.complete != crypto_req_done) +pr_err ("alg: skcipher: changed 'req->base.complete'\n" ); +if (req->base.flags != req_flags) +pr_err ("alg: skcipher: changed 'req->base.flags'\n" ); +if (req->base.data != &wait) +pr_err ("alg: skcipher: changed 'req->base.data'\n" ); +return -EINVAL ; + } +if (is_test_sglist_corrupted (&tsgls->src)) { +pr_err ("alg: skcipher: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return -EINVAL ; + } +if (tsgls->dst.sgl_ptr != tsgls->src.sgl && +is_test_sglist_corrupted (&tsgls->dst)) { +pr_err ("alg: skcipher: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return -EINVAL ; + } +/* Check for the correct output (ciphertext or plaintext) */ err =verify_correct_output (&tsgls->dst, enc ? vec->ctext : vec->ptext, vec->len,0 , true);
Signature · 1 line
2.20.1
PATCH v2 15/15 crypto: testmgr - check for aead_request corruption
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Check that algorithms do not change the aead_request structure, as users may rely on submitting the request again (e.g. after copying new data into the same source buffer) without reinitializing everything.
--- crypto/testmgr.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
crypto/testmgr.c +44 -0
@@ -1253,6 +1253,50 @@ static int test_aead_vec_cfg(const char *driver, int enc,return -EINVAL ; } +/* Check that the algorithm didn't overwrite things it shouldn't have */ +if (req->cryptlen != (enc ? vec->plen : vec->clen) || + req->assoclen != vec->alen || + req->iv != iv || + req->src != tsgls->src.sgl_ptr || + req->dst != tsgls->dst.sgl_ptr || +crypto_aead_reqtfm (req) != tfm || + req->base.complete != crypto_req_done || + req->base.flags != req_flags || + req->base.data != &wait) { +pr_err ("alg: aead: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +if (req->cryptlen != (enc ? vec->plen : vec->clen)) +pr_err ("alg: aead: changed 'req->cryptlen'\n" ); +if (req->assoclen != vec->alen) +pr_err ("alg: aead: changed 'req->assoclen'\n" ); +if (req->iv != iv) +pr_err ("alg: aead: changed 'req->iv'\n" ); +if (req->src != tsgls->src.sgl_ptr) +pr_err ("alg: aead: changed 'req->src'\n" ); +if (req->dst != tsgls->dst.sgl_ptr) +pr_err ("alg: aead: changed 'req->dst'\n" ); +if (crypto_aead_reqtfm (req) != tfm) +pr_err ("alg: aead: changed 'req->base.tfm'\n" ); +if (req->base.complete != crypto_req_done) +pr_err ("alg: aead: changed 'req->base.complete'\n" ); +if (req->base.flags != req_flags) +pr_err ("alg: aead: changed 'req->base.flags'\n" ); +if (req->base.data != &wait) +pr_err ("alg: aead: changed 'req->base.data'\n" ); +return -EINVAL ; + } +if (is_test_sglist_corrupted (&tsgls->src)) { +pr_err ("alg: aead: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return -EINVAL ; + } +if (tsgls->dst.sgl_ptr != tsgls->src.sgl && +is_test_sglist_corrupted (&tsgls->dst)) { +pr_err ("alg: aead: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n" , + driver, op, vec_num, cfg->name); +return -EINVAL ; + } +/* Check for the correct output (ciphertext or plaintext) */ err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext, enc ? vec->clen : vec->plen,
Signature · 1 line
2.20.1
Herbert Xu
maintainerQuoted 87 lines · Eric Biggers
Hello,
Crypto algorithms must produce the same output for the same input regardless of data layout, i.e. how the src and dst scatterlists are divided into chunks and how each chunk is aligned. Request flags such as
CRYPTO_TFM_REQ_MAY_SLEEPmust not affect the result either.However, testing of this currently has many gaps. For example, individual algorithms are responsible for providing their own chunked test vectors. But many don't bother to do this or test only one or two cases, providing poor test coverage. Also, other things such as buffers spanning a page boundary, misaligned IVs, and
CRYPTO_TFM_REQ_MAY_SLEEPare never tested at all.Test code is also duplicated between the chunked and non-chunked cases, making it difficult to make other improvements.
To improve the situation, this patch series basically moves the chunk descriptions into the testmgr itself so that they are shared by all algorithms. However, it's done in an extensible way via a new struct '
testvec_config', which describes not just the scaled chunk lengths but also all other aspects of the crypto operation besides the data itself such as the buffer alignments, the request flags, whether the operation is in-place or not, the IV alignment, and for hash algorithms when to do eachupdate()and when to usefinup()vs.final()vs.digest().Then, this patch series makes skcipher, aead, and hash algorithms be tested against a list of default
testvec_configs, replacing the current test code. This improves overall test coverage, without reducing test performance too much. Note that the test vectors themselves are not changed, except for removing the chunk lists.This series also adds randomized fuzz tests, enabled by a new kconfig option intended for developer use only, where skcipher, aead, and hash algorithms are tested against many randomly generated
testvec_configs. This provides much more comprehensive test coverage.I've run these improved tests on x86, arm32, and arm64 with all crypto algorithms enabled, and they have already found many bugs. Patches 1-7 and the patches from Ard Biesheuvel fix most of the bugs found so far. A bug was also detected in the Rockchip crypto driver which remains to be fixed. Also many AEADs incorrectly change
aead_request::base.tfm, but for now I'm temporarily working around that in the tests as I plan to fix it later after the other types of bugs are addressed.If anyone reading this has access to systems with other architectures or crypto drivers that may not have been tested yet, you can help by applying these patches on your system, enabling
CONFIG_CRYPTO_MANAGER_EXTRA_TESTS, and reporting or fixing any test failures.This patch series can also be found in git at
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git
branch "testmgr-improvements".Changed since v1:
- Made
CONFIG_CRYPTO_MANAGER_EXTRA_TESTSdepend onCONFIG_DEBUG_KERNEL.
- Improved commit description of AEGIS and MORUS fixes.
- A few very minor cleanups to the test code.Eric Biggers (15):
crypto: aegis - fix handling chunked inputs
crypto: morus - fix handling chunked inputs
crypto: x86/aegis - fix handling chunked inputs andMAY_SLEEP
crypto: x86/morus - fix handling chunked inputs andMAY_SLEEP
crypto: x86/aesni-gcm - fix crash on empty plaintext
crypto: ahash - fix another early termination in hash walk
crypto: arm64/aes-neonbs - fix returning final keystream block
crypto: testmgr - addtestvec_configstruct and helper functions
crypto: testmgr - introduceCONFIG_CRYPTO_MANAGER_EXTRA_TESTS
crypto: testmgr - implement randomtestvec_configgeneration
crypto: testmgr - convert skcipher testing to usetestvec_configs
crypto: testmgr - convert aead testing to usetestvec_configs
crypto: testmgr - convert hash testing to usetestvec_configs
crypto: testmgr - check forskcipher_requestcorruption
crypto: testmgr - check foraead_requestcorruptionarch/arm64/crypto/aes-neonbs-core.S | 8 +- arch/x86/crypto/aegis128-aesni-glue.c | 38 +- arch/x86/crypto/aegis128l-aesni-glue.c | 38 +- arch/x86/crypto/aegis256-aesni-glue.c | 38 +- arch/x86/crypto/aesni-intel_glue.c | 13 +- arch/x86/crypto/morus1280_glue.c | 40 +- arch/x86/crypto/morus640_glue.c | 39 +- crypto/Kconfig | 10 + crypto/aegis128.c | 14 +-
Quoted 8 lines
crypto/aegis128l.c | 14 +- crypto/aegis256.c | 14 +- crypto/ahash.c | 14 +- crypto/morus1280.c | 13 +- crypto/morus640.c | 13 +- crypto/testmgr.c | 2549 +++++++++++++----------- crypto/testmgr.h | 407 +--- 16 files changed, 1556 insertions(+), 1706 deletions(-)
All applied. Thanks.
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