chiark / gitweb /
cryptsetup: properly parse cipher= switch
authorLennart Poettering <lennart@poettering.net>
Sun, 14 Nov 2010 01:01:29 +0000 (02:01 +0100)
committerLennart Poettering <lennart@poettering.net>
Sun, 14 Nov 2010 18:59:10 +0000 (19:59 +0100)
src/cryptsetup.c

index 997a61a06320f5e0d3c293153acd0f1a737ac333..91a4436ae1531f75bb75230817ca35dfba63f20a 100644 (file)
@@ -30,7 +30,6 @@
 
 static const char *opt_type = NULL; /* LUKS1 or PLAIN */
 static char *opt_cipher = NULL;
 
 static const char *opt_type = NULL; /* LUKS1 or PLAIN */
 static char *opt_cipher = NULL;
-static char *opt_cipher_mode = NULL;
 static unsigned opt_key_size = 0;
 static char *opt_hash = NULL;
 static unsigned opt_tries = 0;
 static unsigned opt_key_size = 0;
 static char *opt_hash = NULL;
 static unsigned opt_tries = 0;
@@ -70,15 +69,6 @@ static int parse_one_option(const char *option) {
                 free(opt_hash);
                 opt_hash = t;
 
                 free(opt_hash);
                 opt_hash = t;
 
-        } else if (startswith(option, "mode=")) {
-                char *t;
-
-                if (!(t = strdup(option+5)))
-                        return -ENOMEM;
-
-                free(opt_cipher_mode);
-                opt_cipher_mode = t;
-
         } else if (startswith(option, "tries=")) {
 
                 if (safe_atou(option+6, &opt_tries) < 0) {
         } else if (startswith(option, "tries=")) {
 
                 if (safe_atou(option+6, &opt_tries) < 0) {
@@ -140,9 +130,8 @@ static void log_glue(int level, const char *msg, void *usrptr) {
 int main(int argc, char *argv[]) {
         int r = EXIT_FAILURE;
         struct crypt_device *cd = NULL;
 int main(int argc, char *argv[]) {
         int r = EXIT_FAILURE;
         struct crypt_device *cd = NULL;
-        char *password = NULL;
+        char *password = NULL, *truncated_cipher = NULL;
         const char *cipher = NULL, *cipher_mode = NULL, *hash = NULL;
         const char *cipher = NULL, *cipher_mode = NULL, *hash = NULL;
-        crypt_status_info status;
 
         if (argc < 3) {
                 log_error("This program requires at least two arguments.");
 
         if (argc < 3) {
                 log_error("This program requires at least two arguments.");
@@ -159,6 +148,7 @@ int main(int argc, char *argv[]) {
                 unsigned try;
                 const char *key_file = NULL;
                 usec_t until;
                 unsigned try;
                 const char *key_file = NULL;
                 usec_t until;
+                crypt_status_info status;
 
                 if (argc < 4) {
                         log_error("attach requires at least two arguments.");
 
                 if (argc < 4) {
                         log_error("attach requires at least two arguments.");
@@ -197,10 +187,25 @@ int main(int argc, char *argv[]) {
 
                 opt_tries = opt_tries > 0 ? opt_tries : 3;
                 opt_key_size = (opt_key_size > 0 ? opt_key_size : 256);
 
                 opt_tries = opt_tries > 0 ? opt_tries : 3;
                 opt_key_size = (opt_key_size > 0 ? opt_key_size : 256);
-                cipher = opt_cipher ? opt_cipher : "aes";
-                cipher_mode = opt_cipher_mode ? opt_cipher_mode : "cbc-essiv:sha256";
                 hash = opt_hash ? opt_hash : "ripemd160";
 
                 hash = opt_hash ? opt_hash : "ripemd160";
 
+                if (opt_cipher) {
+                        size_t l;
+
+                        l = strcspn(opt_cipher, "-");
+
+                        if (!(truncated_cipher = strndup(opt_cipher, l))) {
+                                log_error("Out of memory");
+                                goto finish;
+                        }
+
+                        cipher = truncated_cipher;
+                        cipher_mode = opt_cipher[l] ? opt_cipher+l+1 : "plain";
+                } else {
+                        cipher = "aes";
+                        cipher_mode = "cbc-essiv:sha256";
+                }
+
                 for (try = 0; try < opt_tries; try++) {
                         bool pass_volume_key = false;
 
                 for (try = 0; try < opt_tries; try++) {
                         bool pass_volume_key = false;
 
@@ -335,9 +340,10 @@ finish:
                 crypt_free(cd);
 
         free(opt_cipher);
                 crypt_free(cd);
 
         free(opt_cipher);
-        free(opt_cipher_mode);
         free(opt_hash);
 
         free(opt_hash);
 
+        free(truncated_cipher);
+
         free(password);
 
         return r;
         free(password);
 
         return r;