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 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
33 #include "libcryptsetup.h"
36 #define MAXIMUM_WIPE_BYTES 1024 * 1024 * 32 /* 32 MiB */
38 static ssize_t _crypt_wipe_zero(int fd, int bsize, char *buffer,
39 uint64_t offset, uint64_t size)
41 memset(buffer, 0, size);
42 return write_lseek_blockwise(fd, bsize, buffer, size, offset);
45 static ssize_t _crypt_wipe_random(int fd, int bsize, char *buffer,
46 uint64_t offset, uint64_t size)
48 if (crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL) < 0)
51 return write_lseek_blockwise(fd, bsize, buffer, size, offset);
55 * Wipe using Peter Gutmann method described in
56 * http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
58 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
62 unsigned char write_modes[][3] = {
63 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
64 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
65 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
66 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
67 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
68 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
69 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
70 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
71 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
74 for(i = 0; i < buffer_size / 3; ++i) {
75 memcpy(buffer, write_modes[turn], 3);
80 static ssize_t _crypt_wipe_disk(int fd, int bsize, char *buffer,
81 uint64_t offset, uint64_t size)
87 for(i = 0; i < 39; ++i) {
89 r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
90 } else if(i >= 5 && i < 32) {
91 wipeSpecial(buffer, size, i - 5);
93 } else if(i >= 32 && i < 38) {
94 r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
95 } else if(i >= 38 && i < 39) {
96 memset(buffer, 0xFF, size);
102 written = write_lseek_blockwise(fd, bsize, buffer, size, offset);
103 if (written < 0 || written != (ssize_t)size)
107 /* Rewrite it finally with random */
108 return _crypt_wipe_random(fd, bsize, buffer, offset, size);
111 static ssize_t _crypt_wipe_ssd(int fd, int bsize, char *buffer,
112 uint64_t offset, uint64_t size)
114 // FIXME: for now just rewrite it by random
115 return _crypt_wipe_random(fd, bsize, buffer, offset, size);
118 int crypt_wipe(struct device *device,
121 crypt_wipe_type type,
126 int devfd, flags, rotational, bsize;
129 if (!size || size % SECTOR_SIZE || (size > MAXIMUM_WIPE_BYTES)) {
130 log_dbg("Unsuported wipe size for device %s: %ld.",
131 device_path(device), (unsigned long)size);
135 if (stat(device_path(device), &st) < 0) {
136 log_dbg("Device %s not found.", device_path(device));
140 if (type == CRYPT_WIPE_DISK && S_ISBLK(st.st_mode)) {
142 if (!crypt_sysfs_get_rotational(major(st.st_rdev),
146 log_dbg("Rotational flag is %d.", rotational);
148 type = CRYPT_WIPE_SSD;
151 bsize = device_block_size(device);
155 buffer = malloc(size);
159 flags = O_RDWR | O_DIRECT | O_SYNC;
161 /* use O_EXCL only for block devices */
162 if (exclusive && S_ISBLK(st.st_mode))
165 /* coverity[toctou] */
166 devfd = open(device_path(device), flags);
169 return errno ? -errno : -EINVAL;
172 // FIXME: use fixed block size and loop here
174 case CRYPT_WIPE_ZERO:
175 written = _crypt_wipe_zero(devfd, bsize, buffer, offset, size);
177 case CRYPT_WIPE_DISK:
178 written = _crypt_wipe_disk(devfd, bsize, buffer, offset, size);
181 written = _crypt_wipe_ssd(devfd, bsize, buffer, offset, size);
183 case CRYPT_WIPE_RANDOM:
184 written = _crypt_wipe_random(devfd, bsize, buffer, offset, size);
187 log_dbg("Unsuported wipe type requested: (%d)", type);
194 if (written != (ssize_t)size || written < 0)