2 * AFsplitter - Anti forensic information splitter
3 * Copyright 2004, Clemens Fruhwirth <clemens@endorphin.org>
4 * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
6 * AFsplitter diffuses information over a large stripe of data,
7 * therefor supporting secure data destruction.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <netinet/in.h>
28 #include <openssl/evp.h>
31 static void XORblock(char const *src1, char const *src2, char *dst, size_t n)
35 for(j = 0; j < n; ++j)
36 dst[j] = src1[j] ^ src2[j];
39 static int hash_buf(char *src, char *dst, uint32_t iv, int len, const EVP_MD *hash_id)
42 unsigned char digest[128];
46 EVP_DigestInit(&mdctx, hash_id);
47 EVP_DigestUpdate(&mdctx, (unsigned char *)&iv, sizeof(iv));
48 EVP_DigestUpdate(&mdctx, src, len);
49 EVP_DigestFinal(&mdctx, digest, NULL);
50 memcpy(dst, digest, len);
55 /* diffuse: Information spreading over the whole dataset with
56 * the help of hash function.
59 static int diffuse(char *src, char *dst, size_t size, const EVP_MD *hash_id)
61 unsigned int digest_size = EVP_MD_size(hash_id);
62 unsigned int i, blocks, padding;
64 blocks = size / digest_size;
65 padding = size % digest_size;
67 for (i = 0; i < blocks; i++)
68 if(hash_buf(src + digest_size * i,
69 dst + digest_size * i,
70 i, digest_size, hash_id))
74 if(hash_buf(src + digest_size * i,
75 dst + digest_size * i,
83 * Information splitting. The amount of data is multiplied by
84 * blocknumbers. The same blocksize and blocknumbers values
85 * must be supplied to AF_merge to recover information.
88 int AF_split(char *src, char *dst, size_t blocksize, unsigned int blocknumbers, const char *hash)
93 const EVP_MD *hash_id;
95 OpenSSL_add_all_digests();
96 if (!(hash_id = EVP_get_digestbyname(hash)))
99 if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
101 /* process everything except the last block */
102 for(i=0; i<blocknumbers-1; i++) {
103 r = getRandom(dst+(blocksize*i),blocksize);
106 XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
107 if(diffuse(bufblock, bufblock, blocksize, hash_id))
110 /* the last block is computed */
111 XORblock(src,bufblock,dst+(i*blocksize),blocksize);
118 int AF_merge(char *src, char *dst, size_t blocksize, unsigned int blocknumbers, const char *hash)
123 const EVP_MD *hash_id;
125 OpenSSL_add_all_digests();
126 if (!(hash_id = EVP_get_digestbyname(hash)))
129 if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
131 memset(bufblock,0,blocksize);
132 for(i=0; i<blocknumbers-1; i++) {
133 XORblock(src+(blocksize*i),bufblock,bufblock,blocksize);
134 if(diffuse(bufblock, bufblock, blocksize, hash_id))
137 XORblock(src + blocksize * i, bufblock, dst, blocksize);