1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file crypt_find_hash_any.c
14   Find a hash, Tom St Denis
15 */
16 
17 /**
18    Find a hash flexibly.  First by name then if not present by digest size
19    @param name        The name of the hash desired
20    @param digestlen   The minimum length of the digest size (octets)
21    @return >= 0 if found, -1 if not present
find_hash_any(const char * name,int digestlen)22 */int find_hash_any(const char *name, int digestlen)
23 {
24    int x, y, z;
25    LTC_ARGCHK(name != NULL);
26 
27    x = find_hash(name);
28    if (x != -1) return x;
29 
30    LTC_MUTEX_LOCK(&ltc_hash_mutex);
31    y = MAXBLOCKSIZE+1;
32    z = -1;
33    for (x = 0; x < TAB_SIZE; x++) {
34        if (hash_descriptor[x] == NULL) {
35           continue;
36        }
37        if ((int)hash_descriptor[x]->hashsize >= digestlen && (int)hash_descriptor[x]->hashsize < y) {
38           z = x;
39           y = hash_descriptor[x]->hashsize;
40        }
41    }
42    LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
43    return z;
44 }
45 
46 /* ref:         $Format:%D$ */
47 /* git commit:  $Format:%H$ */
48 /* commit time: $Format:%ai$ */
49