Blame SOURCES/cryptsetup-2.6.0-Move-cipher_dm2c-to-crypto-utilities.patch

5c2830
From 9a9ddc7d22e14e14c9a6e97860cffada406adac3 Mon Sep 17 00:00:00 2001
5c2830
From: Ondrej Kozina <okozina@redhat.com>
5c2830
Date: Tue, 11 Oct 2022 10:50:17 +0200
5c2830
Subject: [PATCH 2/5] Move cipher_dm2c to crypto utilities.
5c2830
5c2830
(Gets renamed to crypt_capi_to_cipher)
5c2830
---
5c2830
 lib/libdevmapper.c | 84 +++-------------------------------------------
5c2830
 lib/utils_crypt.c  | 72 +++++++++++++++++++++++++++++++++++++++
5c2830
 lib/utils_crypt.h  | 11 ++++--
5c2830
 3 files changed, 85 insertions(+), 82 deletions(-)
5c2830
5c2830
diff --git a/lib/libdevmapper.c b/lib/libdevmapper.c
5c2830
index 0e45a789..7fcf843f 100644
5c2830
--- a/lib/libdevmapper.c
5c2830
+++ b/lib/libdevmapper.c
5c2830
@@ -476,27 +476,22 @@ static size_t int_log10(uint64_t x)
5c2830
 	return r;
5c2830
 }
5c2830
 
5c2830
-#define CLEN    64   /* 2*MAX_CIPHER_LEN */
5c2830
-#define CLENS  "63"  /* for sscanf length + '\0' */
5c2830
-#define CAPIL  144   /* should be enough to fit whole capi string */
5c2830
-#define CAPIS "143"  /* for sscanf of crypto API string + 16  + \0 */
5c2830
-
5c2830
 static int cipher_dm2c(const char *org_c, const char *org_i, unsigned tag_size,
5c2830
 		       char *c_dm, int c_dm_size,
5c2830
 		       char *i_dm, int i_dm_size)
