1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2018 The strace developers.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "tests.h"
30
31 #ifdef HAVE_LINUX_CRYPTOUSER_H
32
33 # include <stdio.h>
34 # include <stdint.h>
35 # include "test_nlattr.h"
36 # include <linux/cryptouser.h>
37
38 # define CRYPTOCFGA_REPORT_LARVAL 2
39
40 static void
init_crypto_user_alg(struct nlmsghdr * const nlh,const unsigned int msg_len)41 init_crypto_user_alg(struct nlmsghdr *const nlh, const unsigned int msg_len)
42 {
43 SET_STRUCT(struct nlmsghdr, nlh,
44 .nlmsg_len = msg_len,
45 .nlmsg_type = CRYPTO_MSG_GETALG,
46 .nlmsg_flags = NLM_F_DUMP
47 );
48
49 struct crypto_user_alg *const alg = NLMSG_DATA(nlh);
50 SET_STRUCT(struct crypto_user_alg, alg,
51 .cru_name = "abcd",
52 .cru_driver_name = "efgh",
53 .cru_module_name = "ijkl",
54 );
55 }
56
57 static void
print_crypto_user_alg(const unsigned int msg_len)58 print_crypto_user_alg(const unsigned int msg_len)
59 {
60 printf("{len=%u, type=CRYPTO_MSG_GETALG"
61 ", flags=NLM_F_DUMP, seq=0, pid=0}"
62 ", {cru_name=\"abcd\", cru_driver_name=\"efgh\""
63 ", cru_module_name=\"ijkl\", cru_type=0"
64 ", cru_mask=0, cru_refcnt=0, cru_flags=0}",
65 msg_len);
66 }
67
68 int
main(void)69 main(void)
70 {
71 skip_if_unavailable("/proc/self/fd/");
72
73 const int fd = create_nl_socket(NETLINK_CRYPTO);
74 const unsigned int hdrlen = sizeof(struct crypto_user_alg);
75 /*
76 * There are also other structures, but they are not bigger than
77 * DEFAULT_STRLEN so far.
78 */
79 void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
80 NLA_HDRLEN + DEFAULT_STRLEN);
81
82 static char pattern[4096];
83 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
84
85 char *const str = tail_alloc(DEFAULT_STRLEN);
86 fill_memory_ex(str, DEFAULT_STRLEN, '0', 10);
87 TEST_NLATTR(fd, nlh0, hdrlen,
88 init_crypto_user_alg, print_crypto_user_alg,
89 CRYPTOCFGA_REPORT_LARVAL,
90 DEFAULT_STRLEN, str, DEFAULT_STRLEN,
91 printf("{type=\"%.*s\"...}", DEFAULT_STRLEN, str));
92 str[DEFAULT_STRLEN - 1] = '\0';
93 TEST_NLATTR(fd, nlh0, hdrlen,
94 init_crypto_user_alg, print_crypto_user_alg,
95 CRYPTOCFGA_REPORT_LARVAL,
96 DEFAULT_STRLEN, str, DEFAULT_STRLEN,
97 printf("{type=\"%s\"}", str));
98
99 #ifdef HAVE_STRUCT_CRYPTO_REPORT_HASH
100 static const struct crypto_report_hash rhash = {
101 .type = "efgh",
102 .blocksize = 0xabcdefdc,
103 .digestsize = 0xfebcdacd
104 };
105 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
106 init_crypto_user_alg, print_crypto_user_alg,
107 CRYPTOCFGA_REPORT_HASH,
108 pattern, rhash, print_quoted_memory,
109 printf("{type=\"efgh\"");
110 PRINT_FIELD_U(", ", rhash, blocksize);
111 PRINT_FIELD_U(", ", rhash, digestsize);
112 printf("}"));
113 #endif
114
115 #ifdef HAVE_STRUCT_CRYPTO_REPORT_BLKCIPHER
116 static const struct crypto_report_blkcipher rblkcipher = {
117 .type = "abcd",
118 .geniv = "efgh",
119 .blocksize = 0xabcdefac,
120 .min_keysize = 0xfeadbcda,
121 .max_keysize = 0xbdacdeac,
122 .ivsize = 0xefacbdac
123 };
124 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
125 init_crypto_user_alg, print_crypto_user_alg,
126 CRYPTOCFGA_REPORT_BLKCIPHER,
127 pattern, rblkcipher, print_quoted_memory,
128 printf("{type=\"abcd\", geniv=\"efgh\"");
129 PRINT_FIELD_U(", ", rblkcipher, blocksize);
130 PRINT_FIELD_U(", ", rblkcipher, min_keysize);
131 PRINT_FIELD_U(", ", rblkcipher, max_keysize);
132 PRINT_FIELD_U(", ", rblkcipher, ivsize);
133 printf("}"));
134 #endif
135
136 #ifdef HAVE_STRUCT_CRYPTO_REPORT_AEAD
137 static const struct crypto_report_aead raead = {
138 .type = "abcd",
139 .geniv = "efgh",
140 .blocksize = 0xbaefdbac,
141 .maxauthsize = 0xfdbdbcda,
142 .ivsize = 0xacbefdac
143 };
144 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
145 init_crypto_user_alg, print_crypto_user_alg,
146 CRYPTOCFGA_REPORT_AEAD,
147 pattern, raead, print_quoted_memory,
148 printf("{type=\"abcd\", geniv=\"efgh\"");
149 PRINT_FIELD_U(", ", raead, blocksize);
150 PRINT_FIELD_U(", ", raead, maxauthsize);
151 PRINT_FIELD_U(", ", raead, ivsize);
152 printf("}"));
153 #endif
154
155 #ifdef HAVE_STRUCT_CRYPTO_REPORT_RNG
156 static const struct crypto_report_rng rrng = {
157 .type = "abcd",
158 .seedsize = 0xabcdefac
159 };
160 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
161 init_crypto_user_alg, print_crypto_user_alg,
162 CRYPTOCFGA_REPORT_RNG,
163 pattern, rrng, print_quoted_memory,
164 printf("{type=\"abcd\"");
165 PRINT_FIELD_U(", ", rrng, seedsize);
166 printf("}"));
167 #endif
168
169 #ifdef HAVE_STRUCT_CRYPTO_REPORT_CIPHER
170 static const struct crypto_report_cipher rcipher = {
171 .type = "abcd",
172 .blocksize = 0xabcdefac,
173 .min_keysize = 0xfeadbcda,
174 .max_keysize = 0xbdacdeac,
175 };
176 TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
177 init_crypto_user_alg, print_crypto_user_alg,
178 CRYPTOCFGA_REPORT_CIPHER,
179 pattern, rcipher, print_quoted_memory,
180 printf("{type=\"abcd\"");
181 PRINT_FIELD_U(", ", rcipher, blocksize);
182 PRINT_FIELD_U(", ", rcipher, min_keysize);
183 PRINT_FIELD_U(", ", rcipher, max_keysize);
184 printf("}"));
185 #endif
186
187 puts("+++ exited with 0 +++");
188 return 0;
189 }
190
191 #else
192
193 SKIP_MAIN_UNDEFINED("HAVE_LINUX_CRYPTOUSER_H");
194
195 #endif
196