1*9c046792SMark Johnston /*-
2*9c046792SMark Johnston * SPDX-License-Identifier: BSD-2-Clause
3*9c046792SMark Johnston *
4*9c046792SMark Johnston * Copyright (c) 2023 The FreeBSD Foundation
5*9c046792SMark Johnston *
6*9c046792SMark Johnston * This software was developed by Mark Johnston under sponsorship from
7*9c046792SMark Johnston * the FreeBSD Foundation.
8*9c046792SMark Johnston *
9*9c046792SMark Johnston * Redistribution and use in source and binary forms, with or without
10*9c046792SMark Johnston * modification, are permitted provided that the following conditions are
11*9c046792SMark Johnston * met:
12*9c046792SMark Johnston * 1. Redistributions of source code must retain the above copyright
13*9c046792SMark Johnston * notice, this list of conditions and the following disclaimer.
14*9c046792SMark Johnston * 2. Redistributions in binary form must reproduce the above copyright
15*9c046792SMark Johnston * notice, this list of conditions and the following disclaimer in
16*9c046792SMark Johnston * the documentation and/or other materials provided with the distribution.
17*9c046792SMark Johnston *
18*9c046792SMark Johnston * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*9c046792SMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*9c046792SMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*9c046792SMark Johnston * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*9c046792SMark Johnston * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*9c046792SMark Johnston * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*9c046792SMark Johnston * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*9c046792SMark Johnston * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*9c046792SMark Johnston * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*9c046792SMark Johnston * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*9c046792SMark Johnston * SUCH DAMAGE.
29*9c046792SMark Johnston */
30*9c046792SMark Johnston
31*9c046792SMark Johnston /*
32*9c046792SMark Johnston * Idea from a test case by Andrew "RhodiumToad" Gierth in Bugzilla PR 271766.
33*9c046792SMark Johnston */
34*9c046792SMark Johnston
35*9c046792SMark Johnston #include <sys/param.h>
36*9c046792SMark Johnston #include <sys/disk.h>
37*9c046792SMark Johnston #include <sys/mman.h>
38*9c046792SMark Johnston
39*9c046792SMark Johnston #include <crypto/cryptodev.h>
40*9c046792SMark Johnston
41*9c046792SMark Johnston #include <err.h>
42*9c046792SMark Johnston #include <fcntl.h>
43*9c046792SMark Johnston #include <stdlib.h>
44*9c046792SMark Johnston #include <string.h>
45*9c046792SMark Johnston #include <unistd.h>
46*9c046792SMark Johnston
47*9c046792SMark Johnston int
main(int argc,char ** argv)48*9c046792SMark Johnston main(int argc, char **argv)
49*9c046792SMark Johnston {
50*9c046792SMark Johnston const char *disk;
51*9c046792SMark Johnston char *buf1, *buf2;
52*9c046792SMark Johnston off_t disksz;
53*9c046792SMark Johnston size_t bufsz, iosz;
54*9c046792SMark Johnston ssize_t n;
55*9c046792SMark Johnston unsigned int offsets, secsz;
56*9c046792SMark Johnston int fd;
57*9c046792SMark Johnston
58*9c046792SMark Johnston if (argc != 2)
59*9c046792SMark Johnston errx(1, "Usage: %s <disk>", argv[0]);
60*9c046792SMark Johnston disk = argv[1];
61*9c046792SMark Johnston
62*9c046792SMark Johnston fd = open(disk, O_RDWR);
63*9c046792SMark Johnston if (fd < 0)
64*9c046792SMark Johnston err(1, "open(%s)", disk);
65*9c046792SMark Johnston
66*9c046792SMark Johnston if (ioctl(fd, DIOCGSECTORSIZE, &secsz) != 0)
67*9c046792SMark Johnston err(1, "ioctl(DIOCGSECTORSIZE)");
68*9c046792SMark Johnston if (secsz == 0)
69*9c046792SMark Johnston errx(1, "ioctl(DIOCGSECTORSIZE) returned 0");
70*9c046792SMark Johnston if (ioctl(fd, DIOCGMEDIASIZE, &disksz) != 0)
71*9c046792SMark Johnston err(1, "ioctl(DIOCGMEDIASIZE)");
72*9c046792SMark Johnston if (disksz / secsz < 2)
73*9c046792SMark Johnston errx(1, "disk needs to be at least 2 sectors in size");
74*9c046792SMark Johnston iosz = 2 * secsz;
75*9c046792SMark Johnston
76*9c046792SMark Johnston bufsz = iosz + secsz;
77*9c046792SMark Johnston buf1 = mmap(NULL, bufsz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
78*9c046792SMark Johnston -1, 0);
79*9c046792SMark Johnston if (buf1 == MAP_FAILED)
80*9c046792SMark Johnston err(1, "mmap");
81*9c046792SMark Johnston buf2 = mmap(NULL, bufsz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
82*9c046792SMark Johnston -1, 0);
83*9c046792SMark Johnston if (buf2 == MAP_FAILED)
84*9c046792SMark Johnston err(1, "mmap");
85*9c046792SMark Johnston
86*9c046792SMark Johnston arc4random_buf(buf1, bufsz);
87*9c046792SMark Johnston n = pwrite(fd, buf1, bufsz, 0);
88*9c046792SMark Johnston if (n < 0 || (size_t)n != bufsz)
89*9c046792SMark Johnston err(1, "pwrite");
90*9c046792SMark Johnston
91*9c046792SMark Johnston /*
92*9c046792SMark Johnston * Limit the number of offsets we test with, to avoid spending too much
93*9c046792SMark Johnston * time when the sector size is large.
94*9c046792SMark Johnston */
95*9c046792SMark Johnston offsets = MAX(EALG_MAX_BLOCK_LEN, HMAC_MAX_BLOCK_LEN) + 1;
96*9c046792SMark Johnston
97*9c046792SMark Johnston /*
98*9c046792SMark Johnston * Read test: read the first 2 sectors into buf1, then do the same with
99*9c046792SMark Johnston * buf2, except at varying offsets into buf2. After each read, compare
100*9c046792SMark Johnston * the buffers and make sure they're identical. This exercises corner
101*9c046792SMark Johnston * cases in the crypto layer's buffer handling.
102*9c046792SMark Johnston */
103*9c046792SMark Johnston n = pread(fd, buf1, iosz, 0);
104*9c046792SMark Johnston if (n < 0 || (size_t)n != iosz)
105*9c046792SMark Johnston err(1, "pread");
106*9c046792SMark Johnston for (unsigned int i = 0; i < offsets; i++) {
107*9c046792SMark Johnston n = pread(fd, buf2 + i, iosz, 0);
108*9c046792SMark Johnston if (n < 0 || (size_t)n != iosz)
109*9c046792SMark Johnston err(1, "pread");
110*9c046792SMark Johnston if (memcmp(buf1, buf2 + i, iosz) != 0)
111*9c046792SMark Johnston errx(1, "read mismatch at offset %u/%u", i, secsz);
112*9c046792SMark Johnston }
113*9c046792SMark Johnston
114*9c046792SMark Johnston /*
115*9c046792SMark Johnston * Write test. Try writing buffers at various alignments, and verify
116*9c046792SMark Johnston * that we read back what we wrote.
117*9c046792SMark Johnston */
118*9c046792SMark Johnston arc4random_buf(buf1, bufsz);
119*9c046792SMark Johnston for (unsigned int i = 0; i < offsets; i++) {
120*9c046792SMark Johnston n = pwrite(fd, buf1 + i, iosz, 0);
121*9c046792SMark Johnston if (n < 0 || (size_t)n != iosz)
122*9c046792SMark Johnston err(1, "pwrite");
123*9c046792SMark Johnston n = pread(fd, buf2, iosz, 0);
124*9c046792SMark Johnston if (n < 0 || (size_t)n != iosz)
125*9c046792SMark Johnston err(1, "pread");
126*9c046792SMark Johnston if (memcmp(buf1 + i, buf2, iosz) != 0)
127*9c046792SMark Johnston errx(1, "write mismatch at offset %u/%u", i, secsz);
128*9c046792SMark Johnston }
129*9c046792SMark Johnston
130*9c046792SMark Johnston return (0);
131*9c046792SMark Johnston }
132