Skip to content

Commit 0a0969b

Browse files
committed
private - ltc_algname_match (relaxed alg name matching)
1 parent a3ecbbc commit 0a0969b

3 files changed

Lines changed: 44 additions & 23 deletions

File tree

src/headers/tomcrypt_private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,9 @@ struct get_char {
511511
void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
512512
void password_free(struct password *pw, const struct password_ctx *ctx);
513513

514+
/* Case-insensitive ASCII compare that ignores ' ', '-' and '_'. Returns 1 on match, 0 otherwise. */
515+
int ltc_algname_match(const char *left, const char *right);
516+
514517
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);
515518
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);
516519

src/misc/algname_match.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2+
/* SPDX-License-Identifier: Unlicense */
3+
#include "tomcrypt_private.h"
4+
5+
/**
6+
@file algname_match.c
7+
Shared relaxed name-matching helper for algorithm lookup tables.
8+
*/
9+
10+
/**
11+
Compare two algorithm-name strings.
12+
13+
Matching is case-insensitive (ASCII) and ignores ' ', '-' and '_' on both sides
14+
e.g. "SECP256R1", "secp_256_r1", "secp-256-r1" and "secp 256 r1" all match
15+
@param left First NUL-terminated string
16+
@param right Second NUL-terminated string
17+
@return 1 if the strings match under the relaxed rules, 0 otherwise
18+
*/
19+
int ltc_algname_match(const char *left, const char *right)
20+
{
21+
char lc_r, lc_l;
22+
23+
if (left == NULL || right == NULL) return 0;
24+
25+
while ((*left != '\0') && (*right != '\0')) {
26+
while ((*left == ' ') || (*left == '-') || (*left == '_')) left++;
27+
while ((*right == ' ') || (*right == '-') || (*right == '_')) right++;
28+
if (*left == '\0' || *right == '\0') break;
29+
lc_r = *right;
30+
lc_l = *left;
31+
if ((lc_r >= 'A') && (lc_r <= 'Z')) lc_r += 32;
32+
if ((lc_l >= 'A') && (lc_l <= 'Z')) lc_l += 32;
33+
if (lc_l != lc_r) return 0;
34+
left++;
35+
right++;
36+
}
37+
38+
if ((*left == '\0') && (*right == '\0')) return 1;
39+
return 0;
40+
}

src/pk/ecc/ecc_find_curve.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,28 +189,6 @@ static const struct {
189189
}
190190
};
191191

192-
/* case-insensitive match + ignore '-', '_', ' ' */
193-
static int s_name_match(const char *left, const char *right)
194-
{
195-
char lc_r, lc_l;
196-
197-
while ((*left != '\0') && (*right != '\0')) {
198-
while ((*left == ' ') || (*left == '-') || (*left == '_')) left++;
199-
while ((*right == ' ') || (*right == '-') || (*right == '_')) right++;
200-
if (*left == '\0' || *right == '\0') break;
201-
lc_r = *right;
202-
lc_l = *left;
203-
if ((lc_r >= 'A') && (lc_r <= 'Z')) lc_r += 32;
204-
if ((lc_l >= 'A') && (lc_l <= 'Z')) lc_l += 32;
205-
if (lc_l != lc_r) return 0;
206-
left++;
207-
right++;
208-
}
209-
210-
if ((*left == '\0') && (*right == '\0')) return 1;
211-
return 0;
212-
}
213-
214192
int ecc_get_curve_names(const char *oid, const char * const **names)
215193
{
216194
unsigned long i;
@@ -241,7 +219,7 @@ int ecc_find_curve(const char *name_or_oid, const ltc_ecc_curve **cu)
241219
OID = s_curve_names[i].OID;
242220
}
243221
for (j = 0; s_curve_names[i].names[j] != NULL && !OID; j++) {
244-
if (s_name_match(s_curve_names[i].names[j], name_or_oid)) {
222+
if (ltc_algname_match(s_curve_names[i].names[j], name_or_oid)) {
245223
OID = s_curve_names[i].OID;
246224
}
247225
}

0 commit comments

Comments
 (0)