2 * utils_wipe - wipe a device
4 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2009-2012, 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.
27 #include <sys/types.h>
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
34 #include "libcryptsetup.h"
37 #define MAXIMUM_WIPE_BYTES 1024 * 1024 * 32 /* 32 MiB */
39 static ssize_t _crypt_wipe_zero(int fd, int bsize, char *buffer,
40 uint64_t offset, uint64_t size)
42 memset(buffer, 0, size);
43 return write_lseek_blockwise(fd, bsize, buffer, size, offset);
46 static ssize_t _crypt_wipe_random(int fd, int bsize, char *buffer,
47 uint64_t offset, uint64_t size)
49 if (crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL) < 0)
52 return write_lseek_blockwise(fd, bsize, buffer, size, offset);
56 * Wipe using Peter Gutmann method described in
57 * http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
59 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
63 unsigned char write_modes[][3] = {
64 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
65 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
66 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
67 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
68 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
69 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
70 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
71 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
72 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
75 for(i = 0; i < buffer_size / 3; ++i) {
76 memcpy(buffer, write_modes[turn], 3);
81 static ssize_t _crypt_wipe_disk(int fd, int bsize, char *buffer,
82 uint64_t offset, uint64_t size)
88 for(i = 0; i < 39; ++i) {
90 r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
91 } else if(i >= 5 && i < 32) {
92 wipeSpecial(buffer, size, i - 5);
94 } else if(i >= 32 && i < 38) {
95 r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
96 } else if(i >= 38 && i < 39) {
97 memset(buffer, 0xFF, size);
103 written = write_lseek_blockwise(fd, bsize, buffer, size, offset);
104 if (written < 0 || written != (ssize_t)size)
108 /* Rewrite it finally with random */
109 return _crypt_wipe_random(fd, bsize, buffer, offset, size);
112 static ssize_t _crypt_wipe_ssd(int fd, int bsize, char *buffer,
113 uint64_t offset, uint64_t size)
115 // FIXME: for now just rewrite it by random
116 return _crypt_wipe_random(fd, bsize, buffer, offset, size);
119 int crypt_wipe(struct device *device,
122 crypt_wipe_type type,
127 int devfd, flags, bsize;
130 if (!size || size % SECTOR_SIZE || (size > MAXIMUM_WIPE_BYTES)) {
131 log_dbg("Unsuported wipe size for device %s: %ld.",
132 device_path(device), (unsigned long)size);
136 if (stat(device_path(device), &st) < 0) {
137 log_dbg("Device %s not found.", device_path(device));
141 if (type == CRYPT_WIPE_DISK && S_ISBLK(st.st_mode)) {
142 if (!crypt_dev_is_rotational(major(st.st_rdev),
143 minor(st.st_rdev))) {
144 type = CRYPT_WIPE_SSD;
145 log_dbg("Non-rotational device, using SSD wipe mode.");
147 log_dbg("Rotational device, using normal wipe mode.");
150 bsize = device_block_size(device);
154 buffer = malloc(size);
160 /* use O_EXCL only for block devices */
161 if (exclusive && S_ISBLK(st.st_mode))
164 /* coverity[toctou] */
165 devfd = device_open(device, flags);
168 return errno ? -errno : -EINVAL;
171 // FIXME: use fixed block size and loop here
173 case CRYPT_WIPE_ZERO:
174 written = _crypt_wipe_zero(devfd, bsize, buffer, offset, size);
176 case CRYPT_WIPE_DISK:
177 written = _crypt_wipe_disk(devfd, bsize, buffer, offset, size);
180 written = _crypt_wipe_ssd(devfd, bsize, buffer, offset, size);
182 case CRYPT_WIPE_RANDOM:
183 written = _crypt_wipe_random(devfd, bsize, buffer, offset, size);
186 log_dbg("Unsuported wipe type requested: (%d)", type);
193 if (written != (ssize_t)size || written < 0)