Skip to content

Commit ea34c8a

Browse files
committed
Move crypt_capi_to_cipher in lib utils.
The function is never used in tools or tests so let's move it to lib exclusive utils file.
1 parent 4288880 commit ea34c8a

2 files changed

Lines changed: 88 additions & 88 deletions

File tree

lib/utils.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "internal.h"
1919
#include "utils_storage_wrappers.h"
2020

21+
#define MAX_CAPI_LEN_STR "143" /* for sscanf of crypto API string + 16 + \0 */
22+
2123
size_t crypt_getpagesize(void)
2224
{
2325
long r = sysconf(_SC_PAGESIZE);
@@ -449,3 +451,89 @@ int crypt_check_cipher(struct crypt_device *cd,
449451
crypt_free_volume_key(empty_vk);
450452
return r;
451453
}
454+
455+
int crypt_capi_to_cipher(char **org_c, char **org_i, const char *c_dm, const char *i_dm)
456+
{
457+
char cipher[MAX_CAPI_ONE_LEN], mode[MAX_CAPI_ONE_LEN], iv[MAX_CAPI_ONE_LEN],
458+
auth[MAX_CAPI_ONE_LEN], tmp[MAX_CAPI_LEN], dmcrypt_tmp[MAX_CAPI_LEN*2],
459+
capi[MAX_CAPI_LEN+1];
460+
size_t len;
461+
int i;
462+
463+
if (!c_dm)
464+
return -EINVAL;
465+
466+
/* legacy mode */
467+
if (strncmp(c_dm, "capi:", 5)) {
468+
if (!(*org_c = strdup(c_dm)))
469+
return -ENOMEM;
470+
if (i_dm) {
471+
if (!(*org_i = strdup(i_dm))) {
472+
free(*org_c);
473+
*org_c = NULL;
474+
return -ENOMEM;
475+
}
476+
} else
477+
*org_i = NULL;
478+
return 0;
479+
}
480+
481+
/* modes with capi: prefix */
482+
i = sscanf(c_dm, "capi:%" MAX_CAPI_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", tmp, iv);
483+
if (i != 2)
484+
return -EINVAL;
485+
486+
/* non-cryptsetup compatible mode (generic driver with dash?) */
487+
if (strrchr(iv, ')')) {
488+
if (i_dm)
489+
return -EINVAL;
490+
if (!(*org_c = strdup(c_dm)))
491+
return -ENOMEM;
492+
return 0;
493+
}
494+
495+
len = strlen(tmp);
496+
if (len < 2)
497+
return -EINVAL;
498+
499+
if (tmp[len-1] == ')')
500+
tmp[len-1] = '\0';
501+
502+
if (sscanf(tmp, "rfc4309(%" MAX_CAPI_LEN_STR "s", capi) == 1) {
503+
if (!(*org_i = strdup("aead")))
504+
return -ENOMEM;
505+
} else if (sscanf(tmp, "rfc7539(%" MAX_CAPI_LEN_STR "[^,],%" MAX_CAPI_ONE_LEN_STR "s", capi, auth) == 2) {
506+
if (!(*org_i = strdup(auth)))
507+
return -ENOMEM;
508+
} else if (sscanf(tmp, "authenc(%" MAX_CAPI_ONE_LEN_STR "[^,],%" MAX_CAPI_LEN_STR "s", auth, capi) == 2) {
509+
if (!(*org_i = strdup(auth)))
510+
return -ENOMEM;
511+
} else {
512+
if (i_dm) {
513+
if (!(*org_i = strdup(i_dm)))
514+
return -ENOMEM;
515+
} else
516+
*org_i = NULL;
517+
memset(capi, 0, sizeof(capi));
518+
strncpy(capi, tmp, sizeof(capi)-1);
519+
}
520+
521+
i = sscanf(capi, "%" MAX_CAPI_ONE_LEN_STR "[^(](%" MAX_CAPI_ONE_LEN_STR "[^)])", mode, cipher);
522+
if (i == 2)
523+
i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv);
524+
else
525+
i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv);
526+
if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) {
527+
free(*org_i);
528+
*org_i = NULL;
529+
return -EINVAL;
530+
}
531+
532+
if (!(*org_c = strdup(dmcrypt_tmp))) {
533+
free(*org_i);
534+
*org_i = NULL;
535+
return -ENOMEM;
536+
}
537+
538+
return 0;
539+
}

