Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / lib / luks1 / af.c
index 461a5a1..a781376 100644 (file)
@@ -2,14 +2,15 @@
  * AFsplitter - Anti forensic information splitter
  *
  * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2010 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
  *
  * AFsplitter diffuses information over a large stripe of data,
- * therefor supporting secure data destruction.
+ * therefore supporting secure data destruction.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
-#include <netinet/in.h>
 #include <errno.h>
-#include "crypto_backend.h"
 #include "internal.h"
+#include "af.h"
 
 static void XORblock(const char *src1, const char *src2, char *dst, size_t n)
 {
        size_t j;
 
-       for(j = 0; j < n; ++j)
+       for (j = 0; j < n; j++)
                dst[j] = src1[j] ^ src2[j];
 }
 
@@ -42,40 +42,56 @@ static int hash_buf(const char *src, char *dst, uint32_t iv,
 {
        struct crypt_hash *hd = NULL;
        char *iv_char = (char *)&iv;
+       int r;
 
-       iv = htonl(iv);
+       iv = be32_to_cpu(iv);
        if (crypt_hash_init(&hd, hash_name))
-               return 1;
-       crypt_hash_write(hd, iv_char, sizeof(uint32_t));
-       crypt_hash_write(hd, src, len);
-       crypt_hash_final(hd, dst, len);
+               return -EINVAL;
+
+       if ((r = crypt_hash_write(hd, iv_char, sizeof(uint32_t))))
+               goto out;
+
+       if ((r = crypt_hash_write(hd, src, len)))
+               goto out;
+
+       r = crypt_hash_final(hd, dst, len);
+out:
        crypt_hash_destroy(hd);
-       return 0;
+       return r;
 }
 
-/* diffuse: Information spreading over the whole dataset with
+/*
+ * diffuse: Information spreading over the whole dataset with
  * the help of hash function.
  */
-
 static int diffuse(char *src, char *dst, size_t size, const char *hash_name)
 {
-       unsigned int digest_size = crypt_hash_size(hash_name);
+       int r, hash_size = crypt_hash_size(hash_name);
+       unsigned int digest_size;
        unsigned int i, blocks, padding;
 
+       if (hash_size <= 0)
+               return -EINVAL;
+       digest_size = hash_size;
+
        blocks = size / digest_size;
        padding = size % digest_size;
 
-       for (i = 0; i < blocks; i++)
-               if(hash_buf(src + digest_size * i,
+       for (i = 0; i < blocks; i++) {
+               r = hash_buf(src + digest_size * i,
                            dst + digest_size * i,
-                           i, (size_t)digest_size, hash_name))
-                       return 1;
+                           i, (size_t)digest_size, hash_name);
+               if (r < 0)
+                       return r;
+       }
 
-       if(padding)
-               if(hash_buf(src + digest_size * i,
+       if (padding) {
+               r = hash_buf(src + digest_size * i,
                            dst + digest_size * i,
-                           i, (size_t)padding, hash_name))
-                       return 1;
+                           i, (size_t)padding, hash_name);
+               if (r < 0)
+                       return r;
+       }
 
        return 0;
 }
@@ -85,52 +101,70 @@ static int diffuse(char *src, char *dst, size_t size, const char *hash_name)
  * blocknumbers. The same blocksize and blocknumbers values
  * must be supplied to AF_merge to recover information.
  */
-
-int AF_split(char *src, char *dst, size_t blocksize,
-            unsigned int blocknumbers, const char *hash)
+int AF_split(struct crypt_device *ctx, const char *src, char *dst,
+            size_t blocksize, unsigned int blocknumbers, const char *hash)
 {
        unsigned int i;
        char *bufblock;
-       int r = -EINVAL;
+       int r;
 
-       if((bufblock = calloc(blocksize, 1)) == NULL) return -ENOMEM;
+       bufblock = crypt_safe_alloc(blocksize);
+       if (!bufblock)
+               return -ENOMEM;
 
        /* process everything except the last block */
-       for(i=0; i<blocknumbers-1; i++) {
-               r = crypt_random_get(NULL, dst+(blocksize*i), blocksize, CRYPT_RND_NORMAL);
-               if(r < 0) goto out;
+       for (i = 0; i < blocknumbers - 1; i++) {
+               r = crypt_random_get(ctx, dst + blocksize * i, blocksize, CRYPT_RND_NORMAL);
+               if (r < 0)
+                       goto out;
 
-               XORblock(dst+(blocksize*i),bufblock,bufblock,blocksize);
-               if(diffuse(bufblock, bufblock, blocksize, hash))
+               XORblock(dst + blocksize * i, bufblock, bufblock, blocksize);
+               r = diffuse(bufblock, bufblock, blocksize, hash);
+               if (r < 0)
                        goto out;
        }
        /* the last block is computed */
-       XORblock(src,bufblock,dst+(i*blocksize),blocksize);
+       XORblock(src, bufblock, dst + blocksize * i, blocksize);
        r = 0;
 out:
-       free(bufblock);
+       crypt_safe_free(bufblock);
        return r;
 }
 
-int AF_merge(char *src, char *dst, size_t blocksize,
-            unsigned int blocknumbers, const char *hash)
+int AF_merge(struct crypt_device *ctx __attribute__((unused)), const char *src, char *dst,
+            size_t blocksize, unsigned int blocknumbers, const char *hash)
 {
        unsigned int i;
        char *bufblock;
-       int r = -EINVAL;
+       int r;
 
-       if((bufblock = calloc(blocksize, 1)) == NULL)
+       bufblock = crypt_safe_alloc(blocksize);
+       if (!bufblock)
                return -ENOMEM;
 
-       memset(bufblock,0,blocksize);
-       for(i=0; i<blocknumbers-1; i++) {
-               XORblock(src+(blocksize*i),bufblock,bufblock,blocksize);
-               if(diffuse(bufblock, bufblock, blocksize, hash))
+       for(i = 0; i < blocknumbers - 1; i++) {
+               XORblock(src + blocksize * i, bufblock, bufblock, blocksize);
+               r = diffuse(bufblock, bufblock, blocksize, hash);
+               if (r < 0)
                        goto out;
        }
        XORblock(src + blocksize * i, bufblock, dst, blocksize);
        r = 0;
 out:
-       free(bufblock);
+       crypt_safe_free(bufblock);
        return r;
 }
+
+/* Size of final split data including sector alignment */
+size_t AF_split_sectors(size_t blocksize, unsigned int blocknumbers)
+{
+       size_t af_size;
+
+       /* data material * stripes */
+       af_size = blocksize * blocknumbers;
+
+       /* round up to sector */
+       af_size = (af_size + (SECTOR_SIZE - 1)) / SECTOR_SIZE;
+
+       return af_size;
+}