5c2830
 {
5c2830
 	int c_size = 0, i_size = 0, i;
5c2830
-	char cipher[CLEN], mode[CLEN], iv[CLEN+1], tmp[CLEN];
5c2830
-	char capi[CAPIL];
5c2830
+	char cipher[MAX_CAPI_ONE_LEN], mode[MAX_CAPI_ONE_LEN], iv[MAX_CAPI_ONE_LEN+1],
5c2830
+	     tmp[MAX_CAPI_ONE_LEN], capi[MAX_CAPI_LEN];
5c2830
 
5c2830
 	if (!c_dm || !c_dm_size || !i_dm || !i_dm_size)
5c2830
 		return -EINVAL;
5c2830
 
5c2830
-	i = sscanf(org_c, "%" CLENS "[^-]-%" CLENS "s", cipher, tmp);
5c2830
+	i = sscanf(org_c, "%" MAX_CAPI_ONE_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", cipher, tmp);
5c2830
 	if (i != 2)
5c2830
 		return -EINVAL;
5c2830
 
5c2830
-	i = sscanf(tmp, "%" CLENS "[^-]-%" CLENS "s", mode, iv);
5c2830
+	i = sscanf(tmp, "%" MAX_CAPI_ONE_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", mode, iv);
5c2830
 	if (i == 1) {
5c2830
 		memset(iv, 0, sizeof(iv));
5c2830
 		strncpy(iv, mode, sizeof(iv)-1);
5c2830
@@ -543,75 +538,6 @@ static int cipher_dm2c(const char *org_c, const char *org_i, unsigned tag_size,
5c2830
 	return 0;
5c2830
 }
5c2830
 
5c2830
-static int cipher_c2dm(char **org_c, char **org_i, const char *c_dm, const char *i_dm)
5c2830
-{
5c2830
-	char cipher[CLEN], mode[CLEN], iv[CLEN], auth[CLEN];
5c2830
-	char tmp[CAPIL], dmcrypt_tmp[CAPIL*2], capi[CAPIL+1];
5c2830
-	size_t len;
5c2830
-	int i;
5c2830
-
5c2830
-	if (!c_dm)
5c2830
-		return -EINVAL;
5c2830
-
5c2830
-	/* legacy mode */
5c2830
-	if (strncmp(c_dm, "capi:", 4)) {
5c2830
-		if (!(*org_c = strdup(c_dm)))
5c2830
-			return -ENOMEM;
5c2830
-		*org_i = NULL;
5c2830
-		return 0;
5c2830
-	}
5c2830
-
5c2830
-	/* modes with capi: prefix */
5c2830
-	i = sscanf(c_dm, "capi:%" CAPIS "[^-]-%" CLENS "s", tmp, iv);
5c2830
-	if (i != 2)
5c2830
-		return -EINVAL;
5c2830
-
5c2830
-	len = strlen(tmp);
5c2830
-	if (len < 2)
5c2830
-		return -EINVAL;
5c2830
-
5c2830
-	if (tmp[len-1] == ')')
5c2830
-		tmp[len-1] = '\0';
5c2830
-
5c2830
-	if (sscanf(tmp, "rfc4309(%" CAPIS "s", capi) == 1) {
5c2830
-		if (!(*org_i = strdup("aead")))
5c2830
-			return -ENOMEM;
5c2830
-	} else if (sscanf(tmp, "rfc7539(%" CAPIS "[^,],%" CLENS "s", capi, auth) == 2) {
5c2830
-		if (!(*org_i = strdup(auth)))
5c2830
-			return -ENOMEM;
5c2830
-	} else if (sscanf(tmp, "authenc(%" CLENS "[^,],%" CAPIS "s", auth, capi) == 2) {
5c2830
-		if (!(*org_i = strdup(auth)))
5c2830
-			return -ENOMEM;
5c2830
-	} else {
5c2830
-		if (i_dm) {
5c2830
-			if (!(*org_i = strdup(i_dm)))
5c2830
-				return -ENOMEM;
5c2830
-		} else
5c2830
-			*org_i = NULL;
5c2830
-		memset(capi, 0, sizeof(capi));
5c2830
-		strncpy(capi, tmp, sizeof(capi)-1);
5c2830
-	}
5c2830
-
5c2830
-	i = sscanf(capi, "%" CLENS "[^(](%" CLENS "[^)])", mode, cipher);
5c2830
-	if (i == 2)
5c2830
-		i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv);
5c2830
-	else
5c2830
-		i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv);
5c2830
-	if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) {
5c2830
-		free(*org_i);
5c2830
-		*org_i = NULL;
5c2830
-		return -EINVAL;
5c2830
-	}
5c2830
-
5c2830
-	if (!(*org_c = strdup(dmcrypt_tmp))) {
5c2830
-		free(*org_i);
5c2830
-		*org_i = NULL;
5c2830
-		return -ENOMEM;
5c2830
-	}
5c2830
-
5c2830
-	return 0;
5c2830
-}
5c2830
-
5c2830
 static char *_uf(char *buf, size_t buf_size, const char *s, unsigned u)
5c2830
 {
5c2830
 	size_t r = snprintf(buf, buf_size, " %s:%u", s, u);
5c2830
@@ -2066,7 +1992,7 @@ static int _dm_target_query_crypt(struct crypt_device *cd, uint32_t get_flags,
5c2830
 
5c2830
 	/* cipher */
5c2830
 	if (get_flags & DM_ACTIVE_CRYPT_CIPHER) {
5c2830
-		r = cipher_c2dm(CONST_CAST(char**)&cipher,
5c2830
+		r = crypt_capi_to_cipher(CONST_CAST(char**)&cipher,
5c2830
 				CONST_CAST(char**)&integrity,
5c2830
 				rcipher, rintegrity);
5c2830
 		if (r < 0)
5c2830
diff --git a/lib/utils_crypt.c b/lib/utils_crypt.c
5c2830
index 83d0a2c5..4f4dbba8 100644
5c2830
--- a/lib/utils_crypt.c
5c2830
+++ b/lib/utils_crypt.c
5c2830
@@ -31,6 +31,8 @@
5c2830
 #include "libcryptsetup.h"
5c2830
 #include "utils_crypt.h"
5c2830
 
5c2830
+#define MAX_CAPI_LEN_STR "143" /* for sscanf of crypto API string + 16  + \0 */
5c2830
+
5c2830
 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
5c2830
 			      char *cipher_mode)
5c2830
 {
5c2830
@@ -266,3 +268,73 @@ bool crypt_is_cipher_null(const char *cipher_spec)
5c2830
 		return false;
5c2830
 	return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null"));
5c2830
 }
5c2830
+
5c2830
+int crypt_capi_to_cipher(char **org_c, char **org_i, const char *c_dm, const char *i_dm)
5c2830
+{
5c2830
+	char cipher[MAX_CAPI_ONE_LEN], mode[MAX_CAPI_ONE_LEN], iv[MAX_CAPI_ONE_LEN],
5c2830
+	     auth[MAX_CAPI_ONE_LEN], tmp[MAX_CAPI_LEN], dmcrypt_tmp[MAX_CAPI_LEN*2],
5c2830
+	     capi[MAX_CAPI_LEN+1];
5c2830
+	size_t len;
5c2830
+	int i;
5c2830
+
5c2830
+	if (!c_dm)
5c2830
+		return -EINVAL;
5c2830
+
5c2830
+	/* legacy mode */
5c2830
+	if (strncmp(c_dm, "capi:", 4)) {
5c2830
+		if (!(*org_c = strdup(c_dm)))
5c2830
+			return -ENOMEM;
5c2830
+		*org_i = NULL;
5c2830
+		return 0;
5c2830
+	}
5c2830
+
5c2830
+	/* modes with capi: prefix */
5c2830
+	i = sscanf(c_dm, "capi:%" MAX_CAPI_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", tmp, iv);
5c2830
+	if (i != 2)
5c2830
+		return -EINVAL;
5c2830
+
5c2830
+	len = strlen(tmp);
5c2830
+	if (len < 2)
5c2830
+		return -EINVAL;
5c2830
+
5c2830
+	if (tmp[len-1] == ')')
5c2830
+		tmp[len-1] = '\0';
5c2830
+
5c2830
+	if (sscanf(tmp, "rfc4309(%" MAX_CAPI_LEN_STR "s", capi) == 1) {
5c2830
+		if (!(*org_i = strdup("aead")))
5c2830
+			return -ENOMEM;
5c2830
+	} else if (sscanf(tmp, "rfc7539(%" MAX_CAPI_LEN_STR "[^,],%" MAX_CAPI_ONE_LEN_STR "s", capi, auth) == 2) {
5c2830
+		if (!(*org_i = strdup(auth)))
5c2830
+			return -ENOMEM;
5c2830
+	} else if (sscanf(tmp, "authenc(%" MAX_CAPI_ONE_LEN_STR "[^,],%" MAX_CAPI_LEN_STR "s", auth, capi) == 2) {
5c2830
+		if (!(*org_i = strdup(auth)))
5c2830
+			return -ENOMEM;
5c2830
+	} else {
5c2830
+		if (i_dm) {
5c2830
+			if (!(*org_i = strdup(i_dm)))
5c2830
+				return -ENOMEM;
5c2830
+		} else
5c2830
+			*org_i = NULL;
5c2830
+		memset(capi, 0, sizeof(capi));
5c2830
+		strncpy(capi, tmp, sizeof(capi)-1);
5c2830
+	}
5c2830
+
5c2830
+	i = sscanf(capi, "%" MAX_CAPI_ONE_LEN_STR "[^(](%" MAX_CAPI_ONE_LEN_STR "[^)])", mode, cipher);
5c2830
+	if (i == 2)
5c2830
+		i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv);
5c2830
+	else
5c2830
+		i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv);
5c2830
+	if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) {
5c2830
+		free(*org_i);
5c2830
+		*org_i = NULL;
5c2830
+		return -EINVAL;
5c2830
+	}
5c2830
+
5c2830
+	if (!(*org_c = strdup(dmcrypt_tmp))) {
5c2830
+		free(*org_i);
5c2830
+		*org_i = NULL;
5c2830
+		return -ENOMEM;
5c2830
+	}
5c2830
+
5c2830
+	return 0;
5c2830
+}
5c2830
diff --git a/lib/utils_crypt.h b/lib/utils_crypt.h
5c2830
index 5922350a..a4a9b6ca 100644
5c2830
--- a/lib/utils_crypt.h
5c2830
+++ b/lib/utils_crypt.h
5c2830
@@ -27,9 +27,12 @@
5c2830
 #include <stdbool.h>
5c2830
 #include <unistd.h>
5c2830
 
5c2830
-#define MAX_CIPHER_LEN		32
5c2830
-#define MAX_CIPHER_LEN_STR	"31"
5c2830
-#define MAX_KEYFILES		32
5c2830
+#define MAX_CIPHER_LEN       32
5c2830
+#define MAX_CIPHER_LEN_STR   "31"
5c2830
+#define MAX_KEYFILES         32
5c2830
+#define MAX_CAPI_ONE_LEN     2 * MAX_CIPHER_LEN
5c2830
+#define MAX_CAPI_ONE_LEN_STR "63"  /* for sscanf length + '\0' */
5c2830
+#define MAX_CAPI_LEN         144   /* should be enough to fit whole capi string */
5c2830
 
5c2830
 int crypt_parse_name_and_mode(const char *s, char *cipher,
5c2830
 			      int *key_nums, char *cipher_mode);
5c2830
@@ -46,4 +49,6 @@ void crypt_log_hex(struct crypt_device *cd,
5c2830
 
5c2830
 bool crypt_is_cipher_null(const char *cipher_spec);
5c2830
 
5c2830
+int crypt_capi_to_cipher(char **org_c, char **org_i, const char *c_dm, const char *i_dm);
5c2830
+
5c2830
 #endif /* _UTILS_CRYPT_H */
5c2830
-- 
5c2830
2.38.1
5c2830