xref: /netbsd-src/tests/dev/cgd/t_cgd_adiantum.c (revision f3d2f6ec5505ccfa666babd3c679968660b26da7)
1 /*	$NetBSD: t_cgd_adiantum.c,v 1.5 2020/08/20 13:33:54 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 
31 #include <fcntl.h>
32 #include <stdint.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <util.h>
37 
38 #include <dev/cgdvar.h>
39 
40 #include <rump/rump.h>
41 #include <rump/rump_syscalls.h>
42 
43 #include <atf-c.h>
44 
45 #include "h_macros.h"
46 
47 #define	MAXSECSIZE	512	/* for now; should be cgd parameter */
48 #define	IMGSIZE		0x101*512
49 
50 /* Used as buffer for cgd device I/O, must be at least 32-bit aligned.  */
51 static const uint8_t zerosector[512] __aligned(4);
52 
53 static const struct {
54 	uint8_t		key[32];
55 	uint64_t	blkno;
56 	unsigned	secsize;
57 	const uint8_t	*ptxt;
58 	const uint8_t	*ctxt;
59 } C[] = {
60 	[0] = {
61 		.key = {0},
62 		.blkno = 0,
63 		.secsize = 512,
64 		.ptxt = zerosector,
65 		.ctxt = (const uint8_t[512]) {
66 			0x51,0x6d,0xe2,0x81, 0x26,0xd5,0xc8,0xd7,
67 			0xff,0xc6,0xc2,0xff, 0x39,0xbf,0x15,0x15,
68 			0x46,0x80,0x44,0x65, 0x76,0xa1,0x56,0xae,
69 			0xa0,0xb6,0x44,0x05, 0xb7,0xb1,0x32,0x23,
70 			0x80,0x07,0xdd,0x31, 0x57,0x69,0xf5,0x10,
71 			0x2d,0x53,0x54,0x8a, 0x1c,0x30,0x15,0x53,
72 			0x40,0xb4,0x75,0xb2, 0xa1,0x8a,0xbe,0xdf,
73 			0xf7,0x10,0xe0,0x38, 0xf9,0x70,0x29,0xda,
74 			0xf0,0x95,0xcd,0xe9, 0x47,0xa1,0x32,0xa3,
75 			0x83,0xca,0xe3,0x36, 0xc3,0x21,0x00,0xc2,
76 			0x0a,0xb4,0x0e,0x67, 0x69,0xe6,0xe8,0x14,
77 			0x74,0x98,0x69,0xd0, 0x6e,0xab,0x23,0xbc,
78 			0xa9,0x1e,0xf8,0x2d, 0x98,0x59,0x98,0x81,
79 			0x29,0x70,0xa8,0x1e, 0x26,0x13,0xba,0x53,
80 			0x9e,0x83,0xe9,0x35, 0x73,0x8c,0xf9,0xb6,
81 			0x10,0x17,0xda,0xe8, 0x21,0xcc,0x7d,0xd2,
82 			0x8e,0x23,0xb9,0x63, 0xde,0xcf,0xa7,0x53,
83 			0x56,0x1c,0xc8,0x53, 0x91,0x17,0x8f,0xec,
84 			0x93,0x66,0x8b,0x0f, 0x18,0x6e,0xa5,0x9d,
85 			0x8e,0x99,0x36,0x1c, 0x23,0xb6,0x0f,0x5d,
86 			0x75,0xc3,0xfd,0x35, 0xc5,0x68,0x9c,0xe1,
87 			0xba,0x19,0x1a,0x09, 0xca,0x40,0x1f,0xee,
88 			0x0f,0x76,0x84,0x92, 0x72,0xdf,0x62,0x1b,
89 			0x2e,0xa9,0x36,0xbe, 0xca,0x7e,0xc6,0x69,
90 			0xc6,0x27,0xf8,0x12, 0xbf,0x6e,0xd3,0xf0,
91 			0xb0,0x10,0x9c,0x67, 0x76,0x40,0xc8,0x36,
92 			0x8e,0x73,0xec,0xa2, 0xdb,0x4a,0x0a,0xd9,
93 			0x1b,0xa3,0x28,0x30, 0x84,0xa4,0xff,0xa0,
94 			0xe7,0x1e,0xf4,0xb2, 0xfe,0x59,0x79,0xdf,
95 			0x8d,0x66,0x12,0xac, 0xf6,0x1a,0x0f,0xa6,
96 			0x4e,0x86,0x8c,0x80, 0x95,0x11,0xee,0x55,
97 			0xe3,0xe0,0x43,0x56, 0xa2,0xfc,0xa2,0xbd,
98 			0xad,0x6f,0xfc,0xf9, 0x4c,0x04,0x51,0xf4,
99 			0xd9,0x17,0x96,0xdc, 0xd3,0xd0,0xd7,0xeb,
100 			0xa8,0xdc,0x34,0x65, 0xc7,0xcf,0xed,0x06,
101 			0xf8,0xa3,0xff,0x31, 0x3e,0x15,0x2f,0x62,
102 			0x8c,0x73,0x7f,0x8c, 0x80,0x4d,0x4b,0x6d,
103 			0xcf,0xc6,0xd0,0xdd, 0x7e,0x3a,0x1e,0x88,
104 			0xb7,0xdd,0x23,0xa6, 0xa0,0x0d,0x6c,0xaf,
105 			0xd6,0x5b,0xfd,0x76, 0x66,0xee,0x02,0xa6,
106 			0x10,0xda,0x42,0xfb, 0x15,0xc3,0xe4,0xa7,
107 			0x8b,0x2b,0xfa,0x5d, 0xba,0xce,0xcd,0x9f,
108 			0x76,0x38,0x66,0xff, 0x74,0x08,0x34,0xf3,
109 			0x3d,0x12,0xf4,0x8d, 0x5e,0x54,0x2b,0x37,
110 			0x06,0xd3,0x03,0xc9, 0xd9,0x29,0x53,0x65,
111 			0x76,0x00,0x24,0x50, 0x30,0x06,0x6c,0x69,
112 			0x31,0xcc,0x89,0x7c, 0x97,0xae,0xac,0x74,
113 			0x35,0x43,0xa3,0xe5, 0x40,0x58,0x3d,0xb9,
114 			0x08,0x46,0x5e,0x5f, 0x07,0xc5,0x41,0x32,
115 			0xab,0xa4,0x5a,0xab, 0x59,0x2b,0x54,0xee,
116 			0x24,0x92,0xd3,0x08, 0xb8,0x99,0x9e,0x13,
117 			0x3c,0x2c,0x05,0xe6, 0xc1,0x6f,0xa1,0x5d,
118 			0xa9,0x09,0x1a,0x96, 0x76,0xe4,0x31,0xc6,
119 			0xcc,0xad,0x28,0x58, 0x73,0x4d,0x1a,0x19,
120 			0x3d,0xcd,0xaf,0x8c, 0xd8,0x24,0xff,0x72,
121 			0xdc,0x4e,0x07,0x6e, 0xd8,0xbc,0x3b,0x2b,
122 			0xf5,0xe5,0xfa,0x30, 0x7d,0xaa,0x59,0x40,
123 			0x78,0x01,0xa4,0x55, 0xdc,0xe6,0x7b,0xae,
124 			0x87,0x8e,0x11,0xbb, 0x65,0xf7,0x8a,0x4f,
125 			0x37,0x7e,0xe1,0xac, 0x62,0xf1,0x64,0x8f,
126 			0xc1,0xfd,0x3e,0x34, 0x1f,0x60,0xba,0x61,
127 			0x98,0xae,0x19,0xce, 0x54,0x22,0x64,0x09,
128 			0x67,0x82,0x6b,0x4b, 0xdf,0x26,0x77,0xde,
129 			0xd6,0x13,0x00,0xee, 0x2c,0x18,0x49,0xd9,
130 		},
131 	},
132 	[1] = {
133 		.key = {0},
134 		.blkno = 1,
135 		.secsize = 512,
136 		.ptxt = zerosector,
137 		.ctxt = (const uint8_t[512]) {
138 			0xf2,0x23,0x68,0x5a, 0x15,0x11,0x56,0xa1,
139 			0x71,0x57,0x5c,0x5e, 0x32,0xd4,0xdd,0xbb,
140 			0x7a,0x0c,0x84,0x23, 0xe9,0x2f,0x1b,0x63,
141 			0x3c,0x4d,0xad,0xfd, 0x6e,0xc0,0xdb,0x79,
142 			0x23,0xa5,0x13,0xfe, 0x17,0x3c,0x4a,0x27,
143 			0xb9,0xbc,0xf0,0xf6, 0x67,0x98,0xa8,0x64,
144 			0xce,0xf0,0x17,0x0a, 0xa8,0x05,0x0f,0xf2,
145 			0xff,0xb0,0x7a,0x9e, 0x1a,0xcf,0x5d,0x0e,
146 			0x9f,0xb0,0x9a,0xd0, 0x7c,0xf2,0x88,0x96,
147 			0xe2,0x8d,0xdb,0xa2, 0x19,0x30,0x3d,0x5d,
148 			0x66,0x28,0x40,0x53, 0xb9,0x8d,0xbb,0x24,
149 			0x3a,0x4c,0x00,0xac, 0x20,0x86,0x96,0x83,
150 			0x2c,0x77,0x5e,0x18, 0x0c,0xfa,0x27,0x51,
151 			0xe1,0x5d,0xd3,0xd9, 0xe1,0x5d,0x27,0x1f,
152 			0x49,0x74,0xfd,0x2a, 0xc3,0xe5,0xa0,0xf6,
153 			0x5a,0x58,0xe3,0x1f, 0x4a,0xa6,0xc2,0x25,
154 			0xe4,0xb5,0xc8,0x0d, 0x9f,0xa7,0xc0,0x6e,
155 			0xab,0xb3,0xfc,0x9f, 0xe1,0x72,0x8a,0x69,
156 			0xf1,0xc6,0x54,0xb8, 0xeb,0x70,0xed,0xfe,
157 			0x95,0xf7,0x0d,0x55, 0x95,0x13,0x7a,0x82,
158 			0xac,0x83,0xd2,0xa3, 0xdc,0x5b,0xba,0x4e,
159 			0xae,0xdd,0xe9,0x22, 0x9e,0xe2,0x72,0xaf,
160 			0x9a,0xc0,0x53,0x96, 0xb9,0x7d,0x47,0x28,
161 			0x4a,0x93,0x6a,0xfb, 0x59,0x25,0x49,0x39,
162 			0xda,0x23,0xe8,0x28, 0x42,0xba,0x58,0x26,
163 			0x29,0xf5,0x4c,0x85, 0xbb,0x62,0xfc,0x12,
164 			0x28,0xbd,0xec,0x3f, 0xf4,0x86,0x80,0xf0,
165 			0x69,0x81,0x99,0xe3, 0x95,0x0d,0xe8,0x8f,
166 			0xeb,0x60,0xb6,0x2a, 0xbf,0xf1,0x41,0xe4,
167 			0x68,0x4f,0x4b,0xe3, 0x49,0x2c,0x1e,0xad,
168 			0x0d,0x8f,0x63,0x40, 0xb9,0xee,0x4d,0x09,
169 			0x12,0x45,0x97,0x64, 0x97,0xd5,0x5f,0xa3,
170 			0xb0,0x4b,0xdf,0x3f, 0x59,0x9f,0xab,0x12,
171 			0x3d,0x4b,0x54,0xdc, 0xea,0xe0,0x55,0x5e,
172 			0x1c,0xfd,0xe9,0x7e, 0x40,0x24,0x88,0x6c,
173 			0x8d,0xfc,0xc2,0x57, 0xd2,0x37,0xb2,0x12,
174 			0xc2,0x03,0x0d,0xac, 0xb8,0x9b,0x62,0x61,
175 			0x23,0xc0,0x7a,0x06, 0xdb,0x62,0x86,0x06,
176 			0xaf,0xa5,0x98,0x75, 0xd9,0x4e,0x8a,0xf2,
177 			0xc5,0x64,0xad,0xf2, 0xf4,0xc2,0x7f,0xa2,
178 			0x25,0xf4,0xd0,0x44, 0x57,0x8b,0x89,0xe2,
179 			0x08,0xea,0x86,0x72, 0x37,0xe3,0x7e,0x92,
180 			0x22,0xa0,0x32,0x05, 0x30,0x90,0xcc,0x44,
181 			0x6f,0x2c,0x75,0xae, 0x28,0x90,0x34,0xe3,
182 			0x05,0x88,0xcd,0x77, 0x1d,0x6a,0x72,0x56,
183 			0x49,0x3f,0x3d,0x0b, 0x49,0x04,0x98,0x65,
184 			0x66,0x0e,0xfd,0x7d, 0xca,0x32,0x74,0x66,
185 			0xa0,0xd7,0x04,0xdb, 0x83,0x4b,0x7f,0x83,
186 			0x22,0x43,0x98,0x93, 0x0d,0x0b,0xb1,0x8d,
187 			0x8c,0x8b,0x9e,0x08, 0xb9,0xb0,0xd9,0x82,
188 			0xcd,0x20,0x5e,0x19, 0x5d,0xa0,0x6a,0x71,
189 			0x05,0xf9,0x18,0x3d, 0x6b,0xb7,0xb6,0x56,
190 			0x03,0xa3,0x53,0x58, 0x7d,0xf8,0x25,0xca,
191 			0x26,0x02,0xc1,0xa6, 0x72,0x70,0xc3,0xe3,
192 			0x59,0x64,0xe1,0x25, 0x34,0x79,0xb3,0x5e,
193 			0x08,0xe9,0xb8,0x91, 0xb6,0x5d,0x3a,0x44,
194 			0x20,0x60,0x61,0xf4, 0x28,0x93,0x8f,0x89,
195 			0xbe,0xea,0x55,0xda, 0x43,0x38,0x96,0xc8,
196 			0x50,0x01,0x09,0xaf, 0x76,0x92,0x83,0xae,
197 			0x3b,0x82,0x6f,0x49, 0x0b,0x18,0x9c,0xef,
198 			0x92,0x06,0x11,0xeb, 0x41,0x34,0xf4,0x7b,
199 			0xc4,0x9a,0x9f,0xe4, 0xb4,0xe7,0x1a,0x84,
200 			0xd8,0x8b,0x3a,0x29, 0xb5,0x4e,0xf3,0x97,
201 			0x6c,0xef,0xe9,0x62, 0x21,0x89,0x23,0xfd,
202 		},
203 	},
204 	[2] = {
205 		.key = {0},
206 		.blkno = 0x100,
207 		.secsize = 512,
208 		.ptxt = zerosector,
209 		.ctxt = (const uint8_t[512]) {
210 			0x32,0x26,0xaf,0x56, 0xbc,0x43,0xac,0x37,
211 			0xb2,0x8d,0xa4,0xfb, 0x32,0xdc,0x09,0x03,
212 			0xd9,0x44,0xce,0x4e, 0x70,0xaf,0xed,0x83,
213 			0x4b,0x9c,0x85,0x11, 0xd2,0x6a,0x70,0x15,
214 			0xea,0x7b,0x5e,0xac, 0x5d,0x08,0x25,0xd7,
215 			0x8c,0x23,0x7c,0x15, 0xb7,0x20,0xd1,0x08,
216 			0xe0,0x81,0x71,0xbe, 0x68,0xca,0xe2,0xcd,
217 			0x98,0xe5,0x40,0xe0, 0xf5,0x84,0xcc,0x6a,
218 			0x3c,0xa0,0xe8,0x2c, 0x02,0x4c,0x95,0xb5,
219 			0x58,0x86,0x86,0x61, 0x71,0x7f,0xd7,0xf9,
220 			0xd9,0x64,0x80,0xf6, 0xea,0x92,0xbc,0x65,
221 			0x3b,0x07,0x77,0xaa, 0xb1,0xb1,0xf5,0xd6,
222 			0x6d,0x89,0x63,0x14, 0xc0,0xcc,0x7a,0x2b,
223 			0xc4,0x32,0x63,0xda, 0xa6,0xc6,0xc8,0xc6,
224 			0x4c,0x4e,0x10,0x63, 0x3b,0x93,0x80,0x77,
225 			0x3e,0x54,0xd2,0x38, 0x13,0x79,0xbc,0x6c,
226 			0x0b,0xd4,0x71,0x5c, 0x26,0xc0,0x81,0x09,
227 			0xc7,0xd8,0x7a,0x04, 0x58,0x2e,0x50,0x6a,
228 			0x3d,0xca,0xa1,0x66, 0x72,0xca,0xee,0x5a,
229 			0xdd,0x13,0x67,0xb1, 0x54,0x72,0x41,0x2d,
230 			0xfd,0x95,0x24,0xe3, 0x96,0x4a,0x41,0x03,
231 			0xeb,0xeb,0x99,0x49, 0x52,0xac,0x3a,0x28,
232 			0x81,0x54,0x1a,0xfb, 0xc3,0xcd,0x8e,0x9d,
233 			0x0c,0x64,0x95,0xbb, 0x27,0xb8,0x6b,0x51,
234 			0x7b,0xc4,0x57,0xc9, 0x29,0x4e,0x85,0x31,
235 			0x1c,0xaa,0x63,0x2e, 0x7a,0x37,0x2e,0x06,
236 			0xdc,0x58,0x39,0x3b, 0x60,0x34,0x59,0x15,
237 			0x4f,0xba,0x33,0x52, 0x13,0xb0,0x7b,0x7c,
238 			0x7e,0x00,0x0b,0x49, 0x15,0x9c,0x48,0xf4,
239 			0x67,0xdd,0xc6,0x72, 0x87,0xbe,0xe7,0xf7,
240 			0x21,0x95,0x82,0xc3, 0x41,0x3b,0x19,0xe3,
241 			0xf3,0x28,0xcc,0x14, 0x5f,0xae,0x6f,0x07,
242 			0x35,0x94,0x05,0x46, 0x02,0x5c,0x3c,0x46,
243 			0xb1,0x2d,0xeb,0x6e, 0xa0,0x0f,0xea,0x40,
244 			0x3e,0x35,0x6e,0x50, 0xc4,0x22,0xeb,0x93,
245 			0xba,0x49,0xfb,0xf0, 0x8e,0x2a,0xa1,0xaf,
246 			0xf4,0x91,0xb2,0xc5, 0x7d,0x8e,0xba,0x45,
247 			0x53,0x75,0xc3,0xcc, 0x3e,0x02,0x0e,0x4d,
248 			0x2e,0xda,0x45,0xd2, 0x31,0xc7,0x1b,0x6b,
249 			0x99,0x71,0x8d,0xd8, 0x8c,0x94,0xa2,0x02,
250 			0x6c,0xb0,0x32,0x8f, 0xce,0x04,0x61,0x0a,
251 			0x3f,0x17,0x3a,0x28, 0xda,0x31,0xdc,0xec,
252 			0xbc,0xea,0x1b,0x37, 0x9b,0x36,0x04,0xb1,
253 			0xb5,0x7f,0xfe,0x1a, 0xd8,0x11,0xb7,0x0a,
254 			0x77,0x2e,0x6d,0x22, 0x79,0x9e,0x54,0x47,
255 			0xea,0xf5,0x17,0x38, 0xd0,0xe2,0x23,0x68,
256 			0x92,0x88,0x42,0x59, 0x2c,0x61,0x53,0x2b,
257 			0x99,0xed,0x7b,0x85, 0xfb,0xb8,0xe8,0x0c,
258 			0x4b,0x81,0x1e,0x0f, 0x42,0x04,0x8b,0x55,
259 			0x2c,0x34,0x46,0x98, 0x9c,0x47,0x08,0x70,
260 			0x46,0x45,0x5e,0xa8, 0x62,0x92,0x94,0xcd,
261 			0x73,0x1c,0xef,0x8b, 0x96,0x5f,0x6d,0x76,
262 			0x07,0x99,0x6f,0xe0, 0x1d,0xdc,0x1d,0x1c,
263 			0x3f,0xb4,0x5f,0x9b, 0x34,0x0c,0x75,0x10,
264 			0x7e,0x0d,0xf8,0xbb, 0xc3,0x8a,0x2a,0x15,
265 			0x01,0x3a,0x56,0x73, 0x5b,0xe9,0x5f,0xf2,
266 			0x6a,0x1b,0x17,0xce, 0xf3,0x3e,0xc9,0xdf,
267 			0x76,0xe8,0xcd,0xf2, 0x6d,0xb1,0xdc,0x29,
268 			0x8c,0xa3,0x89,0x73, 0x69,0x86,0xa9,0x05,
269 			0xbe,0x63,0xc8,0x7c, 0x36,0xc0,0x88,0x74,
270 			0x64,0x91,0xdd,0xb7, 0x92,0x73,0x7e,0xc1,
271 			0x01,0x95,0xb3,0x95, 0x53,0x33,0x16,0xcd,
272 			0xe9,0xd7,0x56,0x61, 0x71,0x49,0x24,0x9b,
273 			0x9a,0x10,0x7e,0x50, 0x7e,0xd3,0xe2,0x9d,
274 		},
275 	},
276 };
277 
278 static void
hexdump(const void * buf,size_t len)279 hexdump(const void *buf, size_t len)
280 {
281 	const unsigned char *p = buf;
282 	size_t i;
283 
284 	for (i = 0; i < len; i++) {
285 		if (i % 16 == 8)
286 			printf(" ");
287 		printf(" %02hhx", p[i]);
288 		if ((i + 1) % 16 == 0)
289 			printf("\n");
290 	}
291 	if (i % 16)
292 		printf("\n");
293 }
294 
295 static int
configure_cgd(int fd,const char * dkpath,const char * alg,const char * ivmethod,const void * key,size_t keybytes)296 configure_cgd(int fd, const char *dkpath, const char *alg,
297     const char *ivmethod, const void *key, size_t keybytes)
298 {
299 	struct cgd_ioctl ci;
300 
301 	memset(&ci, 0, sizeof(ci));
302 	ci.ci_disk = dkpath;
303 	ci.ci_alg = alg;
304 	ci.ci_ivmethod = ivmethod;
305 	ci.ci_keylen = 8*keybytes;
306 	ci.ci_key = key;
307 	ci.ci_blocksize = (size_t)-1;
308 
309 	return rump_sys_ioctl(fd, CGDIOCSET, &ci);
310 }
311 
312 static int
unconfigure_cgd(int fd)313 unconfigure_cgd(int fd)
314 {
315 	struct cgd_ioctl ci;
316 
317 	return rump_sys_ioctl(fd, CGDIOCCLR, &ci);
318 }
319 
320 ATF_TC(cgd_adiantum);
ATF_TC_HEAD(cgd_adiantum,tc)321 ATF_TC_HEAD(cgd_adiantum, tc)
322 {
323 
324 	atf_tc_set_md_var(tc, "descr", "Adiantum tests");
325 }
326 
ATF_TC_BODY(cgd_adiantum,tc)327 ATF_TC_BODY(cgd_adiantum, tc)
328 {
329 	static uint8_t buf[MAXSECSIZE];
330 	static const char imgpath[] = "adiantum.img";
331 	static const char dkpath[] = "/dev/dk";
332 	char cgdpath[MAXPATHLEN];
333 	int dkfd, cgdfd;
334 	unsigned i;
335 	ssize_t nwrit, nread;
336 
337 	rump_init();
338 
339 	RL(dkfd = open(imgpath, O_CREAT|O_RDWR|O_TRUNC, 0600));
340 	RL(ftruncate(dkfd, IMGSIZE));
341 	RL(rump_pub_etfs_register_withsize(dkpath, imgpath, RUMP_ETFS_BLK, 0,
342 		IMGSIZE));
343 	snprintf(cgdpath, sizeof cgdpath, "/dev/rcgd0%c",
344 	    getrawpartition() + 'a');
345 	RL(cgdfd = rump_sys_open(cgdpath, O_RDWR));
346 
347 	for (i = 0; i < __arraycount(C); i++) {
348 		/* write the plaintext out via cgd */
349 		RL(configure_cgd(cgdfd, dkpath, "adiantum", "encblkno1",
350 			C[i].key, 32));
351 		RL(nwrit = rump_sys_pwrite(cgdfd, C[i].ptxt, C[i].secsize,
352 			C[i].blkno * C[i].secsize));
353 		RL(unconfigure_cgd(cgdfd));
354 		if ((size_t)nwrit != C[i].secsize) {
355 			atf_tc_fail_nonfatal("truncated write: %zd != %u",
356 			    nwrit, C[i].secsize);
357 			continue;
358 		}
359 
360 		/* read the ciphertext out from the underlying file */
361 		RL(nread = pread(dkfd, buf, C[i].secsize,
362 			C[i].blkno * C[i].secsize));
363 		if ((size_t)nread != C[i].secsize) {
364 			atf_tc_fail_nonfatal("truncated read: %zd != %u",
365 			    nread, C[i].secsize);
366 			continue;
367 		}
368 		if (memcmp(buf, C[i].ctxt, C[i].secsize)) {
369 			hexdump(buf, C[i].secsize);
370 			hexdump(C[i].ctxt, C[i].secsize);
371 			atf_tc_fail_nonfatal("case %u ctxt mismatch", i);
372 			continue;
373 		}
374 
375 		/* read the plaintext back via cgd */
376 		RL(configure_cgd(cgdfd, dkpath, "adiantum", "encblkno1",
377 			C[i].key, 32));
378 		RL(nread = rump_sys_pread(cgdfd, buf, C[i].secsize,
379 			C[i].blkno * C[i].secsize));
380 		RL(unconfigure_cgd(cgdfd));
381 		if ((size_t)nread != C[i].secsize) {
382 			atf_tc_fail_nonfatal("truncated read: %zd != %u",
383 			    nread, C[i].secsize);
384 			continue;
385 		}
386 		if (memcmp(buf, C[i].ptxt, C[i].secsize)) {
387 			hexdump(buf, C[i].secsize);
388 			atf_tc_fail_nonfatal("case %u ptxt mismatch", i);
389 			continue;
390 		}
391 	}
392 
393 	RL(rump_sys_close(cgdfd));
394 	RL(close(dkfd));
395 }
396 
ATF_TP_ADD_TCS(tp)397 ATF_TP_ADD_TCS(tp)
398 {
399 
400 	ATF_TP_ADD_TC(tp, cgd_adiantum);
401 
402 	return atf_no_error();
403 }
404