1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10
11 #include <stdio.h>
12 #include <stdint.h>
13 #include "test_nlattr.h"
14 #include <linux/cryptouser.h>
15
16 #define XLAT_MACROS_ONLY
17 # include "xlat/netlink_protocols.h"
18 #undef XLAT_MACROS_ONLY
19
20 #define CRYPTOCFGA_REPORT_LARVAL 2
21
22 static void
23 init_crypto_user_alg(struct nlmsghdr *const nlh, const unsigned int msg_len)
24 {
25 SET_STRUCT(struct nlmsghdr, nlh,
26 .nlmsg_len = msg_len,
27 .nlmsg_type = CRYPTO_MSG_GETALG,
28 .nlmsg_flags = NLM_F_DUMP
29 );
30
31 struct crypto_user_alg *const alg = NLMSG_DATA(nlh);
32 SET_STRUCT(struct crypto_user_alg, alg,
33 .cru_name = "abcd",
34 .cru_driver_name = "efgh",
35 .cru_module_name = "ijkl",
36 );
37 }
38
39 static void
40 print_crypto_user_alg(const unsigned int msg_len)
41 {
42 printf("{nlmsg_len=%u, nlmsg_type=CRYPTO_MSG_GETALG"
43 ", nlmsg_flags=NLM_F_DUMP, nlmsg_seq=0, nlmsg_pid=0}"
44 ", {cru_name=\"abcd\", cru_driver_name=\"efgh\""
45 ", cru_module_name=\"ijkl\", cru_type=0"
46 ", cru_mask=0, cru_refcnt=0, cru_flags=0}",
47 msg_len);
48 }
49
50 int
51 main(void)
52 {
53 skip_if_unavailable("/proc/self/fd/");
54
55 const int fd = create_nl_socket(NETLINK_CRYPTO);
56 const unsigned int hdrlen = sizeof(struct crypto_user_alg);
57 /*
58 * There are also other structures, but they are not bigger than
59 * DEFAULT_STRLEN so far.
60 */
61 void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
62 NLA_HDRLEN + DEFAULT_STRLEN);
63
64 static char pattern[4096];
65 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
66
67 char *const str = tail_alloc(DEFAULT_STRLEN);
68 fill_memory_ex(str, DEFAULT_STRLEN, '0', 10);
69 TEST_NLATTR(fd, nlh0, hdrlen,
70 init_crypto_user_alg, print_crypto_user_alg,
71 CRYPTOCFGA_REPORT_LARVAL,
72 DEFAULT_STRLEN, str, DEFAULT_STRLEN,
73 printf("{type=\"%.*s\"...}", DEFAULT_STRLEN, str));
74 str[DEFAULT_STRLEN - 1] = '\0';
75 TEST_NLATTR(fd, nlh0, hdrlen,
76 init_crypto_user_alg, print_crypto_user_alg,
77 CRYPTOCFGA_REPORT_LARVAL,
78 DEFAULT_STRLEN, str, DEFAULT_STRLEN,
79 printf("{type=\"%s\"}", str));
80
81 static const struct crypto_report_hash rhash = {
82 .type = "efgh",
83 .blocksize = 0xabcdefdc,
84 .digestsize = 0xfebcdacd
85 };
86 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
87 init_crypto_user_alg, print_crypto_user_alg,
88 CRYPTOCFGA_REPORT_HASH,
89 pattern, rhash, sizeof(rhash),
90 print_quoted_memory,
91 printf("{type=\"efgh\"");
92 printf(", ");
93 PRINT_FIELD_U(rhash, blocksize);
94 printf(", ");
95 PRINT_FIELD_U(rhash, digestsize);
96 printf("}"));
97
98 static const struct crypto_report_blkcipher rblkcipher = {
99 .type = "abcd",
100 .geniv = "efgh",
101 .blocksize = 0xabcdefac,
102 .min_keysize = 0xfeadbcda,
103 .max_keysize = 0xbdacdeac,
104 .ivsize = 0xefacbdac
105 };
106 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
107 init_crypto_user_alg, print_crypto_user_alg,
108 CRYPTOCFGA_REPORT_BLKCIPHER,
109 pattern, rblkcipher, sizeof(rblkcipher),
110 print_quoted_memory,
111 printf("{type=\"abcd\", geniv=\"efgh\"");
112 printf(", ");
113 PRINT_FIELD_U(rblkcipher, blocksize);
114 printf(", ");
115 PRINT_FIELD_U(rblkcipher, min_keysize);
116 printf(", ");
117 PRINT_FIELD_U(rblkcipher, max_keysize);
118 printf(", ");
119 PRINT_FIELD_U(rblkcipher, ivsize);
120 printf("}"));
121
122 static const struct crypto_report_aead raead = {
123 .type = "abcd",
124 .geniv = "efgh",
125 .blocksize = 0xbaefdbac,
126 .maxauthsize = 0xfdbdbcda,
127 .ivsize = 0xacbefdac
128 };
129 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
130 init_crypto_user_alg, print_crypto_user_alg,
131 CRYPTOCFGA_REPORT_AEAD,
132 pattern, raead, sizeof(raead),
133 print_quoted_memory,
134 printf("{type=\"abcd\", geniv=\"efgh\"");
135 printf(", ");
136 PRINT_FIELD_U(raead, blocksize);
137 printf(", ");
138 PRINT_FIELD_U(raead, maxauthsize);
139 printf(", ");
140 PRINT_FIELD_U(raead, ivsize);
141 printf("}"));
142
143 static const struct crypto_report_rng rrng = {
144 .type = "abcd",
145 .seedsize = 0xabcdefac
146 };
147 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
148 init_crypto_user_alg, print_crypto_user_alg,
149 CRYPTOCFGA_REPORT_RNG,
150 pattern, rrng, sizeof(rrng), print_quoted_memory,
151 printf("{type=\"abcd\"");
152 printf(", ");
153 PRINT_FIELD_U(rrng, seedsize);
154 printf("}"));
155
156 static const struct crypto_report_cipher rcipher = {
157 .type = "abcd",
158 .blocksize = 0xabcdefac,
159 .min_keysize = 0xfeadbcda,
160 .max_keysize = 0xbdacdeac,
161 };
162 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
163 init_crypto_user_alg, print_crypto_user_alg,
164 CRYPTOCFGA_REPORT_CIPHER,
165 pattern, rcipher, sizeof(rcipher),
166 print_quoted_memory,
167 printf("{type=\"abcd\"");
168 printf(", ");
169 PRINT_FIELD_U(rcipher, blocksize);
170 printf(", ");
171 PRINT_FIELD_U(rcipher, min_keysize);
172 printf(", ");
173 PRINT_FIELD_U(rcipher, max_keysize);
174 printf("}"));
175
176 puts("+++ exited with 0 +++");
177 return 0;
178 }