[U-Boot] [PATCH v3 6/6] test: add rsa_verify() unit test
Simon Glass
sjg at chromium.org
Wed Nov 20 02:59:57 UTC 2019
Hi Takahiro,
On Tue, 12 Nov 2019 at 16:47, AKASHI Takahiro
<takahiro.akashi at linaro.org> wrote:
>
> In this patch, a very simple test is added to verify that rsa_verify()
> using rsa_verify_with_pkey() work correctly.
>
> To keep the code simple, all the test data, either public key and
> verified binary data, are embedded in the source.
>
> Signed-off-by: AKASHI Takahiro <takahiro.akashi at linaro.org>
> ---
> test/Kconfig | 12 +++
> test/lib/Makefile | 1 +
> test/lib/rsa.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 220 insertions(+)
> create mode 100644 test/lib/rsa.c
>
> diff --git a/test/Kconfig b/test/Kconfig
> index cb7954041eda..64d76c3b20a5 100644
> --- a/test/Kconfig
> +++ b/test/Kconfig
> @@ -28,6 +28,18 @@ config UT_LIB_ASN1
> Enables a test which exercises asn1 compiler and decoder function
> via various parsers.
>
> +config UT_LIB_RSA
> + bool "Unit test for rsa_verify() function"
> + imply RSA
> + imply ASYMMETRIC_KEY_TYPE
> + imply ASYMMETRIC_PUBLIC_KEY_SUBTYPE
> + imply RSA_PUBLIC_KEY_PARSER
> + imply RSA_VERIFY_WITH_PKEY
> + default y
> + help
> + Enables rsa_verify() test, currently rsa_verify_with_pkey only()
> + only, at the 'ut lib' command.
> +
> endif
>
> config UT_TIME
> diff --git a/test/lib/Makefile b/test/lib/Makefile
> index 72d2ec74b5f4..2bf6ef3935bb 100644
> --- a/test/lib/Makefile
> +++ b/test/lib/Makefile
> @@ -8,3 +8,4 @@ obj-y += lmb.o
> obj-y += string.o
> obj-$(CONFIG_ERRNO_STR) += test_errno_str.o
> obj-$(CONFIG_UT_LIB_ASN1) += asn1.o
> +obj-$(CONFIG_UT_LIB_RSA) += rsa.o
> diff --git a/test/lib/rsa.c b/test/lib/rsa.c
> new file mode 100644
> index 000000000000..ef3860b59a2b
> --- /dev/null
> +++ b/test/lib/rsa.c
> @@ -0,0 +1,207 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (c) 2019 Linaro Limited
> + * Author: AKASHI Takahiro
> + *
> + * Unit test for rsa_verify() function
> + */
> +
> +#include <common.h>
> +#include <command.h>
> +#include <test/lib.h>
> +#include <test/test.h>
> +#include <test/ut.h>
> +
> +#include <image.h>
This should go below command.h above
[..]
> +/**
> + * lib_rsa_verify_valid() - unit test for rsa_verify()
> + *
> + * Test rsa_verify() with valid hash
> + *
> + * @uts: unit test state
> + * Return: 0 = success, 1 = failure
> + */
> +static int lib_rsa_verify_valid(struct unit_test_state *uts)
> +{
> + struct image_sign_info info;
> + struct image_region reg;
> + int ret;
> +
> + memset(&info, '\0', sizeof(info));
> + info.name = "sha256,rsa2048";
> + info.padding = image_get_padding_algo("pkcs-1.5");
> + info.checksum = image_get_checksum_algo("sha256,rsa2048");
> + info.crypto = image_get_crypto_algo(info.name);
> +
> + info.key = public_key;
> + info.keylen = public_key_len;
> +
> + reg.data = data_raw;
> + reg.size = data_raw_len;
> + ret = rsa_verify(&info, ®, 1, data_enc, data_enc_len);
> + ut_assertf(ret == 0, "verification unexpectedly failed (%d)\n", ret);
Should there not be a test for success as well?
> +
> + return CMD_RET_SUCCESS;
> +}
> +
> +LIB_TEST(lib_rsa_verify_valid, 0);
More information about the U-Boot
mailing list