Version 1.4.2.
[platform/upstream/cryptsetup.git] / lib / utils_wipe.c
1 /*
2  * utils_wipe - wipe a device
3  *
4  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
5  * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <fcntl.h>
31
32 #include "libcryptsetup.h"
33 #include "internal.h"
34
35 #define MAXIMUM_WIPE_BYTES      1024 * 1024 * 32 /* 32 MiB */
36
37 static ssize_t _crypt_wipe_zero(int fd, char *buffer, uint64_t offset, uint64_t size)
38 {
39         memset(buffer, 0, size);
40         return write_lseek_blockwise(fd, buffer, size, offset);
41 }
42
43 /*
44  * Wipe using Peter Gutmann method described in
45  * http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
46  */
47 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
48 {
49         unsigned int i;
50
51         unsigned char write_modes[][3] = {
52                 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
53                 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
54                 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
55                 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
56                 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
57                 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
58                 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
59                 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
60                 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
61         };
62
63         for(i = 0; i < buffer_size / 3; ++i) {
64                 memcpy(buffer, write_modes[turn], 3);
65                 buffer += 3;
66         }
67 }
68
69 static ssize_t _crypt_wipe_disk(int fd, char *buffer, uint64_t offset, uint64_t size)
70 {
71         unsigned int i;
72         ssize_t written;
73
74         for(i = 0; i < 39; ++i) {
75                 if                (i <  5) crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
76                 else if(i >=  5 && i < 32) wipeSpecial(buffer, size, i - 5);
77                 else if(i >= 32 && i < 38) crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
78                 else if(i >= 38 && i < 39) memset(buffer, 0xFF, size);
79
80                 written = write_lseek_blockwise(fd, buffer, size, offset);
81                 if (written < 0 || written != (ssize_t)size)
82                         return written;
83         }
84
85         return written;
86 }
87
88 static ssize_t _crypt_wipe_random(int fd, char *buffer, uint64_t offset, uint64_t size)
89 {
90         crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
91         return write_lseek_blockwise(fd, buffer, size, offset);
92 }
93
94 static ssize_t _crypt_wipe_ssd(int fd, char *buffer, uint64_t offset, uint64_t size)
95 {
96         // FIXME: for now just rewrite it by random
97         return _crypt_wipe_random(fd, buffer, offset, size);
98 }
99
100 int crypt_wipe(const char *device,
101                uint64_t offset,
102                uint64_t size,
103                crypt_wipe_type type,
104                int exclusive)
105 {
106         struct stat st;
107         char *buffer;
108         int devfd, flags, rotational;
109         ssize_t written;
110
111         if (!size || size % SECTOR_SIZE || (size > MAXIMUM_WIPE_BYTES)) {
112                 log_dbg("Unsuported wipe size for device %s: %ld.",
113                         device, (unsigned long)size);
114                 return -EINVAL;
115         }
116
117         if (stat(device, &st) < 0) {
118                 log_dbg("Device %s not found.", device);
119                 return -EINVAL;
120         }
121
122         if (type == CRYPT_WIPE_DISK) {
123                 if (!crypt_sysfs_get_rotational(major(st.st_rdev),
124                                                 minor(st.st_rdev),
125                                                 &rotational))
126                         rotational = 1;
127                 log_dbg("Rotational flag is %d.", rotational);
128                 if (!rotational)
129                         type = CRYPT_WIPE_SSD;
130         }
131
132         buffer = malloc(size);
133         if (!buffer)
134                 return -ENOMEM;
135
136         flags = O_WRONLY | O_DIRECT | O_SYNC;
137
138         /* use O_EXCL only for block devices */
139         if (exclusive && S_ISBLK(st.st_mode))
140                 flags |= O_EXCL;
141
142         devfd = open(device, flags);
143         if (devfd == -1) {
144                 free(buffer);
145                 return errno == EBUSY ? -EBUSY : -EINVAL;
146         }
147
148         // FIXME: use fixed block size and loop here
149         switch (type) {
150                 case CRYPT_WIPE_ZERO:
151                         written = _crypt_wipe_zero(devfd, buffer, offset, size);
152                         break;
153                 case CRYPT_WIPE_DISK:
154                         written = _crypt_wipe_disk(devfd, buffer, offset, size);
155                 case CRYPT_WIPE_SSD:
156                         written = _crypt_wipe_ssd(devfd, buffer, offset, size);
157                         break;
158                 case CRYPT_WIPE_RANDOM:
159                         written = _crypt_wipe_random(devfd, buffer, offset, size);
160                 default:
161                         log_dbg("Unsuported wipe type requested: (%d)", type);
162                         written = -1;
163         }
164
165         close(devfd);
166         free(buffer);
167
168         if (written != (ssize_t)size || written < 0)
169                 return -EIO;
170
171         return 0;
172 }