From ceed3c0c3b1675de4744ab26eff04a4da6926115 Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Mon, 25 Apr 2022 19:25:31 +0200 Subject: [PATCH] Introduce crypt_log_hex helper and use it for log_std output. --- lib/bitlk/bitlk.c | 10 +--------- lib/setup.c | 25 +++++++++---------------- lib/utils_crypt.c | 15 +++++++++++++++ lib/utils_crypt.h | 5 +++++ src/cryptsetup.c | 35 +++++------------------------------ src/veritysetup.c | 8 +++----- 6 files changed, 38 insertions(+), 60 deletions(-) diff --git a/lib/bitlk/bitlk.c b/lib/bitlk/bitlk.c index 5c2ff6fa..eec9ae46 100644 --- a/lib/bitlk/bitlk.c +++ b/lib/bitlk/bitlk.c @@ -233,14 +233,6 @@ static const char* get_bitlk_type_string(BITLKEncryptionType type) } } -/* TODO -- move to some utils file */ -static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep) -{ - int i; - for(i = 0; i < n; i++) - log_std(cd, "%02hhx%s", (const char)d[i], sep); -} - static uint64_t filetime_to_unixtime(uint64_t time) { return (time - EPOCH_AS_FILETIME) / HUNDREDS_OF_NANOSECONDS; @@ -729,7 +721,7 @@ int BITLK_dump(struct crypt_device *cd, struct device *device, struct bitlk_meta log_std(cd, "\tGUID: \t%s\n", vmk_p->guid); log_std(cd, "\tProtection: \t%s\n", get_vmk_protection_string (vmk_p->protection)); log_std(cd, "\tSalt: \t"); - hexprint(cd, (const char *) vmk_p->salt, 16, ""); + crypt_log_hex(cd, (const char *) vmk_p->salt, 16, "", 0, NULL); log_std(cd, "\n"); vk_p = vmk_p->vk; diff --git a/lib/setup.c b/lib/setup.c index 8ff9f512..3a6ee471 100644 --- a/lib/setup.c +++ b/lib/setup.c @@ -5058,13 +5058,6 @@ crypt_status_info crypt_status(struct crypt_device *cd, const char *name) return CRYPT_INACTIVE; } -static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep) -{ - int i; - for(i = 0; i < n; i++) - log_std(cd, "%02hhx%s", (const char)d[i], sep); -} - static int _luks_dump(struct crypt_device *cd) { int i; @@ -5077,12 +5070,12 @@ static int _luks_dump(struct crypt_device *cd) log_std(cd, "Payload offset:\t%" PRIu32 "\n", cd->u.luks1.hdr.payloadOffset); log_std(cd, "MK bits: \t%" PRIu32 "\n", cd->u.luks1.hdr.keyBytes * 8); log_std(cd, "MK digest: \t"); - hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " "); + crypt_log_hex(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ", 0, NULL); log_std(cd, "\n"); log_std(cd, "MK salt: \t"); - hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " "); + crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ", 0, NULL); log_std(cd, "\n \t"); - hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " "); + crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL); log_std(cd, "\n"); log_std(cd, "MK iterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.mkDigestIterations); log_std(cd, "UUID: \t%s\n\n", cd->u.luks1.hdr.uuid); @@ -5092,11 +5085,11 @@ static int _luks_dump(struct crypt_device *cd) log_std(cd, "\tIterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.keyblock[i].passwordIterations); log_std(cd, "\tSalt: \t"); - hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt, - LUKS_SALTSIZE/2, " "); + crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt, + LUKS_SALTSIZE/2, " ", 0, NULL); log_std(cd, "\n\t \t"); - hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt + - LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " "); + crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt + + LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL); log_std(cd, "\n"); log_std(cd, "\tKey material offset:\t%" PRIu32 "\n", @@ -5121,13 +5114,13 @@ static int _verity_dump(struct crypt_device *cd) log_std(cd, "Hash algorithm: \t%s\n", cd->u.verity.hdr.hash_name); log_std(cd, "Salt: \t"); if (cd->u.verity.hdr.salt_size) - hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, ""); + crypt_log_hex(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "", 0, NULL); else log_std(cd, "-"); log_std(cd, "\n"); if (cd->u.verity.root_hash) { log_std(cd, "Root hash: \t"); - hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, ""); + crypt_log_hex(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "", 0, NULL); log_std(cd, "\n"); } return 0; diff --git a/lib/utils_crypt.c b/lib/utils_crypt.c index 3ffaaf31..83d0a2c5 100644 --- a/lib/utils_crypt.c +++ b/lib/utils_crypt.c @@ -245,6 +245,21 @@ char *crypt_bytes_to_hex(size_t size, const char *bytes) return hex; } +void crypt_log_hex(struct crypt_device *cd, + const char *bytes, size_t size, + const char *sep, int numwrap, const char *wrapsep) +{ + unsigned i; + + for (i = 0; i < size; i++) { + if (wrapsep && numwrap && i && !(i % numwrap)) + crypt_logf(cd, CRYPT_LOG_NORMAL, wrapsep); + crypt_logf(cd, CRYPT_LOG_NORMAL, "%c%c%s", + hex2asc((const unsigned char)bytes[i] >> 4), + hex2asc((const unsigned char)bytes[i] & 0xf), sep); + } +} + bool crypt_is_cipher_null(const char *cipher_spec) { if (!cipher_spec) diff --git a/lib/utils_crypt.h b/lib/utils_crypt.h index fd97547c..5922350a 100644 --- a/lib/utils_crypt.h +++ b/lib/utils_crypt.h @@ -25,6 +25,8 @@ #include +struct crypt_device; + #define MAX_CIPHER_LEN 32 #define MAX_CIPHER_LEN_STR "31" #define MAX_KEYFILES 32 @@ -38,6 +40,9 @@ int crypt_parse_pbkdf(const char *s, const char **pbkdf); ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc); char *crypt_bytes_to_hex(size_t size, const char *bytes); +void crypt_log_hex(struct crypt_device *cd, + const char *bytes, size_t size, + const char *sep, int numwrap, const char *wrapsep); bool crypt_is_cipher_null(const char *cipher_spec); diff --git a/src/cryptsetup.c b/src/cryptsetup.c index e36b48cf..627f25b2 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -500,7 +500,6 @@ static int tcryptDump_with_volume_key(struct crypt_device *cd) { char *vk = NULL; size_t vk_size; - unsigned i; int r; if (!ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog( @@ -525,12 +524,7 @@ static int tcryptDump_with_volume_key(struct crypt_device *cd) log_std("Payload offset:\t%d\n", (int)crypt_get_data_offset(cd)); log_std("MK bits: \t%d\n", (int)vk_size * 8); log_std("MK dump:\t"); - - for(i = 0; i < vk_size; i++) { - if (i && !(i % 16)) - log_std("\n\t\t"); - log_std("%02hhx ", (char)vk[i]); - } + crypt_log_hex(NULL, vk, vk_size, " ", 16, "\n\t\t"); log_std("\n"); out: crypt_safe_free(vk); @@ -573,7 +567,6 @@ static int bitlkDump_with_volume_key(struct crypt_device *cd) char *vk = NULL, *password = NULL; size_t passwordLen = 0; size_t vk_size; - unsigned i; int r; if (!ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog( @@ -618,14 +611,8 @@ static int bitlkDump_with_volume_key(struct crypt_device *cd) goto out; } log_std("MK dump:\t"); - - for(i = 0; i < vk_size; i++) { - if (i && !(i % 16)) - log_std("\n\t\t"); - log_std("%02hhx ", (char)vk[i]); - } + crypt_log_hex(NULL, vk, vk_size, " ", 16, "\n\t\t"); log_std("\n"); - out: crypt_safe_free(password); crypt_safe_free(vk); @@ -2024,7 +2011,6 @@ static int luksDump_with_volume_key(struct crypt_device *cd) char *vk = NULL, *password = NULL; size_t passwordLen = 0; size_t vk_size; - unsigned i; int r; if (!ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog( @@ -2070,14 +2056,8 @@ static int luksDump_with_volume_key(struct crypt_device *cd) goto out; } log_std("MK dump:\t"); - - for(i = 0; i < vk_size; i++) { - if (i && !(i % 16)) - log_std("\n\t\t"); - log_std("%02hhx ", (char)vk[i]); - } + crypt_log_hex(NULL, vk, vk_size, " ", 16, "\n\t\t"); log_std("\n"); - out: crypt_safe_free(password); crypt_safe_free(vk); @@ -2089,7 +2069,7 @@ static int luksDump_with_unbound_key(struct crypt_device *cd) crypt_keyslot_info ki; char *uk = NULL, *password = NULL; size_t uk_size, passwordLen = 0; - int i, r; + int r; ki = crypt_keyslot_status(cd, ARG_INT32(OPT_KEY_SLOT_ID)); if (ki != CRYPT_SLOT_UNBOUND) { @@ -2140,12 +2120,7 @@ static int luksDump_with_unbound_key(struct crypt_device *cd) goto out; } log_std("Unbound Key:\t"); - - for(i = 0; i < (int)uk_size; i++) { - if (i && !(i % 16)) - log_std("\n\t\t"); - log_std("%02hhx ", (char)uk[i]); - } + crypt_log_hex(NULL, uk, uk_size, " ", 16, "\n\t\t"); log_std("\n"); out: crypt_safe_free(password); diff --git a/src/veritysetup.c b/src/veritysetup.c index 18f18fa0..cce01a92 100644 --- a/src/veritysetup.c +++ b/src/veritysetup.c @@ -332,7 +332,7 @@ static int action_status(void) struct stat st; char *backing_file, *root_hash; size_t root_hash_size; - unsigned i, path = 0; + unsigned path = 0; int r = 0; /* perhaps a path, not a dm device name */ @@ -385,8 +385,7 @@ static int action_status(void) log_std(" hash name: %s\n", vp.hash_name); log_std(" salt: "); if (vp.salt_size) - for(i = 0; i < vp.salt_size; i++) - log_std("%02hhx", (const char)vp.salt[i]); + crypt_log_hex(NULL, vp.salt, vp.salt_size, "", 0, NULL); else log_std("-"); log_std("\n"); @@ -424,8 +423,7 @@ static int action_status(void) r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash, &root_hash_size, NULL, 0); if (!r) { log_std(" root hash: "); - for (i = 0; i < root_hash_size; i++) - log_std("%02hhx", (const char)root_hash[i]); + crypt_log_hex(NULL, root_hash, root_hash_size, "", 0, NULL); log_std("\n"); } free(root_hash);