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_register_cipher.c
14   Register a cipher, Tom St Denis
15 */
16 
17 /**
18    Register a cipher with the descriptor table
19    @param cipher   The cipher you wish to register
20    @return value >= 0 if successfully added (or already present), -1 if unsuccessful
21 */
register_cipher(const struct ltc_cipher_descriptor * cipher)22 int register_cipher(const struct ltc_cipher_descriptor *cipher)
23 {
24    int x;
25 
26    LTC_ARGCHK(cipher != NULL);
27 
28    /* is it already registered? */
29    LTC_MUTEX_LOCK(&ltc_cipher_mutex);
30    for (x = 0; x < TAB_SIZE; x++) {
31        if (cipher_descriptor[x] != NULL && cipher_descriptor[x]->ID == cipher->ID) {
32           LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
33           return x;
34        }
35    }
36 
37    /* find a blank spot */
38    for (x = 0; x < TAB_SIZE; x++) {
39        if (cipher_descriptor[x] == NULL) {
40           cipher_descriptor[x] = cipher;
41           LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
42           return x;
43        }
44    }
45 
46    /* no spot */
47    LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
48    return -1;
49 }
50 
51 /* ref:         $Format:%D$ */
52 /* git commit:  $Format:%H$ */
53 /* commit time: $Format:%ai$ */
54