Change License from GPLv2 only to GPLv2+ ("or any later").
[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  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
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.
19  *
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.
23  */
24
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <netinet/in.h>
29 #include <errno.h>
30 #include "internal.h"
31 #include "af.h"
32
33 static void XORblock(const char *src1, const char *src2, char *dst, size_t n)
34 {
35         size_t j;
36
37         for(j = 0; j < n; ++j)
38                 dst[j] = src1[j] ^ src2[j];
39 }
40
41 static int hash_buf(const char *src, char *dst, uint32_t iv,
42                     size_t len, const char *hash_name)
43 {
44         struct crypt_hash *hd = NULL;
45         char *iv_char = (char *)&iv;
46         int r;
47
48         iv = htonl(iv);
49         if (crypt_hash_init(&hd, hash_name))
50                 return -EINVAL;
51
52         if ((r = crypt_hash_write(hd, iv_char, sizeof(uint32_t))))
53                 goto out;
54
55         if ((r = crypt_hash_write(hd, src, len)))
56                 goto out;
57
58         r = crypt_hash_final(hd, dst, len);
59 out:
60         crypt_hash_destroy(hd);
61         return r;
62 }
63
64 /* diffuse: Information spreading over the whole dataset with
65  * the help of hash function.
66  */
67
68 static int diffuse(char *src, char *dst, size_t size, const char *hash_name)
69 {
70         int hash_size = crypt_hash_size(hash_name);
71         unsigned int digest_size;
72         unsigned int i, blocks, padding;
73
74         if (hash_size <= 0)
75                 return 1;
76         digest_size = hash_size;
77
78         blocks = size / digest_size;
79         padding = size % digest_size;
80
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))
85                         return 1;
86
87         if(padding)
88                 if(hash_buf(src + digest_size * i,
89                             dst + digest_size * i,
90                             i, (size_t)padding, hash_name))
91                         return 1;
92
93         return 0;
94 }
95
96 /*
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.
100  */
101
102 int AF_split(char *src, char *dst, size_t blocksize,
103              unsigned int blocknumbers, const char *hash)
104 {
105         unsigned int i;
106         char *bufblock;
107         int r = -EINVAL;
108
109         if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
110
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);
114                 if(r < 0) goto out;
115
116                 XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
117                 if(diffuse(bufblock, bufblock, blocksize, hash))
118                         goto out;
119         }
120         /* the last block is computed */
121         XORblock(src,bufblock,dst+(i*blocksize),blocksize);
122         r = 0;
123 out:
124         free(bufblock);
125         return r;
126 }
127
128 int AF_merge(char *src, char *dst, size_t blocksize,
129              unsigned int blocknumbers, const char *hash)
130 {
131         unsigned int i;
132         char *bufblock;
133         int r = -EINVAL;
134
135         if((bufblock = calloc(blocksize, 1)) == NULL)
136                 return -ENOMEM;
137
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))
142                         goto out;
143         }
144         XORblock(src + blocksize * i, bufblock, dst, blocksize);
145         r = 0;
146 out:
147         free(bufblock);
148         return r;
149 }
150
151 /* Size of final split data including sector alignment */
152 size_t AF_split_sectors(size_t blocksize, unsigned int blocknumbers)
153 {
154         size_t af_size;
155
156         /* data material * stripes */
157         af_size = blocksize * blocknumbers;
158
159         /* round up to sector */
160         af_size = (af_size + (SECTOR_SIZE - 1)) / SECTOR_SIZE;
161
162         return af_size;
163 }