xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/keyctl/keyctl09.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) 2022 Google, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2023
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Test that encrypted keys can be instantiated using user-provided decrypted
11*49cdfc7eSAndroid Build Coastguard Worker  * data that is hex-ascii encoded.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/keyctl.h"
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define ENCRYPTED_KEY_VALID_PAYLOAD	"new enc32 user:masterkey 32 abcdefABCDEF1234567890aaaaaaaaaaabcdefABCDEF1234567890aaaaaaaaaa"
18*49cdfc7eSAndroid Build Coastguard Worker #define ENCRYPTED_KEY_INVALID_PAYLOAD	"new enc32 user:masterkey 32 plaintext123@123!123@123!123@123plaintext123@123!123@123!123@123"
19*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)20*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	char buffer[128];
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(add_key("user", "user:masterkey", "foo", 3,
25*49cdfc7eSAndroid Build Coastguard Worker 			    KEY_SPEC_PROCESS_KEYRING));
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
28*49cdfc7eSAndroid Build Coastguard Worker 		return;
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(add_key("encrypted", "ltptestkey1",
31*49cdfc7eSAndroid Build Coastguard Worker 			    ENCRYPTED_KEY_VALID_PAYLOAD,
32*49cdfc7eSAndroid Build Coastguard Worker 			    strlen(ENCRYPTED_KEY_VALID_PAYLOAD),
33*49cdfc7eSAndroid Build Coastguard Worker 			    KEY_SPEC_PROCESS_KEYRING));
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
36*49cdfc7eSAndroid Build Coastguard Worker 		return;
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(keyctl(KEYCTL_READ, TST_RET, buffer, sizeof(buffer)));
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
41*49cdfc7eSAndroid Build Coastguard Worker 		return;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(add_key("encrypted", "ltptestkey2",
44*49cdfc7eSAndroid Build Coastguard Worker 			    ENCRYPTED_KEY_INVALID_PAYLOAD,
45*49cdfc7eSAndroid Build Coastguard Worker 			    strlen(ENCRYPTED_KEY_INVALID_PAYLOAD),
46*49cdfc7eSAndroid Build Coastguard Worker 			    KEY_SPEC_PROCESS_KEYRING), EINVAL);
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	keyctl(KEYCTL_CLEAR, KEY_SPEC_PROCESS_KEYRING);
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
52*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
53*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
54*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_DECRYPTED_DATA=y",
55*49cdfc7eSAndroid Build Coastguard Worker 		NULL
56*49cdfc7eSAndroid Build Coastguard Worker 	},
57*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
58*49cdfc7eSAndroid Build Coastguard Worker 		{ "linux-git", "5adedd42245af"},
59*49cdfc7eSAndroid Build Coastguard Worker 		{}
60*49cdfc7eSAndroid Build Coastguard Worker 	}
61*49cdfc7eSAndroid Build Coastguard Worker };
62