Fix some warnings if compiled with clang.
[platform/upstream/cryptsetup.git] / lib / luks1 / 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 <gcrypt.h>
29 #include "internal.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, int hash_id)
40 {
41         gcry_md_hd_t hd;
42         unsigned char *digest;
43
44         iv = htonl(iv);
45         if (gcry_md_open(&hd, hash_id, 0))
46                 return 1;
47         gcry_md_write(hd, (unsigned char *)&iv, sizeof(iv));
48         gcry_md_write(hd, src, len);
49         digest = gcry_md_read(hd, hash_id);
50         memcpy(dst, digest, len);
51         gcry_md_close(hd);
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, int hash_id)
60 {
61         unsigned int digest_size = gcry_md_get_algo_dlen(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         int hash_id;
94
95         if (!(hash_id = gcry_md_map_name(hash)))
96                 return -EINVAL;
97
98         if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
99
100         /* process everything except the last block */
101         for(i=0; i<blocknumbers-1; i++) {
102                 r = crypt_random_get(NULL, dst+(blocksize*i), blocksize, CRYPT_RND_NORMAL);
103                 if(r < 0) goto out;
104
105                 XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
106                 if(diffuse(bufblock, bufblock, blocksize, hash_id))
107                         goto out;
108         }
109         /* the last block is computed */
110         XORblock(src,bufblock,dst+(i*blocksize),blocksize);
111         r = 0;
112 out:
113         free(bufblock);
114         return r;
115 }
116
117 int AF_merge(char *src, char *dst, size_t blocksize, unsigned int blocknumbers, const char *hash)
118 {
119         unsigned int i;
120         char *bufblock;
121         int r = -EINVAL;
122         int hash_id;
123
124         if (!(hash_id = gcry_md_map_name(hash)))
125                 return -EINVAL;
126
127         if((bufblock = calloc(blocksize, 1)) == NULL)
128                 return -ENOMEM;
129
130         memset(bufblock,0,blocksize);
131         for(i=0; i<blocknumbers-1; i++) {
132                 XORblock(src+(blocksize*i),bufblock,bufblock,blocksize);
133                 if(diffuse(bufblock, bufblock, blocksize, hash_id))
134                         goto out;
135         }
136         XORblock(src + blocksize * i, bufblock, dst, blocksize);
137         r = 0;
138 out:
139         free(bufblock);
140         return r;
141 }