1*b808f320SStephan Müller /* SPDX-License-Identifier: GPL-2.0 */
2*b808f320SStephan Müller
3*b808f320SStephan Müller /*
4*b808f320SStephan Müller * Copyright (C) 2021, Stephan Mueller <smueller@chronox.de>
5*b808f320SStephan Müller */
6*b808f320SStephan Müller
7*b808f320SStephan Müller #ifndef _CRYPTO_KDF_SELFTEST_H
8*b808f320SStephan Müller #define _CRYPTO_KDF_SELFTEST_H
9*b808f320SStephan Müller
10*b808f320SStephan Müller #include <crypto/hash.h>
11*b808f320SStephan Müller #include <linux/uio.h>
12*b808f320SStephan Müller
13*b808f320SStephan Müller struct kdf_testvec {
14*b808f320SStephan Müller unsigned char *key;
15*b808f320SStephan Müller size_t keylen;
16*b808f320SStephan Müller unsigned char *ikm;
17*b808f320SStephan Müller size_t ikmlen;
18*b808f320SStephan Müller struct kvec info;
19*b808f320SStephan Müller unsigned char *expected;
20*b808f320SStephan Müller size_t expectedlen;
21*b808f320SStephan Müller };
22*b808f320SStephan Müller
23*b808f320SStephan Müller static inline int
kdf_test(const struct kdf_testvec * test,const char * name,int (* crypto_kdf_setkey)(struct crypto_shash * kmd,const u8 * key,size_t keylen,const u8 * ikm,size_t ikmlen),int (* crypto_kdf_generate)(struct crypto_shash * kmd,const struct kvec * info,unsigned int info_nvec,u8 * dst,unsigned int dlen))24*b808f320SStephan Müller kdf_test(const struct kdf_testvec *test, const char *name,
25*b808f320SStephan Müller int (*crypto_kdf_setkey)(struct crypto_shash *kmd,
26*b808f320SStephan Müller const u8 *key, size_t keylen,
27*b808f320SStephan Müller const u8 *ikm, size_t ikmlen),
28*b808f320SStephan Müller int (*crypto_kdf_generate)(struct crypto_shash *kmd,
29*b808f320SStephan Müller const struct kvec *info,
30*b808f320SStephan Müller unsigned int info_nvec,
31*b808f320SStephan Müller u8 *dst, unsigned int dlen))
32*b808f320SStephan Müller {
33*b808f320SStephan Müller struct crypto_shash *kmd;
34*b808f320SStephan Müller int ret;
35*b808f320SStephan Müller u8 *buf = kzalloc(test->expectedlen, GFP_KERNEL);
36*b808f320SStephan Müller
37*b808f320SStephan Müller if (!buf)
38*b808f320SStephan Müller return -ENOMEM;
39*b808f320SStephan Müller
40*b808f320SStephan Müller kmd = crypto_alloc_shash(name, 0, 0);
41*b808f320SStephan Müller if (IS_ERR(kmd)) {
42*b808f320SStephan Müller pr_err("alg: kdf: could not allocate hash handle for %s\n",
43*b808f320SStephan Müller name);
44*b808f320SStephan Müller kfree(buf);
45*b808f320SStephan Müller return -ENOMEM;
46*b808f320SStephan Müller }
47*b808f320SStephan Müller
48*b808f320SStephan Müller ret = crypto_kdf_setkey(kmd, test->key, test->keylen,
49*b808f320SStephan Müller test->ikm, test->ikmlen);
50*b808f320SStephan Müller if (ret) {
51*b808f320SStephan Müller pr_err("alg: kdf: could not set key derivation key\n");
52*b808f320SStephan Müller goto err;
53*b808f320SStephan Müller }
54*b808f320SStephan Müller
55*b808f320SStephan Müller ret = crypto_kdf_generate(kmd, &test->info, 1, buf, test->expectedlen);
56*b808f320SStephan Müller if (ret) {
57*b808f320SStephan Müller pr_err("alg: kdf: could not obtain key data\n");
58*b808f320SStephan Müller goto err;
59*b808f320SStephan Müller }
60*b808f320SStephan Müller
61*b808f320SStephan Müller ret = memcmp(test->expected, buf, test->expectedlen);
62*b808f320SStephan Müller if (ret)
63*b808f320SStephan Müller ret = -EINVAL;
64*b808f320SStephan Müller
65*b808f320SStephan Müller err:
66*b808f320SStephan Müller crypto_free_shash(kmd);
67*b808f320SStephan Müller kfree(buf);
68*b808f320SStephan Müller return ret;
69*b808f320SStephan Müller }
70*b808f320SStephan Müller
71*b808f320SStephan Müller #endif /* _CRYPTO_KDF_SELFTEST_H */
72