2 * AFsplitter - Anti forensic information splitter
4 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
7 * AFsplitter diffuses information over a large stripe of data,
8 * therefore 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.
32 static void XORblock(const char *src1, const char *src2, char *dst, size_t n)
36 for (j = 0; j < n; j++)
37 dst[j] = src1[j] ^ src2[j];
40 static int hash_buf(const char *src, char *dst, uint32_t iv,
41 size_t len, const char *hash_name)
43 struct crypt_hash *hd = NULL;
44 char *iv_char = (char *)&iv;
48 if (crypt_hash_init(&hd, hash_name))
51 if ((r = crypt_hash_write(hd, iv_char, sizeof(uint32_t))))
54 if ((r = crypt_hash_write(hd, src, len)))
57 r = crypt_hash_final(hd, dst, len);
59 crypt_hash_destroy(hd);
64 * diffuse: Information spreading over the whole dataset with
65 * the help of hash function.
67 static int diffuse(char *src, char *dst, size_t size, const char *hash_name)
69 int r, hash_size = crypt_hash_size(hash_name);
70 unsigned int digest_size;
71 unsigned int i, blocks, padding;
75 digest_size = hash_size;
77 blocks = size / digest_size;
78 padding = size % digest_size;
80 for (i = 0; i < blocks; i++) {
81 r = hash_buf(src + digest_size * i,
82 dst + digest_size * i,
83 i, (size_t)digest_size, hash_name);
89 r = hash_buf(src + digest_size * i,
90 dst + digest_size * i,
91 i, (size_t)padding, hash_name);
100 * Information splitting. The amount of data is multiplied by
101 * blocknumbers. The same blocksize and blocknumbers values
102 * must be supplied to AF_merge to recover information.
104 int AF_split(struct crypt_device *ctx, const char *src, char *dst,
105 size_t blocksize, unsigned int blocknumbers, const char *hash)
111 bufblock = crypt_safe_alloc(blocksize);
115 /* process everything except the last block */
116 for (i = 0; i < blocknumbers - 1; i++) {
117 r = crypt_random_get(ctx, dst + blocksize * i, blocksize, CRYPT_RND_NORMAL);
121 XORblock(dst + blocksize * i, bufblock, bufblock, blocksize);
122 r = diffuse(bufblock, bufblock, blocksize, hash);
126 /* the last block is computed */
127 XORblock(src, bufblock, dst + blocksize * i, blocksize);
130 crypt_safe_free(bufblock);
134 int AF_merge(struct crypt_device *ctx __attribute__((unused)), const char *src, char *dst,
135 size_t blocksize, unsigned int blocknumbers, const char *hash)
141 bufblock = crypt_safe_alloc(blocksize);
145 for(i = 0; i < blocknumbers - 1; i++) {
146 XORblock(src + blocksize * i, bufblock, bufblock, blocksize);
147 r = diffuse(bufblock, bufblock, blocksize, hash);
151 XORblock(src + blocksize * i, bufblock, dst, blocksize);
154 crypt_safe_free(bufblock);
158 /* Size of final split data including sector alignment */
159 size_t AF_split_sectors(size_t blocksize, unsigned int blocknumbers)
163 /* data material * stripes */
164 af_size = blocksize * blocknumbers;
166 /* round up to sector */
167 af_size = (af_size + (SECTOR_SIZE - 1)) / SECTOR_SIZE;