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