-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgutman.h
103 lines (83 loc) · 2.35 KB
/
gutman.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//gutman.cpp
// "Secure Deletion of Data from Magnetic and Solid-State Memory" Peter Gut-
// mann: http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define SPC_WIPE_BUFSIZE 4096
// Plase see "secure programming cookbook for c and c++" problem "2.5 Erasing Files Securely"
static int write_data(int fd, const void *buf, size_t nbytes) {
size_t towrite, written = 0;
ssize_t result;
do {
if (nbytes - written > SSIZE_MAX) towrite = SSIZE_MAX;
else towrite = nbytes - written;
if ((result = write(fd, (const char *)buf + written, towrite)) >= 0)
written += result;
else if (errno != EINTR) return 0;
} while (written < nbytes);
return 1;
}
static int pattern_pass
(int fd, unsigned char *buf, size_t bufsz, size_t filesz) {
size_t towrite;
if (!bufsz || lseek(fd, 0, SEEK_SET) != 0) return -1;
while (filesz > 0) {
towrite = (filesz > bufsz ? bufsz : filesz);
if (!write_data(fd, buf, towrite)) return -1;
filesz -= towrite;
}
fsync(fd);
return 0;
}
int spc_fd_wipe(int fd) {
int count, i, pass, patternsz;
struct stat st;
unsigned char buf[SPC_WIPE_BUFSIZE], *pattern;
// write 33 pass takes a lot of time, so it just write one
static unsigned char single_pats[1] = { 0xff};
if (fstat(fd, &st) == -1) return -1;
if (!st.st_size) return 0;
for (pass = 0; pass < sizeof(single_pats); pass++) {
memset(buf, single_pats[pass], sizeof(buf));
if (pattern_pass(fd, buf, sizeof(buf), st.st_size) == -1) return -1;
}
return 0;
}
int spc_file_wipe(FILE *f) {
return spc_fd_wipe(fileno(f));
}
int gutman(const char * ftex)
{
FILE * pFile;
pFile = fopen (ftex,"r+w");
if (pFile!=NULL)
{ spc_file_wipe(pFile);
fclose (pFile);
if( remove( ftex ) != 0 )
perror( "Error deleting file\n" );
else
puts( "File successfully deleted\n" );
}
else printf("%s\n","error");
return 0;
}
// Delete file
int srfdel(const char * ftex)
{
FILE * pFile;
pFile = fopen (ftex,"r+w");
if (pFile!=NULL)
{ fclose (pFile);
if( remove( ftex ) != 0 )
perror( "Error deleting file\n" );
else
puts( "File successfully deleted\n" );
}
else printf("%s","error");
return 0;
}