Fix FSF address in license text according to
[platform/upstream/cryptsetup.git] / lib / luks1 / keyencryption.c
index c37724a..680d2e9 100644 (file)
@@ -14,7 +14,7 @@
  *
  * 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 <string.h>
@@ -51,12 +51,21 @@ static int devfd=-1;
 
 static int setup_mapping(const char *cipher, const char *name,
                         const char *device,
-                        const char *key, size_t keyLength,
+                        struct volume_key *vk,
                         unsigned int sector, size_t srcLength,
                         int mode, struct crypt_device *ctx)
 {
        int device_sector_size = sector_size_for_device(device);
-       uint64_t size;
+       struct crypt_dm_active_device dmd = {
+               .device = device,
+               .cipher = cipher,
+               .uuid   = NULL,
+               .vk     = vk,
+               .offset = sector,
+               .iv_offset = 0,
+               .size   = 0,
+               .flags  = (mode == O_RDONLY) ? CRYPT_ACTIVATE_READONLY : 0
+       };
 
        /*
         * we need to round this to nearest multiple of the underlying
@@ -66,11 +75,11 @@ static int setup_mapping(const char *cipher, const char *name,
                log_err(ctx, _("Unable to obtain sector size for %s"), device);
                return -EINVAL;
        }
-       size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
-       cleaner_size = size;
 
-       return dm_create_device(name, device, cipher, "TEMP", NULL, size, 0, sector,
-                               keyLength, key, (mode == O_RDONLY), 0);
+       dmd.size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
+       cleaner_size = dmd.size;
+
+       return dm_create_device(name, "TEMP", &dmd, 0);
 }
 
 static void sigint_handler(int sig __attribute__((unused)))
@@ -88,29 +97,10 @@ static void sigint_handler(int sig __attribute__((unused)))
 static const char *_error_hint(char *cipherMode, size_t keyLength)
 {
        const char *hint= "";
-#ifdef __linux__
-       char c, tmp[4] = {0};
-       struct utsname uts;
-       int i = 0, kernel_minor;
-
-       /* Nothing to suggest here */
-       if (uname(&uts) || strncmp(uts.release, "2.6.", 4))
-               return hint;
-
-       /* Get kernel minor without suffixes */
-       while (i < 3 && (c = uts.release[i + 4]))
-               tmp[i++] = isdigit(c) ? c : '\0';
-       kernel_minor = atoi(tmp);
 
        if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
                hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
-       else if (!strncmp(cipherMode, "xts", 3) && kernel_minor < 24)
-               hint = _("Block mode XTS is available since kernel 2.6.24.\n");
-       if (!strncmp(cipherMode, "lrw", 3) && (keyLength != 256 && keyLength != 512))
-               hint = _("Key size in LRW mode must be 256 or 512 bits.\n");
-       else if (!strncmp(cipherMode, "lrw", 3) && kernel_minor < 20)
-               hint = _("Block mode LRW is available since kernel 2.6.20.\n");
-#endif
+
        return hint;
 }
 
@@ -118,7 +108,7 @@ static const char *_error_hint(char *cipherMode, size_t keyLength)
    handler and global vars for cleaning */
 static int LUKS_endec_template(char *src, size_t srcLength,
                               struct luks_phdr *hdr,
-                              char *key, size_t keyLength,
+                              struct volume_key *vk,
                               const char *device,
                               unsigned int sector,
                               ssize_t (*func)(int, void *, size_t),
@@ -146,12 +136,12 @@ static int LUKS_endec_template(char *src, size_t srcLength,
        cleaner_name = name;
 
        r = setup_mapping(dmCipherSpec, name, device,
-                         key, keyLength, sector, srcLength, mode, ctx);
+                         vk, sector, srcLength, mode, ctx);
        if(r < 0) {
                log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
                        "Check that kernel supports %s cipher (check syslog for more info).\n%s"),
                        device, dmCipherSpec,
-                       _error_hint(hdr->cipherMode, keyLength * 8));
+                       _error_hint(hdr->cipherMode, vk->keylength * 8));
                r = -EIO;
                goto out1;
        }
@@ -188,22 +178,22 @@ static int LUKS_endec_template(char *src, size_t srcLength,
 
 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
                            struct luks_phdr *hdr,
-                           char *key, size_t keyLength,
+                           struct volume_key *vk,
                            const char *device,
                            unsigned int sector,
                            struct crypt_device *ctx)
 {
-       return LUKS_endec_template(src,srcLength,hdr,key,keyLength, device,
+       return LUKS_endec_template(src,srcLength,hdr,vk, device,
                                   sector, write_blockwise, O_RDWR, ctx);
 }
 
 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
                              struct luks_phdr *hdr,
-                             char *key, size_t keyLength,
+                             struct volume_key *vk,
                              const char *device,
                              unsigned int sector,
                              struct crypt_device *ctx)
 {
-       return LUKS_endec_template(dst,dstLength,hdr,key,keyLength, device,
+       return LUKS_endec_template(dst,dstLength,hdr,vk, device,
                                   sector, read_blockwise, O_RDONLY, ctx);
 }