diff --git a/rpcs3/Crypto/key_vault.cpp b/rpcs3/Crypto/key_vault.cpp index 2533b4d7fc..6fa9aa0684 100644 --- a/rpcs3/Crypto/key_vault.cpp +++ b/rpcs3/Crypto/key_vault.cpp @@ -11,10 +11,10 @@ SELF_KEY::SELF_KEY(u64 ver_start, u64 ver_end, u16 rev, u32 type, const std::str version_end = ver_end; revision = rev; self_type = type; - hex_to_bytes(erk, e.c_str(), 0); - hex_to_bytes(riv, r.c_str(), 0); - hex_to_bytes(pub, pb.c_str(), 0); - hex_to_bytes(priv, pr.c_str(), 0); + hex_to_bytes(erk, e, 0); + hex_to_bytes(riv, r, 0); + hex_to_bytes(pub, pb, 0); + hex_to_bytes(priv, pr, 0); curve_type = ct; } diff --git a/rpcs3/Crypto/utils.cpp b/rpcs3/Crypto/utils.cpp index 1858fb1fa0..7432acbf62 100644 --- a/rpcs3/Crypto/utils.cpp +++ b/rpcs3/Crypto/utils.cpp @@ -7,9 +7,11 @@ #include "sha1.h" #include "sha256.h" #include "key_vault.h" +#include +#include #include -#include -#include +#include +#include #include "Utilities/StrUtil.h" #include "Utilities/File.h" @@ -21,50 +23,24 @@ // Auxiliary functions (endian swap, xor). // Hex string conversion auxiliary functions. -u64 hex_to_u64(const char* hex_str) +void hex_to_bytes(unsigned char* data, std::string_view hex_str, unsigned int str_length) { - auto length = std::strlen(hex_str); - u64 tmp = 0; - u64 result = 0; - char c; - - while (length--) - { - c = *hex_str++; - if((c >= '0') && (c <= '9')) - tmp = c - '0'; - else if((c >= 'a') && (c <= 'f')) - tmp = c - 'a' + 10; - else if((c >= 'A') && (c <= 'F')) - tmp = c - 'A' + 10; - else - tmp = 0; - result |= (tmp << (length * 4)); - } - - return result; -} - -void hex_to_bytes(unsigned char* data, const char* hex_str, unsigned int str_length) -{ - const auto strn_length = (str_length > 0) ? str_length : std::strlen(hex_str); - auto data_length = strn_length / 2; - char tmp_buf[3] = {0, 0, 0}; + const auto strn_length = (str_length > 0) ? str_length : hex_str.size(); // Don't convert if the string length is odd. if ((strn_length % 2) == 0) { - while (data_length--) + for (size_t i = 0; i < strn_length; i += 2) { - tmp_buf[0] = *hex_str++; - tmp_buf[1] = *hex_str++; - - *data++ = static_cast(hex_to_u64(tmp_buf) & 0xFF); + const auto [ptr, err] = std::from_chars(hex_str.data() + i, hex_str.data() + i + 2, *data++, 16); + if (err != std::errc()) + { + fmt::throw_exception("Failed to read hex string: %s", std::make_error_code(err).message()); + } } } } - // Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC). void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len) { diff --git a/rpcs3/Crypto/utils.h b/rpcs3/Crypto/utils.h index 25cdeb7193..6d9bb8092b 100644 --- a/rpcs3/Crypto/utils.h +++ b/rpcs3/Crypto/utils.h @@ -6,7 +6,8 @@ #include "util/types.hpp" -#include +#include +#include enum { CRYPTO_MAX_PATH = 4096 }; @@ -15,8 +16,7 @@ char* extract_file_name(const char* file_path, char real_file_name[CRYPTO_MAX_PA std::string sha256_get_hash(const char* data, usz size, bool lower_case); // Hex string conversion auxiliary functions. -u64 hex_to_u64(const char* hex_str); -void hex_to_bytes(unsigned char *data, const char *hex_str, unsigned int str_length); +void hex_to_bytes(unsigned char* data, std::string_view hex_str, unsigned int str_length); // Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC). void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len);