xref: /aosp_15_r20/external/ltp/lib/tst_crypto.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2018 Richard Palethorpe <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *                    Nicolai Stange <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
12*49cdfc7eSAndroid Build Coastguard Worker #include "tst_crypto.h"
13*49cdfc7eSAndroid Build Coastguard Worker 
tst_crypto_add_alg(struct tst_netlink_context * ctx,const struct crypto_user_alg * alg)14*49cdfc7eSAndroid Build Coastguard Worker int tst_crypto_add_alg(struct tst_netlink_context *ctx,
15*49cdfc7eSAndroid Build Coastguard Worker 		       const struct crypto_user_alg *alg)
16*49cdfc7eSAndroid Build Coastguard Worker {
17*49cdfc7eSAndroid Build Coastguard Worker 	struct nlmsghdr nh = {
18*49cdfc7eSAndroid Build Coastguard Worker 		.nlmsg_type = CRYPTO_MSG_NEWALG,
19*49cdfc7eSAndroid Build Coastguard Worker 		.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
20*49cdfc7eSAndroid Build Coastguard Worker 	};
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker 	NETLINK_ADD_MESSAGE(ctx, &nh, alg, sizeof(struct crypto_user_alg));
23*49cdfc7eSAndroid Build Coastguard Worker 	return NETLINK_SEND_VALIDATE(ctx) ? 0 : -tst_netlink_errno;
24*49cdfc7eSAndroid Build Coastguard Worker }
25*49cdfc7eSAndroid Build Coastguard Worker 
tst_crypto_del_alg(struct tst_netlink_context * ctx,const struct crypto_user_alg * alg,unsigned int retries)26*49cdfc7eSAndroid Build Coastguard Worker int tst_crypto_del_alg(struct tst_netlink_context *ctx,
27*49cdfc7eSAndroid Build Coastguard Worker 	const struct crypto_user_alg *alg, unsigned int retries)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
30*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i = 0;
31*49cdfc7eSAndroid Build Coastguard Worker 	struct nlmsghdr nh = {
32*49cdfc7eSAndroid Build Coastguard Worker 		.nlmsg_type = CRYPTO_MSG_DELALG,
33*49cdfc7eSAndroid Build Coastguard Worker 		.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
34*49cdfc7eSAndroid Build Coastguard Worker 	};
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < retries; i++) {
37*49cdfc7eSAndroid Build Coastguard Worker 		NETLINK_ADD_MESSAGE(ctx, &nh, alg,
38*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(struct crypto_user_alg));
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 		if (NETLINK_SEND_VALIDATE(ctx))
41*49cdfc7eSAndroid Build Coastguard Worker 			return 0;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 		ret = -tst_netlink_errno;
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 		if (ret != -EBUSY)
46*49cdfc7eSAndroid Build Coastguard Worker 			break;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 		usleep(1);
49*49cdfc7eSAndroid Build Coastguard Worker 	}
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
52*49cdfc7eSAndroid Build Coastguard Worker }
53