private - ltc_algname_match (relaxed alg name matching)

This commit is contained in:
Karel Miko 2026-07-27 10:38:28 +02:00
parent a3ecbbc800
commit 0a0969b3d2
3 changed files with 44 additions and 23 deletions

View file

@ -511,6 +511,9 @@ struct get_char {
void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
void password_free(struct password *pw, const struct password_ctx *ctx);
/* Case-insensitive ASCII compare that ignores ' ', '-' and '_'. Returns 1 on match, 0 otherwise. */
int ltc_algname_match(const char *left, const char *right);
int ltc_compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);
int ltc_do_compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);

40
src/misc/algname_match.c Normal file
View file

@ -0,0 +1,40 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file algname_match.c
Shared relaxed name-matching helper for algorithm lookup tables.
*/
/**
Compare two algorithm-name strings.
Matching is case-insensitive (ASCII) and ignores ' ', '-' and '_' on both sides
e.g. "SECP256R1", "secp_256_r1", "secp-256-r1" and "secp 256 r1" all match
@param left First NUL-terminated string
@param right Second NUL-terminated string
@return 1 if the strings match under the relaxed rules, 0 otherwise
*/
int ltc_algname_match(const char *left, const char *right)
{
char lc_r, lc_l;
if (left == NULL || right == NULL) return 0;
while ((*left != '\0') && (*right != '\0')) {
while ((*left == ' ') || (*left == '-') || (*left == '_')) left++;
while ((*right == ' ') || (*right == '-') || (*right == '_')) right++;
if (*left == '\0' || *right == '\0') break;
lc_r = *right;
lc_l = *left;
if ((lc_r >= 'A') && (lc_r <= 'Z')) lc_r += 32;
if ((lc_l >= 'A') && (lc_l <= 'Z')) lc_l += 32;
if (lc_l != lc_r) return 0;
left++;
right++;
}
if ((*left == '\0') && (*right == '\0')) return 1;
return 0;
}

View file

@ -189,28 +189,6 @@ static const struct {
}
};
/* case-insensitive match + ignore '-', '_', ' ' */
static int s_name_match(const char *left, const char *right)
{
char lc_r, lc_l;
while ((*left != '\0') && (*right != '\0')) {
while ((*left == ' ') || (*left == '-') || (*left == '_')) left++;
while ((*right == ' ') || (*right == '-') || (*right == '_')) right++;
if (*left == '\0' || *right == '\0') break;
lc_r = *right;
lc_l = *left;
if ((lc_r >= 'A') && (lc_r <= 'Z')) lc_r += 32;
if ((lc_l >= 'A') && (lc_l <= 'Z')) lc_l += 32;
if (lc_l != lc_r) return 0;
left++;
right++;
}
if ((*left == '\0') && (*right == '\0')) return 1;
return 0;
}
int ecc_get_curve_names(const char *oid, const char * const **names)
{
unsigned long i;
@ -241,7 +219,7 @@ int ecc_find_curve(const char *name_or_oid, const ltc_ecc_curve **cu)
OID = s_curve_names[i].OID;
}
for (j = 0; s_curve_names[i].names[j] != NULL && !OID; j++) {
if (s_name_match(s_curve_names[i].names[j], name_or_oid)) {
if (ltc_algname_match(s_curve_names[i].names[j], name_or_oid)) {
OID = s_curve_names[i].OID;
}
}