--- util-linux-2.12-fixed-key-size/mount/lomount.c 2003-07-16 19:56:53.000000000 -0400 +++ util-linux-2.12/mount/lomount.c 2003-08-07 13:34:22.000000000 -0400 @@ -24,12 +24,15 @@ #include #include #include +#include #include #include +#include #include #include #include #include +#include #include "loop.h" #include "lomount.h" @@ -249,7 +252,7 @@ set_loop(const char *device, const char *file, int offset, const char *encryption, int pfd, int *loopro) { struct loop_info64 loopinfo64; - int fd, ffd, mode; + int fd, ffd, mode, keysize = 0; char *pass; mode = (*loopro ? O_RDONLY : O_RDWR); @@ -275,9 +278,38 @@ if (digits_only(encryption)) { loopinfo64.lo_encrypt_type = atoi(encryption); } else { + regex_t keysize_re; + regmatch_t keysize_rm; + int rerror; + char rerror_buf[LINE_MAX+1], + *encryption_dup = xstrdup(encryption); + loopinfo64.lo_encrypt_type = LO_CRYPT_CRYPTOAPI; + + if ((rerror = regcomp(&keysize_re, + "-[[:digit:]]+$", + REG_EXTENDED)) != 0) { + fprintf(stderr, _("regcomp: ")); + regerror(rerror, &keysize_re, rerror_buf, + LINE_MAX); + fprintf(stderr, "%s\n", rerror_buf); + } else { + if (regexec(&keysize_re, encryption_dup, + 1, &keysize_rm, 0) == 0) { + keysize = atoi(encryption_dup + + keysize_rm.rm_so + 1); + /* convert #bits to #bytes */ + keysize /= 8; + /* cut the keysize out now */ + encryption_dup[keysize_rm.rm_so] = '\0'; + } + + regfree(&keysize_re); + } + snprintf(loopinfo64.lo_crypt_name, LO_NAME_SIZE, - "%s", encryption); + "%s", encryption_dup); + free(encryption_dup); } } @@ -307,9 +339,14 @@ strlen(loopinfo64.lo_encrypt_key); break; default: + if (keysize == 0) { + fprintf(stderr, _("You must specify a key size (in bits) for use with CryptoAPI encryption.\n")); + return -1; + } + /* FIXME we should be checking keysize against the min/max in /proc/crypto */ pass = xgetpass(pfd, _("Password: ")); xstrncpy(loopinfo64.lo_encrypt_key, pass, LO_KEY_SIZE); - loopinfo64.lo_encrypt_key_size = LO_KEY_SIZE; + loopinfo64.lo_encrypt_key_size = keysize; } if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {