Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / cryptsetup / luks / af.c
1 /*
2  * AFsplitter - Anti forensic information splitter
3  * Copyright 2004, Clemens Fruhwirth <clemens@endorphin.org>
4  * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
5  *
6  * AFsplitter diffuses information over a large stripe of data, 
7  * therefor supporting secure data destruction.
8  *
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.
12  *
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.
17  *
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.
21  */
22  
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <netinet/in.h>
27 #include <errno.h>
28 #include <openssl/evp.h>
29 #include "random.h"
30
31 static void XORblock(char const *src1, char const *src2, char *dst, size_t n)
32 {
33         size_t j;
34
35         for(j = 0; j < n; ++j)
36                 dst[j] = src1[j] ^ src2[j];
37 }
38
39 static int hash_buf(char *src, char *dst, uint32_t iv, int len, const EVP_MD *hash_id)
40 {
41         EVP_MD_CTX mdctx;
42         unsigned char digest[128];
43
44         iv = htonl(iv);
45
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);
51
52         return 0;
53 }
54
55 /* diffuse: Information spreading over the whole dataset with
56  * the help of hash function.
57  */
58
59 static int diffuse(char *src, char *dst, size_t size, const EVP_MD *hash_id)
60 {
61         unsigned int digest_size = EVP_MD_size(hash_id);
62         unsigned int i, blocks, padding;
63
64         blocks = size / digest_size;
65         padding = size % digest_size;
66
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))
71                         return 1;
72
73         if(padding)
74                 if(hash_buf(src + digest_size * i,
75                             dst + digest_size * i,
76                             i, padding, hash_id))
77                         return 1;
78
79         return 0;
80 }
81
82 /*
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.
86  */
87
88 int AF_split(char *src, char *dst, size_t blocksize, unsigned int blocknumbers, const char *hash)
89 {
90         unsigned int i;
91         char *bufblock;
92         int r = -EINVAL;
93         const EVP_MD *hash_id;
94
95         OpenSSL_add_all_digests();
96         if (!(hash_id = EVP_get_digestbyname(hash)))
97                 return -EINVAL;
98
99         if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
100
101         /* process everything except the last block */
102         for(i=0; i<blocknumbers-1; i++) {
103                 r = getRandom(dst+(blocksize*i),blocksize);
104                 if(r < 0) goto out;
105
106                 XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
107                 if(diffuse(bufblock, bufblock, blocksize, hash_id))
108                         goto out;
109         }
110         /* the last block is computed */
111         XORblock(src,bufblock,dst+(i*blocksize),blocksize);
112         r = 0;
113 out:
114         free(bufblock);
115         return r;
116 }
117
118 int AF_merge(char *src, char *dst, size_t blocksize, unsigned int blocknumbers, const char *hash)
119 {
120         unsigned int i;
121         char *bufblock;
122         int r = -EINVAL;
123         const EVP_MD *hash_id;
124
125         OpenSSL_add_all_digests();
126         if (!(hash_id = EVP_get_digestbyname(hash)))
127                 return -EINVAL;
128
129         if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
130
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))
135                         goto out;
136         }
137         XORblock(src + blocksize * i, bufblock, dst, blocksize);
138         r = 0;
139 out:
140         free(bufblock);
141         return 0;
142 }