diff mbox series

[15/20] Make libfsverity_find_hash_alg_by_name() private to the shared library

Message ID 20200424205504.2586682-16-Jes.Sorensen@gmail.com (mailing list archive)
State Superseded
Headers show
Series Split fsverity-utils into a shared library | expand

Commit Message

Jes Sorensen April 24, 2020, 8:54 p.m. UTC
From: Jes Sorensen <jsorensen@fb.com>

This moves struct fsverity_hash_alg out of the public API. Instead
implement show_all_hash_algs() by calling libfsverity_hash_name()
until it returns null.

Signed-off-by: Jes Sorensen <jsorensen@fb.com>
---
 cmd_enable.c          |  2 +-
 cmd_measure.c         | 17 ++++++++---------
 cmd_sign.c            |  2 +-
 fsverity.c            | 16 +++++++---------
 hash_algs.c           |  1 +
 libfsverity.h         | 19 -------------------
 libfsverity_private.h | 19 +++++++++++++++++++
 7 files changed, 37 insertions(+), 39 deletions(-)
diff mbox series

Patch

diff --git a/cmd_enable.c b/cmd_enable.c
index ac977e7..632ac84 100644
--- a/cmd_enable.c
+++ b/cmd_enable.c
@@ -42,7 +42,7 @@  static bool parse_hash_alg_option(const char *arg, u32 *alg_ptr)
 		return true;
 	}
 	error_msg("unknown hash algorithm: '%s'", arg);
-	fputs("Available hash algorithms: ", stderr);
+	fputs("Available hash algorithms:", stderr);
 	show_all_hash_algs(stderr);
 	putc('\n', stderr);
 
