2 * AFsplitter - Anti forensic information splitter
4 * Copyright (C) 2004, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7 * AFsplitter diffuses information over a large stripe of data,
8 * therefor supporting secure data destruction.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <netinet/in.h>
33 static void XORblock(const char *src1, const char *src2, char *dst, size_t n)
37 for(j = 0; j < n; ++j)
38 dst[j] = src1[j] ^ src2[j];
41 static int hash_buf(const char *src, char *dst, uint32_t iv,
42 size_t len, const char *hash_name)
44 struct crypt_hash *hd = NULL;
45 char *iv_char = (char *)&iv;
49 if (crypt_hash_init(&hd, hash_name))
52 if ((r = crypt_hash_write(hd, iv_char, sizeof(uint32_t))))
55 if ((r = crypt_hash_write(hd, src, len)))
58 r = crypt_hash_final(hd, dst, len);
60 crypt_hash_destroy(hd);
64 /* diffuse: Information spreading over the whole dataset with
65 * the help of hash function.
68 static int diffuse(char *src, char *dst, size_t size, const char *hash_name)
70 int hash_size = crypt_hash_size(hash_name);
71 unsigned int digest_size;
72 unsigned int i, blocks, padding;
76 digest_size = hash_size;
78 blocks = size / digest_size;
79 padding = size % digest_size;
81 for (i = 0; i < blocks; i++)
82 if(hash_buf(src + digest_size * i,
83 dst + digest_size * i,
84 i, (size_t)digest_size, hash_name))
88 if(hash_buf(src + digest_size * i,
89 dst + digest_size * i,
90 i, (size_t)padding, hash_name))
97 * Information splitting. The amount of data is multiplied by
98 * blocknumbers. The same blocksize and blocknumbers values
99 * must be supplied to AF_merge to recover information.
102 int AF_split(char *src, char *dst, size_t blocksize,
103 unsigned int blocknumbers, const char *hash)
109 if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
111 /* process everything except the last block */
112 for(i=0; i<blocknumbers-1; i++) {
113 r = crypt_random_get(NULL, dst+(blocksize*i), blocksize, CRYPT_RND_NORMAL);
116 XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
117 if(diffuse(bufblock, bufblock, blocksize, hash))
120 /* the last block is computed */
121 XORblock(src,bufblock,dst+(i*blocksize),blocksize);
128 int AF_merge(char *src, char *dst, size_t blocksize,
129 unsigned int blocknumbers, const char *hash)
135 if((bufblock = calloc(blocksize, 1)) == NULL)
138 memset(bufblock,0,blocksize);
139 for(i=0; i<blocknumbers-1; i++) {
140 XORblock(src+(blocksize*i),bufblock,bufblock,blocksize);
141 if(diffuse(bufblock, bufblock, blocksize, hash))
144 XORblock(src + blocksize * i, bufblock, dst, blocksize);
151 /* Size of final split data including sector alignment */
152 size_t AF_split_sectors(size_t blocksize, unsigned int blocknumbers)
156 /* data material * stripes */
157 af_size = blocksize * blocknumbers;
159 /* round up to sector */
160 af_size = (af_size + (SECTOR_SIZE - 1)) / SECTOR_SIZE;