Skip to content

Commit

Permalink
Use enum class for hex case encoding type.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 authored and antonsviridenko committed Feb 3, 2024
1 parent 14ed52b commit 3d1a3d5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/lib/crypto/mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ secure_clear(void *vp, size_t size)
namespace rnp {

bool
hex_encode(const uint8_t *buf, size_t buf_len, char *hex, size_t hex_len, hex_format_t format)
hex_encode(const uint8_t *buf, size_t buf_len, char *hex, size_t hex_len, HexFormat format)
{
uint32_t flags = format == HEX_LOWERCASE ? BOTAN_FFI_HEX_LOWER_CASE : 0;
uint32_t flags = format == HexFormat::Lowercase ? BOTAN_FFI_HEX_LOWER_CASE : 0;

if (hex_len < (buf_len * 2 + 1)) {
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/lib/crypto/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,15 @@ template <typename T, std::size_t N> struct secure_array {
}
};

typedef enum { HEX_LOWERCASE, HEX_UPPERCASE } hex_format_t;
enum class HexFormat { Lowercase, Uppercase };

bool hex_encode(const uint8_t *buf,
size_t buf_len,
char * hex,
size_t hex_len,
hex_format_t format = HEX_UPPERCASE);
HexFormat format = HexFormat::Uppercase);
size_t hex_decode(const char *hex, uint8_t *buf, size_t buf_len);

} // namespace rnp

void secure_clear(void *vp, size_t size);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/crypto/mem_ossl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ secure_clear(void *vp, size_t size)
namespace rnp {

bool
hex_encode(const uint8_t *buf, size_t buf_len, char *hex, size_t hex_len, hex_format_t format)
hex_encode(const uint8_t *buf, size_t buf_len, char *hex, size_t hex_len, HexFormat format)
{
if (hex_len < (buf_len * 2 + 1)) {
return false;
}
static const char *hex_low = "0123456789abcdef";
static const char *hex_up = "0123456789ABCDEF";
const char * hex_ch = (format == HEX_LOWERCASE) ? hex_low : hex_up;
const char * hex_ch = (format == HexFormat::Lowercase) ? hex_low : hex_up;
hex[buf_len * 2] = '\0';
for (size_t i = 0; i < buf_len; i++) {
hex[i << 1] = hex_ch[buf[i] >> 4];
Expand Down
2 changes: 1 addition & 1 deletion src/lib/json-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ json_add_hex(json_object *obj, const char *name, const uint8_t *val, size_t val_
return false;
}

bool res = rnp::hex_encode(val, val_len, hexbuf, hexlen, rnp::HEX_LOWERCASE) &&
bool res = rnp::hex_encode(val, val_len, hexbuf, hexlen, rnp::HexFormat::Lowercase) &&
json_add(obj, name, json_object_new_string(hexbuf));

if (hexbuf != smallbuf) {
Expand Down
43 changes: 22 additions & 21 deletions src/lib/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,30 @@ class LogStop {

#define RNP_LOG(...) RNP_LOG_FD(stderr, __VA_ARGS__)

#define RNP_LOG_KEY(msg, key) \
do { \
if (!(key)) { \
RNP_LOG(msg, "(null)"); \
break; \
} \
char keyid[PGP_KEY_ID_SIZE * 2 + 1] = {0}; \
const pgp_key_id_t &id = (key)->keyid(); \
rnp::hex_encode(id.data(), id.size(), keyid, sizeof(keyid), rnp::HEX_LOWERCASE); \
RNP_LOG(msg, keyid); \
#define RNP_LOG_KEY(msg, key) \
do { \
if (!(key)) { \
RNP_LOG(msg, "(null)"); \
break; \
} \
char keyid[PGP_KEY_ID_SIZE * 2 + 1] = {0}; \
const pgp_key_id_t &id = (key)->keyid(); \
rnp::hex_encode( \
id.data(), id.size(), keyid, sizeof(keyid), rnp::HexFormat::Lowercase); \
RNP_LOG(msg, keyid); \
} while (0)

#define RNP_LOG_KEY_PKT(msg, key) \
do { \
pgp_key_id_t keyid = {}; \
if (pgp_keyid(keyid, (key))) { \
RNP_LOG(msg, "unknown"); \
break; \
}; \
char keyidhex[PGP_KEY_ID_SIZE * 2 + 1] = {0}; \
rnp::hex_encode( \
keyid.data(), keyid.size(), keyidhex, sizeof(keyidhex), rnp::HEX_LOWERCASE); \
RNP_LOG(msg, keyidhex); \
#define RNP_LOG_KEY_PKT(msg, key) \
do { \
pgp_key_id_t keyid = {}; \
if (pgp_keyid(keyid, (key))) { \
RNP_LOG(msg, "unknown"); \
break; \
}; \
char keyidhex[PGP_KEY_ID_SIZE * 2 + 1] = {0}; \
rnp::hex_encode( \
keyid.data(), keyid.size(), keyidhex, sizeof(keyidhex), rnp::HexFormat::Lowercase); \
RNP_LOG(msg, keyidhex); \
} while (0)

#endif
2 changes: 1 addition & 1 deletion src/lib/pgp-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@ pgp_key_t::write_xfer(pgp_dest_t &dst, const rnp::KeyStore *keyring) const
if (!subkey) {
char fphex[PGP_FINGERPRINT_HEX_SIZE] = {0};
rnp::hex_encode(
fp.fingerprint, fp.length, fphex, sizeof(fphex), rnp::HEX_LOWERCASE);
fp.fingerprint, fp.length, fphex, sizeof(fphex), rnp::HexFormat::Lowercase);
RNP_LOG("Warning! Subkey %s not found.", fphex);
continue;
}
Expand Down
7 changes: 2 additions & 5 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,17 +513,14 @@ parse_ks_format(pgp_key_store_format_t *key_store_format, const char *format)
}

static rnp_result_t
hex_encode_value(const uint8_t * value,
size_t len,
char ** res,
rnp::hex_format_t format = rnp::HEX_UPPERCASE)
hex_encode_value(const uint8_t *value, size_t len, char **res)
{
size_t hex_len = len * 2 + 1;
*res = (char *) malloc(hex_len);
if (!*res) {
return RNP_ERROR_OUT_OF_MEMORY;
}
if (!rnp::hex_encode(value, len, *res, hex_len, format)) {
if (!rnp::hex_encode(value, len, *res, hex_len, rnp::HexFormat::Uppercase)) {
free(*res);
*res = NULL;
return RNP_ERROR_GENERIC;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/key-validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ all_keys_valid(const rnp::KeyStore *keyring, pgp_key_t *except = NULL)
key.keyid().size(),
keyid,
sizeof(keyid),
rnp::HEX_LOWERCASE)) {
rnp::HexFormat::Lowercase)) {
throw std::exception();
}
RNP_LOG("key %s is not valid", keyid);
Expand Down
3 changes: 2 additions & 1 deletion src/tests/utils-hex2bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ TEST_F(rnp_tests, test_utils_hex2bin)
{
uint8_t buf[2] = {0xAB, 0xCD};
char hex[4];
assert_false(rnp::hex_encode(buf, sizeof(buf), hex, sizeof(hex), rnp::HEX_LOWERCASE));
assert_false(
rnp::hex_encode(buf, sizeof(buf), hex, sizeof(hex), rnp::HexFormat::Lowercase));
}
}

0 comments on commit 3d1a3d5

Please sign in to comment.