2 * utils_wipe - wipe a device
4 * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2009-2021 Milan Broz
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * Wipe using Peter Gutmann method described in
29 * https://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
30 * Note: used only for rotational device (and even there it is not needed today...)
32 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
36 unsigned char write_modes[][3] = {
37 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
38 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
39 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
40 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
41 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
42 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
43 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
44 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
45 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
48 for (i = 0; i < buffer_size / 3; ++i) {
49 memcpy(buffer, write_modes[turn], 3);
54 static int crypt_wipe_special(struct crypt_device *cd, int fd, size_t bsize,
55 size_t alignment, char *buffer,
56 uint64_t offset, size_t size)
62 for (i = 0; i < 39; ++i) {
64 r = crypt_random_get(cd, buffer, size, CRYPT_RND_NORMAL);
65 } else if (i >= 5 && i < 32) {
66 wipeSpecial(buffer, size, i - 5);
68 } else if (i >= 32 && i < 38) {
69 r = crypt_random_get(cd, buffer, size, CRYPT_RND_NORMAL);
70 } else if (i >= 38 && i < 39) {
71 memset(buffer, 0xFF, size);
77 written = write_lseek_blockwise(fd, bsize, alignment,
78 buffer, size, offset);
79 if (written < 0 || written != (ssize_t)size)
83 /* Rewrite it finally with random */
84 if (crypt_random_get(cd, buffer, size, CRYPT_RND_NORMAL) < 0)
87 written = write_lseek_blockwise(fd, bsize, alignment, buffer, size, offset);
88 if (written < 0 || written != (ssize_t)size)
94 static int wipe_block(struct crypt_device *cd, int devfd, crypt_wipe_pattern pattern,
95 char *sf, size_t device_block_size, size_t alignment,
96 size_t wipe_block_size, uint64_t offset, bool *need_block_init)
100 if (pattern == CRYPT_WIPE_SPECIAL)
101 return crypt_wipe_special(cd, devfd, device_block_size, alignment,
102 sf, offset, wipe_block_size);
104 if (*need_block_init) {
105 if (pattern == CRYPT_WIPE_ZERO) {
106 memset(sf, 0, wipe_block_size);
107 *need_block_init = false;
109 } else if (pattern == CRYPT_WIPE_RANDOM) {
110 r = crypt_random_get(cd, sf, wipe_block_size,
111 CRYPT_RND_NORMAL) ? -EIO : 0;
112 *need_block_init = true;
113 } else if (pattern == CRYPT_WIPE_ENCRYPTED_ZERO) {
115 r = crypt_random_get(cd, sf, wipe_block_size,
116 CRYPT_RND_NORMAL) ? -EIO : 0;
117 *need_block_init = true;
125 if (write_blockwise(devfd, device_block_size, alignment, sf,
126 wipe_block_size) == (ssize_t)wipe_block_size)
132 int crypt_wipe_device(struct crypt_device *cd,
133 struct device *device,
134 crypt_wipe_pattern pattern,
137 size_t wipe_block_size,
138 int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
142 size_t bsize, alignment;
145 bool need_block_init = true;
147 /* Note: LUKS1 calls it with wipe_block not aligned to multiple of bsize */
148 bsize = device_block_size(cd, device);
149 alignment = device_alignment(device);
150 if (!bsize || !alignment || !wipe_block_size)
153 /* FIXME: if wipe_block_size < bsize, then a wipe is highly ineffective */
155 /* Everything must be aligned to SECTOR_SIZE */
156 if (MISALIGNED_512(offset) || MISALIGNED_512(length) || MISALIGNED_512(wipe_block_size))
159 if (device_is_locked(device))
160 devfd = device_open_locked(cd, device, O_RDWR);
162 devfd = device_open(cd, device, O_RDWR);
164 return errno ? -errno : -EINVAL;
167 dev_size = offset + length;
169 r = device_size(device, &dev_size);
173 if (dev_size <= offset) {
179 r = posix_memalign((void **)&sf, alignment, wipe_block_size);
183 if (lseek64(devfd, offset, SEEK_SET) < 0) {
184 log_err(cd, _("Cannot seek to device offset."));
189 if (progress && progress(dev_size, offset, usrptr)) {
190 r = -EINVAL; /* No change yet, treat this as a parameter error */
194 if (pattern == CRYPT_WIPE_SPECIAL && !device_is_rotational(device)) {
195 log_dbg(cd, "Non-rotational device, using random data wipe mode.");
196 pattern = CRYPT_WIPE_RANDOM;
199 while (offset < dev_size) {
200 if ((offset + wipe_block_size) > dev_size)
201 wipe_block_size = dev_size - offset;
203 //log_dbg("Wipe %012" PRIu64 "-%012" PRIu64 " bytes", offset, offset + wipe_block_size);
205 r = wipe_block(cd, devfd, pattern, sf, bsize, alignment,
206 wipe_block_size, offset, &need_block_init);
208 log_err(cd,_("Device wipe error, offset %" PRIu64 "."), offset);
212 offset += wipe_block_size;
214 if (progress && progress(dev_size, offset, usrptr)) {
220 device_sync(cd, device);
226 int crypt_wipe(struct crypt_device *cd,
227 const char *dev_path,
228 crypt_wipe_pattern pattern,
231 size_t wipe_block_size,
233 int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
236 struct device *device;
243 device = crypt_data_device(cd);
245 r = device_alloc_no_check(&device, dev_path);
249 if (flags & CRYPT_WIPE_NO_DIRECT_IO)
250 device_disable_direct_io(device);
253 if (!wipe_block_size)
254 wipe_block_size = 1024*1024;
256 log_dbg(cd, "Wipe [%u] device %s, offset %" PRIu64 ", length %" PRIu64 ", block %zu.",
257 (unsigned)pattern, device_path(device), offset, length, wipe_block_size);
259 r = crypt_wipe_device(cd, device, pattern, offset, length,
260 wipe_block_size, progress, usrptr);
263 device_free(cd, device);