lib/utils_crypt.c

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include "libcryptsetup.h"
1919
#include "utils_crypt.h"
2020

21-
#define MAX_CAPI_LEN_STR "143" /* for sscanf of crypto API string + 16 + \0 */
22-
2321
int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
2422
char *cipher_mode)
2523
{
@@ -285,89 +283,3 @@ bool crypt_is_cipher_null(const char *cipher_spec)
285283
return false;
286284
return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null"));
287285
}
288-
289-
int crypt_capi_to_cipher(char **org_c, char **org_i, const char *c_dm, const char *i_dm)
290-
{
291-
char cipher[MAX_CAPI_ONE_LEN], mode[MAX_CAPI_ONE_LEN], iv[MAX_CAPI_ONE_LEN],
292-
auth[MAX_CAPI_ONE_LEN], tmp[MAX_CAPI_LEN], dmcrypt_tmp[MAX_CAPI_LEN*2],
293-
capi[MAX_CAPI_LEN+1];
294-
size_t len;
295-
int i;
296-
297-
if (!c_dm)
298-
return -EINVAL;
299-
300-
/* legacy mode */
301-
if (strncmp(c_dm, "capi:", 5)) {
302-
if (!(*org_c = strdup(c_dm)))
303-
return -ENOMEM;
304-
if (i_dm) {
305-
if (!(*org_i = strdup(i_dm))) {
306-
free(*org_c);
307-
*org_c = NULL;
308-
return -ENOMEM;
309-
}
310-
} else
311-
*org_i = NULL;
312-
return 0;
313-
}
314-
315-
/* modes with capi: prefix */
316-
i = sscanf(c_dm, "capi:%" MAX_CAPI_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", tmp, iv);
317-
if (i != 2)
318-
return -EINVAL;
319-
320-
/* non-cryptsetup compatible mode (generic driver with dash?) */
321-
if (strrchr(iv, ')')) {
322-
if (i_dm)
323-
return -EINVAL;
324-
if (!(*org_c = strdup(c_dm)))
325-
return -ENOMEM;
326-
return 0;
327-
}
328-
329-
len = strlen(tmp);
330-
if (len < 2)
331-
return -EINVAL;
332-
333-
if (tmp[len-1] == ')')
334-
tmp[len-1] = '\0';
335-
336-
if (sscanf(tmp, "rfc4309(%" MAX_CAPI_LEN_STR "s", capi) == 1) {
337-
if (!(*org_i = strdup("aead")))
338-
return -ENOMEM;
339-
} else if (sscanf(tmp, "rfc7539(%" MAX_CAPI_LEN_STR "[^,],%" MAX_CAPI_ONE_LEN_STR "s", capi, auth) == 2) {
340-
if (!(*org_i = strdup(auth)))
341-
return -ENOMEM;
342-
} else if (sscanf(tmp, "authenc(%" MAX_CAPI_ONE_LEN_STR "[^,],%" MAX_CAPI_LEN_STR "s", auth, capi) == 2) {
343-
if (!(*org_i = strdup(auth)))
344-
return -ENOMEM;
345-
} else {
346-
if (i_dm) {
347-
if (!(*org_i = strdup(i_dm)))
348-
return -ENOMEM;
349-
} else
350-
*org_i = NULL;
351-
memset(capi, 0, sizeof(capi));
352-
strncpy(capi, tmp, sizeof(capi)-1);
353-
}
354-
355-
i = sscanf(capi, "%" MAX_CAPI_ONE_LEN_STR "[^(](%" MAX_CAPI_ONE_LEN_STR "[^)])", mode, cipher);
356-
if (i == 2)
357-
i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv);
358-
else
359-
i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv);
360-
if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) {
361-
free(*org_i);
362-
*org_i = NULL;
363-
return -EINVAL;
364-
}
365-
366-
if (!(*org_c = strdup(dmcrypt_tmp))) {
367-
free(*org_i);
368-
*org_i = NULL;
369-
return -ENOMEM;
370-
}
371-
372-
return 0;
373-
}

0 commit comments

Comments
 (0)