aboutsummaryrefslogtreecommitdiff
path: root/lib/tst_crypto.c
blob: 4495d0baa0c28826995cd2e4f429dc07657d811d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2018 Richard Palethorpe <rpalethorpe@suse.com>
 *                    Nicolai Stange <nstange@suse.de>
 */

#include <errno.h>
#include <stdio.h>

#define TST_NO_DEFAULT_MAIN
#include "tst_test.h"
#include "tst_crypto.h"

int tst_crypto_add_alg(struct tst_netlink_context *ctx,
		       const struct crypto_user_alg *alg)
{
	struct nlmsghdr nh = {
		.nlmsg_type = CRYPTO_MSG_NEWALG,
		.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
	};

	NETLINK_ADD_MESSAGE(ctx, &nh, alg, sizeof(struct crypto_user_alg));
	return NETLINK_SEND_VALIDATE(ctx) ? 0 : -tst_netlink_errno;
}

int tst_crypto_del_alg(struct tst_netlink_context *ctx,
	const struct crypto_user_alg *alg, unsigned int retries)
{
	int ret;
	unsigned int i = 0;
	struct nlmsghdr nh = {
		.nlmsg_type = CRYPTO_MSG_DELALG,
		.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
	};

	for (i = 0; i < retries; i++) {
		NETLINK_ADD_MESSAGE(ctx, &nh, alg,
			sizeof(struct crypto_user_alg));

		if (NETLINK_SEND_VALIDATE(ctx))
			return 0;

		ret = -tst_netlink_errno;

		if (ret != -EBUSY)
			break;

		usleep(1);
	}

	return ret;
}