xref: /optee_os/core/lib/libtomcrypt/src/misc/crypt/crypt_register_cipher.c (revision 8411e6ad673d20c4742ed30c785e3f5cdea54dfa)
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file crypt_register_cipher.c
7   Register a cipher, Tom St Denis
8 */
9 
10 /**
11    Register a cipher with the descriptor table
12    @param cipher   The cipher you wish to register
13    @return value >= 0 if successfully added (or already present), -1 if unsuccessful
14 */
register_cipher(const struct ltc_cipher_descriptor * cipher)15 int register_cipher(const struct ltc_cipher_descriptor *cipher)
16 {
17    int x;
18 
19    LTC_ARGCHK(cipher != NULL);
20 
21    /* is it already registered? */
22    LTC_MUTEX_LOCK(&ltc_cipher_mutex);
23    for (x = 0; x < TAB_SIZE; x++) {
24        if (cipher_descriptor[x] != NULL && cipher_descriptor[x]->ID == cipher->ID) {
25           LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
26           return x;
27        }
28    }
29 
30    /* find a blank spot */
31    for (x = 0; x < TAB_SIZE; x++) {
32        if (cipher_descriptor[x] == NULL) {
33           cipher_descriptor[x] = cipher;
34           LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
35           return x;
36        }
37    }
38 
39    /* no spot */
40    LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
41    return -1;
42 }
43