diff --git a/cmd_measure.c b/cmd_measure.c
index 4c0777f..df39da0 100644
--- a/cmd_measure.c
+++ b/cmd_measure.c
@@ -22,9 +22,8 @@  int fsverity_cmd_measure(const struct fsverity_command *cmd,
 	struct fsverity_digest *d = NULL;
 	struct filedes file;
 	char digest_hex[FS_VERITY_MAX_DIGEST_SIZE * 2 + 1];
-	const struct fsverity_hash_alg *hash_alg;
 	char _hash_alg_name[32];
-	const char *hash_alg_name;
+	char *hash_alg_name;
 	int status;
 	int i;
 
@@ -48,14 +47,14 @@  int fsverity_cmd_measure(const struct fsverity_command *cmd,
 
 		ASSERT(d->digest_size <= FS_VERITY_MAX_DIGEST_SIZE);
 		bin2hex(d->digest, d->digest_size, digest_hex);
-		hash_alg = libfsverity_find_hash_alg_by_num(d->digest_algorithm);
-		if (hash_alg) {
-			hash_alg_name = hash_alg->name;
-		} else {
+		hash_alg_name = libfsverity_hash_name(d->digest_algorithm);
+		if (!hash_alg_name)
 			sprintf(_hash_alg_name, "ALG_%u", d->digest_algorithm);
-			hash_alg_name = _hash_alg_name;
-		}
-		printf("%s:%s %s\n", hash_alg_name, digest_hex, argv[i]);
+
+		printf("%s:%s %s\n",
+		       hash_alg_name ? hash_alg_name :_hash_alg_name,
+		       digest_hex, argv[i]);
+		free(hash_alg_name);
 	}
 	status = 0;
 out:
diff --git a/cmd_sign.c b/cmd_sign.c
index 80e62d5..57a9250 100644
--- a/cmd_sign.c
+++ b/cmd_sign.c
@@ -87,7 +87,7 @@  int fsverity_cmd_sign(const struct fsverity_command *cmd,
 			if (!alg_nr) {
 				error_msg("unknown hash algorithm: '%s'",
 					  optarg);
-				fputs("Available hash algorithms: ", stderr);
+				fputs("Available hash algorithms:", stderr);
 				show_all_hash_algs(stderr);
 				putc('\n', stderr);
 				goto out_usage;
diff --git a/fsverity.c b/fsverity.c
index a176ead..2e2b553 100644
--- a/fsverity.c
+++ b/fsverity.c
@@ -51,14 +51,12 @@  static const struct fsverity_command {
 void show_all_hash_algs(FILE *fp)
 {
 	int i = 1;
-	const char *sep = "";
-	const struct fsverity_hash_alg *alg;
-
-	while ((alg = libfsverity_find_hash_alg_by_num(i++))) {
-		if (alg && alg->name) {
-			fprintf(fp, "%s%s", sep, alg->name);
-			sep = ", ";
-		}
+	const char *sep = " ";
+	char *alg;
+
+	while ((alg = libfsverity_hash_name(i++))) {
+		fprintf(fp, "%s%s", sep, alg);
+		free(alg);
 	}
 }
 
@@ -75,7 +73,7 @@  static void usage_all(FILE *fp)
 "    fsverity --help\n"
 "    fsverity --version\n"
 "\n"
-"Available hash algorithms: ", fp);
+"Available hash algorithms:", fp);
 	show_all_hash_algs(fp);
 	putc('\n', fp);
 }
diff --git a/hash_algs.c b/hash_algs.c
index 120d1be..03b9de9 100644
--- a/hash_algs.c
+++ b/hash_algs.c
@@ -15,6 +15,7 @@ 
 #include "helpers.h"
 #include "fsverity_uapi.h"
 #include "libfsverity.h"
+#include "libfsverity_private.h"
 #include "hash_algs.h"
 
 /* ========== libcrypto (OpenSSL) wrappers ========== */
diff --git a/libfsverity.h b/libfsverity.h
index a505cbe..4f0f885 100644
--- a/libfsverity.h
+++ b/libfsverity.h
@@ -56,14 +56,6 @@  struct libfsverity_signature_params {
 	uint64_t reserved[11];
 };
 
-struct fsverity_hash_alg {
-	const char *name;
-	int digest_size;
-	unsigned int block_size;
-	uint16_t hash_num;
-	struct hash_ctx *(*create_ctx)(const struct fsverity_hash_alg *alg);
-};
-
 /*
  * libfsverity_compute_digest - Compute digest of a file
  * @fd: open file descriptor of file to compute digest for
@@ -112,17 +104,6 @@  libfsverity_sign_digest(const struct libfsverity_digest *digest,
  */
 uint16_t libfsverity_find_hash_alg_by_name(const char *name);
 
-/*
- * libfsverity_find_hash_alg_by_num - Find hash algorithm by number
- * @name: Number of hash algorithm
- *
- * Returns:
- * struct fsverity_hash_alg success
- * NULL on error
- */
-const struct fsverity_hash_alg *
-libfsverity_find_hash_alg_by_num(unsigned int num);
-
 /*
  * libfsverity_digest_size - Return size of digest for a given algorithm
  * @alg_nr: Valid hash algorithm number
diff --git a/libfsverity_private.h b/libfsverity_private.h
index 5f3e1b4..f8eebe2 100644
--- a/libfsverity_private.h
+++ b/libfsverity_private.h
@@ -30,4 +30,23 @@  struct fsverity_descriptor {
 	uint8_t signature[];	/* optional PKCS#7 signature */
 };
 
+struct fsverity_hash_alg {
+	const char *name;
+	int digest_size;
+	unsigned int block_size;
+	uint16_t hash_num;
+	struct hash_ctx *(*create_ctx)(const struct fsverity_hash_alg *alg);
+};
+
+/*
+ * libfsverity_find_hash_alg_by_num - Find hash algorithm by number
+ * @name: Number of hash algorithm
+ *
+ * Returns:
+ * struct fsverity_hash_alg success
+ * NULL on error
+ */
+const struct fsverity_hash_alg *
+libfsverity_find_hash_alg_by_num(unsigned int num);
+
 #endif