Use std::string_view in crypto code

This commit is contained in:
Megamouse 2026-06-14 06:25:39 +02:00
parent 5c4be5dd3b
commit b8ca6d953f
6 changed files with 11 additions and 13 deletions

View File

@ -5,7 +5,7 @@
LOG_CHANNEL(key_vault_log, "KEY_VAULT"); LOG_CHANNEL(key_vault_log, "KEY_VAULT");
SELF_KEY::SELF_KEY(u64 ver_start, u64 ver_end, u16 rev, u32 type, const std::string& e, const std::string& r, const std::string& pb, const std::string& pr, u32 ct) SELF_KEY::SELF_KEY(u64 ver_start, u64 ver_end, u16 rev, u32 type, std::string_view e, std::string_view r, std::string_view pb, std::string_view pr, u32 ct)
{ {
version_start = ver_start; version_start = ver_start;
version_end = ver_end; version_end = ver_end;

View File

@ -30,7 +30,7 @@ struct SELF_KEY
u8 priv[0x15]{}; u8 priv[0x15]{};
u32 curve_type{}; u32 curve_type{};
SELF_KEY(u64 ver_start, u64 ver_end, u16 rev, u32 type, const std::string& e, const std::string& r, const std::string& pb, const std::string& pr, u32 ct); SELF_KEY(u64 ver_start, u64 ver_end, u16 rev, u32 type, std::string_view e, std::string_view r, std::string_view pb, std::string_view pr, u32 ct);
}; };
constexpr u32 PASSPHRASE_KEY_LEN = 16; constexpr u32 PASSPHRASE_KEY_LEN = 16;

View File

@ -679,7 +679,7 @@ u128 GetEdatRifKeyFromRapFile(const fs::file& rap_file)
return rifkey; return rifkey;
} }
bool VerifyEDATHeaderWithKLicense(const fs::file& input, const std::string& input_file_name, const u8* custom_klic, NPD_HEADER* npd_out) bool VerifyEDATHeaderWithKLicense(const fs::file& input, std::string_view input_file_name, const u8* custom_klic, NPD_HEADER* npd_out)
{ {
// Setup NPD and EDAT/SDAT structs. // Setup NPD and EDAT/SDAT structs.
NPD_HEADER NPD; NPD_HEADER NPD;
@ -702,7 +702,7 @@ bool VerifyEDATHeaderWithKLicense(const fs::file& input, const std::string& inpu
// Perform header validation (EDAT only). // Perform header validation (EDAT only).
char real_file_name[CRYPTO_MAX_PATH]{}; char real_file_name[CRYPTO_MAX_PATH]{};
extract_file_name(input_file_name.c_str(), real_file_name); extract_file_name(input_file_name, real_file_name);
if (!validate_npd_hashes(real_file_name, custom_klic, &NPD, &EDAT, false)) if (!validate_npd_hashes(real_file_name, custom_klic, &NPD, &EDAT, false))
{ {
edat_log.error("NPD hash validation failed!"); edat_log.error("NPD hash validation failed!");
@ -810,7 +810,7 @@ bool EDATADecrypter::ReadHeader()
{ {
// extract key from RIF // extract key from RIF
char real_file_name[CRYPTO_MAX_PATH]{}; char real_file_name[CRYPTO_MAX_PATH]{};
extract_file_name(m_file_name.c_str(), real_file_name); extract_file_name(m_file_name, real_file_name);
if (!validate_npd_hashes(real_file_name, reinterpret_cast<const u8*>(&dec_key), &npdHeader, &edatHeader, false)) if (!validate_npd_hashes(real_file_name, reinterpret_cast<const u8*>(&dec_key), &npdHeader, &edatHeader, false))
{ {

View File

@ -62,7 +62,7 @@ struct EDAT_HEADER
extern fs::file DecryptEDAT(const fs::file& input, const std::string& input_file_name, int mode, u8 *custom_klic); extern fs::file DecryptEDAT(const fs::file& input, const std::string& input_file_name, int mode, u8 *custom_klic);
extern void read_npd_edat_header(const fs::file* input, NPD_HEADER& NPD, EDAT_HEADER& EDAT); extern void read_npd_edat_header(const fs::file* input, NPD_HEADER& NPD, EDAT_HEADER& EDAT);
extern bool VerifyEDATHeaderWithKLicense(const fs::file& input, const std::string& input_file_name, const u8* custom_klic, NPD_HEADER *npd_out = nullptr); extern bool VerifyEDATHeaderWithKLicense(const fs::file& input, std::string_view input_file_name, const u8* custom_klic, NPD_HEADER *npd_out = nullptr);
u128 GetEdatRifKeyFromRapFile(const fs::file& rap_file); u128 GetEdatRifKeyFromRapFile(const fs::file& rap_file);

View File

@ -136,17 +136,15 @@ void cmac_hash_forge(unsigned char *key, int /*key_len*/, unsigned char *in, usz
aes_cmac(&ctx, in_len, in, hash); aes_cmac(&ctx, in_len, in, hash);
} }
char* extract_file_name(const char* file_path, char real_file_name[CRYPTO_MAX_PATH]) char* extract_file_name(std::string_view file_path, char real_file_name[CRYPTO_MAX_PATH])
{ {
std::string_view v(file_path); if (const auto pos = file_path.find_last_of(fs::delim); pos != umax)
if (const auto pos = v.find_last_of(fs::delim); pos != umax)
{ {
v.remove_prefix(pos + 1); file_path.remove_prefix(pos + 1);
} }
std::span r(real_file_name, CRYPTO_MAX_PATH); std::span r(real_file_name, CRYPTO_MAX_PATH);
strcpy_trunc(r, v); strcpy_trunc(r, file_path);
return real_file_name; return real_file_name;
} }

View File

@ -11,7 +11,7 @@
enum { CRYPTO_MAX_PATH = 4096 }; enum { CRYPTO_MAX_PATH = 4096 };
char* extract_file_name(const char* file_path, char real_file_name[CRYPTO_MAX_PATH]); char* extract_file_name(std::string_view file_path, char real_file_name[CRYPTO_MAX_PATH]);
std::string sha256_get_hash(const char* data, usz size, bool lower_case); std::string sha256_get_hash(const char* data, usz size, bool lower_case);