Home | History | Annotate | Line # | Download | only in cgd
t_cgd_adiantum.c revision 1.2
      1 /*	$NetBSD: t_cgd_adiantum.c,v 1.2 2020/06/30 04:15:46 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 static const struct {
     51 	uint8_t		key[32];
     52 	uint64_t	blkno;
     53 	unsigned	secsize;
     54 	const uint8_t	*ptxt;
     55 	const uint8_t	*ctxt;
     56 } C[] = {
     57 	[0] = {
     58 		.key = {0},
     59 		.blkno = 0,
     60 		.secsize = 512,
     61 		.ptxt = (const uint8_t[512]) {0},
     62 		.ctxt = (const uint8_t[512]) {
     63 			0x51,0x6d,0xe2,0x81, 0x26,0xd5,0xc8,0xd7,
     64 			0xff,0xc6,0xc2,0xff, 0x39,0xbf,0x15,0x15,
     65 			0x46,0x80,0x44,0x65, 0x76,0xa1,0x56,0xae,
     66 			0xa0,0xb6,0x44,0x05, 0xb7,0xb1,0x32,0x23,
     67 			0x80,0x07,0xdd,0x31, 0x57,0x69,0xf5,0x10,
     68 			0x2d,0x53,0x54,0x8a, 0x1c,0x30,0x15,0x53,
     69 			0x40,0xb4,0x75,0xb2, 0xa1,0x8a,0xbe,0xdf,
     70 			0xf7,0x10,0xe0,0x38, 0xf9,0x70,0x29,0xda,
     71 			0xf0,0x95,0xcd,0xe9, 0x47,0xa1,0x32,0xa3,
     72 			0x83,0xca,0xe3,0x36, 0xc3,0x21,0x00,0xc2,
     73 			0x0a,0xb4,0x0e,0x67, 0x69,0xe6,0xe8,0x14,
     74 			0x74,0x98,0x69,0xd0, 0x6e,0xab,0x23,0xbc,
     75 			0xa9,0x1e,0xf8,0x2d, 0x98,0x59,0x98,0x81,
     76 			0x29,0x70,0xa8,0x1e, 0x26,0x13,0xba,0x53,
     77 			0x9e,0x83,0xe9,0x35, 0x73,0x8c,0xf9,0xb6,
     78 			0x10,0x17,0xda,0xe8, 0x21,0xcc,0x7d,0xd2,
     79 			0x8e,0x23,0xb9,0x63, 0xde,0xcf,0xa7,0x53,
     80 			0x56,0x1c,0xc8,0x53, 0x91,0x17,0x8f,0xec,
     81 			0x93,0x66,0x8b,0x0f, 0x18,0x6e,0xa5,0x9d,
     82 			0x8e,0x99,0x36,0x1c, 0x23,0xb6,0x0f,0x5d,
     83 			0x75,0xc3,0xfd,0x35, 0xc5,0x68,0x9c,0xe1,
     84 			0xba,0x19,0x1a,0x09, 0xca,0x40,0x1f,0xee,
     85 			0x0f,0x76,0x84,0x92, 0x72,0xdf,0x62,0x1b,
     86 			0x2e,0xa9,0x36,0xbe, 0xca,0x7e,0xc6,0x69,
     87 			0xc6,0x27,0xf8,0x12, 0xbf,0x6e,0xd3,0xf0,
     88 			0xb0,0x10,0x9c,0x67, 0x76,0x40,0xc8,0x36,
     89 			0x8e,0x73,0xec,0xa2, 0xdb,0x4a,0x0a,0xd9,
     90 			0x1b,0xa3,0x28,0x30, 0x84,0xa4,0xff,0xa0,
     91 			0xe7,0x1e,0xf4,0xb2, 0xfe,0x59,0x79,0xdf,
     92 			0x8d,0x66,0x12,0xac, 0xf6,0x1a,0x0f,0xa6,
     93 			0x4e,0x86,0x8c,0x80, 0x95,0x11,0xee,0x55,
     94 			0xe3,0xe0,0x43,0x56, 0xa2,0xfc,0xa2,0xbd,
     95 			0xad,0x6f,0xfc,0xf9, 0x4c,0x04,0x51,0xf4,
     96 			0xd9,0x17,0x96,0xdc, 0xd3,0xd0,0xd7,0xeb,
     97 			0xa8,0xdc,0x34,0x65, 0xc7,0xcf,0xed,0x06,
     98 			0xf8,0xa3,0xff,0x31, 0x3e,0x15,0x2f,0x62,
     99 			0x8c,0x73,0x7f,0x8c, 0x80,0x4d,0x4b,0x6d,
    100 			0xcf,0xc6,0xd0,0xdd, 0x7e,0x3a,0x1e,0x88,
    101 			0xb7,0xdd,0x23,0xa6, 0xa0,0x0d,0x6c,0xaf,
    102 			0xd6,0x5b,0xfd,0x76, 0x66,0xee,0x02,0xa6,
    103 			0x10,0xda,0x42,0xfb, 0x15,0xc3,0xe4,0xa7,
    104 			0x8b,0x2b,0xfa,0x5d, 0xba,0xce,0xcd,0x9f,
    105 			0x76,0x38,0x66,0xff, 0x74,0x08,0x34,0xf3,
    106 			0x3d,0x12,0xf4,0x8d, 0x5e,0x54,0x2b,0x37,
    107 			0x06,0xd3,0x03,0xc9, 0xd9,0x29,0x53,0x65,
    108 			0x76,0x00,0x24,0x50, 0x30,0x06,0x6c,0x69,
    109 			0x31,0xcc,0x89,0x7c, 0x97,0xae,0xac,0x74,
    110 			0x35,0x43,0xa3,0xe5, 0x40,0x58,0x3d,0xb9,
    111 			0x08,0x46,0x5e,0x5f, 0x07,0xc5,0x41,0x32,
    112 			0xab,0xa4,0x5a,0xab, 0x59,0x2b,0x54,0xee,
    113 			0x24,0x92,0xd3,0x08, 0xb8,0x99,0x9e,0x13,
    114 			0x3c,0x2c,0x05,0xe6, 0xc1,0x6f,0xa1,0x5d,
    115 			0xa9,0x09,0x1a,0x96, 0x76,0xe4,0x31,0xc6,
    116 			0xcc,0xad,0x28,0x58, 0x73,0x4d,0x1a,0x19,
    117 			0x3d,0xcd,0xaf,0x8c, 0xd8,0x24,0xff,0x72,
    118 			0xdc,0x4e,0x07,0x6e, 0xd8,0xbc,0x3b,0x2b,
    119 			0xf5,0xe5,0xfa,0x30, 0x7d,0xaa,0x59,0x40,
    120 			0x78,0x01,0xa4,0x55, 0xdc,0xe6,0x7b,0xae,
    121 			0x87,0x8e,0x11,0xbb, 0x65,0xf7,0x8a,0x4f,
    122 			0x37,0x7e,0xe1,0xac, 0x62,0xf1,0x64,0x8f,
    123 			0xc1,0xfd,0x3e,0x34, 0x1f,0x60,0xba,0x61,
    124 			0x98,0xae,0x19,0xce, 0x54,0x22,0x64,0x09,
    125 			0x67,0x82,0x6b,0x4b, 0xdf,0x26,0x77,0xde,
    126 			0xd6,0x13,0x00,0xee, 0x2c,0x18,0x49,0xd9,
    127 		},
    128 	},
    129 	[1] = {
    130 		.key = {0},
    131 		.blkno = 1,
    132 		.secsize = 512,
    133 		.ptxt = (const uint8_t[512]) {0},
    134 		.ctxt = (const uint8_t[512]) {
    135 			0xf2,0x23,0x68,0x5a, 0x15,0x11,0x56,0xa1,
    136 			0x71,0x57,0x5c,0x5e, 0x32,0xd4,0xdd,0xbb,
    137 			0x7a,0x0c,0x84,0x23, 0xe9,0x2f,0x1b,0x63,
    138 			0x3c,0x4d,0xad,0xfd, 0x6e,0xc0,0xdb,0x79,
    139 			0x23,0xa5,0x13,0xfe, 0x17,0x3c,0x4a,0x27,
    140 			0xb9,0xbc,0xf0,0xf6, 0x67,0x98,0xa8,0x64,
    141 			0xce,0xf0,0x17,0x0a, 0xa8,0x05,0x0f,0xf2,
    142 			0xff,0xb0,0x7a,0x9e, 0x1a,0xcf,0x5d,0x0e,
    143 			0x9f,0xb0,0x9a,0xd0, 0x7c,0xf2,0x88,0x96,
    144 			0xe2,0x8d,0xdb,0xa2, 0x19,0x30,0x3d,0x5d,
    145 			0x66,0x28,0x40,0x53, 0xb9,0x8d,0xbb,0x24,
    146 			0x3a,0x4c,0x00,0xac, 0x20,0x86,0x96,0x83,
    147 			0x2c,0x77,0x5e,0x18, 0x0c,0xfa,0x27,0x51,
    148 			0xe1,0x5d,0xd3,0xd9, 0xe1,0x5d,0x27,0x1f,
    149 			0x49,0x74,0xfd,0x2a, 0xc3,0xe5,0xa0,0xf6,
    150 			0x5a,0x58,0xe3,0x1f, 0x4a,0xa6,0xc2,0x25,
    151 			0xe4,0xb5,0xc8,0x0d, 0x9f,0xa7,0xc0,0x6e,
    152 			0xab,0xb3,0xfc,0x9f, 0xe1,0x72,0x8a,0x69,
    153 			0xf1,0xc6,0x54,0xb8, 0xeb,0x70,0xed,0xfe,
    154 			0x95,0xf7,0x0d,0x55, 0x95,0x13,0x7a,0x82,
    155 			0xac,0x83,0xd2,0xa3, 0xdc,0x5b,0xba,0x4e,
    156 			0xae,0xdd,0xe9,0x22, 0x9e,0xe2,0x72,0xaf,
    157 			0x9a,0xc0,0x53,0x96, 0xb9,0x7d,0x47,0x28,
    158 			0x4a,0x93,0x6a,0xfb, 0x59,0x25,0x49,0x39,
    159 			0xda,0x23,0xe8,0x28, 0x42,0xba,0x58,0x26,
    160 			0x29,0xf5,0x4c,0x85, 0xbb,0x62,0xfc,0x12,
    161 			0x28,0xbd,0xec,0x3f, 0xf4,0x86,0x80,0xf0,
    162 			0x69,0x81,0x99,0xe3, 0x95,0x0d,0xe8,0x8f,
    163 			0xeb,0x60,0xb6,0x2a, 0xbf,0xf1,0x41,0xe4,
    164 			0x68,0x4f,0x4b,0xe3, 0x49,0x2c,0x1e,0xad,
    165 			0x0d,0x8f,0x63,0x40, 0xb9,0xee,0x4d,0x09,
    166 			0x12,0x45,0x97,0x64, 0x97,0xd5,0x5f,0xa3,
    167 			0xb0,0x4b,0xdf,0x3f, 0x59,0x9f,0xab,0x12,
    168 			0x3d,0x4b,0x54,0xdc, 0xea,0xe0,0x55,0x5e,
    169 			0x1c,0xfd,0xe9,0x7e, 0x40,0x24,0x88,0x6c,
    170 			0x8d,0xfc,0xc2,0x57, 0xd2,0x37,0xb2,0x12,
    171 			0xc2,0x03,0x0d,0xac, 0xb8,0x9b,0x62,0x61,
    172 			0x23,0xc0,0x7a,0x06, 0xdb,0x62,0x86,0x06,
    173 			0xaf,0xa5,0x98,0x75, 0xd9,0x4e,0x8a,0xf2,
    174 			0xc5,0x64,0xad,0xf2, 0xf4,0xc2,0x7f,0xa2,
    175 			0x25,0xf4,0xd0,0x44, 0x57,0x8b,0x89,0xe2,
    176 			0x08,0xea,0x86,0x72, 0x37,0xe3,0x7e,0x92,
    177 			0x22,0xa0,0x32,0x05, 0x30,0x90,0xcc,0x44,
    178 			0x6f,0x2c,0x75,0xae, 0x28,0x90,0x34,0xe3,
    179 			0x05,0x88,0xcd,0x77, 0x1d,0x6a,0x72,0x56,
    180 			0x49,0x3f,0x3d,0x0b, 0x49,0x04,0x98,0x65,
    181 			0x66,0x0e,0xfd,0x7d, 0xca,0x32,0x74,0x66,
    182 			0xa0,0xd7,0x04,0xdb, 0x83,0x4b,0x7f,0x83,
    183 			0x22,0x43,0x98,0x93, 0x0d,0x0b,0xb1,0x8d,
    184 			0x8c,0x8b,0x9e,0x08, 0xb9,0xb0,0xd9,0x82,
    185 			0xcd,0x20,0x5e,0x19, 0x5d,0xa0,0x6a,0x71,
    186 			0x05,0xf9,0x18,0x3d, 0x6b,0xb7,0xb6,0x56,
    187 			0x03,0xa3,0x53,0x58, 0x7d,0xf8,0x25,0xca,
    188 			0x26,0x02,0xc1,0xa6, 0x72,0x70,0xc3,0xe3,
    189 			0x59,0x64,0xe1,0x25, 0x34,0x79,0xb3,0x5e,
    190 			0x08,0xe9,0xb8,0x91, 0xb6,0x5d,0x3a,0x44,
    191 			0x20,0x60,0x61,0xf4, 0x28,0x93,0x8f,0x89,
    192 			0xbe,0xea,0x55,0xda, 0x43,0x38,0x96,0xc8,
    193 			0x50,0x01,0x09,0xaf, 0x76,0x92,0x83,0xae,
    194 			0x3b,0x82,0x6f,0x49, 0x0b,0x18,0x9c,0xef,
    195 			0x92,0x06,0x11,0xeb, 0x41,0x34,0xf4,0x7b,
    196 			0xc4,0x9a,0x9f,0xe4, 0xb4,0xe7,0x1a,0x84,
    197 			0xd8,0x8b,0x3a,0x29, 0xb5,0x4e,0xf3,0x97,
    198 			0x6c,0xef,0xe9,0x62, 0x21,0x89,0x23,0xfd,
    199 		},
    200 	},
    201 	[2] = {
    202 		.key = {0},
    203 		.blkno = 0x100,
    204 		.secsize = 512,
    205 		.ptxt = (const uint8_t[512]) {0},
    206 		.ctxt = (const uint8_t[512]) {
    207 			0x32,0x26,0xaf,0x56, 0xbc,0x43,0xac,0x37,
    208 			0xb2,0x8d,0xa4,0xfb, 0x32,0xdc,0x09,0x03,
    209 			0xd9,0x44,0xce,0x4e, 0x70,0xaf,0xed,0x83,
    210 			0x4b,0x9c,0x85,0x11, 0xd2,0x6a,0x70,0x15,
    211 			0xea,0x7b,0x5e,0xac, 0x5d,0x08,0x25,0xd7,
    212 			0x8c,0x23,0x7c,0x15, 0xb7,0x20,0xd1,0x08,
    213 			0xe0,0x81,0x71,0xbe, 0x68,0xca,0xe2,0xcd,
    214 			0x98,0xe5,0x40,0xe0, 0xf5,0x84,0xcc,0x6a,
    215 			0x3c,0xa0,0xe8,0x2c, 0x02,0x4c,0x95,0xb5,
    216 			0x58,0x86,0x86,0x61, 0x71,0x7f,0xd7,0xf9,
    217 			0xd9,0x64,0x80,0xf6, 0xea,0x92,0xbc,0x65,
    218 			0x3b,0x07,0x77,0xaa, 0xb1,0xb1,0xf5,0xd6,
    219 			0x6d,0x89,0x63,0x14, 0xc0,0xcc,0x7a,0x2b,
    220 			0xc4,0x32,0x63,0xda, 0xa6,0xc6,0xc8,0xc6,
    221 			0x4c,0x4e,0x10,0x63, 0x3b,0x93,0x80,0x77,
    222 			0x3e,0x54,0xd2,0x38, 0x13,0x79,0xbc,0x6c,
    223 			0x0b,0xd4,0x71,0x5c, 0x26,0xc0,0x81,0x09,
    224 			0xc7,0xd8,0x7a,0x04, 0x58,0x2e,0x50,0x6a,
    225 			0x3d,0xca,0xa1,0x66, 0x72,0xca,0xee,0x5a,
    226 			0xdd,0x13,0x67,0xb1, 0x54,0x72,0x41,0x2d,
    227 			0xfd,0x95,0x24,0xe3, 0x96,0x4a,0x41,0x03,
    228 			0xeb,0xeb,0x99,0x49, 0x52,0xac,0x3a,0x28,
    229 			0x81,0x54,0x1a,0xfb, 0xc3,0xcd,0x8e,0x9d,
    230 			0x0c,0x64,0x95,0xbb, 0x27,0xb8,0x6b,0x51,
    231 			0x7b,0xc4,0x57,0xc9, 0x29,0x4e,0x85,0x31,
    232 			0x1c,0xaa,0x63,0x2e, 0x7a,0x37,0x2e,0x06,
    233 			0xdc,0x58,0x39,0x3b, 0x60,0x34,0x59,0x15,
    234 			0x4f,0xba,0x33,0x52, 0x13,0xb0,0x7b,0x7c,
    235 			0x7e,0x00,0x0b,0x49, 0x15,0x9c,0x48,0xf4,
    236 			0x67,0xdd,0xc6,0x72, 0x87,0xbe,0xe7,0xf7,
    237 			0x21,0x95,0x82,0xc3, 0x41,0x3b,0x19,0xe3,
    238 			0xf3,0x28,0xcc,0x14, 0x5f,0xae,0x6f,0x07,
    239 			0x35,0x94,0x05,0x46, 0x02,0x5c,0x3c,0x46,
    240 			0xb1,0x2d,0xeb,0x6e, 0xa0,0x0f,0xea,0x40,
    241 			0x3e,0x35,0x6e,0x50, 0xc4,0x22,0xeb,0x93,
    242 			0xba,0x49,0xfb,0xf0, 0x8e,0x2a,0xa1,0xaf,
    243 			0xf4,0x91,0xb2,0xc5, 0x7d,0x8e,0xba,0x45,
    244 			0x53,0x75,0xc3,0xcc, 0x3e,0x02,0x0e,0x4d,
    245 			0x2e,0xda,0x45,0xd2, 0x31,0xc7,0x1b,0x6b,
    246 			0x99,0x71,0x8d,0xd8, 0x8c,0x94,0xa2,0x02,
    247 			0x6c,0xb0,0x32,0x8f, 0xce,0x04,0x61,0x0a,
    248 			0x3f,0x17,0x3a,0x28, 0xda,0x31,0xdc,0xec,
    249 			0xbc,0xea,0x1b,0x37, 0x9b,0x36,0x04,0xb1,
    250 			0xb5,0x7f,0xfe,0x1a, 0xd8,0x11,0xb7,0x0a,
    251 			0x77,0x2e,0x6d,0x22, 0x79,0x9e,0x54,0x47,
    252 			0xea,0xf5,0x17,0x38, 0xd0,0xe2,0x23,0x68,
    253 			0x92,0x88,0x42,0x59, 0x2c,0x61,0x53,0x2b,
    254 			0x99,0xed,0x7b,0x85, 0xfb,0xb8,0xe8,0x0c,
    255 			0x4b,0x81,0x1e,0x0f, 0x42,0x04,0x8b,0x55,
    256 			0x2c,0x34,0x46,0x98, 0x9c,0x47,0x08,0x70,
    257 			0x46,0x45,0x5e,0xa8, 0x62,0x92,0x94,0xcd,
    258 			0x73,0x1c,0xef,0x8b, 0x96,0x5f,0x6d,0x76,
    259 			0x07,0x99,0x6f,0xe0, 0x1d,0xdc,0x1d,0x1c,
    260 			0x3f,0xb4,0x5f,0x9b, 0x34,0x0c,0x75,0x10,
    261 			0x7e,0x0d,0xf8,0xbb, 0xc3,0x8a,0x2a,0x15,
    262 			0x01,0x3a,0x56,0x73, 0x5b,0xe9,0x5f,0xf2,
    263 			0x6a,0x1b,0x17,0xce, 0xf3,0x3e,0xc9,0xdf,
    264 			0x76,0xe8,0xcd,0xf2, 0x6d,0xb1,0xdc,0x29,
    265 			0x8c,0xa3,0x89,0x73, 0x69,0x86,0xa9,0x05,
    266 			0xbe,0x63,0xc8,0x7c, 0x36,0xc0,0x88,0x74,
    267 			0x64,0x91,0xdd,0xb7, 0x92,0x73,0x7e,0xc1,
    268 			0x01,0x95,0xb3,0x95, 0x53,0x33,0x16,0xcd,
    269 			0xe9,0xd7,0x56,0x61, 0x71,0x49,0x24,0x9b,
    270 			0x9a,0x10,0x7e,0x50, 0x7e,0xd3,0xe2,0x9d,
    271 		},
    272 	},
    273 };
    274 
    275 static void
    276 hexdump(const void *buf, size_t len)
    277 {
    278 	const unsigned char *p = buf;
    279 	size_t i;
    280 
    281 	for (i = 0; i < len; i++) {
    282 		if (i % 16 == 8)
    283 			printf(" ");
    284 		printf(" %02hhx", p[i]);
    285 		if ((i + 1) % 16 == 0)
    286 			printf("\n");
    287 	}
    288 	if (i % 16)
    289 		printf("\n");
    290 }
    291 
    292 static int
    293 configure_cgd(int fd, const char *dkpath, const char *alg,
    294     const char *ivmethod, const void *key, size_t keybytes)
    295 {
    296 	struct cgd_ioctl ci;
    297 
    298 	memset(&ci, 0, sizeof(ci));
    299 	ci.ci_disk = dkpath;
    300 	ci.ci_alg = alg;
    301 	ci.ci_ivmethod = ivmethod;
    302 	ci.ci_keylen = 8*keybytes;
    303 	ci.ci_key = key;
    304 	ci.ci_blocksize = (size_t)-1;
    305 
    306 	return rump_sys_ioctl(fd, CGDIOCSET, &ci);
    307 }
    308 
    309 static int
    310 unconfigure_cgd(int fd)
    311 {
    312 	struct cgd_ioctl ci;
    313 
    314 	return rump_sys_ioctl(fd, CGDIOCCLR, &ci);
    315 }
    316 
    317 ATF_TC(cgd_adiantum);
    318 ATF_TC_HEAD(cgd_adiantum, tc)
    319 {
    320 
    321 	atf_tc_set_md_var(tc, "descr", "Adiantum tests");
    322 }
    323 
    324 ATF_TC_BODY(cgd_adiantum, tc)
    325 {
    326 	static uint8_t buf[MAXSECSIZE];
    327 	static const char imgpath[] = "adiantum.img";
    328 	static const char dkpath[] = "/dev/dk";
    329 	char cgdpath[MAXPATHLEN];
    330 	int dkfd, cgdfd;
    331 	unsigned i;
    332 	ssize_t nwrit, nread;
    333 
    334 	rump_init();
    335 
    336 	RL(dkfd = open(imgpath, O_CREAT|O_RDWR|O_TRUNC, 0600));
    337 	RL(ftruncate(dkfd, IMGSIZE));
    338 	RL(rump_pub_etfs_register_withsize(dkpath, imgpath, RUMP_ETFS_BLK, 0,
    339 		IMGSIZE));
    340 	snprintf(cgdpath, sizeof cgdpath, "/dev/rcgd0%c",
    341 	    getrawpartition() + 'a');
    342 	RL(cgdfd = rump_sys_open(cgdpath, O_RDWR));
    343 
    344 	for (i = 0; i < __arraycount(C); i++) {
    345 		/* write the plaintext out via cgd */
    346 		RL(configure_cgd(cgdfd, dkpath, "adiantum", "encblkno1",
    347 			C[i].key, 32));
    348 		RL(nwrit = rump_sys_pwrite(cgdfd, C[i].ptxt, C[i].secsize,
    349 			C[i].blkno * C[i].secsize));
    350 		RL(unconfigure_cgd(cgdfd));
    351 		if (nwrit != C[i].secsize) {
    352 			atf_tc_fail_nonfatal("truncated write: %zd != %u",
    353 			    nwrit, C[i].secsize);
    354 			continue;
    355 		}
    356 
    357 		/* read the ciphertext out from the underlying file */
    358 		RL(nread = pread(dkfd, buf, C[i].secsize,
    359 			C[i].blkno * C[i].secsize));
    360 		if ((size_t)nread != C[i].secsize) {
    361 			atf_tc_fail_nonfatal("truncated read: %zd != %u",
    362 			    nread, C[i].secsize);
    363 			continue;
    364 		}
    365 		if (memcmp(buf, C[i].ctxt, C[i].secsize)) {
    366 			hexdump(buf, C[i].secsize);
    367 			hexdump(C[i].ctxt, C[i].secsize);
    368 			atf_tc_fail_nonfatal("case %u ctxt mismatch", i);
    369 			continue;
    370 		}
    371 
    372 		/* read the plaintext back via cgd */
    373 		RL(configure_cgd(cgdfd, dkpath, "adiantum", "encblkno1",
    374 			C[i].key, 32));
    375 		RL(nread = rump_sys_pread(cgdfd, buf, C[i].secsize,
    376 			C[i].blkno * C[i].secsize));
    377 		RL(unconfigure_cgd(cgdfd));
    378 		if ((size_t)nread != C[i].secsize) {
    379 			atf_tc_fail_nonfatal("truncated read: %zd != %u",
    380 			    nread, C[i].secsize);
    381 			continue;
    382 		}
    383 		if (memcmp(buf, C[i].ptxt, C[i].secsize)) {
    384 			hexdump(buf, C[i].secsize);
    385 			atf_tc_fail_nonfatal("case %u ptxt mismatch", i);
    386 			continue;
    387 		}
    388 	}
    389 
    390 	RL(rump_sys_close(cgdfd));
    391 	RL(close(dkfd));
    392 }
    393 
    394 ATF_TP_ADD_TCS(tp)
    395 {
    396 
    397 	ATF_TP_ADD_TC(tp, cgd_adiantum);
    398 
    399 	return atf_no_error();
    400 }
    401