Fix FSF address in license text according to
[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-2011, 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 "crypto_backend.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         unsigned int digest_size = crypt_hash_size(hash_name);
71         unsigned int i, blocks, padding;
72
73         blocks = size / digest_size;
74         padding = size % digest_size;
75
76         for (i = 0; i < blocks; i++)
77                 if(hash_buf(src + digest_size * i,
78                             dst + digest_size * i,
79                             i, (size_t)digest_size, hash_name))
80                         return 1;
81
82         if(padding)
83                 if(hash_buf(src + digest_size * i,
84                             dst + digest_size * i,
85                             i, (size_t)padding, hash_name))
86                         return 1;
87
88         return 0;
89 }
90
91 /*
92  * Information splitting. The amount of data is multiplied by
93  * blocknumbers. The same blocksize and blocknumbers values
94  * must be supplied to AF_merge to recover information.
95  */
96
97 int AF_split(char *src, char *dst, size_t blocksize,
98              unsigned int blocknumbers, const char *hash)
99 {
100         unsigned int i;
101         char *bufblock;
102         int r = -EINVAL;
103
104         if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
105
106         /* process everything except the last block */
107         for(i=0; i<blocknumbers-1; i++) {
108                 r = crypt_random_get(NULL, dst+(blocksize*i), blocksize, CRYPT_RND_NORMAL);
109                 if(r < 0) goto out;
110
111                 XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
112                 if(diffuse(bufblock, bufblock, blocksize, hash))
113                         goto out;
114         }
115         /* the last block is computed */
116         XORblock(src,bufblock,dst+(i*blocksize),blocksize);
117         r = 0;
118 out:
119         free(bufblock);
120         return r;
121 }
122
123 int AF_merge(char *src, char *dst, size_t blocksize,
124              unsigned int blocknumbers, const char *hash)
125 {
126         unsigned int i;
127         char *bufblock;
128         int r = -EINVAL;
129
130         if((bufblock = calloc(blocksize, 1)) == NULL)
131                 return -ENOMEM;
132
133         memset(bufblock,0,blocksize);
134         for(i=0; i<blocknumbers-1; i++) {
135                 XORblock(src+(blocksize*i),bufblock,bufblock,blocksize);
136                 if(diffuse(bufblock, bufblock, blocksize, hash))
137                         goto out;
138         }
139         XORblock(src + blocksize * i, bufblock, dst, blocksize);
140         r = 0;
141 out:
142         free(bufblock);
143         return r;
144 }