1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file crypt_find_hash_any.c
7   Find a hash, Tom St Denis
8 */
9 
10 /**
11    Find a hash flexibly.  First by name then if not present by digest size
12    @param name        The name of the hash desired
13    @param digestlen   The minimum length of the digest size (octets)
14    @return >= 0 if found, -1 if not present
find_hash_any(const char * name,int digestlen)15 */int find_hash_any(const char *name, int digestlen)
16 {
17    int x, y, z;
18    LTC_ARGCHK(name != NULL);
19 
20    x = find_hash(name);
21    if (x != -1) return x;
22 
23    LTC_MUTEX_LOCK(&ltc_hash_mutex);
24    y = MAXBLOCKSIZE+1;
25    z = -1;
26    for (x = 0; x < TAB_SIZE; x++) {
27        if (hash_descriptor[x] == NULL) {
28           continue;
29        }
30        if ((int)hash_descriptor[x]->hashsize >= digestlen && (int)hash_descriptor[x]->hashsize < y) {
31           z = x;
32           y = hash_descriptor[x]->hashsize;
33        }
34    }
35    LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
36    return z;
37 }
38