1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file crypt_find_cipher_any.c
7   Find a cipher in the descriptor tables, Tom St Denis
8 */
9 
10 /**
11    Find a cipher flexibly.  First by name then if not present by block and key size
12    @param name        The name of the cipher desired
13    @param blocklen    The minimum length of the block cipher desired (octets)
14    @param keylen      The minimum length of the key size desired (octets)
15    @return >= 0 if found, -1 if not present
16 */
find_cipher_any(const char * name,int blocklen,int keylen)17 int find_cipher_any(const char *name, int blocklen, int keylen)
18 {
19    int x;
20 
21    if(name != NULL) {
22       x = find_cipher(name);
23       if (x != -1) return x;
24    }
25 
26    LTC_MUTEX_LOCK(&ltc_cipher_mutex);
27    for (x = 0; x < TAB_SIZE; x++) {
28        if (cipher_descriptor[x] == NULL) {
29           continue;
30        }
31        if (blocklen <= (int)cipher_descriptor[x]->block_length && keylen <= (int)cipher_descriptor[x]->max_key_length) {
32           LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
33           return x;
34        }
35    }
36    LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
37    return -1;
38 }
39