9c50c1616248149d1912333f2e8ebf60986dd07d
[platform/upstream/cryptsetup.git] / lib / luks1 / af.c
1 /*
2  * AFsplitter - Anti forensic information splitter
3  *
4  * Copyright (C) 2004, Clemens Fruhwirth <clemens@endorphin.org>
5  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
6  *
7  * AFsplitter diffuses information over a large stripe of data,
8  * therefor supporting secure data destruction.
9  *
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  * version 2 as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <netinet/in.h>
28 #include <errno.h>
29 #include "internal.h"
30 #include "af.h"
31
32 static void XORblock(const char *src1, const char *src2, char *dst, size_t n)
33 {
34         size_t j;
35
36         for(j = 0; j < n; ++j)
37                 dst[j] = src1[j] ^ src2[j];
38 }
39
40 static int hash_buf(const char *src, char *dst, uint32_t iv,
41                     size_t len, const char *hash_name)
42 {
43         struct crypt_hash *hd = NULL;
44         char *iv_char = (char *)&iv;
45         int r;
46
47         iv = htonl(iv);
48         if (crypt_hash_init(&hd, hash_name))
49                 return -EINVAL;
50
51         if ((r = crypt_hash_write(hd, iv_char, sizeof(uint32_t))))
52                 goto out;
53
54         if ((r = crypt_hash_write(hd, src, len)))
55                 goto out;
56
57         r = crypt_hash_final(hd, dst, len);
58 out:
59         crypt_hash_destroy(hd);
60         return r;
61 }
62
63 /* diffuse: Information spreading over the whole dataset with
64  * the help of hash function.
65  */
66
67 static int diffuse(char *src, char *dst, size_t size, const char *hash_name)
68 {
69         int hash_size = crypt_hash_size(hash_name);
70         unsigned int digest_size;
71         unsigned int i, blocks, padding;
72
73         if (hash_size <= 0)
74                 return 1;
75         digest_size = hash_size;
76
77         blocks = size / digest_size;
78         padding = size % digest_size;
79
80         for (i = 0; i < blocks; i++)
81                 if(hash_buf(src + digest_size * i,
82                             dst + digest_size * i,
83                             i, (size_t)digest_size, hash_name))
84                         return 1;
85
86         if(padding)
87                 if(hash_buf(src + digest_size * i,
88                             dst + digest_size * i,
89                             i, (size_t)padding, hash_name))
90                         return 1;
91
92         return 0;
93 }
94
95 /*
96  * Information splitting. The amount of data is multiplied by
97  * blocknumbers. The same blocksize and blocknumbers values
98  * must be supplied to AF_merge to recover information.
99  */
100
101 int AF_split(char *src, char *dst, size_t blocksize,
102              unsigned int blocknumbers, const char *hash)
103 {
104         unsigned int i;
105         char *bufblock;
106         int r = -EINVAL;
107
108         if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
109
110         /* process everything except the last block */
111         for(i=0; i<blocknumbers-1; i++) {
112                 r = crypt_random_get(NULL, dst+(blocksize*i), blocksize, CRYPT_RND_NORMAL);
113                 if(r < 0) goto out;
114
115                 XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
116                 if(diffuse(bufblock, bufblock, blocksize, hash))
117                         goto out;
118         }
119         /* the last block is computed */
120         XORblock(src,bufblock,dst+(i*blocksize),blocksize);
121         r = 0;
122 out:
123         free(bufblock);
124         return r;
125 }
126
127 int AF_merge(char *src, char *dst, size_t blocksize,
128              unsigned int blocknumbers, const char *hash)
129 {
130         unsigned int i;
131         char *bufblock;
132         int r = -EINVAL;
133
134         if((bufblock = calloc(blocksize, 1)) == NULL)
135                 return -ENOMEM;
136
137         memset(bufblock,0,blocksize);
138         for(i=0; i<blocknumbers-1; i++) {
139                 XORblock(src+(blocksize*i),bufblock,bufblock,blocksize);
140                 if(diffuse(bufblock, bufblock, blocksize, hash))
141                         goto out;
142         }
143         XORblock(src + blocksize * i, bufblock, dst, blocksize);
144         r = 0;
145 out:
146         free(bufblock);
147         return r;
148 }
149
150 /* Size of final split data including sector alignment */
151 size_t AF_split_sectors(size_t blocksize, unsigned int blocknumbers)
152 {
153         size_t af_size;
154
155         /* data material * stripes */
156         af_size = blocksize * blocknumbers;
157
158         /* round up to sector */
159         af_size = (af_size + (SECTOR_SIZE - 1)) / SECTOR_SIZE;
160
161         return af_size;
162 }